TimeScale problem

 

Hello! I need help. 

I have constructed an indicator which obtains results for every tick based on 2 other indicators which I call by iCustom function. I pass the timeframe and all the necessary arguments but the problem is that it gives me different results on H4 and H1 scales but the arguments are the same. I dont use any scale specific concept like "Bars" or "Period()" but when I switch time scales it gives me different results. Any suggestions about what it might be??? Thanks in advance.

 David 

 
It will be useful to show the code.
 
  1. for(int i = 0; i < limit; i++)
    {
    Stoch_0 = iCustom(Symbol(), TimeFrame, "StochasticDiNapoli",FastK, SlowK, SlowD, "1 - on", 0, 80, 20, 0, i);
    Stoch_1 = iCustom(Symbol(), TimeFrame, "StochasticDiNapoli",FastK, SlowK, SlowD, "1 - on", 0, 80, 20, 0, i-1);
    Get in the habit of always counting down. You must when closeing and deleteing multiple orders
  2. i-1 is the future value you want i+1 previous value.
  3. You are mixing apples and oranges. i is the current chart's bar - current chart's timeframe. incompatible with another TF.
    for(int i = 0; i < limit; i++)
    {
      int iTF = iBarShift(Symbol(), TimeFrame, Time[i]);
      Stoch_0 = iCustom(Symbol(), TimeFrame, "StochasticDiNapoli",FastK, SlowK, SlowD, "1 - on", 0, 80, 20, 0, iTF);
      Stoch_1 = iCustom(Symbol(), TimeFrame, "StochasticDiNapoli",FastK, SlowK, SlowD, "1 - on", 0, 80, 20, 0, iTF+1);

  4. Your code
    int CalcTimeFrame(int timeFrame)
    {
    int retval = 0;
    
    switch(timeFrame)
    {
    case 1: retval = 1; break;
    case 5: retval = 1; break;
    case 15: retval = 5; break;
    case 30: retval = 15; break;
    case 60: retval = 30; break;
    case 240: retval = 60; break;
    case 1440: retval = 240; break;
    case 10080: retval = 1440; break;
    case 43200: retval = 10080; break;
    }
    
    return (retval);
    }
    Equivalent code, better named function
    int LowerTimeFrame(int timeFrame)
    {
       static int validTFs[] = { 0, PERIOD_M1, 
                                    PERIOD_M1, PERIOD_M5, PERIOD_M15, PERIOD_M30,
                                    PERIOD_H1, PERIOD_H4, PERIOD_D1,  PERIOD_W1   };
       for(int iTF = ArraySize(validTFs)-1; iTF > 1; iTF--)
          if(timeFrame == validTFs[iTF]) break;
       return( validTFs[iTF-1] );
    }
 

1) I know i is the current bar and i-1 is the previous bar's index. Im trying to get current and previous bar's values at each tick.

2) I didnt quite well get your answer about the current chart's timeframe. Im not tweaking timeframe at all in accordance with chart. Whatever the chart is, the result should be identical since Im passing the same arguments.

  1. No, it is not. i+1 is. (Eye PLUS one)
  2. It is because you are passing the same arguments. If you are on the H1 and are calculating the M15 value for bar 1 that is the  M15 bar 5. If you are on the D1 it is the H4 bar 7. Only if TimeFrame equals the chart's will the bar number and the iCustom(shift) be the same.
 
Thank you all I managed it.
 

first of all thanks for your contribution to the MQL-community. A lot of my questions are awnsered by your posts.

Many posts concerning the use of HTF timeframe (custom) indicators refer to this conversation. Nevertheless a lot of the messages in this conversation seem to have been lost. I have developped a custom indicator (based on ZigZag). Next I am trying to (for now) make a seperate HTF-indicator based on this custom-ZigZag. I have found a couple of solutions but none seem to work for me. Nevertheless, the solution above looks solid and apart from 1 solution I've found, all solutions make use of iBarShift().

The code below slows down my server dramatically but above all does not generate any buffers. Could you please guide me in the right direction (it is concerning MQL5)? Would be highly appreciated!


P.S. the active/trading TF=1H, the indicator TF=4H 

#property description "HTF ZigZag"


