Problem with Call Supertrend Indicator

 

I have multi timeframe supertrend indicator with code as below (unfortunately I cannot display all code due to limitation of character to send at forum

................... 

#property indicator_chart_window

#property indicator_buffers 3
#property indicator_color1 clrGreen
#property indicator_color2 clrRed

...................

    UpTrend = false;
    DownTrend = false;

    ArrayInitialize(TFTrend, 0);
    SetIndexBuffer(2, BufferZero);
    SetIndexStyle(2, DRAW_NONE);
    SetIndexBuffer(0, TrendUp);
    SetIndexLabel(0, "Trend Up");
    SetIndexBuffer(1, TrendDown);
    SetIndexLabel(1, "Trend Down");
    if (!DrawLinesEnabled)
    {
        SetIndexStyle(0, DRAW_NONE);
        SetIndexStyle(1, DRAW_NONE);
    }

    CalculateLevels();

    return INIT_SUCCEEDED;
}


................... 

//+------------------------------------------------------------------+
//| Main function to detect Positive, Negative, Uncertain state.     |
//+------------------------------------------------------------------+
void CalculateLevels()
{
    int EnabledCount = 0;
    int UpCount = 0;
    int DownCount = 0;
    UpTrend = false;
    DownTrend = false;
    MaxBars = ATRMaxBars;
    ArrayInitialize(TFTrend, 0);
    for (int i = 0; i < ArraySize(TFTrend); i++)
    {
        if (!TFEnabled[i]) continue;
        if (iBars(Symbol(), TFValues[i]) < MaxBars)
        {
            MaxBars = iBars(Symbol(), TFValues[i]);
            Print("Please load more historical candles. Current calculation only on ", MaxBars, " bars for timeframe ", TFText[i], ".");
            if (MaxBars < 0)
            {
                break;
            }
        }
        EnabledCount++;
        int TFValue = TFValues[i];
        string TFDesc = TFText[i];
        double ATRTrend = GetATRTrend(Symbol(), TFValue, Shift);
        if (ATRTrend == 0)
        {
            Print("Not enough historical data, please load more candles for ", TFDesc);
        }
        if (iClose(Symbol(), TFValues[i], Shift) > ATRTrend)
        {
            TFTrend[i] = 1;
            UpCount++;
        }
        if (iClose(Symbol(), TFValues[i], Shift) < ATRTrend)
        {
            TFTrend[i] = -1;
            DownCount++;
        }
    }
    if (UpCount == EnabledCount) UpTrend = true;
    if (DownCount == EnabledCount) DownTrend = true;
}

//+------------------------------------------------------------------+
//| Calculates Superеrend for a giveт timeframe.                     |
//+------------------------------------------------------------------+
double GetATRTrend(string Instrument = NULL, int Timeframe = 0, int shift = 0)
{
    ArrayResize(TrendDownTmp, ATRMaxBars, 0);
    ArrayInitialize(TrendDownTmp, 0);
    ArrayResize(TrendUpTmp, ATRMaxBars, 0);
    ArrayInitialize(TrendUpTmp, 0);
    if (Instrument == NULL) Instrument = Symbol();
    if (Timeframe == 0) Timeframe = Period();
    CalculateSupertrendTmp(Timeframe);

    double ATRTrend1 = TrendUpTmp[shift];
    double ATRTrend2 = TrendDownTmp[shift];

    if (ATRTrend1 > (iClose(Instrument, Timeframe, shift) * 2))
    {
        return NormalizeDouble(ATRTrend2, (int)MarketInfo(Instrument, MODE_DIGITS));
    }
    if (ATRTrend2 > (iClose(Instrument, Timeframe, shift) * 2))
    {
        return NormalizeDouble(ATRTrend1, (int)MarketInfo(Instrument, MODE_DIGITS));
    }
    if (ATRTrend1 == 0)
    {
        Print("Error reading ATR values.");
    }
    return 0;
}

//+------------------------------------------------------------------+
//| Fills indicator buffers.                                         |
//+------------------------------------------------------------------+
void FillBuffers()
{
    if (UpTrend) BufferZero[0] = 1;
    if (DownTrend) BufferZero[0] = -1;
    if ((!UpTrend) && (!DownTrend)) BufferZero[0] = 0;
}


............... 

