Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Executing a trade at a specific time

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

    Executing a trade at a specific time

    I am trying to execute a strategy that places a trade at a specific time. Here is the code I use to check for the specific time (along with some test code):

    public class TestStrategy : Strategy
    {
    private TimeSpan eod5Min = new TimeSpan(9, 25, 0);
    private bool alreadyProcessed = false;

    protected override void Initialize()
    {
    // Add a 5 minute Bars object: BarsInProgress index = 1
    Add(PeriodType.Minute, 5);

    CalculateOnBarClose = false;
    }

    protected override void OnBarUpdate()
    {
    DateTime Min5Time = Times[1][0];
    // Check which Bars object is calling the OnBarUpdate() method
    if (BarsInProgress == 1)
    {
    // time test
    LogMsg logMsg2; // I have my own logging class
    logMsg2 = new LogMsg();
    logMsg2.TimeStamp(Time[0]);
    logMsg2.Append("Min5Time: ", Min5Time.TimeOfDay.ToString() );

    if (Min5Time.TimeOfDay.CompareTo(eod5Min ) == 0)
    {
    logMsg2.Append("equals eod5Min : ", eod5Min .ToString());
    }
    else
    {
    logMsg2.Append("not equal to eod5Min : ", eod5Min .ToString());
    }
    Log(logMsg2.DisplayLogMessage, LogLevel.Information);
    // time test


    if (Min5Time.TimeOfDay.CompareTo(eod5Min) == 0)
    {
    if (alreadyProcessed)
    return;
    else
    {
    ProcessCurrentState();
    alreadyProcessed = true;
    }
    }
    else
    {
    alreadyProcessed = false;
    return;
    }
    }
    else if (BarsInProgress == 0)
    {
    }
    }
    }

    Here are excerpts from the log:

    2/11/2011 9:23:52 AM|1|4|2/11/2011 9:00:00 AM - Min5Time: : 09:00:00, not equal to test5Min: : 09:25:00
    2/11/2011 9:23:52 AM|1|4|2/11/2011 9:05:00 AM - Min5Time: : 09:05:00, not equal to test5Min: : 09:25:00
    2/11/2011 9:23:52 AM|1|4|2/11/2011 9:10:00 AM - Min5Time: : 09:10:00, not equal to test5Min: : 09:25:00
    2/11/2011 9:23:52 AM|1|4|2/11/2011 9:15:00 AM - Min5Time: : 09:15:00, not equal to test5Min: : 09:25:00
    2/11/2011 9:23:52 AM|1|4|2/11/2011 9:20:00 AM - Min5Time: : 09:20:00, not equal to test5Min: : 09:25:00
    2/11/2011 9:23:52 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:23:56 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:00 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:02 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:03 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:05 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:07 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:08 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:09 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:10 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:12 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:12 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:15 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:16 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:17 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:19 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:26 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:28 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:29 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:30 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:33 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:41 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:45 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:47 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:52 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:24:53 AM|1|4|2/11/2011 9:25:00 AM - Min5Time: : 09:25:00, equals test5Min: : 09:25:00
    2/11/2011 9:25:04 AM|1|4|2/11/2011 9:30:00 AM - Min5Time: : 09:30:00, not equal to test5Min: : 09:25:00
    2/11/2011 9:25:20 AM|1|4|2/11/2011 9:30:00 AM - Min5Time: : 09:30:00, not equal to test5Min: : 09:25:00
    2/11/2011 9:25:21 AM|1|4|2/11/2011 9:30:00 AM - Min5Time: : 09:30:00, not equal to test5Min: : 09:25:00
    2/11/2011 9:25:24 AM|1|4|2/11/2011 9:30:00 AM - Min5Time: : 09:30:00, not equal to test5Min: : 09:25:00

    It doesn’t look like this line of code:

    DateTime Min5Time = Times[1][0];


    stores seconds. It also doesn’t look like there is any consistency as to how often the OnBarUpdate() method is called. I would think it would be every tick/second but according to the log it does not.

    I added code to check for whether the trade has been processed.

    if (Min5Time.TimeOfDay.CompareTo(eod5Min) == 0)
    {
    if (alreadyProcessed)
    return;
    else
    {
    ProcessCurrentState();
    alreadyProcessed = true;
    }
    }
    else
    {
    alreadyProcessed = false;
    return;
    }


    The questions I have are:
    1. Is there a way to get seconds? Although considering that I may not hit the OnBarUpdate() method at exactly the second I want, that may not be the best way to trigger the trade.
    2. Is there a better way of handling the trade than to set a flag that checks to see if it has already been processed?

    #2
    Hello ExactlyMyPoint,

    Times[1][0] is a time object that represents the time stamp of a bar. It includes seconds, but since you're using 5 minute bars, there's only a new value every 5 minutes and will always fall on the :00 portion. You'll see for example 12:35:00, 12:40:00, 12:45:00, etc. You can print this to see what's returned.

    Print(Times[1][0]);

    OnBarUpdate() is called for every tick if running with CalculateOnBarClose = false and real time. If COBC is set to true or you're accessing historic values, it's only processed at the close of a bar.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      I am running CalculateOnBarClose = false, and the log was from real time data. The time stamp from the log seems to calculate seconds, but the price bar, ie,

      DateTime Min5Time = Times[1][0];

      only seems to have hours and minutes, no seconds. Yes, it is a 5 minute bar. Is that the reason??

      So it looks like I will be needing the "alreadyProcessed" flag to assure that I only trade once.

      Comment


        #4
        Yes, it represents the time stamp of a bar, so can only be updated every 5 minutes on a 5 minute chart. The time object includes seconds as part of its structure, but they're not particularly meaningful if the object is only updated every 5 minutes. Print it and check out the output. Here's an example:

        2/11/2011 10:55:00 AM
        2/11/2011 11:00:00 AM
        2/11/2011 11:05:00 AM

        If you want to use your computer clock as time reference, use DateTime.Now instead of Time[]
        Ryan M.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        647 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        369 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        108 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        572 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        573 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X