sometimes we need to open position immediately after a crossÂ
for example, if our price crossover a special price so open buy position and if our price crossunder a special price so open sell position .
strategy("immdiately open pos after cross", overlay=true)
Myprice = input.float(156)
hline(Myprice,color = color.yellow)
if ta.crossover(close,Myprice)
strategy.entry("long",strategy.long)
if ta.crossunder(close,Myprice)
strategy.entry("short",strategy.short)
as you see in above picture it opens position on close of candle we have not this problem in real chart if you enable calc_on_every_tick but in historical chart for back test result we get this problem.
I found solution for this problem.
we want it buy when price cross over line and vice versa for sell
we should use stop inside strategey.entry
I used this but I got in trouble then I understand should use it under some conditions.
when our price is below of line set stop order
we correct code in bellow
strategy("immdiately open pos after cross", overlay=true)
Myprice = input.float(156)
hline(Myprice,color = color.yellow)
var BuyFlag = true
var SellFlag = true
if close < Myprice
BuyFlag := false
strategy.entry("long",strategy.long,stop = Myprice)
if close > Myprice
SellFlag := false
strategy.entry("short",strategy.short,stop = Myprice)
if ta.crossover(close,Myprice)
BuyFlag := true
if ta.crossunder(close,Myprice)
SellFlag := false
configuration ! now it opens exactly when we have a crossÂ