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 Philippe56140, Today, 02:35 PM
      0 responses
      1 view
      0 likes
      Last Post Philippe56140  
      Started by 00nevest, Today, 02:27 PM
      0 responses
      1 view
      0 likes
      Last Post 00nevest  
      Started by Jonafare, 12-06-2012, 03:48 PM
      5 responses
      3,986 views
      0 likes
      Last Post rene69851  
      Started by Fitspressorest, Today, 01:38 PM
      0 responses
      2 views
      0 likes
      Last Post Fitspressorest  
      Started by Jonker, Today, 01:19 PM
      0 responses
      2 views
      0 likes
      Last Post Jonker
      by Jonker
       
      Working...
      X