Hello!
I’m working on a system that’s only active during US trading hours. That is, I’ve set the trading hours in PRT and have added a set trade time to my partial close.
An issue I’m trying to solve is how to make the stop loss only active during the trading hours the system is restricted to. What happens is that the stop loss is set during trading hours, but if the prices closes beneath/above the stop outside trading hours, it still triggers.
I figured I might add the following to mittigate the issue
If onmarket and TradeTime then
set stop loss sl
endif
If onmarket and not TradeTime then
set stop loss 0
endif
However, I reckon this will set the stop loss to a new level when US trading resumes the next day.
Any ideas how I could set it to the original stop loss, or another way to solve this?
Thanks!
JSParticipant
Senior
Hi @ProRealPierre
You can use a (pending) STOP order…
If LongOnMarket and Close > mySL then
Sell at mySL STOP
EndIf
(If Close does not touch the pending stop order, it will be placed again every next bar)
I figured I might add the following to mittigate the issue
Yes, that would be the solution.
However, I reckon this will set the stop loss to a new level when US trading resumes the next day.
What JS mentions may also do jobs, but it requires some imagination to let it do what you want. Still it will do about what you want, but change it to this :
If LongOnMarket and TradeTime and Close > SL then
Sell at SL STOP
EndIf
It is different to the sense that this operates at the bar level (each time again), while the Set Stop Loss is set once (or twice in your case 😉 ). The Set Stop Loss will therefore be more efficient, generally.
Not if you set up the sl variable properly;
If you only assign that a value at your Buy (or SellShort) command and leave it unchanged while OnMarket, the next day after market open you can just let this part go again :
If onmarket and TradeTime then
set stop loss sl
endif
If onmarket and not TradeTime then
set stop loss 0
endif
(which means it is just always active)
and it will set the same StopLoss level as it did the previous day. Of course meanwhile the price can have dropped through that SL level (your sl variable) and don’t ask me what happens then. Possibly your position is not exited because probably (?) the price must drop through that SL level, while at the moment it did, your StopLoss was not active.
Anyway, something like this :
If Not OnMarket and TradeTime then
Buy x shares at market
sl = Close - y // y is your determined sl price.
endif
If OnMarket and TradeTime then
Set Stop Loss sl
endif
If Onmarket and not TradeTime then // Be careful not to run into cut trade days and this never being executed in time.
set stop loss 0
endif
(not tested)
@JS
Thanks! I can’t seem to get the same results with a limit stop though. Is it because the stop is reset each bar, so it works almost like a trailing?
@PeterSt
So adding ” and Close > SL” to what JS wrote? In your other two examples you write exactly the same as what I did?
Appreciate the help!
JSParticipant
Senior
@ProRealPierre
I thought your question was about “Stop loss only during set trading hours”?
A (pending) STOP order is placed again every next bar and will only change if your “SL” is changed every bar, otherwise it will be fixed.
So adding ” and Close > SL” to what JS wrote? In your other two examples you write exactly the same as what I did?
No … I added “and TradeTime” to JS’s example – the thing what it is about for you.
Neither idea works. The stoploss is still fixed outside of the trading hours of the system.
@nicolas, or
@robertogozzi any ideas?
There you go (tested on DAX, 1h TF):
N = 100
sl = 100 * pipsize
tp = 400 * pipsize
OK = time >= 100000 AND time <= 170000
if OK and not onmarket and close crosses over average[N,0](close) then
buy at market
exitprice = close - sl
endif
if OK and not onmarket and close crosses under average[N,0](close) then
sellshort at market
exitprice = close + sl
endif
set target profit tp
if OK then
if longonmarket then
if close > exitprice then
sell at exitprice STOP
elsif close < exitprice then
sell at exitprice LIMIT
endif
elsif shortonmarket then
if close > exitprice then
exitshort at exitprice LIMIT
elsif close < exitprice then
exitshort at exitprice STOP
endif
endif
endif
graph positionprice * positionperf / pipsize as "tempGain"
graphonprice tradeprice as "Entry price"
graphonprice exitprice coloured(255,0,0,255) as "Stop Loss"
beware that this will set the SL pending oders at the end of your trading time and this pending order will expire at the closing of the next candle, so you may occasionlly experience and exit outside your time range.
Thank you, Roberto!
I’ve added your suggestion to my system, but the issue seems to remain. I’m sure the reason is what you stated that I may occasionally experience an exit outside the time range, but it seems to happen frequently. Any other ideas as how to mitigate it entirely? What would the difference be technically if say I had something like
if not ok then
set stop loss 0
endif
Would this interfere with your code?
The odd and quite frustrating thing is that in back test with your code it works. With add set stop loss 0 it works in back test. Without adding either and only specifying the trade times for the system it works in back test, but the actual results are not the same. Would be swell if there was a function to paus the system outside of the trading hours, sort of like how you wouldn’t be able to sell stock when the market isn’t open..
What is the symbol plotted on chart? an X is a close by code action or a pending STOP/LIMIT order while a square is a SET STOP LOSS.
I’m having some trouble testing it as my system doesn’t run on a very low timeframe. Is there some way I can see it without the stop triggering? Doesn’t show up on backtest as it doesn’t look the same as live.
@Nicolas
Was able to confirm now that it’s plotted with an X.
Backtest stopped at 15:30.
Active system stopped at 08:18
@robertogozzi Any ideas how to solve this?
Appreciate any help.
The pending order way doesn’t work more then 50% of the time so far, so it’s not really usable. I don’t understand why I can’t use a set stop loss when ok and set stop loss 0 when not ok, or the like. Any other ideas? Would really appreciate the help as this is the only limitation on an otherwise great system. Not to mention it works in backtest, but not when running..