Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

I'm trying to enter a trade at a specific time

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

    I'm trying to enter a trade at a specific time

    Hello,

    I'm working on a strategy to enter a trade just before typical economic news at 10amEST, which would be 7amPST for my time zone. So ideally it would enter long in this case at 6:59:45am.

    I think I've got the correct code, but when I run the strategy, I see my buy orders are filled(see the Data Box>Execution>Time) at exactly 7am.

    Am I doing something wrong or should I test this in again using real-time data? I've pasted my code below.

    Thank you!



    Code:
    protected override void OnBarUpdate()
    {
    // Define your target entry time (e.g., 09:59:45)
    DateTime targetEntryTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 6, 59, 45);
    
    // Debug // Print the Target Entry Time
    print targetEntryTime;
    // Debug
    
    // Get the current time in the exchange's timezone
    DateTime currentTime = Time[0].AddMinutes(BarsPeriod.Value * (BarsInProgress + 1)).AddSeconds(-15); // Adjusting for a 5-minute bar, entering 15 seconds before the bar closes
    
    // Debug // Print the current time
    print currentTime;
    // Debug
    
    // Check if it's time to enter the position
    if (currentTime >= targetEntryTime && currentTime < targetEntryTime.AddSeconds(30)) // 30-second window to act
    {
    // Conditions to enter a long position, e.g., EnterLong();
    EnterLong();
    }
    }


    #2
    Hello devatechnologies,

    Thanks for your post and welcome to the NinjaTrader Forums.

    I see you mentioned " should I test this in again using real-time data?".

    To clarify, how exactly are you testing the strategy?

    Are you backtesting the strategy in the Strategy Analyzer window? Or, are you testing the strategy using the Playback connection with Market Replay data?

    If you are backtesting the strategy in the Strategy Analyzer, the strategy will only process logic OnBarClose as it does not have intrabar information available to process. This means that all logic would be processed at the close of a bar and all trades would be placed at the close of a bar.

    When in historical data, only the Open, High, Low, and Close will be available and there will be no intra-bar data. This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known.

    Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick' and the script will not have the intra-bar information to accurately fill an order at the exact price and time.

    Please review the help guide document on the differences on real-time vs backtest (historical).


    To improve the accuracy of a backtest, you may use Tick Replay along with an added 1-tick series to have logic processed intra-bar and have orders filled intrabar.

    Tick Replay would be used to have the logic process OnEachTick or OnPriceChange with historical data, but this does not allow for intra-bar order fills. You would need to add a single tick data series and submit orders to that single tick data series for a strategy that uses Tick Replay.

    High Order Fill Resolution allows for intra-bar order fills with historical processing, but is not compatible with Tick Replay.

    Please reference the SampleIntrabarBacktest example and the following Help Guide links for more information.

    SampleIntrabarBacktest 'Backtesting NinjaScript Strategies with an intrabar granularity' - https://ninjatrader.com/support/helpGuides/nt8/backtesting_ninjascript_strate.htm

    TickReplay — https://ninjatrader.com/support/help...ick_replay.htm

    Developing for Tick Replay -
    https://ninjatrader.com/support/helpGuides/nt8/developing_for__tick_replay.htm?zoom_highlightsub= developing+for+tick+replay

    Additional information may be found in this NinjaTrader Forum post —
    https://ninjatrader.com/support/forum/forum/ninjatrader-8/strategy-development/100192-comparing-real-time-historical-and-replay-performance?t=102504​​​
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hi Brandon,

      Thanks for the helpful reply.

      "To clarify, how exactly are you testing the strategy?"
      The first test was just a five day chart of the MES. So I assume it was using historical data. Which makes sense now, as it would only have OHLC.

      I did test today live and I and the buy order was filled at 6:50:28am. Later on I downloaded five days of Market Replay data. When I applied the strategy a buy order was triggered and filled at 6:50:01. For some reason the buys are triggering just after 6:50am.


      So it seems it may be a logic error in the code. I'm stuck.


      Click image for larger version

Name:	6_50_01.png
Views:	129
Size:	55.3 KB
ID:	1289979 Click image for larger version

Name:	6_50_28.png
Views:	131
Size:	62.7 KB
ID:	1289980

      Comment


        #4
        Hello devatechnologies,

        Thanks for your notes.

        If the strategy is not placing trades as you are expecting it to then debugging prints need to be added to the strategy that print out all the values being used in the conditions to place an order.

        In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

        Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

        ​Below is a link to a forum post that demonstrates how to use prints to understand behavior.
        https://ninjatrader.com/support/foru...121#post791121

        That said, you may want to consider adding a 15-second secondary series to your script using the AddDataSeries() method and check if the Times[1][0] of the secondary series is equal to 6:59:45 AM and call your Entry order method to submit the order at that time.

        AddDataSeries(): https://ninjatrader.com/support/help...dataseries.htm
        Working with Multi-Timeframe/Multi-Instrument NinjaScripts: https://ninjatrader.com/support/help...nstruments.htm
        Times: https://ninjatrader.com/support/help...ries_times.htm
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Hi Brandon,

          Thanks for that. I've think I've gotten this part of my strategy completed. It seems to work with Market Replay data, now that I've added the 15-sec data series. I'll test it tomorrow with live data.

          Here is the code so far. It may help someone in the future.




          Code:
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your strategy here.";
          Name = "YourStrategyName"; // Give your strategy a unique name
          // Set other defaults as needed
          }
          else if (State == State.Configure)
          {
          // Add a 15-second series to your strategy
          AddDataSeries("YourInstrumentName", BarsPeriodType.Second, 15);
          }
          }
          
          protected override void OnBarUpdate()
          {
          // Make sure we're working with the secondary series
          if (BarsInProgress != 1) return; // Assuming '0' is your primary series, '1' is the secondary 15-second series
          
          // Define your target entry time (6:59:45 AM)
          DateTime targetEntryTime = new DateTime(Times[1][0].Year, Times[1][0].Month, Times[1][0].Day, 6, 59, 45);
          
          // Check if the current time of the secondary series matches the target entry time
          if (Times[1][0].TimeOfDay == targetEntryTime.TimeOfDay)
          {
          // Conditions to enter a long position, e.g., EnterLong();
          EnterLong();
          }
          }
          Last edited by devatechnologies; 02-07-2024, 03:14 PM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by burtoninlondon, Today, 12:38 AM
          0 responses
          10 views
          0 likes
          Last Post burtoninlondon  
          Started by AaronKoRn, Yesterday, 09:49 PM
          0 responses
          14 views
          0 likes
          Last Post AaronKoRn  
          Started by carnitron, Yesterday, 08:42 PM
          0 responses
          11 views
          0 likes
          Last Post carnitron  
          Started by strategist007, Yesterday, 07:51 PM
          0 responses
          13 views
          0 likes
          Last Post strategist007  
          Started by StockTrader88, 03-06-2021, 08:58 AM
          44 responses
          3,983 views
          3 likes
          Last Post jhudas88  
          Working...
          X