Multitimleframes Moving Averages in sink

Again for some reason my simple code is not working as expected though it compiles without error.
I just want that arrows are drawn in 1 minute chart
if the up- or downcolors of the moving averages are in sink, but the arrows are not drawn as if the conditions are never filled.
Thank you for your assistance.

_SECTION_BEGIN("EMA 3TFs");
Periods = Param("Periods",8, 1, 50, 1 );
bullcolor = ParamColor( "bullcolor", colorBrightGreen );
bearcolor = ParamColor( "bearcolor", colorRed );
neutralcolor = colorWhite;

DC = TimeFrameCompress( C, inDaily );
maD = EMA( DC, Periods );
colorD = IIf( maD > Ref(maD,-1), bullcolor, IIf( maD < Ref(maD,-1), bearcolor, neutralcolor ) );

HC = TimeFrameCompress( C, inHourly );
maH = EMA( HC, Periods );
colorH = IIf( maH > Ref(maH,-1), bullcolor, IIf( maH < Ref(maH,-1), bearcolor, neutralcolor ) );

x = EMA(C, periods);
color = IIf( x > Ref(x,-1), bullcolor, IIf( x < Ref(x,-1), bearcolor, neutralcolor ) );
Plot(x,"EMA",color,stylethick);
Buy = colorD == bullcolor AND colorH == bullcolor AND color == bullcolor;
Short = colorD == bearcolor AND colorH == bearcolor AND color == bearcolor;
buy = ExRem( buy, Short );
Short = ExRem( Short, buy );
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);        
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);     
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
Title = "";
_SECTION_END();

I also tried with the method of TimeFrameSet and TimeFrameRestore() but without any arrows drawn neither....

TimeFrameCompress need TimeFrameExpand

Re-read documentation

https://www.amibroker.com/guide/h_timeframe.html

Well, could you be a bit more specific please ?
I had this article constantly open as I thought to refer to it by writing my code....
I only want the arrows drawn if the direction of the moving averages of daily and hourly are in sink with the direction of the open 1 minute chart.

If your chart is set to 1minute, anything calculated in higher timeframe, must be expendaded to 1minute BEFORE you can use it.

From documentation

TimeFrameSet( in5Minute ); // switch to 5 minute frame

/* MA now operates on 5 minute data, ma5_13 holds time-compressed 13 bar MA of 5min bars */

ma5_13 = MA( C, 13 );

TimeFrameRestore(); // restore time frame to original

TimeFrameSet( inHourly ); // switch now to hourly

mah_9 = EMA( C, 9 ); // 9 bar moving average from hourly data

TimeFrameRestore(); // restore time frame to original

Plot( Close, "Price", colorWhite, styleCandle );

// plot expanded average

Plot( TimeFrameExpand( ma5_13, in5Minute), "13 bar moving average from 5 min bars", colorRed );
Plot( TimeFrameExpand( mah_9, inHourly), "9 bar moving average from hourly bars", colorRed );

it calculates a MA in 5minutes a EMA in 1 hour and BEFORE using in minute chart
it uses TIMEFRAMEEXPAND

2 Likes

Thank you for your answer.
But as I mentionned I also tried with
TimeFrameSet and TimeFrameRestore()
and still didn't get the expected result.
Here is my code

_SECTION_BEGIN("EMA 3TFs");
Periods = Param("Periods",8, 1, 50, 1 );
bullcolor = ParamColor( "bullcolor", colorBrightGreen );
bearcolor = ParamColor( "bearcolor", colorRed );
neutralcolor = colorWhite;

TimeFrameSet( inDaily );
maD = EMA( C, Periods );
colorD = IIf( maD > Ref(maD,-1), bullcolor, IIf( maD < Ref(maD,-1), bearcolor, neutralcolor ) );
TimeFrameRestore(); 

TimeFrameSet( inHourly );
maH = EMA( C, Periods );
colorH = IIf( maH > Ref(maH,-1), bullcolor, IIf( maH < Ref(maH,-1), bearcolor, neutralcolor ) );
TimeFrameRestore();

x = EMA(C, periods);
color = IIf( x > Ref(x,-1), bullcolor, IIf( x < Ref(x,-1), bearcolor, neutralcolor ) );
Plot(x,"EMA",color,stylethick);

Buy = colorD == bullcolor AND colorH == bullcolor AND color == bullcolor;
Short = colorD == bearcolor AND colorH == bearcolor AND color == bearcolor;
buy = ExRem( buy, Short );
Short = ExRem( Short, buy );

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);        
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);     
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
Title = "";
_SECTION_END();

I see TimeFrameSet, TimeFrameRestore, BUT do not see any TimeFrameEXPAND in your code

