• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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
    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
    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:
        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;
                  }
            }
        to

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

        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 dr0832, 01-04-2019, 10:34 AM
                  7 responses
                  141 views
                  0 likes
                  Last Post Lancer
                  by Lancer
                   
                  Started by cbentrikin, Today, 07:46 PM
                  0 responses
                  4 views
                  0 likes
                  Last Post cbentrikin  
                  Started by RedDuke, Today, 06:14 PM
                  0 responses
                  4 views
                  0 likes
                  Last Post RedDuke
                  by RedDuke
                   
                  Started by DariusDeck, Today, 04:22 PM
                  1 response
                  13 views
                  0 likes
                  Last Post NinjaTrader_Marco  
                  Started by pstrusi, Today, 05:05 PM
                  1 response
                  3 views
                  0 likes
                  Last Post pstrusi
                  by pstrusi
                   
                  Working...
                  X