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=5
//stop loss donchian
C1 = Highest[DC](high)
C2 =Lowest[DC](low)
if longonmarket then
sell a perpoint at c2 stop
endif
if shortonmarket then
sell a perpoint at c1 stop
endif
but 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,
Have I put this in the wrong category? I’m new to the site so apologies if this is not the right place to ask the question.
No 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:
if longonmarket then
sell a perpoint at c2[1] stop
endif
if shortonmarket then
exitshort a perpoint at c1[1] stop
endif
Thank 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 🙂
SELL close buy orders.
Yes trades will close at the exact price you put your pending stop orders at.
Fanstastic, thank you Nicolas – I look forward to testing this and learning!
It 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
Write a time condition like this:
tcondition = time<210000 and time>070000
and add this true condition in the trailing stop function.
Hi 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:
//stop loss donchian
DC=50
donchtime = time<210000 and time>070000
C1= Highest[DC](high)
C2=Lowest[DC](low)
if longonmarket and donchtime then
sell a perpoint at c2[1] stop
endif
if shortonmarket and donchtime then
exitshort a perpoint at c1[1] stop
endif
Can this be done?
Many thanks
Hi, 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:
if longonmarket and donchtime then
sell a perpoint at c2[1] stop
laststop = c2[1]
endif
if shortonmarket and donchtime then
exitshort a perpoint at c1[1] stop
laststop = c1[1]
endif
Then you can set your stop orders when you are not ‘donchtime’ at this level:
if onmarket and not donchtime then
sell at laststop stop
exitshort at laststop stop
endif
Hi 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
Also, 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.
About your first request, you need to test your moving average positions (above/below etc..) only at 7am:
if longonmarket and time=070000 and MAfast<MAslow then
SELL AT MARKET
endif
To 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.
Thank you Nicolas, I have attempted this but I have got it very wrong – the system takes a position every day at 7am!!!!
Here is the full code so far, it will probably help for you to see it in total:
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
//variables
positionsize=2
x=8
y=80
z=160
DC=80
//Time Constraint
NewTRADE = time > 065900 AND time < 211000
CloseTRADE = time > 065900 AND time < 220000
donchtime = time>070000 and time<220000
// Conditions to enter long positions
indicator1 = 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 THEN
BUY positionsize PERPOINT AT MARKET
ENDIF
// Conditions to exit long positions
indicator5 = 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 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator9 = 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 THEN
SELLSHORT positionsize PERPOINT AT MARKET
ENDIF
// Conditions to exit short positions
indicator13 = 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 THEN
EXITSHORT AT MARKET
ENDIF
//7am positions
//close long
if longonmarket and time=070000 and indicator1<indicator8 then
SELL AT MARKET
endif
//close long and go short
if longonmarket and time=070000 and indicator1<indicator2 then
SELLSHORT positionsize PERPOINT AT MARKET
endif
//close short
if shortonmarket and time=070000 and indicator1>indicator16 then
exitshort AT MARKET
endif
//close short and go long
if shortonmarket and time=070000 and indicator1>indicator12 then
BUY positionsize PERPOINT AT MARKET
endif
//no position, open long
if not onmarket and time=070000 and indicator1>indicator12 then
BUY positionsize PERPOINT AT MARKET
endif
//noposition, open short
if not onmarket and time=070000 and indicator1<indicator12 then
SELLSHORT positionsize PERPOINT AT MARKET
endif
//stop loss donchian
C1= Highest[DC](high)
C2=Lowest[DC](low)
if longonmarket and donchtime then
sell positionsize perpoint at c2[1] stop
laststop = c2[1]
endif
if shortonmarket and donchtime then
exitshort positionsize perpoint at c1[1] stop
laststop = c1[1]
endif
if onmarket and not donchtime then
sell at laststop stop
exitshort at laststop stop
endif