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?

    Chris L.NinjaTrader Customer Service

    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 trilliantrader, 04-18-2024, 08:16 AM
      4 responses
      18 views
      0 likes
      Last Post trilliantrader  
      Started by mgco4you, Today, 09:46 PM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by wzgy0920, Today, 09:53 PM
      0 responses
      10 views
      0 likes
      Last Post wzgy0920  
      Started by Rapine Heihei, Today, 08:19 PM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by Rapine Heihei, Today, 08:25 PM
      0 responses
      10 views
      0 likes
      Last Post Rapine Heihei  
      Working...
      X