Your indicator is too slow...

To add comments, please log in or register
llaabbss
54
llaabbss  
Hi, everybody.

Can anybody help me understand how to make things right in my script?

I'm trying to use several indicators at once, and naturally I got the problem of "your indicator is too slow..."

How can I solve this problem?

I NEED TO STUDY 5 PREVIOUS CANDLES. That is why I call [i+n] in each indicator.

The code is as follows:
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict

static datetime lastTradeBarTime;

int OnInit()
{   
   lastTradeBarTime = Time[1];
   
   return(INIT_SUCCEEDED);
   
}

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 &spread[])
{
   
        int toCount = Bars - IndicatorCounted();

        for (int i = toCount - 6; i >= 0 ; i--){

                if (lastTradeBarTime != Time[i]) {

                        double OPEN_BACK0 = Open[i+1];
                        double OPEN_BACK1 = Open[i+2];
                        double OPEN_BACK2 = Open[i+3];
                        double OPEN_BACK3 = Open[i+4];
                        double OPEN_BACK4 = Open[i+5];

                        double HIGH_BACK0 = High[i+1];
                        double HIGH_BACK1 = High[i+2];
                        double HIGH_BACK2 = High[i+3];
                        double HIGH_BACK3 = High[i+4];
                        double HIGH_BACK4 = High[i+5];

                        double LOW_BACK0 = Low[i+1];
                        double LOW_BACK1 = Low[i+2];
                        double LOW_BACK2 = Low[i+3];
                        double LOW_BACK3 = Low[i+4];
                        double LOW_BACK4 = Low[i+5];

                        double CLOSE_BACK0 = Close[i+1];
                        double CLOSE_BACK1 = Close[i+2];
                        double CLOSE_BACK2 = Close[i+3];
                        double CLOSE_BACK3 = Close[i+4];
                        double CLOSE_BACK4 = Close[i+5];

                        double STOCHMAIN_BACK0 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, STO_CLOSECLOSE, MODE_MAIN, i+1);
                        double STOCHMAIN_BACK1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, STO_CLOSECLOSE, MODE_MAIN, i+2);
                        double STOCHMAIN_BACK2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, STO_CLOSECLOSE, MODE_MAIN, i+3);
                        double STOCHMAIN_BACK3 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, STO_CLOSECLOSE, MODE_MAIN, i+4);
                        double STOCHMAIN_BACK4 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, STO_CLOSECLOSE, MODE_MAIN, i+5);

                        double STOCHSIGNAL_BACK0 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, STO_CLOSECLOSE, MODE_SIGNAL, i+1);
                        double STOCHSIGNAL_BACK1 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, STO_CLOSECLOSE, MODE_SIGNAL, i+2);
                        double STOCHSIGNAL_BACK2 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, STO_CLOSECLOSE, MODE_SIGNAL, i+3);
                        double STOCHSIGNAL_BACK3 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, STO_CLOSECLOSE, MODE_SIGNAL, i+4);
                        double STOCHSIGNAL_BACK4 = iStochastic(NULL, 0, 5, 3, 3, MODE_SMA, STO_CLOSECLOSE, MODE_SIGNAL, i+5);

                        double RSI_BACK0 = iRSI(NULL, 0, 14, PRICE_CLOSE, i+1);
                        double RSI_BACK1 = iRSI(NULL, 0, 14, PRICE_CLOSE, i+2);
                        double RSI_BACK2 = iRSI(NULL, 0, 14, PRICE_CLOSE, i+3);
                        double RSI_BACK3 = iRSI(NULL, 0, 14, PRICE_CLOSE, i+4);
                        double RSI_BACK4 = iRSI(NULL, 0, 14, PRICE_CLOSE, i+5);

                        double MA_BACK0 = iMA(NULL, 0, 21, 0, MODE_SMA, PRICE_CLOSE, i+1);
                        double MA_BACK1 = iMA(NULL, 0, 21, 0, MODE_SMA, PRICE_CLOSE, i+2);
                        double MA_BACK2 = iMA(NULL, 0, 21, 0, MODE_SMA, PRICE_CLOSE, i+3);
                        double MA_BACK3 = iMA(NULL, 0, 21, 0, MODE_SMA, PRICE_CLOSE, i+4);
                        double MA_BACK4 = iMA(NULL, 0, 21, 0, MODE_SMA, PRICE_CLOSE, i+5);

                        double WPR1_BACK0 = iWPR(NULL, 0, 3, i+1);
                        double WPR1_BACK1 = iWPR(NULL, 0, 3, i+2);
                        double WPR1_BACK2 = iWPR(NULL, 0, 3, i+3);
                        double WPR1_BACK3 = iWPR(NULL, 0, 3, i+4);
                        double WPR1_BACK4 = iWPR(NULL, 0, 3, i+5);

                        double WPR2_BACK0 = iWPR(NULL, 0, 6, i+1);
                        double WPR2_BACK1 = iWPR(NULL, 0, 6, i+2);
                        double WPR2_BACK2 = iWPR(NULL, 0, 6, i+3);
                        double WPR2_BACK3 = iWPR(NULL, 0, 6, i+4);
                        double WPR2_BACK4 = iWPR(NULL, 0, 6, i+5);

                        double WPR3_BACK0 = iWPR(NULL, 0, 9, i+1);
                        double WPR3_BACK1 = iWPR(NULL, 0, 9, i+2);
                        double WPR3_BACK2 = iWPR(NULL, 0, 9, i+3);
                        double WPR3_BACK3 = iWPR(NULL, 0, 9, i+4);
                        double WPR3_BACK4 = iWPR(NULL, 0, 9, i+5);

                        Print("-------------------------------------------------------------");

                        Print("[", TimeHour(Time[i]),":", TimeMinute(Time[i]), "][", i, "] ",
                        "[OPEN] BACK0:", DoubleToString(OPEN_BACK0),
                                ", BACK1:", DoubleToString(OPEN_BACK1),
                                ", BACK2:", DoubleToString(OPEN_BACK2),
                                ", BACK3:", DoubleToString(OPEN_BACK3),
                                ", BACK4:", DoubleToString(OPEN_BACK4)
                        );
                        Print("[", TimeHour(Time[i]),":", TimeMinute(Time[i]), "][", i, "] ",
                        "[HIGH] BACK0:", DoubleToString(HIGH_BACK0),
                                ", BACK1:", DoubleToString(HIGH_BACK1),
                                ", BACK2:", DoubleToString(HIGH_BACK2),
                                ", BACK3:", DoubleToString(HIGH_BACK3),
                                ", BACK4:", DoubleToString(HIGH_BACK4)
                        );
                        Print("[", TimeHour(Time[i]),":", TimeMinute(Time[i]), "][", i, "] ",
                        "[LOW] BACK0:", DoubleToString(LOW_BACK0),
                                ", BACK1:", DoubleToString(LOW_BACK1),
                                ", BACK2:", DoubleToString(LOW_BACK2),
                                ", BACK3:", DoubleToString(LOW_BACK3),
                                ", BACK4:", DoubleToString(LOW_BACK4)
                        );
                        Print("[", TimeHour(Time[i]),":", TimeMinute(Time[i]), "][", i, "] ",
                        "[CLOSE] BACK0:", DoubleToString(CLOSE_BACK0),
                                ", BACK1:", DoubleToString(CLOSE_BACK1),
                                ", BACK2:", DoubleToString(CLOSE_BACK2),
                                ", BACK3:", DoubleToString(CLOSE_BACK3),
                                ", BACK4:", DoubleToString(CLOSE_BACK4)
                        );

                        Print("[", TimeHour(Time[i]),":", TimeMinute(Time[i]), "][", i, "] ",
                        "[STOCHMAIN] BACK0:", DoubleToString(STOCHMAIN_BACK0),
                                ", BACK1:", DoubleToString(STOCHMAIN_BACK1),
                                ", BACK2:", DoubleToString(STOCHMAIN_BACK2),
                                ", BACK3:", DoubleToString(STOCHMAIN_BACK3),
                                ", BACK4:", DoubleToString(STOCHMAIN_BACK4)
                        );

                        Print("[", TimeHour(Time[i]),":", TimeMinute(Time[i]), "][", i, "] ",
                        "[STOCHSIGNAL] BACK0:", DoubleToString(STOCHSIGNAL_BACK0),
                                ", BACK1:", DoubleToString(STOCHSIGNAL_BACK1),
                                ", BACK2:", DoubleToString(STOCHSIGNAL_BACK2),
                                ", BACK3:", DoubleToString(STOCHSIGNAL_BACK3),
                                ", BACK4:", DoubleToString(STOCHSIGNAL_BACK4)
                        );

                        Print("[", TimeHour(Time[i]),":", TimeMinute(Time[i]), "][", i, "] ",
                        "[RSI] BACK0:", DoubleToString(RSI_BACK0),
                                ", BACK1:", DoubleToString(RSI_BACK1),
                                ", BACK2:", DoubleToString(RSI_BACK2),
                                ", BACK3:", DoubleToString(RSI_BACK3),
                                ", BACK4:", DoubleToString(RSI_BACK4)
                        );

                        Print("[", TimeHour(Time[i]),":", TimeMinute(Time[i]), "][", i, "] ",
                                "[MA] BACK0:", DoubleToString(MA_BACK0),
                                ", BACK1:", DoubleToString(MA_BACK1),
                                ", BACK2:", DoubleToString(MA_BACK2),
                                ", BACK3:", DoubleToString(MA_BACK3),
                                ", BACK4:", DoubleToString(MA_BACK4)
                        );

                        Print("[", TimeHour(Time[i]),":", TimeMinute(Time[i]), "][", i, "] ",
                                "[WPR1] BACK0:", DoubleToString(WPR1_BACK0),
                                ", BACK1:", DoubleToString(WPR1_BACK1),
                                ", BACK2:", DoubleToString(WPR1_BACK2),
                                ", BACK3:", DoubleToString(WPR1_BACK3),
                                ", BACK4:", DoubleToString(WPR1_BACK4)
                        );

                        Print("[", TimeHour(Time[i]),":", TimeMinute(Time[i]), "][", i, "] ",
                                "[WPR2] BACK0:", DoubleToString(WPR2_BACK0),
                                ", BACK1:", DoubleToString(WPR2_BACK1),
                                ", BACK2:", DoubleToString(WPR2_BACK2),
                                ", BACK3:", DoubleToString(WPR2_BACK3),
                                ", BACK4:", DoubleToString(WPR2_BACK4)
                        );

                        Print("[", TimeHour(Time[i]),":", TimeMinute(Time[i]), "][", i, "] ",
                                "[WPR3] BACK0:", DoubleToString(WPR3_BACK0),
                                ", BACK1:", DoubleToString(WPR3_BACK1),
                                ", BACK2:", DoubleToString(WPR3_BACK2),
                                ", BACK3:", DoubleToString(WPR3_BACK3),
                                ", BACK4:", DoubleToString(WPR3_BACK4)
                        );

                }

        }
   
   return(rates_total);
   
}

