Multi time frame indicator issue

I am using two time frames viz 15 mins & 30 mins.
I have STOCHASTIC momentum indicator in both charts.

Now I want when Stochastic indicator changes direction in 15 mins time frame and the new direction is in same direction of stochastic indicator in 30 mins chart, it will draw UP arrow and DOWN arrow.

Issue :

  1. I put 30 mins Stochastic indicator in 15 mins chart. When I compare with 30 mins Stochastic indicator, it does not match.
  2. The UP arrows and DOWN arrows are drawn not as per the logic built.
    I am uploading my AFL scripts. Please see what I am missing. I will also upload the screenshot.
LookBack = Param("Lookback", 13, 2, 100 );
Smooth1 = Param("Smooth 1", 25, 1, 100 );
Smooth2 = Param("Smooth 2", 2, 1, 100 );

HH = HHV( H, LookBack );
LL = LLV( L, LookBack );

StoMom = 100 * EMA( EMA( C - 0.5 * ( HH + LL ), Smooth1 ), Smooth2 ) / 
( 0.5 * EMA( EMA( HH - LL, Smooth1 ), Smooth2 ) );

Plot(StoMom, _DEFAULT_NAME(), ParamColor("Color", ColorCycle ) );

//===============30 mins=======================================
tf15= 2*in15Minute;
TimeFrameSet( tf15 ); // switch to 15 minute time frame
HH30 = HHV( H, LookBack );
LL30 = LLV( L, LookBack );
StoMom30 = 100 * EMA( EMA( C - 0.5 * ( HH + LL ), Smooth1 ), Smooth2 ) / 
( 0.5 * EMA( EMA( HH30 - LL30, Smooth1 ), Smooth2 ) );
Plot(StoMom30, _DEFAULT_NAME(), ParamColor("Color", ColorCycle ) );
TimeFrameRestore(); // restore time frame to original
// ====================END of 30 Mins===================================
sm30=TimeFrameExpand(StoMom30,2*in15Minute);

long=StoMom>Ref(StoMom,-1) AND Ref(StoMom,-1)<Ref(StoMom,-2) AND sm30>Ref(sm30,-1) ;
shor=StoMom<Ref(StoMom,-1) AND Ref(StoMom,-1)>Ref(StoMom,-2) AND sm30<Ref(sm30,-1);

Plotshapes(IIf(shor,shapedownTriangle, Null),colorLightBlue, layer=0, yposition=StoMom, offset=-10,XShift=0);
Plotshapes(IIf(long,shapeupTriangle, Null),colorLightGrey, layer=0, yposition=StoMom, offset=-10,XShift=0);

