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

Converting double to int

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

    Converting double to int

    I hope someone can help me here. I created a strategy and backtested on replay for 2 years without an error. I've now put it on SIM and it has order errors consistently. I believe I have narrowed it down to the conversion of a double to an int. Therefore, I used the strategy wizard and created new clean code to test this theory. This code, generated by the wizard, will throw an order execution error and I have no idea why. Sometimes it completes many trades with no problems and eventually has an error and other times it creates the error on the completion of the first trade. It runs fine in replay but not SIM. Can anyone make this run?
    Code:
    /// <summary>
        /// Enter the description of your strategy here
        /// </summary>
        [Description("Enter the description of your strategy here")]
        public class TestCode2 : Strategy
        {
            #region Variables
            // Wizard generated variables
            private int target = 4; // Default setting for Target
            private int stopLoss = 100; // Default setting for StopLoss
            private int startTime = 1; // Default setting for StartTime
            private int stopTime = 230000; // Default setting for StopTime
            private double qty = 1; // Default setting for Qty
            // User defined variables (add any user defined variables below)
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                SetStopLoss(StopLoss, false);
                SetProfitTarget("LongEntry", CalculationMode.Ticks, Target);			
                SetProfitTarget("ShortEntry", CalculationMode.Ticks, Target);
    
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    //			if (CurrentBar < 250 || Historical)
    //				return;
                // Condition set 1
                if (Position.MarketPosition == MarketPosition.Flat
                    && Close[0] > Low[0])
                {
                    EnterLong((int) (Qty), "LongEntry");
                }
    
                // Condition set 2
                if (Position.MarketPosition == MarketPosition.Long
                    && Close[0] > Low[0])
                {
                    EnterLong((int) (Qty), "LongEntry");
                }
    
                // Condition set 3
                if (Position.MarketPosition == MarketPosition.Flat
                    && Close[0] < High[0])
                {
                    EnterShort((int) (Qty), "ShortEntry");
                }
    
                // Condition set 4
                if (Position.MarketPosition == MarketPosition.Short
                    && Close[0] < High[0])
                {
                    EnterShort((int) (Qty), "ShortEntry");
                }
            }
    
            #region Properties
            [Description("")]
            [GridCategory("Parameters")]
            public int Target
            {
                get { return target; }
                set { target = Math.Max(1, value); }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public int StopLoss
            {
                get { return stopLoss; }
                set { stopLoss = Math.Max(1, value); }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public int StartTime
            {
                get { return startTime; }
                set { startTime = Math.Max(1, value); }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public int StopTime
            {
                get { return stopTime; }
                set { stopTime = Math.Max(1, value); }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public double Qty
            {
                get { return qty; }
                set { qty = Math.Max(1, value); }
            }
            #endregion
        }
    }
    Attached Files

    #2
    Hello Tdschulz,
    Thanks for your post.

    Instead of assigning the property/input Qty as a double you can assign it as an integer directly. This way you dont have to cast it to an integer.

    Code:
    private [B]int[/B] qty = 1; // Default setting for Qty
    
    [Description("")]
    [GridCategory("Parameters")]
    public [B]int[/B] Qty
    {
           get { return qty; }
           set { qty = Math.Max(1, value); }
    }

    To assist you further may I know are you getting any error messages on the Log tab or in the Output Window (Tools>Output Window).

    Also the condition 2 and 4 are redundant in your code. I would suggest reviewing your code further.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      For the simplicity of this example, I left qty as 1. Although qty is actually a dynamic variable which changes throughout the real strategy, therefore, it must remain a double. I basically just eliminated all the meat of the real strategy to show the problem I'm seeing with the conversion of the double to int.
      Condition 2 & 4 are not redundant when allowing for multiple entries in the same direction.
      I do set the allow entryies in same direction to 100 to get the error to occur faster.
      Please just try to run the code and see.

      I do not get log or output window errors. I receive "Assertion Failed" errors.
      Attached Files
      Last edited by Tdschulz; 12-05-2012, 10:25 AM.

      Comment


        #4
        Hello Tdschulz,
        Are you getting the error message using the sample code you have posted in post #1.

        If so can you please post a screenshot of the exact parameters you have set to test the strategy and also please let me know the exact steps to reproduce the scenario.
        JoydeepNinjaTrader Customer Service

        Comment


          #5
          Yes, I am getting the error with the sample code. I've been trying alternative methods of the double to int conversion and I've attached the latest. I just produced the error also.
          Additionally, I've just done a complete uninstall and reinstall, including removing all associated Ninja folders.

          Code:
          #region Using declarations
          using System;
          using System.ComponentModel;
          using System.Diagnostics;
          using System.Drawing;
          using System.Drawing.Drawing2D;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Data;
          using NinjaTrader.Indicator;
          using NinjaTrader.Gui.Chart;
          using NinjaTrader.Strategy;
          #endregion
          
          // This namespace holds all strategies and is required. Do not change it.
          namespace NinjaTrader.Strategy
          {
              /// <summary>
              /// Enter the description of your strategy here
              /// </summary>
              [Description("Enter the description of your strategy here")]
              public class TestCode2 : Strategy
              {
                  #region Variables
                  // Wizard generated variables
                  private int target = 4; // Default setting for Target
                  private int stopLoss = 100; // Default setting for StopLoss
                  private int startTime = 1; // Default setting for StartTime
                  private int stopTime = 230000; // Default setting for StopTime
                  private double qty = 1; // Default setting for Qty
          		private int qty1 = 1;
          		
                  // User defined variables (add any user defined variables below)
                  #endregion
          
                  /// <summary>
                  /// This method is used to configure the strategy and is called once before any strategy method is called.
                  /// </summary>
                  protected override void Initialize()
                  {
                      SetStopLoss(StopLoss, false);
                      SetProfitTarget("LongEntry", CalculationMode.Ticks, Target);			
                      SetProfitTarget("ShortEntry", CalculationMode.Ticks, Target);
          			EntriesPerDirection = 100;
                      CalculateOnBarClose = true;
          			EntryHandling = EntryHandling.AllEntries; 
                  }
          
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
          			if (CurrentBar < 250 || Historical)
          				return;
                      // Condition set 1
          			qty = System.Math.Round(1.0,1);
          			int qty1 = (int)qty;
          			
                      if (Position.MarketPosition == MarketPosition.Flat
                          && Close[0] > Low[0])
                      {
                          EnterLong(qty1, "LongEntry");
                      }
          
                      // Condition set 2
                      if (Position.MarketPosition == MarketPosition.Long
                          && Close[0] > Low[0])
                      {
                          EnterLong(qty1, "LongEntry");
                      }
          
                      // Condition set 3
                      if (Position.MarketPosition == MarketPosition.Flat
                          && Close[0] < High[0])
                      {
                          EnterShort(qty1, "ShortEntry");
                      }
          
                      // Condition set 4
                      if (Position.MarketPosition == MarketPosition.Short
                          && Close[0] < High[0])
                      {
                          EnterShort(qty1, "ShortEntry");
                      }
                  }
          
                  #region Properties
                  [Description("")]
                  [GridCategory("Parameters")]
                  public int Target
                  {
                      get { return target; }
                      set { target = Math.Max(1, value); }
                  }
          
                  [Description("")]
                  [GridCategory("Parameters")]
                  public int StopLoss
                  {
                      get { return stopLoss; }
                      set { stopLoss = Math.Max(1, value); }
                  }
          
                  [Description("")]
                  [GridCategory("Parameters")]
                  public int StartTime
                  {
                      get { return startTime; }
                      set { startTime = Math.Max(1, value); }
                  }
          
                  [Description("")]
                  [GridCategory("Parameters")]
                  public int StopTime
                  {
                      get { return stopTime; }
                      set { stopTime = Math.Max(1, value); }
                  }
          
                  [Description("")]
                  [GridCategory("Parameters")]
                  public double Qty
                  {
                      get { return qty; }
                      set { qty = Math.Max(1, value); }
                  }
                  #endregion
              }
          }
          Attached Files

          Comment


            #6
            Hello Tdschulz,
            Thanks for the code.

            I have reproduced the scenario and have informed development regarding it.
            JoydeepNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by kaywai, 09-01-2023, 08:44 PM
            5 responses
            601 views
            0 likes
            Last Post NinjaTrader_Jason  
            Started by xiinteractive, 04-09-2024, 08:08 AM
            6 responses
            22 views
            0 likes
            Last Post xiinteractive  
            Started by Pattontje, Yesterday, 02:10 PM
            2 responses
            18 views
            0 likes
            Last Post Pattontje  
            Started by flybuzz, 04-21-2024, 04:07 PM
            17 responses
            230 views
            0 likes
            Last Post TradingLoss  
            Started by agclub, 04-21-2024, 08:57 PM
            3 responses
            17 views
            0 likes
            Last Post TradingLoss  
            Working...
            X