Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close Trade depending on Time

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Close Trade depending on Time

    Hi,

    In my strategy, I want to close all trades after 3 pm. The code works fine in 99% of all cases.
    I added a time series = 1 min in the Initialize() method and I use a time comparison OnBarUpdate() method.

    protected override void OnBarUpdate()
    {
    [code]
    if(BarsInProgress == 8)
    {
    Print(Time[0].ToString("dd-MMM HH:mm:ss"));
    if(ToTime(Time[0]) >= IniHour*10000+IniMin*100)
    {
    [code to close trades]
    }
    }
    }

    However, when there is no data (I guess that's the reason), the time is not printed in the output window and the trade is not closed.
    I am attaching two images:
    - one showing the gap in the price
    - the other is the output windows where there is a gap also in the time being printed (e.g. 29 nov 12:15 to 01 dec 17:01)

    Is there a workaround that does not depend on the OnBarUpdate() method to close all trades after an specific time?

    Thanks,





    #2
    Hello Capablanca,

    Thanks for your post.

    Your screenshot does not show the chart time nor what instrument is being shown.

    There was an early close (12:15 PM) for the CME on Friday November 29th, 2019 (Day after Thanksgiving). This would seem to match up with your printed time statements that ended at 12:15 and then began again on Sunday evening session). In that case no there would be no market data to trigger OnBarUpdate() or closing trades.

    There would be no workaround other than to change your strategies to check to see what the close of the session is daily and adjusting that way or simply enabling "Exit On session close" when you apply the strategy (and setting the exit on close seconds to match up with how soon before the close to exit).


    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    566 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    330 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    547 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    548 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X