//+------------------------------------------------------------------+
//| Calculates Supertrend indicator's buffers.                       |
//+------------------------------------------------------------------+
void CalculateSuperTrend()
{
    int limit, i, flag, flagh, trend[10000];
    double up[10000], dn[10000], medianPrice, atr;
    int counted_bars = IndicatorCounted();
    if (counted_bars < 0) return;
    if (counted_bars > 0) counted_bars--;
    limit = Bars - counted_bars;
    MaxBars = ATRMaxBars;
    if (Bars < MaxBars + 2 + ATRPeriod) MaxBars = Bars - 2 - ATRPeriod;
    if (MaxBars <= 0)
    {
        Print("Need more historical data to calculate the Supertrend. Currently, the indicator has only ", Bars, " bars.");
        return;
    }
    for (i = MaxBars; i >= 0; i--)
    {
        TrendUp[i] = EMPTY_VALUE;
        TrendDown[i] = EMPTY_VALUE;
        atr = iATR(NULL, 0, ATRPeriod, i);

        medianPrice = (High[i] + Low[i]) / 2;

        up[i] = medianPrice + (ATRMultiplier * atr);
        dn[i] = medianPrice - (ATRMultiplier * atr);
        trend[i] = 1;

        if (Close[i] > up[i + 1])
        {
            trend[i] = 1;
            if (trend[i + 1] == -1) changeOfTrend = 1;
        }
        else if (Close[i] < dn[i + 1])
        {
            trend[i] = -1;
            if (trend[i + 1] == 1) changeOfTrend = 1;
        }
        else if (trend[i + 1] == 1)
        {
            trend[i] = 1;
            changeOfTrend = 0;
        }
        else if (trend[i + 1] == -1)
        {
            trend[i] = -1;
            changeOfTrend = 0;
        }

        if ((trend[i] < 0) && (trend[i + 1] > 0))
        {
            flag = 1;
        }
        else
        {
            flag = 0;
        }

        if ((trend[i] > 0) && (trend[i + 1] < 0))
        {
            flagh = 1;
        }
        else
        {
            flagh = 0;
        }

        if ((trend[i] > 0) && (dn[i] < dn[i + 1]))
        {
            dn[i] = dn[i + 1];
        }
        
        if ((trend[i] < 0) && (up[i] > up[i + 1]))
        {
            up[i] = up[i + 1];
        }

        if (flag == 1)
        {
            up[i] = medianPrice + (ATRMultiplier * atr);
        }

        if (flagh == 1)
        {
            dn[i] = medianPrice - (ATRMultiplier * atr);
        }

        //-- Draw the indicator
        if (trend[i] == 1)
        {
            TrendUp[i] = dn[i];
            if (changeOfTrend == 1)
            {
                TrendUp[i + 1] = TrendDown[i + 1];
                changeOfTrend = 0;
            }
        }
        else if (trend[i] == -1)
        {
            TrendDown[i] = up[i];
            if (changeOfTrend == 1)
            {
                TrendDown[i + 1] = TrendUp[i + 1];
                changeOfTrend = 0;
            }
        }
    }
    WindowRedraw();
}

//+------------------------------------------------------------------+
//| Calculates Supetrend values for a given timeframe.               |
//+------------------------------------------------------------------+
void CalculateSupertrendTmp(int Timeframe)
{
    MaxBars = ATRMaxBars;
    int limit, i, flag, flagh, trend[10000];
    double up[10000], dn[10000], medianPrice, atr;
    int counted_bars = 0;
    if (counted_bars < 0) return;
    if (counted_bars > 0) counted_bars--;
    limit = iBars(Symbol(), Timeframe) - counted_bars - 1;
    MaxBars--;
    if (iBars(Symbol(), Timeframe) < MaxBars + 2 + ATRPeriod) MaxBars = iBars(Symbol(), Timeframe) - 2 - ATRPeriod;
    if (MaxBars <= 0)
    {
        Print("Need more historical data to calculate the Supertrend. Currently have only ", iBars(Symbol(), Timeframe), " bars.");
        return;
    }
    for (i = MaxBars; i >= 0; i--)
    {
        TrendUpTmp[i] = EMPTY_VALUE;
        TrendDownTmp[i] = EMPTY_VALUE;
        atr = iATR(Symbol(), Timeframe, ATRPeriod, i);

        medianPrice = (iHigh(Symbol(), Timeframe, i) + iLow(Symbol(), Timeframe, i)) / 2;
        up[i] = medianPrice + (ATRMultiplier * atr);
        dn[i] = medianPrice - (ATRMultiplier * atr);
        trend[i] = 1;

        if (iClose(Symbol(), Timeframe, i) > up[i + 1])
        {
            trend[i] = 1;
            if (trend[i + 1] == -1) changeOfTrend = 1;

        }
        else if (iClose(Symbol(), Timeframe, i) < dn[i + 1])
        {
            trend[i] = -1;
            if (trend[i + 1] == 1) changeOfTrend = 1;
        }
        else if (trend[i + 1] == 1)
        {
            trend[i] = 1;
            changeOfTrend = 0;
        }
        else if (trend[i + 1] == -1)
        {
            trend[i] = -1;
            changeOfTrend = 0;
        }

        if (trend[i] < 0 && trend[i + 1] > 0)
        {
            flag = 1;
        }
        else
        {
            flag = 0;
        }

        if (trend[i] > 0 && trend[i + 1] < 0)
        {
            flagh = 1;
        }
        else
        {
            flagh = 0;
        }

        if (trend[i] > 0 && dn[i] < dn[i + 1])
            dn[i] = dn[i + 1];

        if (trend[i] < 0 && up[i] > up[i + 1])
            up[i] = up[i + 1];

        if (flag == 1)
            up[i] = medianPrice + (ATRMultiplier * atr);

        if (flagh == 1)
            dn[i] = medianPrice - (ATRMultiplier * atr);

        //-- Draw the indicator
        if (i == MaxBars) continue;
        if (trend[i] == 1)
        {
            TrendUpTmp[i] = dn[i];
            if (changeOfTrend == 1)
            {
                TrendUpTmp[i + 1] = TrendDownTmp[i + 1];
                changeOfTrend = 0;
            }
        }
        else if (trend[i] == -1)
        {
            TrendDownTmp[i] = up[i];
            if (changeOfTrend == 1)
            {
                TrendDownTmp[i + 1] = TrendUpTmp[i + 1];
                changeOfTrend = 0;
            }
        }
    }
    WindowRedraw();
}