#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- indicator plots
#property indicator_type1   DRAW_ARROW
#property indicator_type2   DRAW_ARROW
#property indicator_color1  clrWhite
#property indicator_color2  clrWhite
#property indicator_width1  3
#property indicator_width2  3
#property indicator_label1  "Trend"
#property indicator_label2  "Trend2"


//--- input parameters
input ENUM_TIMEFRAMES      InpTimeFrame_2=PERIOD_H4;              // Timeframe 2 (TF2) period
string               InpIndicator_TF1="Doetgewoon";  // Location of single timeframe indicator

input int      ZZDepth=10;
input int      ZZDev=5;
input int      nShift=5;

//--- indicator buffers
double                     Trend[];
double                     Trend2[];

int   Trend_handle;
int   Trend2_handle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Trend,INDICATOR_DATA);
   SetIndexBuffer(1,Trend2,INDICATOR_DATA);

//--- set buffers as series, most recent entry at index [0]
   ArraySetAsSeries(Trend,true);
   ArraySetAsSeries(Trend2,true);

     
//--- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
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 &TickVolume[],
                const long &Volume[],
                const int &Spread[])
  {
  
     int i=0;
   int limit=0;
//--- set arrays as series, most recent entry at index [0]
   ArraySetAsSeries(Time,true);
   ArraySetAsSeries(Open,true);
   ArraySetAsSeries(High,true);
   ArraySetAsSeries(Low,true);
   ArraySetAsSeries(Close,true);


   if(prev_calculated==0 || prev_calculated<0 || prev_calculated>rates_total)
      limit=rates_total-1;
   else
      limit=rates_total-prev_calculated;


for(i = 0; i < limit; i++)
{
  int iTF = iBarShift(Symbol(), InpTimeFrame_2, Time[i]);
  Trend_handle =  iCustom(Symbol(), InpTimeFrame_2, InpIndicator_TF1,ZZDepth, ZZDev, nShift, iTF);
  Trend2_handle = iCustom(Symbol(), InpTimeFrame_2, InpIndicator_TF1,ZZDepth, ZZDev, nShift, iTF+1);
  
  CopyBuffer(Trend_handle, 32,0,1,Trend);
  CopyBuffer(Trend2_handle, 32,0,1,Trend2); 

}

//--- return value of rates_total, will be used as prev_calculated in next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Kind regards

 
  1. Don't resurrect old threads without a very good reason. A lot has changed since Build 600 and Higher. 2014.02.03

  2. Don't Hijack other threads for your off-topic post. Next time, make your own, new, thread.

  3. Why have you posted your MT5 question in the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  4. 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 &TickVolume[],
                    const long &Volume[],
                    const int &Spread[])
    ⋮
       ArraySetAsSeries(Time,true);
       ArraySetAsSeries(Open,true);
       ArraySetAsSeries(High,true);
       ArraySetAsSeries(Low,true);
       ArraySetAsSeries(Close,true);

    Do not capitalize those arguments. Time … Close are MT4 Predefined Variables

  5.   Trend_handle =  iCustom(Symbol(), InpTimeFrame_2, InpIndicator_TF1,ZZDepth, ZZDev, nShift, iTF);
      Trend2_handle = iCustom(Symbol(), InpTimeFrame_2, InpIndicator_TF1,ZZDepth, ZZDev, nShift, iTF+1);
  6. Perhaps you should read the manual, especially the examples. They all (including iCustom) return a handle (an int.) You get that in OnInit. In OnTick (after the indicator has updated its buffers,) you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010

    Also see my example for encapsulating calls
              Detailed explanation of iCustom - MQL4 programming forum 2017.05.23

    That is your CPU problem — you are starting up thousands of indicators.

    Handles do not have a bar index.Perhaps you should read the manual.
       How To Ask Questions The Smart Way. 2004
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

  7.       limit=rates_total-prev_calculated;
    for(i = 0; i < limit; i++)
    After the first run limit will be zero and you do nothing.
              How to do your lookbacks correctly #9 - #14 & #19
 

Thank you very much for the pointers, I will educate myself with the links you provided.

Next, appologies for points 1-3, I am new to this forum (or forums in general), but I will certainly adhere to them in the future. I clipped the entire article in Evernote in case a moderator indeed decided to delete posts.


Thanks for taking the time, you're a true gent!

Reason: