Hi all,
Grateful for any help. I am trying to put together a code that lets me add more positions (up to a maximum of 4 positions) in an automatic trade.
I’m looking to enter long / short when current price is greater that 0.5% in my favour.
I am also looking to exit the position when the current position moves 3% against me. So far, I have the following but I am not sure this is right. For some reason, it also doesn’t add the additional positions:
// Definition of code parameters
DEFPARAM CumulateOrders = True // Cumulating positions activated
// Conditions to enter long positions
c1 = (close > close[21])
c2 = longonmarket AND Close => (1.005 * TradePrice)
IF c1 OR c2 THEN
BUY 0.5 PERPOINT AT MARKET
ENDIF
// Conditions to exit long positions
indicator1 = ExponentialAverage[30](close)
c3 = (DClose(0) CROSSES UNDER indicator1[9])
indicator2 = RSI[10](RSI[21](close))
c4 = (indicator2 CROSSES OVER 70)
indicator3 = DonchianChannelCenter[30]
c5 = (DClose(0) CROSSES UNDER indicator3)
c6 = PositionPerf[0] <= -3
IF c3 AND c4 AND c5 OR (c6) THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
c6 = (DClose(0) < close[21])
c7 = shortonmarket AND Close <= (TradePrice / 1.005)
IF c6 OR c7 THEN
SELLSHORT 0.5 PERPOINT AT MARKET
ENDIF
// Conditions to exit short positions
c8 = (DClose(0) > close[13])
indicator4 = DonchianChannelCenter[30]
c9 = (DClose(0) CROSSES OVER indicator4[11])
indicator5 = RSI[10](RSI[21](close))
c10 = (indicator5 CROSSES UNDER 30)
c11 = PositionPerf[0] <= -3
IF c8 AND c9 AND c10 or (c11) THEN
EXITSHORT AT MARKET
ENDIF
JSParticipant
Senior
I tried your system but it does open cumulative positions…?
More than 4 positions but this limit is not (yet) in your code…
Thanks! Also looking for help on inserting this limit.
Also does anyone know why you can backtest the stop loss and trailing stop together and as a percentage, but ProOrder won’t let you do both or add percentages?
JSParticipant
Senior
Hi,
To add a limit to the number of contracts, you can use:
If c1 or c2 and CountOfLongShares<=4 then
If c6 and c7 and CountOfShortShares<=4 then
JSParticipant
Senior
Here is a link to a trailing stop function that can be used as an alternative to the standard TS in ProBackTest…
https://www.prorealcode.com/blog/trading/complete-trailing-stop-code-function/
Do you know if this works in an automatic trade. Doesn’t seem to be working for me
Yes, it works, it has been used many times since years for traders in their strategies 🙂
For a specific trailing stop in percentage, you can look at these snippets:
Cannot Set Trailing Stop as % through IG