Stop loss only during set trading hours

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #199012 quote
    ProRealP
    Participant
    Senior

    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!

    #199032 quote
    JS
    Participant
    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)

    #199033 quote
    PeterSt
    Participant
    Master

    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)

    #199068 quote
    ProRealP
    Participant
    Senior

    @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!

    #199069 quote
    JS
    Participant
    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.

    #199077 quote
    PeterSt
    Participant
    Master
    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.
    #199221 quote
    ProRealP
    Participant
    Senior
    Neither idea works. The stoploss is still fixed outside of the trading hours of the system. @nicolas, or @robertogozzi any ideas?
    #199223 quote
    robertogozzi
    Moderator
    Master
    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.
    ProRealP thanked this post
    #200319 quote
    ProRealP
    Participant
    Senior
    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?
    #200331 quote
    ProRealP
    Participant
    Senior
    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..
    #200346 quote
    Nicolas
    Keymaster
    Master
    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.
    #201462 quote
    ProRealP
    Participant
    Senior
    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.
    #201511 quote
    ProRealP
    Participant
    Senior
    @Nicolas Was able to confirm now that it’s plotted with an X. Backtest stopped at 15:30. Active system stopped at 08:18
    Stopped-27sep.png Stopped-27sep.png ActiveSystemStopped.png ActiveSystemStopped.png
    #201867 quote
    ProRealP
    Participant
    Senior
    @robertogozzi Any ideas how to solve this? Appreciate any help.
    #202814 quote
    ProRealP
    Participant
    Senior
    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..
Viewing 15 posts - 1 through 15 (of 17 total)
  • You must be logged in to reply to this topic.

Stop loss only during set trading hours


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
ProRealP @prorealpierre Participant
Summary

This topic contains 16 replies,
has 5 voices, and was last updated by ProRealP
3 years, 3 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/13/2022
Status: Active
Attachments: 2 files
Logo Logo
Loading...