Sell to be happen when price close below low of the bar previous to signal bar without applystop function

Hi All,

Sell to be happen when price close below low of the bar previous to signal bar without applystop function.

Trying the below code , but sell happening when price close below at low of the next bar of Buy .

//plotting price
Plot( C,"",colorDefault,styleCandle);

//condition for buying
Buy = Cross( MACD(), Signal() );

//plot a green arrow below the candle on chart when condition is true
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorGreen,0,L,-12);

//referring to the previous bar before the condition
bar_before_buy=Ref(Buy,-1);


//referring to the previous bar's low before the condition
ll=ValueWhen(bar_before_buy,L);


Plot(ll,"",colorViolet,styleLine|styleDots);

Sell = Cross ( ll , C );

PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorWhite,0,L,-12);

Please guide.

Capture

Thanks and regards,

@RBKOL,

The issue I see is that you are referencing the Buy array. This is a "binary" array of 0/1. So your code of

bar_before_buy=Ref(Buy,-1);

will always return a Zero.

I think you are wanting to use the barindex, to reference the previous bar.

As you requested "Please guide.", I hope this guidance is what you are after.

As always, if you still have problems, do follow up posts.

@RBKOL,

for loss and target stops you have to use ApplyStop function or have write looping code.

This has been discussed already mutliple times.

https://forum.amibroker.com/t/with-defined-buy-sell-criteria-how-to-use-stoploss-as-low-of-long-entry-bar-and-high-of-short-entry-bar/10515/8

https://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/

Thanks @fxshrat for your solution.

Yes that is true for loss and target stops should have to use ApplyStop function or have write looping code . And read the link you have provided.

I must be specific in my earlier post, but i did not so apologize for that.

Actually I want to take short position when price close below low of the bar previous to signal bar. That's why searching for the result without applystop function.

Thanks and regards,

Now it becomes confusing again...

Are you sure you mean Short entry? Your first post talks about Sell which is long exit.

If you really mean entry then look here at Buy limit example

And here at Buy stop example

And change to Short Stop order.

/// based on buy limit/buy stop order examples of
/// @link http://www.amibroker.com/kb/2014/11/26/handling-limit-orders-in-the-backtester/
/// @link https://forum.amibroker.com/t/overriding-amibroker-backtester-settings-generating-unrealistic-results/7374/4
/// responded to in this thread
/// @link https://forum.amibroker.com/t/sell-to-be-happen-when-price-close-below-low-of-the-bar-previous-to-signal-bar-without-applystop-function/12629/5
ShortSignal = Cross( Signal(), MACD() );

// Short on the next bar
Short = Ref(ShortSignal, -1);
ShortStopPrice = Ref(L, -2);// low of the bar previous to signal bar

// now we check if shortstop was hit
Short = Short AND C < ShortStopPrice;

// if Open price is below the shortstop, then we use Open for entry
ShortPrice = Min(Open, ShortStopPrice);

Other than that if in case you still talk about Sell exit and don't want to use ApplyStop then write looping code.

1 Like

Thankks @fxshrat for your detailed reply. Your guidance always helpful and shows correct way.

Thanks again,