Stop loss only during set trading hours
Forums › ProRealTime English forum › ProOrder support › Stop loss only during set trading hours
- This topic has 16 replies, 5 voices, and was last updated 1 year ago by ProRealP.
-
-
08/13/2022 at 11:51 AM #199012
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
1234567If onmarket and TradeTime thenset stop loss slendifIf onmarket and not TradeTime thenset stop loss 0endifHowever, 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!
08/14/2022 at 7:18 AM #199032You 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)
08/14/2022 at 8:13 AM #199033I 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 :
123If LongOnMarket and TradeTime and Close > SL thenSell at SL STOPEndIfIt 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 :1234567If onmarket and TradeTime thenset stop loss slendifIf onmarket and not TradeTime thenset stop loss 0endif(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 :123456789101112If Not OnMarket and TradeTime thenBuy x shares at marketsl = Close - y // y is your determined sl price.endifIf OnMarket and TradeTime thenSet Stop Loss slendifIf Onmarket and not TradeTime then // Be careful not to run into cut trade days and this never being executed in time.set stop loss 0endif(not tested)
08/15/2022 at 7:58 AM #19906808/15/2022 at 8:20 AM #199069I 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.
08/15/2022 at 10:25 AM #199077So 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.
08/19/2022 at 7:17 AM #199221Neither idea works. The stoploss is still fixed outside of the trading hours of the system. @nicolas, or @robertogozzi any ideas?
08/19/2022 at 8:21 AM #199223There you go (tested on DAX, 1h TF):
12345678910111213141516171819202122232425262728293031N = 100sl = 100 * pipsizetp = 400 * pipsizeOK = time >= 100000 AND time <= 170000if OK and not onmarket and close crosses over average[N,0](close) thenbuy at marketexitprice = close - slendifif OK and not onmarket and close crosses under average[N,0](close) thensellshort at marketexitprice = close + slendifset target profit tpif OK thenif longonmarket thenif close > exitprice thensell at exitprice STOPelsif close < exitprice thensell at exitprice LIMITendifelsif shortonmarket thenif close > exitprice thenexitshort at exitprice LIMITelsif close < exitprice thenexitshort at exitprice STOPendifendifendifgraph 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.
1 user thanked author for this post.
09/07/2022 at 5:15 PM #200319Thank 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
123if not ok thenset stop loss 0endifWould this interfere with your code?
09/08/2022 at 6:15 AM #200331The 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..
09/08/2022 at 9:40 AM #20034609/26/2022 at 9:42 PM #20146209/27/2022 at 5:22 PM #20151110/03/2022 at 8:11 PM #201867@robertogozzi Any ideas how to solve this?
Appreciate any help.
10/19/2022 at 8:45 AM #202814The 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..
-
AuthorPosts
Find exclusive trading pro-tools on