........... 


I want to use this indicator as a trigger to open position on my EA with following rules :

1) When color of indicator is green on H4 timeframe, and color of indicator just changed from red to green on M15 timeframe, then open buy

2) When color of indicator is red on H4 timeframe, and color of indicator just changed from green to red on M15 timeframe, then open sell

I use iCustom to call this indicator, and got the value : 1 point something when the line is green, and millions when the line is red. So I conclude that if value<2 on H4 I will prepare buy position only and when value>2 on H4 then I will prepare sell position only.

So, I made a script like below

Super_trend = iCustom(Symbol(),PERIOD_M15,"MQLTA MT4 Supertrend Multi-Timeframe",0,1);
Super_trend2 = iCustom(Symbol(),PERIOD_M15,"MQLTA MT4 Supertrend Multi-Timeframe",0,2);
Super_trend_H41 = iCustom(Symbol(),PERIOD_H4,"MQLTA MT4 Supertrend Multi-Timeframe",0,0);

and ......

bool buy()
{
         
   if(Super_trend_H41<2 && Super_trend<2 && Super_trend2>2)
      {return(true);}
      
   else  {return(false);} 
} 


bool sell()
{
         
   if(Super_trend_H41>2 && Super_trend>2 && Super_trend2<2)
      {return(true);}
      
   else  {return(false);} 
}

I compiled my EA without any error, but when I backtested the EA, it open buy and sell based on Super_trend and Super_trend2 only (M15 TF), and disregard Super_trend_H41 result.

I think I made mistake when calling the indicator, but I don't know exactly my mistake.

Could anyone help me to solve this problem?


Thank you,

 
Agus Widjaja:

I have multi timeframe supertrend indicator with code as below (unfortunately I cannot display all code due to limitation of character to send at forum


I want to use this indicator as a trigger to open position on my EA with following rules :

1) When color of indicator is green on H4 timeframe, and color of indicator just changed from red to green on M15 timeframe, then open buy

2) When color of indicator is red on H4 timeframe, and color of indicator just changed from green to red on M15 timeframe, then open sell

I use iCustom to call this indicator, and got the value : 1 point something when the line is green, and millions when the line is red. So I conclude that if value<2 on H4 I will prepare buy position only and when value>2 on H4 then I will prepare sell position only.

So, I made a script like below

I compiled my EA without any error, but when I backtested the EA, it open buy and sell based on Super_trend and Super_trend2 only (M15 TF), and disregard Super_trend_H41 result.

I think I made mistake when calling the indicator, but I don't know exactly my mistake.

Could anyone help me to solve this problem?


Thank you,

Attach the indicator code.
 
Super_trend = iCustom(Symbol(),PERIOD_M15,"MQLTA MT4 Supertrend Multi-Timeframe",0,1);
Super_trend2 = iCustom(Symbol(),PERIOD_M15,"MQLTA MT4 Supertrend Multi-Timeframe",0,2);
Super_trend_H41 = iCustom(Symbol(),PERIOD_H4,"MQLTA MT4 Supertrend Multi-Timeframe",0,0);

and ......

bool buy()

Always post all relevant code (using Code button) or attach the source file.
     How To Ask Questions The Smart Way. (2004)
          Be precise and informative about your problem

In what function are those assignments?

Reason: