Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help with coding

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

    Help with coding

    I tried to combine the strategy I created with an ATM strategy (The AtmStrategy Template) and copy and pasted to get the following code. I'm coding dumb so what am I doing wrong??

    #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>
    /// All in an honest, automated days work.
    /// </summary>
    [Description("All in an honest, automated days work.")]
    public class Rainmaker : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int targer = 2; // Default setting for Targer
    private int stop = 6; // Default setting for Stop
    private double offset = 2.0; // Default setting for Offset
    private int period = 10; // Default setting for Period
    private int aDXstrength = 40; // Default setting for ADXstrength
    private int stopfollow = 1; // Default setting for Stopfollow
    // 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()
    {

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (Close[1] > KeltnerChannel(Offset, Period).Upper[1]
    && Close[0] < Close[1]
    && ADX(14)[1] >= ADX(14)[0]
    && ADX(14)[1] < ADXstrength)
    {
    EnterShort(DefaultQuantity, "ES");
    }

    // Condition set 2
    if (Close[1] < KeltnerChannel(Offset, Period).Lower[1]
    && Close[0] > Close[1]
    && ADX(14)[1] >= ADX(14)[0]
    && ADX(14)[1] < ADXstrength)
    {
    EnterLong(DefaultQuantity, "EL");
    }
    {if (Historical)
    return;

    // Submits an entry limit order at the current low price to initiate an ATM Strategy if both order id and strategy id are in a reset state
    // **** YOU MUST HAVE AN ATM STRATEGY TEMPLATE NAMED 'AtmStrategyTemplate' CREATED IN NINJATRADER (SUPERDOM FOR EXAMPLE) FOR THIS TO WORK ****
    if (orderId.Length == 0 && atmStrategyId.Length == 0 && Close[0] > Open[0])
    {
    atmStrategyId = GetAtmStrategyUniqueId();
    orderId = GetAtmStrategyUniqueId();
    AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Limit, Low[0], 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);
    }


    // Check for a pending entry order
    if (orderId.Length > 0)
    {
    string[] status = GetAtmStrategyEntryOrderStatus(orderId);

    // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
    if (status.GetLength(0) > 0)
    {
    // Print out some information about the order to the output window
    Print("The entry order average fill price is: " + status[0]);
    Print("The entry order filled amount is: " + status[1]);
    Print("The entry order order state is: " + status[2]);

    // If the order state is terminal, reset the order id value
    if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
    orderId = string.Empty;
    }
    } // If the strategy has terminated reset the strategy id
    else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
    atmStrategyId = string.Empty;


    if (atmStrategyId.Length > 0)
    {
    // You can change the stop price
    if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat)
    AtmStrategyChangeStopTarget(0, Low[0] - 3 * TickSize, "STOP1", atmStrategyId);

    // Print some information about the strategy to the output window
    Print("The current ATM Strategy market position is: " + GetAtmStrategyMarketPosition(atmStrategyId));
    Print("The current ATM Strategy position quantity is: " + GetAtmStrategyPositionQuantity(atmStrategyId));
    Print("The current ATM Strategy average price is: " + GetAtmStrategyPositionAveragePrice(atmStrategyId)) ;
    Print("The current ATM Strategy Unrealized PnL is: " + GetAtmStrategyUnrealizedProfitLoss(atmStrategyId)) ;
    }
    }
    }

    #2
    Hello,

    Thank you for the post.

    It looks like there are quite a few missing or misplaced curly braces for the protected override methods and also the enclosing class.

    I looked the code over and I believe it would be of more benefit to reconstruct a new script based on what you are trying to do. The reason for this is that there are already a lot of misplaced and missing braces in this script so you would in turn spend more time trying to resolve these errors than it would take to combine the two again correctly.

    When you are copying and pasting from one script to another there are a few things that you will need to do in order for the script to work and compile.

    First you need to pay attention to the scope of what you are copying and then what scope you are pasting in. What I mean by this would be if you are trying to take code from script A and put it in script B you will need to ensure to only copy the code needed and place it in the correct area between the curly braces.

    Here is a basic explanation of things to take note of:

    Code:
    namespace NinjaTrader.Strategy
    {
        public class TestStrat : Strategy
        {
    
        }
    }
    This is the basic structure of your strategy, the namespace and the class contained inside the namespaces curly brackets.

    Inside your public class you will have your protected override methods along with any other methods you need.

    This would be the basic structure to follow, when copying you need to make sure that you copy the entire scope, so if you are copying a entire method copy everything to the matching closing curly brace "}".

    Code:
    namespace NinjaTrader.Strategy
    {
        public class TestStrat : Strategy
        {
            protected override void Initialize()
            {
    			
            }
    
            protected override void OnBarUpdate()
            {
    
            }
        }
    }
    These are the basic methods needed for a strategy, this would also be the needed form the script needs to be in to compile. Any code you create would go inside the body of these methods or any of the other available overrides depending on what you are trying to accomplish.

    While you are combining scripts it is helpful to compile after each copy to ensure it compiles, if so then you can start changing the code to suit your script.

    If you have any questions while recreating this please feel free to ask and I will be happy to assist.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Ok. I tried what you said and came up with this...

      It is telling me I have errors on lines 27 and 72, but I dont understand why.

      //
      // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
      // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
      //

      #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.Strategy;
      #endregion


      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      /// <summary>
      ///
      /// </summary>
      [Description("")]
      public class SampleAtmStrategy : Strategy
      {
      #region Variables
      private string atmStrategyId = string.Empty;
      private string orderId = string.Empty;
      private string ADXstrength =40;
      private string Offset =2;
      private string Period =10;
      #endregion

      /// <summary>
      /// This method is used to configure the strategy and is called once before any strategy method is called.
      /// </summary>v
      protected override void Initialize()
      {
      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Condition set 1
      if (Close[1] > KeltnerChannel(Offset, Period).Upper[1]
      && Close[0] < Close[1]
      && ADX(14)[1] >= ADX(14)[0]
      && ADX(14)[1] < ADXstrength)
      {
      EnterShort(DefaultQuantity, "ES");
      }

      // Condition set 2
      if (Close[1] < KeltnerChannel(Offset, Period).Lower[1]
      && Close[0] > Close[1]
      && ADX(14)[1] >= ADX(14)[0]
      && ADX(14)[1] < ADXstrength)
      {
      EnterLong(DefaultQuantity, "EL");
      }

      {
      // HELP DOCUMENTATION REFERENCE: Please see the Help Guide section "Using ATM Strategies"

      // Make sure this strategy does not execute against historical data
      {
      if (Historical)
      return;
      }

      // Submits an entry limit order at the current low price to initiate an ATM Strategy if both order id and strategy id are in a reset state
      // **** YOU MUST HAVE AN ATM STRATEGY TEMPLATE NAMED 'AtmStrategyTemplate' CREATED IN NINJATRADER (SUPERDOM FOR EXAMPLE) FOR THIS TO WORK ****
      if (orderId.Length == 0 && atmStrategyId.Length == 0 && Close[0] > Open[0])
      {
      atmStrategyId = GetAtmStrategyUniqueId();
      orderId = GetAtmStrategyUniqueId();
      AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Limit, Low[0], 0, TimeInForce.Day, orderId, "AtmStrategyTemplate", atmStrategyId);
      }


      // Check for a pending entry order
      if (orderId.Length > 0)
      {
      string[] status = GetAtmStrategyEntryOrderStatus(orderId);

      // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
      if (status.GetLength(0) > 0)
      {
      // Print out some information about the order to the output window
      Print("The entry order average fill price is: " + status[0]);
      Print("The entry order filled amount is: " + status[1]);
      Print("The entry order order state is: " + status[2]);

      // If the order state is terminal, reset the order id value
      if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
      orderId = string.Empty;
      }
      } // If the strategy has terminated reset the strategy id
      else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
      atmStrategyId = string.Empty;


      if (atmStrategyId.Length > 0)
      {
      // You can change the stop price
      if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat)
      AtmStrategyChangeStopTarget(0, Low[0] - 3 * TickSize, "STOP1", atmStrategyId);

      // Print some information about the strategy to the output window
      Print("The current ATM Strategy market position is: " + GetAtmStrategyMarketPosition(atmStrategyId));
      Print("The current ATM Strategy position quantity is: " + GetAtmStrategyPositionQuantity(atmStrategyId));
      Print("The current ATM Strategy average price is: " + GetAtmStrategyPositionAveragePrice(atmStrategyId)) ;
      Print("The current ATM Strategy Unrealized PnL is: " + GetAtmStrategyUnrealizedProfitLoss(atmStrategyId)) ;
      }
      }

      #region Properties
      #endregion
      }}
      }

      Comment


        #4
        Hello,

        Thank you for remaking this.

        Looking at this script there are a few items.

        One is the name of the Class. a NinjaScript file has its internal name which will be the class name and there is a filename as well which is what you will see in windows. These names do not have to match but it is frowned upon when they do not match. It is just good practice to name the file the same as the class name so it is easy to find in both windows and your strategy menu.

        The Class name you currently have is already in use by the original file, so when you are copy pasting you need to be aware of this.

        Here is what you have
        Code:
        public class SampleAtmStrategy : Strategy
            {
        This needs to be a unique name that has not been used by any other strategy, you could simply add another letter or a number or change it all together, here is a fixed version
        Code:
        public class SampleAtmStrategy2 : Strategy
            {
        After correcting the class name there are some errors in the variables that have been set.

        you currently have
        Code:
        private string	atmStrategyId	= string.Empty;
        private string	orderId	= string.Empty;
        private string ADXstrength	=40;
        private string	Offset	=2;
        private string	Period	=10;
        A string will be used for any text value, these values later in the script are being used for a strength, offset and a period all of which are integer or whole number values.

        This would be the correct way to define these variables as an integer

        Code:
        private string	atmStrategyId	= string.Empty;
        private string	orderId	= string.Empty;
        private int ADXstrength	=40;
        private int	Offset	=2;
        private int	Period	=10;
        After these changes the script should compile.

        I will only post these segments as it is good practice to go back and update these values manually so you can see what exactly was in error and needed updated.

        When you are working with numbers in NinjaTrader just keep in mind:
        Any whole number 1,2,3,4,5 will be an integer or an int
        Any number with a decimal in generally will be a double
        A text value would be a string
        A true or false would be a bool.

        These are just some general items that you will see a lot so it is good to memorize each.

        Please let me know if I may be of additional assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by DJ888, 04-16-2024, 06:09 PM
        6 responses
        18 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by Jon17, Today, 04:33 PM
        0 responses
        1 view
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        6 views
        0 likes
        Last Post Javierw.ok  
        Started by timmbbo, Today, 08:59 AM
        2 responses
        10 views
        0 likes
        Last Post bltdavid  
        Started by alifarahani, Today, 09:40 AM
        6 responses
        41 views
        0 likes
        Last Post alifarahani  
        Working...
        X