Sure because it's a different method.
Like in your citation of the code, the expansion
concerns the drawing only.
I don't want to draw the expanded MA,
I only want to test the colors in sink for all 3 timeframes.

come on, I am trying to help,
just try using TimeFrameExpand before using it

AGAIN extracted from DOCUMENTATION

IMPORTANT: TimeFrameExpand IS REQUIRED for any formula that uses TimeFrame functions. If you don't expand time compressed data you will have incorrect timestamps (see description below in "How it works").*

3 Likes

@awilson is trying hard to point you in the right direction, so I suggest showing him some appreciation. The point that I think you may not be grasping is that TimeFrameRestore will ONLY expand the built-in arrays like OHLC, etc. However, it will not do anything to variables that you have defined. So even if you use TimeFrameSet/TimeFrameRestore to create a new compressed array without the use of TimeFrameCompress, you STILL have to use TimeFrameExpand before you can use that compressed array in your base timeframe.

2 Likes

I still don't get it
I tried

TimeFrameSet( inDaily );
Dc = TimeFrameexpand( Close, inDaily );
maD = EMA( DC, Periods );
colorD = IIf( maD > Ref(maD,-1), bullcolor, IIf( maD < Ref(maD,-1), bearcolor, neutralcolor ) );
TimerameRestore();

and I tried simply this

Dc = TimeFrameexpand( Close, inDaily );
maD = EMA( DC, Periods );
colorD = IIf( maD > Ref(maD,-1), bullcolor, IIf( maD < Ref(maD,-1), bearcolor, neutralcolor ) );

Nothing works !

Tomasz has provided a clear and well-documented example in the TimeFrameCompress documentation. Perhaps that will clear things up for you.

TimeFrameSet( inDaily );
maD = EMA( DC, Periods );
colorD = IIf( maD > Ref(maD,-1), bullcolor, IIf( maD < Ref(maD,-1), bearcolor, neutralcolor ) );
TimerameRestore();

colorD = TimeFrameexpand( colorD, inDaily );

if you intend to use maD in minute timeframe, you need to expand it too

I also tried this in 6 differtent ways, no result !
May be someone could be so kind to deliver the code,
because I'm not sure someone tested it to see the results...

It sure does, post your last code with corrections

Thank you, I can't see the code, but at first sight
it seems to me the daily timeframe part is lacking.

It's not really reflecting the original code, which runs
in a 1 minute timeframe. The direction of the
same period moving average in the daily, the hourly
and the 1 minute chart should be in sink
to enter a position, very simple principle.

I tried now first to draw the lines and I reduced the timeframes to simplify.
Now I notice that the color or the lines are not corresponding and even change constantly when
I scroll. Any suggestion why this happens ?

_SECTION_BEGIN("EMA 3TFs");
Periods = Param("Periods",8, 1, 50, 1 );
bearcolor = ParamColor( "bearcolor", colorRed );
bullcolor = ParamColor( "bullcolor", colorbrightGreen );
neutralcolor = coloryellow;

x = EMA(C, periods);
color = IIf( x>Ref(x,-1), bullcolor, IIf( x<Ref(x,-1), bearcolor, neutralcolor ) );
color = ValueWhen(color != neutralcolor, color);

TimeFrameSet( 3*inHourly );
maD = EMA( C, Periods );
colorD = IIf( maD>Ref(maD,-1), bullcolor, IIf( maD<Ref(maD,-1), bearcolor, neutralcolor ) );
colorD = ValueWhen(colorD != neutralcolor, color);
TimeFrameRestore();

TimeFrameSet( inHourly );
maH = EMA( C, Periods );
colorH = IIf( maH>Ref(maH,-1), bullcolor, IIf( maH<Ref(maH,-1), bearcolor, neutralcolor ) );
colorH = ValueWhen(colorH != neutralcolor, color);
TimeFrameRestore(); 

maDexp = TimeFrameExpand( maD, 3*inHourly);
colorDexp = TimeFrameExpand( colorD, 3*inHourly);

maHexp = TimeFrameExpand( maH, inHourly);
colorHexp = TimeFrameExpand( colorH, inHourly);

Plot( x, "MA", color, stylethick | styleNoLabel | styleNoTitle); 
Plot( maDexp, "maDexp", colorDexp, stylethick | styleNoLabel | styleNoTitle); 
Plot( maHexp, "maHexp", colorHexp, stylethick | styleNoLabel | styleNoTitle); 
Title = "";
_SECTION_END();

For starters, you have copy & paste errors in your code:

colorD = ValueWhen(colorD != neutralcolor, color);

Should be

colorD = ValueWhen(colorD != neutralcolor, colorD);

Same error for colorH.

1 Like

Thank you very much, simple inattention !
Sorry for the disturbance.