BiParticipant
New
Hi
I am trying to build a long only strategy where I have the below trade management rules.
- StopLoss set at 1 ATR (14)
- First Target set at 1 ATR(14)
- Trailing stop starting at 2 ATR(14) and trailing step at 1.5 ATR(14)
My StopLoss, First target and trailing seems to work but for some weird reason system is opening short positions even though I have no Short orders. The only sell orders I have exit ones, I tried add the code “Longonmarket” as well as “Countofpositions” still I am getting short orders
Much appreciate if you could let me know where I am going wrong please?
// Definition of code parameters
DEFPARAM CumulateOrders = TRUE // Cumulating positions deactivated
// Conditions to enter long positions
keltnersma = ExponentialAverage[20](close)
keltnerpos1atr = keltnersma+ExponentialAverage[20](AverageTrueRange[1](close)*1)
keltnerneg1atr = keltnersma+ExponentialAverage[20](AverageTrueRange[1](close)*-1)
longindicator1 = Call Strategy01LongV1
longindicator2 = Call Strategy02LongV2
l1 = (longindicator1 = 1)
l2 = (longindicator2 = 1)
within1ATR = close >keltnerneg1atr and close<keltnerpos1atr
//trade management
lotsize = 2 //original position size
Lot2Close =1 //close one lot while reaching first target
stoploss = Averagetruerange[14](close)*SLFactor
firsttarget= Averagetruerange[14](close)*PTFactor
trailingstart = Averagetruerange[14](close)*2
trailingstep = Averagetruerange[14](close)*1.5
//trade entry and exits
if l1 or l2 and within1ATR then
Buy lotsize LOT AT MARKET
SET STOP PLOSS stoploss
endif
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
IF newSL=0 AND close-tradeprice(1)>=firsttarget THEN
Sell lot2close LOT At Market
newSL=tradeprice(1)+firsttarget
ENDIF
//first move (breakeven)
IF newSL>0 AND close-newSL>=trailingstart THEN
newSL = newSL+trailingstart
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep THEN
newSL = newSL+trailingstep
ENDIF
ENDIF
//stop order to exit the positions
IF Countofposition>0 and LONGONMARKET AND newSL>0 THEN
SELL AT newSL STOP
ENDIF
(See attached strategy and code)
I am trying to run it on my platform to see if I get the same … well weird??
Is the Strategy01LongV1 and v2 in the Library on this website?
BiParticipant
New
Thank you, below the v1,v2 code. I believe rest of the conditions are in the strategy code itself.
//v1
atr = AverageTrueRange[14](close)*1/4
v1low = ABS(low - low[1])> atr
v1lowcheck = low<low[1]
v1close= close>close[1]
if v1low and v1lowcheck and v1close then
value = 1
else
value = 0
endif
return value
//V2
atr = AverageTrueRange[14](close)*1/4
v1low = ABS(low[1] - low[2])> atr
v1lowcheck = low[1] < low[2]
notv1= close[1] < close[2]
v2close1 = close > close[1]
v2close2 = close > close[2]
v2low = low>low[1]
v2gap = abs(close[1]-open)<=atr
if v1low and v1lowcheck and notv1 and v2close1 and v2close2 and v2low and v2gap then
value = 1
else
value = 0
endif
return value
The problem is in the implementation of the lot closure when reaching first target; removing this condition from the trailing stop there’s no shorts
I confirm same … so you are not seeing things! Unless we are both deluded!? 🙂
I get 1 short trade when running on DJI 1000 bars and 5 min TF and spread = 2.
When I put spread = 4 I do not get a short trade on DJI 1000 bars and 5 min TF.
That’s one of the weirdest thing I’ve ever come across since I’ve been using PRT??
I think it has something to do with the line of code below.
When I change to ‘Buy at Market’, I get no shorts … yet!? 🙂
Buy lotsize LOT AT MARKET
Somehow the code is buying 2 and selling 3 to exit??
Various values for ptfactor will give shorts or stop shorts also
firsttarget= Averagetruerange[14](close)*PTFactor
Seems there are several changes that will stop the Shorts, but how can a System go short when there is NO SellShort command in the code??
Very worrying!!??
BiParticipant
New
thank you for looking into it. I do agree this is odd.
Also I understand scaling out on a auto-trading is not possible but I am only try to backtest my strategy how I would manually trade it. Hence the first target close is important. But why market is shorting even with the below condition is beyond my understanding.
IF Countofposition>0 and LONGONMARKET AND newSL>0 THEN
BiParticipant
New
I think i fixed it, the issue was the calculation of breakeven and trailingstart were off and also I had to specify lot to close (remaining) at the end. Which I think is the reason for the short position.
Yeah at certain conditions you are selling 3 after buying 2 (?) … still weird though as I would have expected a System Reject / Stop covered by one of IG’s ambiguous screen messages!?
Maybe one day somebody might be able to make profitable use of this loophole!? 🙂
BiParticipant
New
haha yes! the shorts were actually making my system look profitable got a bit carried away with the results. Glad to have checked it.
Re profitable … how about introducing (proper) shorts into the strategy?
BiParticipant
New
I do have shorts as well with the same logic reversed but wanted to keep the strategies Long only and Short only to review the results accurately. Also I am testing this on US shares where there is a long bias but have to test this out in other markets like FX or Commodities where trends are not one sided.
Please feel free to improve it, if you do manage to please do let me know.
Please feel free to improve it
I had a quick go this morning … I could see that it needed a filter to stop Longs when price was clearly in a big downtrend.
I tried below and I used values of up to x = 10000 on a 5 min TF, but still it was taking Longs … I thought more weirdness!? 🙂
Close > Average[x](Close)