Buy when current price is 1% above day opening

Hi,

I am trying to place buy order ( only one order ) when present candle close is greater than 1% above day opening .

I am trying with this below code. But it did not generate buy signal.

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() ); 


de = Day();
de1 = Ref(de, -1);
FC = de != de1;



TdayO = TimeFrameGetPrice( "O", inDaily );

TimeFrameRestore();

Plot(TdayO*1.01, "1%highTdayO", colorLightGrey, styleDashed | styleNoTitle);

buycond = FC AND  C > TdayO *1.01  ;

Buy =  buycond ;
Short = 0; 
Sell = 0 ;
Cover = 0;

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

BuyPrice = ValueWhen(Buy, C);
SellPrice = ValueWhen(Sell, C);
ShortPrice = ValueWhen(Short, C);
CoverPrice = ValueWhen(Cover, C);

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

Capture

Thanks and regards,

TimeFrameRestore(); this call isn't required.

What you can do is write the Buy condition more like this:

TO = TimeFrameGetPrice( "O", inDaily, -1);
dn = DateNum();
buy1 = dn == LastValue(dn) AND C > TO*1.01;
Buy = Cum( buy1 ) == 1;

PlotShapes( Buy*shapeUpArrow, colorGreen, 0, L, 0, 0);

First part of condition checks for current day and C > 1% of Open.
Then you can just count the buy signals and execute only the first one.

image

2 Likes

Thanks @travick , It works for current day. When i am scanning it for previous day no signal generated . Can you please guide.

Thanks and regards,

Code should always work on entire array in first place.

/// @link https://forum.amibroker.com/t/buy-when-current-price-is-1-above-day-opening/12555/4
dn = DateNum();
newday = dn != Ref( dn, -1);

DayOp = ValueWhen(newday, Open);
bars = BarsSince(newday)+1;

Buy = Cross(C, DayOp * 1.01);// C > DayOp * 1.01; //
Buy = Buy AND Sum(Buy,bars) == 1;

Plot( C, "Price", colorDefault, styleBar );
PlotShapes( Buy*shapeUpArrow, colorGreen, 0, L, -12, 0);

53

6 Likes

Many thanks @fxshrat. it works perfectly as required.