Widen stoploss overnight

Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #77849 quote
    Seb
    Participant
    Average

    Hello, I am trying to code a stoploss that widens when the DAX futures market closes. I have set the chart of the DAX CFD from IG to custom trading hours 08:00:00 – 22:00:00 (because this is the data I want to use for indicators). I tried it with this code:

    if longonmarket and time >= 080000 and time < 215500 then
    sell at SLtight stop
    else
    if longonmarket then
    sell at SLwide stop
    endif
    endif

    As can be seen in the picture in the attachment (this is activity from the demo account), the code doesn’t do what it’s supposed to do. It widens the stoploss at 21:55 and at 22:00 it takes the stoploss out entirely. Than at 01:00 it puts back the wide stop and 08:05 it goes back to the tight stop. Ideal would be stoploss wider at 22:00 and stoploss back to tight at 08:00, when the DAX futures open. Can it be done?

    activity.png activity.png
    #77851 quote
    Vonasi
    Moderator
    Master

    Why not use a SET order to place the orders just once and then modify them at the desired time?

    SLTight = 50
    SLWide = 100
    
    if onmarket and time = 080000 then
    set stop ploss SLTight
    endif
    
    if onmarket and time = 220000 then
    set stop ploss SLWide
    endif
    

    If you have a limited risk account then your variable stop distance will not work because you cannot move a stop further away on a limited risk account.

    #77854 quote
    Seb
    Participant
    Average

    Thank you for your reply Vonasi, The reason that I don’t use the ploss function is because I use an indicator based trailing stop. I tried coding with the ploss function in the past, but I believe the reason I didn’t use it is that it set faulty stops when there was a gap between the open and close of two candles.

    #77857 quote
    Vonasi
    Moderator
    Master

    If you calculate the number of pips in the stop distance with POSITIONPRICE – (your indicators stop price) then it would be set at the correct level. 

    It would not be possible to calculate this for one bar so you could use a STOP order for the first bar and then use the SET instruction afterwards just to widen and narrow the stop distance twice a day.

    #77861 quote
    Seb
    Participant
    Average

    Thanks I will try it! So this also works with a trailing stop based on for example a moving average?

    #77862 quote
    Vonasi
    Moderator
    Master

    Yes – no reason why not.

    POSITIONPRICE is the average of all open positions. TRADEPRICE is the price of the last position opened. You need to be aware that if you have multiple positions open then when using the SET instruction each one will have a separate stop order set at whatever distance you calculate. With the pending STOP order you were first using then just one level is set and all positions closed at this price.

    #77864 quote
    Seb
    Participant
    Average

    So if you scale into a long position on different entry levels, set stop ploss (POSITIONPRICE – moving average) would trail correctly and close the position if price reaches the moving average?

    #77869 quote
    Seb
    Participant
    Average

    To make my question more concrete, how can you achieve the trailing stoploss of the following code with the SET STOP PLOSS function? (to be able to widen the stop at 22:00)

    defparam preloadbars = 150
    defparam cumulateorders = false
     
    mm = average[100](close)
    ATRsl = mm - highest[50](averagetruerange[100](close)*2)
     
    condition = close crosses over mm
     
    if condition then
     buy 1 lot at market
     //first stoploss:
     SL = close-ATRsl
     set stop ploss SL
     dynamicSL = SL
    endif
     
    //dynamicSL
    if longonmarket then 
     if(ATRsl>dynamicSL) then 
      dynamicSL=ATRsl
     endif
     sell at dynamicSL stop
    endif
     
    GRAPH dynamicSL as "dyn"

     

    #77873 quote
    Vonasi
    Moderator
    Master

    Sorry but I’m a bit too busy at the moment to look into your code with too much detail but I think it is as I explained before.  Every time your buy condition is met then the stop loss levels for all of the already open positions will be altered to the newly calculated level from their opening prices – so they will all have different levels of stops. For your example due to the scaling in SET is not the right instruction to use.

    You will need to go back to pending stop orders and work out why they are not working for you.

    #77934 quote
    Seb
    Participant
    Average

    Alright, that’s a shame. The documentation quotes “Set a stop loss x units from the average position price.” for the SET STOP LOSS function. This is a bit confusing than, since “average” position price suggests the position consists of multiple prices (a scaled-in position).

    #77935 quote
    robertogozzi
    Moderator
    Master

    Your line 12-13

     SL = close-ATRsl
     set stop ploss SL

    works with DAX, but not with FX pairs, since PLOSS requires PIPS, while SL is a difference in price. You should either use, in line 13:

    set stop ploss SL/pipsize

    or use LOSS, instead:

    set stop loss SL

    Moreover, line 22 is trying to exit a LONG position SELLing at a difference in price (say 0085), instead of a price (say 12510). Pending orders require a PRICE to enter/exit at some point.  

    #77943 quote
    Seb
    Participant
    Average

    Thank you Roberto, I’m testing with DAX, so the FX issue is not causing any problems. For stoplosses I’ve up ’till now used the technique from the code I posted yesterday, which I got from a PRC blog post (link is above the code). (i.e.:)

    if longonmarket then
    sell at SL stop
    endif

    This works fine for indicator based trailing stops (that only move your way) in combination with scaling in. I just define at what price level SL has to be and than the whole position closes at that level if price gets there.

    From Vonasi’s posts I believe that with the SET STOP function indicator based trailing stops can be achieved, but not if you scale-in. I’m testing it in my demo account now, but this takes a while.

    #77948 quote
    Seb
    Participant
    Average

    My logic is that if you want to make an indicator based trailing stop with the SET STOP function, (Positionprice – trailing indicator level) becomes a negative amount if the trailing indicator gets beyond your entry price (for a long). You are using a negative number for SET STOP than. Because you are not taking a loss but a profit at the trailing level. Correct?

    #77951 quote
    robertogozzi
    Moderator
    Master

    That’s correct. You can use ABS() to make sure you have a positive result.

     

    #77952 quote
    Seb
    Participant
    Average

    But if you use ABS() you have a trailing stop that never takes a profit I assume, which is not the intention of a trailing stop.

     

Viewing 15 posts - 1 through 15 (of 21 total)
  • You must be logged in to reply to this topic.

Widen stoploss overnight


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Seb @seb Participant
Summary

This topic contains 20 replies,
has 4 voices, and was last updated by GraHal
7 years, 6 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 08/09/2018
Status: Active
Attachments: 1 files
Logo Logo
Loading...