Multiple Time Frame SAR

Dear Seniors,

Current interval is 1 minute.
Trying to plot 1 min SAR and 3 min SAR in chart. Try with this code below.

acc = Param("Acceleration", 0.02, 0, 1, 0.001 );
accm = Param("Max. acceleration", 0.2, 0, 1, 0.001 );
Plot( SAR( acc, accm ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style", styleDots | styleNoLine, maskDefault | styleDots | styleNoLine ) );



TimeFrameSet( 3*in1Minute ); // switch now to 3min 

acc = Param("Acceleration", 0.02, 0, 1, 0.001 );
accm = Param("Max. acceleration", 0.2, 0, 1, 0.001 );
Plot( SAR( acc, accm ), _DEFAULT_NAME(), ParamColor( "Color", colorCycle ), ParamStyle("Style", styleDots | styleNoLine, maskDefault | styleDots | styleNoLine ) );

TimeFrameRestore(); // restore time frame to original 

Definitely i am doing wrong. 3 min SAR not plotted properly in the chart.

Capture

Please guide.

Thanks and regards,

Have you read here?
http://www.amibroker.com/guide/h_timeframe.html

It is all there. Pay special attention to the orange marked blocks there.

You have to expand after compression. So apply TimeFrameExpand after TimeframeRestore. Look at the examples of upper link.

Also Plot() belongs to after TimeFrameRestore() function call.

Params don't have to be inside time frame set procedure. They are not arrays but return numbers.

4 Likes

Thanks @fxshrat for your immediate reply. Let me go through it properly and try with this. Will get back.

Thanks and Regards,

Now this TWO SAR ( 1 min AND 3 min ) plotted on the chart with below code.

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 


acc = Param("Acceleration", 0.02, 0, 1, 0.001 );
accm = Param("Max. acceleration", 0.2, 0, 1, 0.001 );
FSAR = SAR( acc, accm );
Plot( FSAR, "SAR", colorRed, styleDots | styleNoLine  );

TimeFrameSet( 3*in1Minute ); // switch now to 3min 

FSAR3m = SAR( acc, accm );

TimeFrameRestore(); // restore time frame to original 

// plot expanded 3*in1Minute SAR

Plot( TimeFrameExpand( FSAR3m, 3*in1Minute), " 3 min SAR", colorWhite, styleDots | styleNoLine ); 

Now i am trying to add BUY AND SELL condition on that.
BUY
If Close greater than 1 min SAR and 3 Min SAR .

SELL
If Close less than 1 min SAR and 3 Min SAR .

Below the code is . But it not gives proper signal.

Buy = C > FSAR AND C > FSAR3m;

Sell = C < FSAR AND C < FSAR3m;

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);


BuyPrice = ValueWhen(Buy, C);
SellPrice = ValueWhen(Sell, C);


PlotShapes(Buy * shapeUpTriangle, colorBlue, 0, L, -10);

PlotShapes(Sell * shapeDownTriangle, colorOrange, 0, H, -10);

InkedCapture_LI

Please guide.

Thanks & Regards,

You signals still use compressed arrays.

You need to assign TImeFrameExpand(...) to a variable to be called later in further code.

Here is correction.

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

acc = Param("Acceleration", 0.02, 0, 1, 0.001 );
accm = Param("Max. acceleration", 0.2, 0, 1, 0.001 );
FSAR = SAR( acc, accm );
Plot( FSAR, "SAR", colorRed, styleDots | styleNoLine  );

TimeFrameSet( 3*in1Minute ); // switch now to 3min 
	FSAR3m = SAR( acc, accm );
TimeFrameRestore(); // restore time frame to original 

// expanding FSAR3m to align it to selected interval
FSAR3m = TimeFrameExpand(FSAR3m, 3*in1Minute);

// plot expanded 3*in1Minute SAR
Plot( FSAR3m, " 3 min SAR", colorWhite, styleDots | styleNoLine ); 

Buy = C > FSAR AND C > FSAR3m;
Sell = C < FSAR AND C < FSAR3m;

Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

BuyPrice = C;
SellPrice = C;

PlotShapes(Buy * shapeUpTriangle, colorBlue, 0, L, -10);
PlotShapes(Sell * shapeDownTriangle, colorOrange, 0, H, -10);
5 Likes

Thanks @fxshrat.It works now.
That means whenever combining more than one time frame signal , need to assign TImeFrameExpand(...).

Thanks again for your guidance.

Got nothing to do with signals.

It is good practice to always assign TimeFrameExpand() to a variable after TimeFrameRestore.

  1. Avoiding to forget about expansion (variable(s)) when adding further code later (making use of multi time frames).
  2. Avoiding multiple same function calls.
  3. Better code readability.
3 Likes

Thank You. Will keep in mind.

It is all in the manual. Just read the ORANGE BOXES in the manual http://www.amibroker.com/guide/h_timeframe.html
The text inside orange boxes is extremely important

1 Like