William Roeder
20207
William Roeder  
llaabbss:

Can anybody help me understand how to make things right in my script?

I'm trying to use several indicators at once, and naturally I got the problem of "your indicator is too slow..."

I NEED TO STUDY 5 PREVIOUS CANDLES. That is why I call [i+n] in each indicator.

  1. int OnCalculate(const int rates_total, … const int &spread[]){
       
            int toCount = Bars - IndicatorCounted();
            for (int i = toCount - 6; i >= 0 ; i--){
    
    You should stop using the old IndicatorCounted and start using the new Event Handling Functions.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference
              How to do your lookbacks correctly.

  2. After the first run, toCount will be zero and your loop stops working. Do your lookbacks correctly.
  3. if (lastTradeBarTime != Time[i]) {
    
    Drop new bar code - That is used in EAs. Indicators should evaluate all bars and reevaluate bar zero each tick. If you don't want to process bar zero, change your loop exit.

  4. llaabbss: I NEED TO STUDY 5 PREVIOUS CANDLES. That is why I call [i+n] in each indicator.
    double OPEN_BACK0 = Open[i+1];
    ⋮
    double OPEN_BACK4 = Open[i+5];
    
    Don't SHOUT at us, that is RUDE.
  5. You are processing candle i. Therefor you should be looking at that (plus the previous four candles.) I.e. [i+0 … i+4] not [i+1 … i+5].

  6. As for being too slow, if it takes too long to compute, reduce Tools → Options (control-O) → Charts → Max bars in chart to something reasonable (like 1K.) Or, as a last resort, use my attached include.
    1. It starts the timer, and merges OnTimer and OnCalculate calls to the function OnCalculateSegment(int rates_total, int prev_calculated).
    2. At the start of each iteration, you check to see if your indicator is taking too long and if so, exit and resume on the next call.
    3. Reread lookbacks correctly.
    4. I recommend you test for time out at start of the loop and after each indicator group.
            if(gSegmentedCalculation.is_timeout() )
               return rates_total-iBar-REDRAW_BAR;
      
llaabbss
54
llaabbss  
William Roeder:
  1. You should stop using the old IndicatorCounted and start using the new Event Handling Functions.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference
              How to do your lookbacks correctly.

  2. After the first run, toCount will be zero and your loop stops working. Do your lookbacks correctly.
  3. Drop new bar code - That is used in EAs. Indicators should evaluate all bars and reevaluate bar zero each tick. If you don't want to process bar zero, change your loop exit.

  4. Don't SHOUT at us, that is RUDE.
  5. You are processing candle i. Therefor you should be looking at that (plus the previous four candles.) I.e. [i+0 … i+4] not [i+1 … i+5].

  6. As for being too slow, if it takes too long to compute, reduce Tools → Options (control-O) → Charts → Max bars in chart to something reasonable (like 1K.) Or, as a last resort, use my attached include.
    1. It starts the timer, and merges OnTimer and OnCalculate calls to the function OnCalculateSegment(int rates_total, int prev_calculated).
    2. At the start of each iteration, you check to see if your indicator is taking too long and if so, exit and resume on the next call.
    3. Reread lookbacks correctly.
    4. I recommend you test for time out at start of the loop and after each indicator group.

Dear William,

Thank you. It helped a lot!

To add comments, please log in or register