【问题标题】:Not able to return the value through indicator MQL5无法通过指标 MQL5 返回值
【发布时间】:2018-05-28 19:00:34
【问题描述】:

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

#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);
  }

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

【问题讨论】:

    标签: mql5 metatrader5


    【解决方案1】:

    首先,不知道为什么需要指示器来完成这项简单的任务。

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

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

    【讨论】:

    • 谢谢丹尼尔。我只是想变得花哨。因此,正在寻找从指标缓冲区中获取值的方法。
    • 欢迎。一旦您检查了指标句柄是否正常,您当然可以隐藏/删除该块
    猜你喜欢
    • 2021-11-17
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 2014-10-13
    • 2018-09-26
    • 1970-01-01
    相关资源
    最近更新 更多