Need Help with Exrem and Value when as they tend to cancel out each other

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

When a Trade is taken, why don't you store those Values to a static variable. Anyway, past data since Trade taken wont change.

If Incase you detect changes in levels after the trade is taken, then you can keep updating the static variables.

Have you tried something like this?

1 Like

I have used Buy1 and short1 for this purpose only.. To get the values for stop loss and Target.

But then position size is coming out different then it should be.

I really need help with this.. any help is appreaciated.

Where have you mentioned that they are static variables? or maybe you don't know what static variables are.
If you really need help, you should provide all the info.

I have provided all the code above if you can understand it as there is no more info needed apart from this for an expert.

That's the problem with some, still no research. Only being adamant.
Keep wondering why no one else is bothered.

These are not static variables,
https://www.amibroker.com/guide/afl/staticvarset.html its here.

Also, you would've got the answer way back if you acted more sensible.

see this, it is the mother of all major problems, but you chose not to use it. ApplyStop()
https://www.amibroker.com/guide/afl/applystop.html

With this function, all type of exits can be implemented.
Infact, one doesn't even have to break the head to calculate the Entry price. Its handled implicitly.

Anyway, I wont bother responding but thought i'll leave with a solution.

It can also be used with Equity() to achieve some very cool stuff.

http://www.amibroker.com/kb/2016/01/28/how-does-risk-mode-trailing-stop-work/
ApplyStop() works with the Entry price and just does a lot of work.

1 Like

You see... you have provided some insights now without me giving any additional information about it which you could have provided earlier rather be demeaning to new learners. Nevertheless I have solved it myself.

And try putting apply stop where you want a stop loss as lowest of last 5 candles. Good luck!!

Hi @Chirag1,

Can you please share how you solved this? I am having the same issue of excess buy signals impacting my code that exrem does not help.

Thanks,

Alex