Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy based on day of week

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

    Strategy based on day of week

    Hello
    I try to test a very simpe strategy, buying on Monday and selling on Tuesday, but somehow I always get an error message. I used the strategy builder and clicked me through to the point "Conditions and Actions". There I built the following sets of conditions:
    If (Times[0][0].DayOfWeek == DayOfWeek.Monday), then EnterLong(DefaultQuantity,"");
    If (Times[0][0].DayOfWeek == DayOfWeek.Tuesday), then ExitLong(DefaultQuantity,"");

    But when I press the "Compile"-button, I get the error message "Could not compile NinjaScript". The reason seems to be that my NinjaScript does not have a definition of "Monday" and "Tuesday" (this is what the Ninja Script Editor tells me). So do I have to define the days of the week myself (and how would I do that?) or what else am I doing wrong?

    #2
    Hello value57,

    Thank you for the post.

    From the given condition that seems to be correct, could you attach the strategy to your post so I can see what code had been generated?

    You can find the strategy file in the folder: Documents\NinjaTrader 8\bin\Custom\Strategies


    I look forward to being of further assistance.

    Comment


      #3
      Hello Jesse

      Thank you for your reply. Unfortunately, I get error messages when I try to attach the file, and I'm not very good with computers, so I have no idea why this happens. But I copied the part of the strategy code I believe to be the most important. Does this tell you what's wrong?


      #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 DayOfWeek : Strategy
      {
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "DayOfWeek";
      Calculate = Calculate.OnBarClose;
      EntriesPerDirection = 1;
      EntryHandling = EntryHandling.AllEntries;
      IsExitOnSessionCloseStrategy = true;
      ExitOnSessionCloseSeconds = 30;
      IsFillLimitOnTouch = false;
      MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
      OrderFillResolution = OrderFillResolution.Standard;
      Slippage = 0;
      StartBehavior = StartBehavior.WaitUntilFlat;
      TimeInForce = TimeInForce.Gtc;
      TraceOrders = false;
      RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
      StopTargetHandling = StopTargetHandling.PerEntryExecution;
      BarsRequiredToTrade = 20;
      // Disable this property for performance gains in Strategy Analyzer optimizations
      // See the Help Guide for additional information
      IsInstantiatedOnEachOptimizationIteration = true;
      }
      else if (State == State.Configure)
      {
      }
      }

      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;

      if (CurrentBars[0] < 1)
      return;

      // Set 1
      if (Times[0][0].DayOfWeek == DayOfWeek.Monday)
      {
      EnterLong(Convert.ToInt32(DefaultQuantity), "");
      }

      // Set 2
      if (Times[0][0].DayOfWeek == DayOfWeek.Tuesday)
      {
      ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
      }

      }
      }
      }

      #region Wizard settings, neither change nor remove


      Comment


        #4
        Hello value57,

        Thank you for the reply.

        What error did you get when trying to attach the file?

        This unfortunately does not help me to understand what the builder would generate that error. I tried making the same condition on my end and see it working. This is the code that generated:

        Code:
        // Set 1
        if (Times[0][0].DayOfWeek == DayOfWeek.Monday)
        {
        }

        Are you using the current release of NinjaTrader, you can find the version in the Help -> Abount menu. The current version is 8.0.24.2


        I look forward to being of further assistance.

        Comment


          #5
          Helle Jesse

          Thank you for your patience. I'm not sure what solved my problem, but now I can compile the strategy. May be that's because I changed the settings of my antivirus software or the simple fact that I restarted and resetted my computer. Unfortunately, I don't know the decisive factor, so this is of little help for other users. But I'm very happy that Ninjatrader works again and my problem is magically solved. Thank you very much for your help, Jesse.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          70 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          143 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          76 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          47 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          51 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X