Code adjustment to avoid 3 trades per day on Breakout Code

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #14747 quote
    Cosmic1
    Participant
    Senior

    Morning, So the breakout code is working well on 3 markets and over the last 5 weeks everything is in profit so good news there. One issue is that if a trade is opened and closed within the same bar then 3 trades are possible, this is not ideal as it can skew the results and cause unexpected drawdown like yesterday on the DAX with three small losers. I know why it does this but unsure how to fix it. This fix if there is a possible one will also help another strat I’m working on.

    Cheers

    #14751 quote
    Tom C
    Participant
    New

    I’m also interested in a fix for this. What BO code are you using?

    thanks

    #14752 quote
    Nicolas
    Keymaster
    Master

    One solution that come to mind: store the barindex when you launch a trade and test if the current one is the same as the old one when trying to initiate a new trade..

    #14791 quote
    Cosmic1
    Participant
    Senior

    What you are saying makes sense but I’m not sure how to code it. Is there any examples in the library that you are aware of or a blog?

    Tom: It’s the breakout code I posted in the library which is a port from the original CAC

    #14807 quote
    Nicolas
    Keymaster
    Master

    Could you please remember us why there is this behaviour of the 3 trades possible here? I know we have together already discussed about it, but I don’t remember sorry 🙂

    The code I were thinking of is the same as in this french topic: http://www.prorealcode.com/topic/cloture-systematique-a-la-fin-de-bougie-suivant-le-signal/

    #14864 quote
    Cosmic1
    Participant
    Senior
    
    //-------------------------------------------------------------------------
    // Main code : Breakout ProOrder EN DAX
    //-------------------------------------------------------------------------
    // We do not store datas until the system starts.
    // If it is the first day that the system is launched and if it is afternoon,
    // it will be waiting until the next day for defining sell and buy orders
    
    //All times are UK Time Zone
    
    DEFPARAM PreLoadBars = 0
    // Position is closed at 20h00 PM
    DEFPARAM FlatAfter = 200000
    // No new position will be initiated after the 16h00 PM candlestick
    LimitHour = 161500
    // Market scan begin with the 15 minute candlestick that closed at 8h15 AM
    StartHour = 081500
    // The 24th and 31th days of December will not be traded because market close before 7h45 PM
    IF (Month = 5 AND Day = 1) OR (Month = 12 AND (Day = 24 OR Day = 25 OR Day = 26 OR Day = 30 OR Day = 31)) THEN
    TradingDay = 0
    ELSE
    TradingDay = 1
    ENDIF
    // Variables that would be adapted to your preferences
    if time = 074500 then
    //PositionSize = max(2,2+ROUND((strategyprofit-1000)/1000)) //gain re-invest trade volume
    PositionSize = 1 //constant trade volume over the time
    endif
    MaxAmplitude = 170 //170 //140 //160
    MinAmplitude = 22 //24 //15
    OrderDistance = 9 //9 //7  //10
    PourcentageMin = 19 //19 //30 //32
    // Variable initilization once at system start
    ONCE StartTradingDay = -1
    // Variables that can change in intraday are initiliazed
    // at first bar on each new day
    IF (Time <= StartHour AND StartTradingDay <> 0) OR IntradayBarIndex = 0 THEN
    BuyTreshold = 0
    SellTreshold = 0
    BuyPosition = 0
    SellPosition = 0
    StartTradingDay = 0
    ELSIF Time >= StartHour AND StartTradingDay = 0 AND TradingDay = 1 THEN
    // We store the first trading day bar index
    DayStartIndex = IntradayBarIndex
    StartTradingDay = 1
    ELSIF StartTradingDay = 1 AND Time <= LimitHour THEN
    // For each trading day, we define each 15 minutes
    // the higher and lower price value of the instrument since StartHour
    // until the buy and sell tresholds are not defined
    IF BuyTreshold = 0 OR SellTreshold = 0 THEN
    HighLevel = Highest[IntradayBarIndex - DayStartIndex + 1](High)
    LowLevel = Lowest [IntradayBarIndex - DayStartIndex + 1](Low)
    // Spread calculation between the higher and the
    // lower value of the instrument since StartHour
    DaySpread = HighLevel - LowLevel
    // Minimal spread calculation allowed to consider a significant price breakout
    // of the higher and lower value
    MinSpread = DaySpread * PourcentageMin / 100
    // Buy and sell tresholds for the actual if conditions are met
    IF DaySpread <= MaxAmplitude THEN
    IF SellTreshold = 0 AND (Close - LowLevel) >= MinSpread THEN
    SellTreshold = LowLevel + OrderDistance
    ENDIF
    IF BuyTreshold = 0 AND (HighLevel - Close) >= MinSpread THEN
    BuyTreshold = HighLevel - OrderDistance
    ENDIF
    ENDIF
    ENDIF
    // Creation of the buy and sell orders for the day
    // if the conditions are met
    IF SellTreshold > 0 AND BuyTreshold > 0 AND (BuyTreshold - SellTreshold) >= MinAmplitude THEN
    IF BuyPosition = 0 THEN
    IF LongOnMarket THEN
    BuyPosition = 1
    ELSE
    BUY PositionSize CONTRACT AT BuyTreshold STOP
    ENDIF
    ENDIF
    IF SellPosition = 0 THEN
    IF ShortOnMarket THEN
    SellPosition = 1
    ELSE
    SELLSHORT PositionSize CONTRACT AT SellTreshold STOP
    ENDIF
    ENDIF
    ENDIF
    ENDIF
    // Conditions definitions to exit market when a buy or sell order is already launched
    IF LongOnMarket AND ((Time <= LimitHour AND SellPosition = 1) OR Time > LimitHour) THEN
    SELL AT SellTreshold STOP
    ELSIF ShortOnMarket AND ((Time <= LimitHour AND BuyPosition = 1) OR Time > LimitHour) THEN
    EXITSHORT AT BuyTreshold STOP
    ENDIF
    // Maximal risk definition of loss per position
    // in case of bad evolution of the instrument price
    SET STOP PLOSS MaxAmplitude
    //set target pprofit 190//190
    
    #14865 quote
    Cosmic1
    Participant
    Senior

    It is your original CAC Breakout code modified to work on DAX. Are you saying that the thread you linked to holds the key to sorting this issue? If so I will spend some time trying to implement it.

    The reason this happens is because the strategy can sometimes open and close a trade within the same bar and so is only recorded as one trade. This creates an option for a third.

    #14931 quote
    Nicolas
    Keymaster
    Master

    Yes it is a common problem that will be solved with multitimeframe support hopefully.

    At the beginning of the code, you can add something like this, that will test is a trade is currently on market:

    if longonmarket then 
     currentbar = barindex
    endif

    Then each time you launch a trade add this just below the trade launch instruction:

    BUY 1 SHARE AT MARKET 
    savedbar=barindex1

    and now you could test if the “currentbar” is not the same as the “savedbar” or at least 1 bar old. I think the tradeindex function can’t help because it needs at least  1 bar to compute.

    Sometimes we have to remember that the code are read from beginning to the end, so it is obviously a good idea to refresh variables state at the beginning of the code.

    This should work .. or not, just a Saturday morning idea, between coffee and children’s cartoons on TV 🙂

    Cosmic1 thanked this post
    #14982 quote
    Cosmic1
    Participant
    Senior

    Thanks Nicolas, will check this out this week. I did try to implement tradeindex without success.

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

Code adjustment to avoid 3 trades per day on Breakout Code


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Cosmic1 @cosmic1 Participant
Summary

This topic contains 9 replies,
has 3 voices, and was last updated by Cosmic1
9 years, 3 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/12/2016
Status: Active
Attachments: No files
Logo Logo
Loading...