Donchian Stop Loss
Forums › ProRealTime English forum › ProOrder support › Donchian Stop Loss
- This topic has 28 replies, 3 voices, and was last updated 7 years ago by Mark.
-
-
09/07/2016 at 7:33 PM #12839
Hi all,
I’m sure this is really simple, and I have been trying lots of different ways to produce this…. I want to add in a stop loss that trails my order by following a donchian channel – and as soon as the price hits it, the order is closed.
I’ve tried this approach
DC=20
a=512345678910//stop loss donchianC1 = Highest[DC](high)C2 =Lowest[DC](low)if longonmarket thensell a perpoint at c2 stopendifif shortonmarket thensell a perpoint at c1 stopendifbut it doesn’t actually close the order, just buys or sells.
Also, this gets me out at the low of the bar after it’s breached the stop loss – I want a stop loss that closes the order immediately when the price touches the donchian channel value.
Any help would be greatly appreciated!
Thanks,
09/08/2016 at 11:09 AM #1285909/08/2016 at 11:24 AM #12861No worry, I have moved your topic to the correct section of the forum.
You are stuck like many other people with the correct highest high and lowest low value in real time! 🙂
Because when the current low of the actual candlestick breach the lowest low over the last x periods, it becomes the new lowest low, so you can’t test the [0] value of your donchian channel to make your condition, you need to test the value of at least one bar in the past. You also made a mistake about closure of the sell positions, you need to use EXITSHORT to close them:
1234567if longonmarket thensell a perpoint at c2[1] stopendifif shortonmarket thenexitshort a perpoint at c1[1] stopendif09/08/2016 at 12:22 PM #12864Thank you very much Nicolas, and apologies for putting the request in the wrong place!
Does this mean that the price will automatically now trigger the stop immediately, and not at the end of the bar (in live mode)? For example, if a donchian channel low is at 99, and the price falls below this during the bar and closes the bar at 97 – will the order be executed at 99 or 97?
Also (maybe a newbie question), do I need to use “exitlong” rather than sell?
I’ll add my total system code once I get this bit working 🙂
09/08/2016 at 12:36 PM #1286509/08/2016 at 2:07 PM #1286709/09/2016 at 9:20 AM #12880It worked perfectly, thank you Nicolas!
Is there away to turn the trailling off overnight – so that the stop loss stays at the value it was at 9pm until 7am (then it begins to trail the donchian again?)
I’m trying to reproduce a manual system that I have traded for a long time, but only when I come to code it do I realise exactly what I do!
Thanks
09/09/2016 at 11:34 AM #1288209/11/2016 at 5:21 PM #12952Hi Nicolas,
Hope you had a good weekend! I’ve been testing the system and it is getting a lot closer to my actual performance – which is great.
However, the way I have programmed the stop loss meana that it turns off the stop loss at 9pm, rather than freezing the value at the 9pm value until 7am.
What I would like to achieve is a stop loss that trails the donchian channel 7am-9pm, and at 9pm stays at that level until the next morning.
Here is how I have coded it:
1234567891011121314//stop loss donchianDC=50donchtime = time<210000 and time>070000C1= Highest[DC](high)C2=Lowest[DC](low)if longonmarket and donchtime thensell a perpoint at c2[1] stopendifif shortonmarket and donchtime thenexitshort a perpoint at c1[1] stopendifCan this be done?
Many thanks
09/12/2016 at 7:37 AM #12961Hi, thanks have a good weekend, done some coding jobs 🙂
Glad to hear that you have successfully coded your manual trading to an automated one. Please tell us on what instrument and timeframe you are trading it, it would be of interest of other traders here.
So if you want to fix your stoploss when you are not in the ‘donchtime’ period, you first need to save your last stop positions:
123456789if longonmarket and donchtime thensell a perpoint at c2[1] stoplaststop = c2[1]endifif shortonmarket and donchtime thenexitshort a perpoint at c1[1] stoplaststop = c1[1]endifThen you can set your stop orders when you are not ‘donchtime’ at this level:
1234if onmarket and not donchtime thensell at laststop stopexitshort at laststop stopendif09/13/2016 at 2:00 PM #13019Hi Nicolas, my code gets ever closer to being finished! Once it’s ready I look forward to uploading on to the site.
I have 1 part of my system which is proving difficult, but currently it is missing and appears to be a big part of the profitability!
My active trading day is 7am-9pm – overnight my stop loss is active but I would not close trades for any other reason. This system uses 3 moving averages, and crosses trigger both the entry and exits during the trading day. Basically, if the fast MA is above the slow MA I’m long, and if it crosses below I’m short. The middle EMA is used for exits with the same logic (if long, and the fast MA crosses below the middle MA I close).
My issue is that, for example – if I was long overnight and the fast MA crosses back between the middle and slow MA’s at 6.30am for example, PRT will not close the trade at 7am as the cross happened outside of trading hours – this is causing some large losses as nothing closes the trade until the stop loss is hit.
What I need is for the programme to do this (using long as example):
If longonmarket at 7am open:
Immediately close long trade if fast MA is below middle MA but above slow MA
Immediately close long trade if fast MA is below slow MA and open short position
Opposite for short trades – Is this possible?
Many thanks
09/13/2016 at 2:09 PM #13021Also, if flat overnight and there is a crossover of the fast and slow MA then I’d like to open a position at 7am.
For example, if I’m flat at 9pm (no position), but at 4am the fast MA crosses over the Slow MA – in this situation I’d like to go long at 7am.
09/13/2016 at 4:34 PM #13028About your first request, you need to test your moving average positions (above/below etc..) only at 7am:
123if longonmarket and time=070000 and MAfast<MAslow thenSELL AT MARKETendifTo initiate a new trade at 7am because a cross over/under occurred during the night, a solution would be to test the MA trend at last bar of your trading day and at first bar. If your test return a change (from bullish to bearish for example), then launch a trade accordingly.
09/13/2016 at 8:29 PM #1303609/13/2016 at 8:30 PM #13037123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114// Definition of code parametersDEFPARAM CumulateOrders = False // Cumulating positions deactivated//variablespositionsize=2x=8y=80z=160DC=80//Time ConstraintNewTRADE = time > 065900 AND time < 211000CloseTRADE = time > 065900 AND time < 220000donchtime = time>070000 and time<220000// Conditions to enter long positionsindicator1 = ExponentialAverage[x](close)indicator2 = ExponentialAverage[z](close)c1 = (indicator1 CROSSES OVER indicator2)indicator3 = ExponentialAverage[x](close)indicator4 = ExponentialAverage[z](close)c2 = (indicator3 > indicator4)IF c1 AND c2 AND NewTrade THENBUY positionsize PERPOINT AT MARKETENDIF// Conditions to exit long positionsindicator5 = ExponentialAverage[x](close[1])indicator6 = ExponentialAverage[y](close[1])c3 = (indicator5 CROSSES UNDER indicator6)indicator7 = ExponentialAverage[x](close)indicator8 = ExponentialAverage[y](close)c4 = (indicator7 < indicator8)IF c3 AND c4 AND CloseTrade THENSELL AT MARKETENDIF// Conditions to enter short positionsindicator9 = ExponentialAverage[x](close[1])indicator10 = ExponentialAverage[z](close[1])c5 = (indicator9 CROSSES UNDER indicator10)indicator11 = ExponentialAverage[x](close)indicator12 = ExponentialAverage[z](close)c6 = (indicator11 < indicator12)IF c5 AND c6 AND NewTrade THENSELLSHORT positionsize PERPOINT AT MARKETENDIF// Conditions to exit short positionsindicator13 = ExponentialAverage[x](close[1])indicator14 = ExponentialAverage[y](close[1])c7 = (indicator13 CROSSES OVER indicator14)indicator15 = ExponentialAverage[x](close)indicator16 = ExponentialAverage[y](close)c8 = (indicator15 > indicator16)IF c7 AND c8 AND CloseTrade THENEXITSHORT AT MARKETENDIF//7am positions//close longif longonmarket and time=070000 and indicator1<indicator8 thenSELL AT MARKETendif//close long and go shortif longonmarket and time=070000 and indicator1<indicator2 thenSELLSHORT positionsize PERPOINT AT MARKETendif//close shortif shortonmarket and time=070000 and indicator1>indicator16 thenexitshort AT MARKETendif//close short and go longif shortonmarket and time=070000 and indicator1>indicator12 thenBUY positionsize PERPOINT AT MARKETendif//no position, open longif not onmarket and time=070000 and indicator1>indicator12 thenBUY positionsize PERPOINT AT MARKETendif//noposition, open shortif not onmarket and time=070000 and indicator1<indicator12 thenSELLSHORT positionsize PERPOINT AT MARKETendif//stop loss donchianC1= Highest[DC](high)C2=Lowest[DC](low)if longonmarket and donchtime thensell positionsize perpoint at c2[1] stoplaststop = c2[1]endifif shortonmarket and donchtime thenexitshort positionsize perpoint at c1[1] stoplaststop = c1[1]endifif onmarket and not donchtime thensell at laststop stopexitshort at laststop stopendif -
AuthorPosts
Find exclusive trading pro-tools on