• 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

  • mylanel
    started a topic increase or decrease contract

    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
    }
    }

  • mylanel
    replied
    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.

    Leave a comment:


  • NinjaTrader_JessicaP
    replied
    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

    Leave a comment:


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

    Leave a comment:


  • NinjaTrader_JessicaP
    replied
    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?

    Leave a comment:


  • mylanel
    replied
    mmm dosen't work again, but dosen't work all strategy because i receive as result in 0 in backtest.

    How can i do?

    Leave a comment:


  • NinjaTrader_JessicaP
    replied
    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;
              }
        }
    

    Leave a comment:


  • mylanel
    replied
    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
    }
    }

    Leave a comment:


  • NinjaTrader_JessicaP
    replied
    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

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by shmarola, Today, 01:01 AM
0 responses
1 view
0 likes
Last Post shmarola  
Started by CriticalMind, 11-29-2015, 12:39 PM
3 responses
1,321 views
0 likes
Last Post NinjaTrader_ChelseaB  
Started by novel168, 06-14-2019, 09:20 PM
1 response
7 views
0 likes
Last Post NinjaTrader_ChelseaB  
Started by johnnybegoode, Yesterday, 08:00 PM
3 responses
9 views
0 likes
Last Post NinjaTrader_ChelseaB  
Started by digibob, Yesterday, 03:00 PM
1 response
6 views
0 likes
Last Post NinjaTrader_ChelseaB  
Working...
X