```![mutiframe%20indicator|690x343](upload://eCBqWEbPVYrE0l42sZgFLDpt2ZX.jpg)

Not tested! You should not plot while inside TimeFrame and before expanding, unless you know what you are doing

LookBack = Param("Lookback", 13, 2, 100 );
Smooth1 = Param("Smooth 1", 25, 1, 100 );
Smooth2 = Param("Smooth 2", 2, 1, 100 );

HH = HHV( H, LookBack );
LL = LLV( L, LookBack );

StoMom = 100 * EMA( EMA( C - 0.5 * ( HH + LL ), Smooth1 ), Smooth2 ) / 
( 0.5 * EMA( EMA( HH - LL, Smooth1 ), Smooth2 ) );

Plot(StoMom, _DEFAULT_NAME(), ParamColor("Color", ColorCycle ) );

//===============30 mins=======================================
tf15= 2*in15Minute;
TimeFrameSet( tf15 ); // switch to 15 minute time frame
HH30 = HHV( H, LookBack );
LL30 = LLV( L, LookBack );
StoMom30 = 100 * EMA( EMA( C - 0.5 * ( HH + LL ), Smooth1 ), Smooth2 ) / 
( 0.5 * EMA( EMA( HH30 - LL30, Smooth1 ), Smooth2 ) );
TimeFrameRestore(); // restore time frame to original
// ====================END of 30 Mins===================================
sm30=TimeFrameExpand(StoMom30,tf15);
Plot(sm30, _DEFAULT_NAME(), ParamColor("Color", ColorCycle ) );

long=StoMom>Ref(StoMom,-1) AND Ref(StoMom,-1)<Ref(StoMom,-2) AND sm30>Ref(sm30,-1) ;
shor=StoMom<Ref(StoMom,-1) AND Ref(StoMom,-1)>Ref(StoMom,-2) AND sm30<Ref(sm30,-1);

Plotshapes(IIf(shor,shapedownTriangle, Null),colorLightBlue, layer=0, yposition=StoMom, offset=-10,XShift=0);
Plotshapes(IIf(long,shapeupTriangle, Null),colorLightGrey, layer=0, yposition=StoMom, offset=-10,XShift=0);

Thanks you for your response. I modified the code and put the plot call outside of the different time frame.
It seems system drew the indicators as it was doing earlier.
I am attaching one screenshot where I see different result.
in 15 mins, indicator changed direction to UPWARD but the indicator still is DOWNWARD in 30 mins time frame.
System still drew the UP arrow,
mutiframe%20indicator2

You had an error at the line where you calculate StoMom30, try now

LookBack = Param("Lookback", 13, 2, 100 );
Smooth1 = Param("Smooth 1", 25, 1, 100 );
Smooth2 = Param("Smooth 2", 2, 1, 100 );

HH = HHV( H, LookBack );
LL = LLV( L, LookBack );

StoMom = 100 * EMA( EMA( C - 0.5 * ( HH + LL ), Smooth1 ), Smooth2 ) / 
( 0.5 * EMA( EMA( HH - LL, Smooth1 ), Smooth2 ) );

Plot(StoMom, "StoMom", ParamColor("ColorSto", ColorCycle ), styleDots );

//===============30 mins=======================================
tf15= 2*in15Minute;
TimeFrameSet( tf15 ); // switch to 15 minute time frame
HH30 = HHV( H, LookBack );
LL30 = LLV( L, LookBack );
StoMom30 = 100 * EMA( EMA( C - 0.5 * ( HH30 + LL30 ), Smooth1 ), Smooth2 ) / 
( 0.5 * EMA( EMA( HH30 - LL30, Smooth1 ), Smooth2 ) );
TimeFrameRestore(); // restore time frame to original
// ====================END of 30 Mins===================================
sm30=TimeFrameExpand(StoMom30,tf15);
Plot(sm30, "sm30", ParamColor("Colorsm30", ColorCycle ), styleDots );

long=StoMom>Ref(StoMom,-1) AND Ref(StoMom,-1)<Ref(StoMom,-2) AND sm30>Ref(sm30,-1) ;
shor=StoMom<Ref(StoMom,-1) AND Ref(StoMom,-1)>Ref(StoMom,-2) AND sm30<Ref(sm30,-1);

Plotshapes(IIf(shor,shapedownTriangle, Null),colorLightBlue, layer=0, yposition=StoMom, offset=-10,XShift=0);
Plotshapes(IIf(long,shapeupTriangle, Null),colorLightGrey, layer=0, yposition=StoMom, offset=-10,XShift=0);

1 Like

Hi awilson,

Thank you very much for pointing out the error in the code. Now it seems the Indicators are coming correctly.

For making the indicators direction visibly distinctive, I want put one color for UPWARD movement and another color for DOWNWARD movement. Total 4 colors for 2 indicators in 2 different time frames.

They are -

  1. For 15 mins timeframe indicator - I used BLUE for uptrend & WHITE for downtrend=> both appearing correctly.
  2. For 30 mins timeframe indicator - I used GREEN for uptrend and RED for downtrend => Uptrend is colored correctly in GREEN, but the downtrend instead of coloring in RED - it is mixed with RED & GREEN.
    Can you check what is wrong/suggest ways to get correct color.
    I am giving additional piece of code and the screenshot.
sm30c=IIf(sm30>=Ref(sm30,-1),colorGreen, colorRed);
stomc=IIf(StoMom>=Ref(StoMom,-1),colorBlue,colorwhite);

Plot(StoMom, "StoMom", stomc, styleDots );
Plot(sm30, "sm30", sm30c, styleDots );

Color%20issue

Look for other plots in your code.
Looks like you have another plot in your code.

Let me put the whole code here:

LookBack = Param("Lookback", 13, 2, 100 );
Smooth1 = Param("Smooth 1", 25, 1, 100 );
Smooth2 = Param("Smooth 2", 2, 1, 100 );

HH = HHV( H, LookBack );
LL = LLV( L, LookBack );

StoMom = 100 * EMA( EMA( C - 0.5 * ( HH + LL ), Smooth1 ), Smooth2 ) / 
( 0.5 * EMA( EMA( HH - LL, Smooth1 ), Smooth2 ) );
//===============30 mins=======================================
tf15= 2*in15Minute;
TimeFrameSet( tf15 ); // switch to 15 minute time frame
HH30 = HHV( H, LookBack );
LL30 = LLV( L, LookBack );
StoMom30 = 100 * EMA( EMA( C - 0.5 * ( HH30 + LL30 ), Smooth1 ), Smooth2 ) / 
( 0.5 * EMA( EMA( HH30 - LL30, Smooth1 ), Smooth2 ) );
TimeFrameRestore(); // restore time frame to original
// ====================END of 30 Mins===================================
sm30=TimeFrameExpand(StoMom30,tf15);

sm30c=IIf(sm30>=Ref(sm30,-1),colorGreen, colorRed);
stomc=IIf(StoMom>=Ref(StoMom,-1),colorBlue,colorwhite);

Plot(StoMom, "StoMom", stomc, styleDots );
Plot(sm30, "sm30", sm30c, styleDots );

long=StoMom>Ref(StoMom,-1) AND Ref(StoMom,-1)<Ref(StoMom,-2) AND sm30>Ref(sm30,-1) ;
shor=StoMom<Ref(StoMom,-1) AND Ref(StoMom,-1)>Ref(StoMom,-2) AND sm30<Ref(sm30,-1);
PlotGrid(0, colorYellow, 10, 2, False ); 

Plotshapes(IIf(shor,shapedownTriangle, Null),colorLightBlue, layer=0, yposition=StoMom, offset=-10,XShift=0);
Plotshapes(IIf(long,shapeupTriangle, Null),colorLightGrey, layer=0, yposition=StoMom, offset=-10,XShift=0);

LookBack = Param("Lookback", 13, 2, 100 );
Smooth1 = Param("Smooth 1", 25, 1, 100 );
Smooth2 = Param("Smooth 2", 2, 1, 100 );

HH = HHV( H, LookBack );
LL = LLV( L, LookBack );

StoMom = 100 * EMA( EMA( C - 0.5 * ( HH + LL ), Smooth1 ), Smooth2 ) / 
( 0.5 * EMA( EMA( HH - LL, Smooth1 ), Smooth2 ) );
//===============30 mins=======================================
tf15= 2*in15Minute;
TimeFrameSet( tf15 ); // switch to 15 minute time frame
HH30 = HHV( H, LookBack );
LL30 = LLV( L, LookBack );
StoMom30 = 100 * EMA( EMA( C - 0.5 * ( HH30 + LL30 ), Smooth1 ), Smooth2 ) / 
( 0.5 * EMA( EMA( HH30 - LL30, Smooth1 ), Smooth2 ) );
sm30c=IIf(StoMom30>=Ref(StoMom30,-1),colorGreen, colorRed);
TimeFrameRestore(); // restore time frame to original
// ====================END of 30 Mins===================================
sm30=TimeFrameExpand(StoMom30,tf15);
sm30c=TimeFrameExpand(sm30c,tf15);

stomc=IIf(StoMom>=Ref(StoMom,-1),colorBlue,colorwhite);

Plot(StoMom, "StoMom", stomc, styleDots );
Plot(sm30, "sm30", sm30c, styleDots );

long=StoMom>Ref(StoMom,-1) AND Ref(StoMom,-1)<Ref(StoMom,-2) AND sm30>Ref(sm30,-1) ;
shor=StoMom<Ref(StoMom,-1) AND Ref(StoMom,-1)>Ref(StoMom,-2) AND sm30<Ref(sm30,-1);
PlotGrid(0, colorYellow, 10, 2, False ); 

Plotshapes(IIf(shor,shapedownTriangle, Null),colorLightBlue, layer=0, yposition=StoMom, offset=-10,XShift=0);
Plotshapes(IIf(long,shapeupTriangle, Null),colorLightGrey, layer=0, yposition=StoMom, offset=-10,XShift=0);

3 Likes

Excellent !!!
It works.

Thank you so much.