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

increase or decrease contract

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

    increase or decrease contract

    I need to increse or decrease the quantity of contract in base to last trade, for exemple i i win i need to add 1 contract and if i lost i have to decrease 1 contract.

    This is the 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 DMcc : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int stoploss = 50; // Default setting for Stoploss
    // User defined variables (add any user defined variables below)
    #endregion

    private bool once = false;
    private IOrder entryOrder;

    /// <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("", CalculationMode.Ticks, Stoploss, false);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()


    {


    // Condition set 1
    if (CrossAbove(DM(14).DiPlus, DM(14).DiMinus, 1)
    && ChaikinVolatility(14)[0] > 0)
    {
    EnterLong(DefaultQuantity, "");
    }

    // Condition set 2
    if (CrossAbove(DM(14).DiMinus, DM(14).DiPlus, 1)
    && ChaikinVolatility(14)[0] > 0)
    {
    EnterShort(DefaultQuantity, "");
    }
    }


    #region Properties
    [Description("")]
    [GridCategory("Parameters")]
    public int Stoploss
    {
    get { return stoploss; }
    set { stoploss = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #2
    Hello mylanel,

    I have attached a copy of your strategy which achieves the goals you specified. Please let us know if you have any questions.

    Documentation on methods I used :

    AllTrades


    TradeCollection


    Trade


    OnExecution
    Attached Files
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your reply, i've tried your code but the strategy dosen't work

      #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 DMcc : Strategy
      {
      #region Variables
      // Wizard generated variables
      private int stoploss = 50; // Default setting for Stoploss
      // User defined variables (add any user defined variables below)
      private bool once = false; //
      private IOrder entryOrder; //
      private int currentQuantity; // How many trades to enter
      private int minQuantity = 1; // minimum number of trades to enter
      private int maxQuantity = 5; // maximum number of trades to enter
      #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("", CalculationMode.Ticks, Stoploss, false);

      CalculateOnBarClose = true;
      currentQuantity = DefaultQuantity;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {


      // Condition set 1
      if (CrossAbove(DM(14).DiPlus, DM(14).DiMinus, 1)
      && ChaikinVolatility(14)[0] > 0)
      {
      EnterLong(currentQuantity, "");
      }

      // Condition set 2
      if (CrossAbove(DM(14).DiMinus, DM(14).DiPlus, 1)
      && ChaikinVolatility(14)[0] > 0)
      {
      EnterShort(currentQuantity, "");
      }
      }

      /// <summary>
      /// The OnExecution() method is called on an incoming execution.
      /// An execution is another name for a fill of an order.
      ///
      /// AllTrades
      /// http://ninjatrader.com/support/helpG.../alltrades.htm
      ///
      /// TradeCollection
      /// http://ninjatrader.com/support/helpG...collection.htm
      ///
      /// Trade
      /// http://ninjatrader.com/support/helpGuides/nt7/trade.htm
      ///
      /// OnExecution
      /// http://ninjatrader.com/support/helpG...nexecution.htm
      /// </summary>
      /// <param name="execution"></param>
      protected override void OnExecution(IExecution execution)
      {
      if (Performance.AllTrades[0].ProfitPoints > 0.0 && currentQuantity < maxQuantity)
      {
      ++currentQuantity;
      }
      if (Performance.AllTrades[0].ProfitPoints < 0.0 && currentQuantity > minQuantity)
      {
      --currentQuantity;
      }
      }

      #region Properties
      [Description("")]
      [GridCategory("Parameters")]
      public int Stoploss
      {
      get { return stoploss; }
      set { stoploss = Math.Max(1, value); }
      }
      #endregion
      }
      }

      Comment


        #4
        Hello mylanel,

        There is a small mistake in my code. While most arrays in Ninja are ordered newest to oldest, we can see from here that TradeCollections are the opposite,

        Originally posted by http://ninjatrader.com/support/helpGuides/nt8/en-us/flatten.htm
        Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
        Trade firstTrade = Performance.AllTrades[0];
        Therefore we will want to change

        Code:
        [FONT=Courier New]protected override void OnExecution(IExecution execution)
        {
                  if (Performance.AllTrades[0].ProfitPoints > 0.0 && currentQuantity < maxQuantity)
                  {
                        ++currentQuantity;
                  }
                  if (Performance.AllTrades[0].ProfitPoints < 0.0 && currentQuantity > minQuantity)
                  {
                        --currentQuantity;
                  }
            }[/FONT]
        to

        Code:
        [FONT=Courier New]protected override void OnExecution(IExecution execution)
        {
          Trade lastTrade = [/FONT]
        [FONT=Courier New][FONT=Courier New]Performance.AllTrades[[/FONT][FONT=Courier New]Performance.AllTrades.Count - 1];
        
        [/FONT]          if (lastTrade[/FONT][FONT=Courier New].ProfitPoints > 0.0 && currentQuantity < maxQuantity)
                  {
                        ++currentQuantity;
                  }
                  if (lastTrade.ProfitPoints < 0.0 && currentQuantity > minQuantity)
                  {
                        --currentQuantity;
                  }
            }
        [/FONT]

        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          mmm dosen't work again, but dosen't work all strategy because i receive as result in 0 in backtest.

          How can i do?

          Comment


            #6
            Hello mylanel,

            The code sample I provided was for educational purposes only. I would like to ask, can you let me know what happens when you attempt to run your code without the changes I made in adding an OnExecution method, as far as backtesting?
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              Without your code i receive the correct number of backtest, while if i try with your code dosen't became enable all strategy

              Comment


                #8
                Hello mylanel,

                I am providing an updated and tested sample.

                This sample added, in brief, the following :

                • An OnExecution overload, which takes place whenever a trade is placed
                • A condition : if a trade is profitable, take one action, otherwise, take another action
                • A quantity argument which is used by your entry methods.

                Please let us know if there are any other ways we can help.

                If you have any further code questions, please also either copy out excerpts, or attach a file to your post. This will make our thread more useful and friendly to other members of our forum.
                Attached Files
                Jessica P.NinjaTrader Customer Service

                Comment


                  #9
                  Ok now is better, is not perfect but we are very close.
                  I'll try on my own to make it in right way.

                  Thanks
                  Jessica
                  Last edited by mylanel; 06-08-2016, 11:25 AM.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by maybeimnotrader, Today, 05:46 PM
                  0 responses
                  5 views
                  0 likes
                  Last Post maybeimnotrader  
                  Started by quantismo, Today, 05:13 PM
                  0 responses
                  6 views
                  0 likes
                  Last Post quantismo  
                  Started by AttiM, 02-14-2024, 05:20 PM
                  8 responses
                  166 views
                  0 likes
                  Last Post jeronymite  
                  Started by cre8able, Today, 04:22 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post cre8able  
                  Started by RichStudent, Today, 04:21 PM
                  0 responses
                  5 views
                  0 likes
                  Last Post RichStudent  
                  Working...
                  X