Hi Reader,
I have created a simple system where I have my Buy and Sell criteria pre defined. The problem I am facing is that my stoploss and targets are based on the buy and short price. Thus I need to use exrem before calculating sl and target levels as multiple buy signals disturbs these levels.
My basic aim is that when buy signal is generated, lowest low of last 5 candle before the signal will act - some atr will act as stop loss. Position size is calculated based on 500 Rs per trade, that's how i get no. of shares and Target is 4 times of risk.
I though this will be simple to deployed but when I run the backtest for 3 months my position size is taken from the last trade rather then the one it should have take during the time of trade.
Buy = 0;
Short = 0;
Sell = 0;
Cover = 0;
Buy1 = 0;
Short1 = 0;
Buy = PriceActionCriteriaLong AND Status("BarInRAnge") AND TimeNum()<150500;
Short = PriceActionCriteriaShort AND Status("BarInRAnge") AND TimeNum()<150500;
Buy1 = PriceActionCriteriaLong AND Status("BarInRAnge") AND TimeNum()<150500;
Short1 = PriceActionCriteriaShort AND Status("BarInRAnge") AND TimeNum()<150500;
Buy1 = (ExRem(Buy1, Sell));
Short1 = (ExRem(Short1,Cover));
BuyPrice = ValueWhen(Buy1,Close,1);
Stoplevelbuy = 0;
Targetlevelbuy = 0;
Stoplevelbuy = ValueWhen(Buy1,LLV(Low,5)-1.2*ATR(21),1);
Targetlevelbuy = ValueWhen(Buy1,BuyPrice + (BuyPrice-Stoplevelbuy )*8,1);
StopBuy = Cross(Stoplevelbuy,Low);
TargetBuy = Cross(High,Targetlevelbuy);
ShortPrice = ValueWhen(Short1,Close,1);
Stoplevelshort = 0;
Targetlevelshort = 0;
Stoplevelshort = ValueWhen(Short1,HHV(High,5)+1.2*ATR(21),1);
TargetlevelShort = ValueWhen(Short1,ShortPrice-(Stoplevelshort-ShortPrice)*8,1);
StopShort = Cross(High,Stoplevelshort);
TargetShort = Cross(TargetlevelShort,Low);
RiskPerShare = IIf(Buy1 == 1,BuyPrice - Stoplevelbuy,Stoplevelshort - ShortPrice);
x = 500/RiskPerShare;
SetPositionSize(x,spsShares);
Sell = IIf((Sum(Buy,BarsOfDay)-Sum(Sell,BarsofDay))>0, (StopBuy OR TargetBuy OR TimeNum()==151359),0) AND Status("BarInRAnge");
Cover =IIf((Sum(Short,BarsOfDay)-Sum(Cover,BarsofDay))>0,(StopShort OR TargetShort or TimeNum()==151359),0) AND Status("BarInRAnge");
Buy = (ExRem(Buy, Sell));
Short = (ExRem(Short,Cover));
Sell = Exrem(Sell,Buy);
Cover = Exrem(Cover,Short);
Any help in achieving this task is highly appreciated.
Regards,
Chirag