Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time

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

    Time

    I am really struggling with the time functions. I have seen the doc on ToTime(), DateTime.Now, DateTime.Compare() etc. I've seen in the forums and tried to use the following and sometimes it works and other times it doesn't (inexplicably). 10:00am comes and nothing happens. Is there a sure way to compare two times? Thanks.



    if ( ToTime(Time[0]) >= ToTime( 10, 00, 00) && ToTime(Time[0]) <= ToTime( 15, 00, 00) )

    {


    Print ( ToTime(Time[0]));


    }

    #2
    Hello, thanks for your post.

    The best way of finding out why the condition is not becoming true when you think it should be tru is to Print out your condition variables before the evaluation to see their value.

    e.g.

    Code:
    Print([B]ToTime(Time[0])[/B]);
    Print([B]ToTime( 10, 00, 00)[/B]);
    Print([B]ToTime( 15, 00, 00)[/B]);
    Print([B]ToTime(Time[0]) >= ToTime( 10, 00, 00)[/B]);
    Print([B]ToTime(Time[0]) <= ToTime( 15, 00, 00)[/B]);
    
    [B]if ( ToTime(Time[0]) >= ToTime( 10, 00, 00) && ToTime(Time[0]) <= ToTime( 15, 00, 00) )[/B]
    
    [B]{[/B]
    
    [B]Print ( ToTime(Time[0]));[/B]
    
    [B]}[/B]
    We have a reference sample here that shows how to add a time filter to your script.

    If you are still having trouble after trying this could you please post a reduced, simple version of your script that still re-creates the problem?

    Comment


      #3
      Originally posted by NinjaTrader_ChrisL View Post
      Hello, thanks for your post.

      The best way of finding out why the condition is not becoming true when you think it should be tru is to Print out your condition variables before the evaluation to see their value.

      e.g.

      Code:
      Print([B]ToTime(Time[0])[/B]);
      Print([B]ToTime( 10, 00, 00)[/B]);
      Print([B]ToTime( 15, 00, 00)[/B]);
      Print([B]ToTime(Time[0]) &gt;= ToTime( 10, 00, 00)[/B]);
      Print([B]ToTime(Time[0]) &lt;= ToTime( 15, 00, 00)[/B]);
      
      [B]if ( ToTime(Time[0]) &gt;= ToTime( 10, 00, 00) &amp;&amp; ToTime(Time[0]) &lt;= ToTime( 15, 00, 00) )[/B]
      
      [B]{[/B]
      
      [B]Print ( ToTime(Time[0]));[/B]
      
      [B]}[/B]
      We have a reference sample here that shows how to add a time filter to your script.

      If you are still having trouble after trying this could you please post a reduced, simple version of your script that still re-creates the problem?


      #region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Gui.SuperDom;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.NinjaScript.Indicators;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion

      //This namespace holds Strategies in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Strategies
      {
      public class Strategy : Strategy
      {
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "Strategy";
      Calculate = Calculate.OnBarClose;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.ImmediatelySubmitSynchronizeAccount; //StartBehavior.AdoptAccountPosition StartBehavior.ImmediatelySubmit StartBehavior.ImmediatelySubmitSynchronizeAccount StartBehavior.WaitUntilFlat StartBehavior.WaitUntilFlatSynchronizeAccount
      TimeInForce = TimeInForce.Day;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 1;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;
      }
      }


      //Bar update ================================================== ============


      protected override void OnBarUpdate()
      {

      if (CurrentBars[0] &lt; BarsRequiredToTrade)
      return;

      // Only trade between certain times============================================= ======
      // Print(ToTime(Time[0]));
      // Print(ToTime(2, 30, 00));
      // Print(ToTime(8, 30, 00));
      // Print(ToTime(14, 30, 00));
      // Print(Instrument.MasterInstrument.Name);

      //CL
      if(Instrument.MasterInstrument.Name == "CL")
      {
      // Only trade between 10:00 AM and 2:30 PM
      if ( ToTime(Time[0]) &gt;= ToTime(10,00,00) &amp;&amp; ToTime(Time[0]) &lt;= ToTime(14,30,00) )
      {
      if ( condition 1)
      {
      EnterLong();
      }
      else if( condition 2)
      {
      EnterShort();
      }
      }
      else if(Position.MarketPosition != MarketPosition.Flat)
      {
      ExitLong();
      ExitShort();
      return;
      }
      }

      //NQ
      if(Instrument.MasterInstrument.Name == "NQ")
      {
      // Only trade between 10:00 AM and 3:00 PM
      if ( ToTime(Time[0]) &gt;= ToTime(10,00,00) &amp;&amp; ToTime(Time[0]) &lt;= ToTime(15,00,00) )
      {
      if ( condition 1)
      {
      EnterLong();
      }
      else if( condition 2)
      {
      EnterShort();
      }
      }
      else if(Position.MarketPosition != MarketPosition.Flat)
      {
      ExitLong();
      ExitShort();
      return;
      }
      }



      //GC
      if(Instrument.MasterInstrument.Name == "GC")
      {
      // Only trade between 10:00 AM and 1:30 PM
      if ( ToTime(Time[0]) &gt;= ToTime(10,00,00) &amp;&amp; ToTime(Time[0]) &lt;= ToTime(13,30,00) )
      {
      if ( condition 1)
      {
      EnterLong();
      }
      else if( condition 2)
      {
      EnterShort();
      }
      }
      else if(Position.MarketPosition != MarketPosition.Flat)
      {
      ExitLong();
      ExitShort();
      return;
      }
      }


      }

      #region Properties
      #endregion
      }
      }

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      83 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      47 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      29 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      32 views
      0 likes
      Last Post TheRealMorford  
      Started by Mindset, 02-28-2026, 06:16 AM
      0 responses
      66 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Working...
      X