0

这是返回新柱剩余时间的指标:

#property indicator_chart_window
#property strict
#property indicator_buffers 1
//---- input parameters
input color Clock_Color = clrWhite;
input ENUM_BASE_CORNER Corner = CORNER_LEFT_LOWER;

string objname="Spread&Bar";
double s1[];
double remainingtime[];
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   SetIndexBuffer(0,remainingtime,INDICATOR_CALCULATIONS);
        ObjectCreate(0, objname, OBJ_LABEL,0, 0, 0);
        ObjectSetInteger(0, objname, OBJPROP_CORNER, Corner);
        ObjectSetInteger(0, objname, OBJPROP_XDISTANCE, 10);
        ObjectSetInteger(0, objname, OBJPROP_YDISTANCE, 2);
        ENUM_ANCHOR_POINT Anchor = ANCHOR_LEFT_UPPER;
        switch (Corner)
        {
                case CORNER_LEFT_UPPER: Anchor=ANCHOR_LEFT_UPPER; break;
                case CORNER_RIGHT_UPPER: Anchor=ANCHOR_RIGHT_UPPER; break;
                case CORNER_LEFT_LOWER: Anchor=ANCHOR_LEFT_LOWER; break;
                case CORNER_RIGHT_LOWER: Anchor=ANCHOR_RIGHT_LOWER; break;
        }
        ObjectSetInteger(0, objname, OBJPROP_ANCHOR, Anchor);

        return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason) { ObjectDelete(0, objname); } 

//+------------------------------------------------------------------+
int OnCalculate(const int rates_total, const int prev_calculated, const datetime& time[], const double& open[], const double& high[], const double& low[], const double& close[], const long& tick_volume[], const long& volume[], const int& spreads[])
{
        ArraySetAsSeries(time, true);
        int m=int(time[0]+PeriodSeconds()-TimeCurrent());
        int s=m%60;
        m=(m-s)/60;
        long spread=SymbolInfoInteger(Symbol(), SYMBOL_SPREAD);

        string _sp="",_m="",_s="";
        if (spread<10) _sp="..";
        else if (spread<100) _sp=".";
        if (m<10) _m="0";
        if (s<10) _s="0";

        ObjectSetString(0, objname, OBJPROP_TEXT, "Spread: " +IntegerToString(spread)+_sp+" Next Bar in "+_m+IntegerToString(m)+":"+_s+IntegerToString(s));
        remainingtime[0] = s;
        Print(remainingtime[0]);
        ObjectSetInteger(0, objname, OBJPROP_FONTSIZE, 10);
        ObjectSetInteger(0, objname, OBJPROP_COLOR, Clock_Color);
        ObjectSetString(0, objname, OBJPROP_FONT, "Courier");

        return(rates_total);
}

我在打印时得到了正确的值。但是当我通过专家调用它时,我得到的只是零。

这里是专家:

#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

int candletime;
double remaining_time[];


int OnInit()
  {
//---
   candletime = iCustom(_Symbol,_Period,"candle_time_end_and_spread.ex5");

   ArraySetAsSeries(remaining_time,true);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

   IndicatorRelease(candletime);

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
////---
     CopyBuffer(candletime,0,0,1,remaining_time);
   ArrayPrint(remaining_time);
  }

请让我知道我做错了什么。

4

1 回答 1

1

首先,不知道为什么你需要指标来完成这个简单的任务。

 const int seconds  =PeriodSeconds();  
 int secondsRemaining = seconds - TimeCurrent()%seconds;

第二,不清楚你ArrayPrint()在做什么。我建议检查一下remaining_time[0],也不需要将该数组设置为时间序列。当然,您应该检查您的句柄是否不是INVALID_HANDLE,并且您设法复制了至少 1 个元素。简单的调试会告诉你问题。我觉得指标的名字不用ex5后缀就可以了

于 2018-05-28T11:00:34.407 回答