Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position Sizing During Backtest

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

  • NinjaTrader_Bertrand
    replied
    You take your trades in OnBarUpdate(), as that's where you conditions would update and fire as a new bar is seen - so that's then the correct place for the trades updates to track as well.

    Leave a comment:


  • sburtt
    replied
    Originally posted by NinjaTrader_Bertrand View Post
    You would need to keep track of the start bar / DateTime, so you would have a reference for the SUM to work with. If you want to know a bar number for a DateTime object, use GetBar.

    http://www.ninjatrader.com/support/h...ightsub=GetBar
    Thanks to one of our veteran subs I think I found a good solution, by simply adding an iteration DataSeries, such as: CumulativeLastProfit.Set(CumulativeLastProfit[1] + lastProfit[0]);

    Now my final doubt is the following, please have a look at the code below. What I need is these DataSeries: LastProfit and CumulativeLastProfit to be calculated only at the end of a trade/ or beginning of a new trade, NOT OnBarUpdate(), do you think my code below does this properly? Thanks for your help!

    Code:
    namespace NinjaTrader.Strategy
    {
                [Description("")]
                public class ABC : Strategy
                {
                            #region Variables
                            private double   AccbalNewIR                             = 0; 
                            private double   AccountSize                              = 100000; //In US$ Terms
                            private double   Riskf                                        = 1; //Percent of Account Size to Risk on each Trade
                            private double   Psize                                        = 0;
                            private DataSeries lastProfit;
                            private DataSeries CumulativeLastProfit;
                            #endregion
    
                            protected override void Initialize()
                            {
                                        lastProfit = new DataSeries(this, MaximumBarsLookBack.Infinite);
                                        CumulativeLastProfit = new DataSeries(this, MaximumBarsLookBack.Infinite);
    			SetTrailingStop(...);
                            }
    
                            protected override void OnBarUpdate()
                            {
                                        if (Close[0] > Close[1])
                                        {
                                                   GoLong();
                                                   EnterLong((int)Psize,"");
                                        }
                            }
    
    void GoLong()
    
                            {
                                                                lastProfit.Set(lastTrade.ProfitCurrency * lastTrade.Quantity / lastTrade.Exit);
                                                                CumulativeLastProfit.Set(CumulativeLastProfit[1] + lastProfit[0]);
                                                    AccbalNewIR = AccountSize + CumulativeLastProfit;                                                               
    Psize = Math.Round(...);
    }
    }
    }

    Leave a comment:


  • NinjaTrader_Bertrand
    replied
    You would need to keep track of the start bar / DateTime, so you would have a reference for the SUM to work with. If you want to know a bar number for a DateTime object, use GetBar.

    Leave a comment:


  • sburtt
    replied
    Originally posted by NinjaTrader_Bertrand View Post
    Sure, this would be documented in our helpguide - http://www.ninjatrader.com/support/h...mation_sum.htm

    However then you would need to specify over how many bars you would want the SUM to be taken.

    An alternative way using C# collections was discussed for example here -

    https://www.bigmiketrading.com/ninja...-question.html
    So the simple summation would work, but how could I make sure that it sums all observations since the launch of the strategy? any idea?

    Leave a comment:


  • NinjaTrader_Bertrand
    replied
    Sure, this would be documented in our helpguide - http://www.ninjatrader.com/support/h...mation_sum.htm

    However then you would need to specify over how many bars you would want the SUM to be taken.

    An alternative way using C# collections was discussed for example here -

    Leave a comment:


  • sburtt
    replied
    Originally posted by NinjaTrader_Bertrand View Post
    There's a summation method available that takes a series as inpu
    Do you know where I could find this?

    Leave a comment:


  • sburtt
    replied
    Originally posted by NinjaTrader_Bertrand View Post
    There's a summation method available that takes a series as input, however before doing that - why not use the CumProfit from the PerformanceClass directly? You have AllTraders and Realtimetrades here as choice as well to further differentiate.

    http://www.ninjatrader.com/support/h.../cumprofit.htm
    the reason is I need this for trading forex and the CumProfit will be in TermAmount whist I need the CumProfit in BaseAmount that is obtainable by dividing the profit by the ExitRate at the time the trade occured

    Leave a comment:


  • NinjaTrader_Bertrand
    replied
    There's a summation method available that takes a series as input, however before doing that - why not use the CumProfit from the PerformanceClass directly? You have AllTraders and Realtimetrades here as choice as well to further differentiate.

    Leave a comment:


  • sburtt
    replied
    Originally posted by NinjaTrader_Bertrand View Post
    sburtt, sorry do not recall any specifics now after those 2 yrs.
    Bertrand
    . In my strategy I need to create a DataSeries object to store values. I’ve defined a variable “lastProfit” to hold the DataSeries object and created a new DataSeries in the Initialize() method. Finally in the OnBarUpdate() method I’ve defined how to define the value to be stored in the DataSeries (please see code below).

    What I would need to know is what function to use to obtain the cumulative sum of the values stored in the DataSeries, basically I want my strategy to return the cumulative sum of the lastProfit DataSeries. Please help

    Code:
    {
                [Description("")]
                public class ABC : Strategy
                {
                            #region Variables
    		private DataSeries lastProfit;
                            #endregion
    
                            protected override void Initialize()
                            {
    			lastProfit = new DataSeries(this, MaximumBarsLookBack.Infinite);
                            }
    
                            protected override void OnBarUpdate()
                            {
                                        if (...)
                                        {
                                             	lastProfit.Set(lastTrade.ProfitCurrency * lastTrade.Quantity / lastTrade.Exit);
    			else
    				lastProfit.Set(0);
    }
    }
    }

    Leave a comment:


  • NinjaTrader_Bertrand
    replied
    sburtt, sorry do not recall any specifics now after those 2 yrs.

    Leave a comment:


  • sburtt
    replied
    Originally posted by NinjaTrader_Bertrand View Post
    Thanks for the kind words - just replied to your note sent to support, I feel it's an issue of the scope of your variable defining the position size used.
    Bertrand, would you be able to give us a hint on how you solved this issue?

    Leave a comment:


  • sburtt
    replied
    Originally posted by trend747 View Post
    It works! Thanks bro!
    trend747 would you be able to post the required adjustment, or send me a private msg. I am looking for the same solution, but I am stuck. Thanks

    Leave a comment:


  • markus1000
    replied
    could you please publish the working code?

    i know the reply is a bit late

    Leave a comment:


  • trend747
    replied
    Originally posted by NinjaTrader_Bertrand View Post
    Thanks for the kind words - just replied to your note sent to support, I feel it's an issue of the scope of your variable defining the position size used.
    It works! Thanks bro!
    Last edited by trend747; 07-14-2011, 01:24 PM.

    Leave a comment:


  • NinjaTrader_Bertrand
    replied
    Thanks for the kind words - just replied to your note sent to support, I feel it's an issue of the scope of your variable defining the position size used.

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by CaptainJack, 05-29-2026, 05:09 AM
0 responses
310 views
0 likes
Last Post CaptainJack  
Started by CaptainJack, 05-29-2026, 12:02 AM
0 responses
200 views
0 likes
Last Post CaptainJack  
Started by charlesugo_1, 05-26-2026, 05:03 PM
0 responses
186 views
1 like
Last Post charlesugo_1  
Started by DannyP96, 05-18-2026, 02:38 PM
1 response
278 views
0 likes
Last Post NinjaTrader_ChelseaB  
Started by CarlTrading, 05-11-2026, 05:56 AM
0 responses
229 views
0 likes
Last Post CarlTrading  
Working...
X