Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position Sizing During Backtest

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

    #16
    sburtt, sorry do not recall any specifics now after those 2 yrs.
    BertrandNinjaTrader Customer Service

    Comment


      #17
      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);
      }
      }
      }

      Comment


        #18
        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.

        BertrandNinjaTrader Customer Service

        Comment


          #19
          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

          Comment


            #20
            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?

            Comment


              #21
              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 -

              Howdy-- I have created an indicator that assigns each bar a unique 0, 1 or 2 "score" based on a series of logic conditions. This piece is working fine. I then wanted to create a running "score" of the unique bar "scores" over the previous n bars. To do this, I figured that creating an array to hold each bar's score would be best, as the size of the array could be limited with a parameter (ie, 10 bars, 50 bars, 100 bars, etc). I then wanted to sum this array values to create …
              BertrandNinjaTrader Customer Service

              Comment


                #22
                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?

                Comment


                  #23
                  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.

                  BertrandNinjaTrader Customer Service

                  Comment


                    #24
                    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(...);
                    }
                    }
                    }

                    Comment


                      #25
                      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.
                      BertrandNinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by r68cervera, Today, 05:29 AM
                      0 responses
                      2 views
                      0 likes
                      Last Post r68cervera  
                      Started by geddyisodin, Today, 05:20 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post geddyisodin  
                      Started by JonesJoker, 04-22-2024, 12:23 PM
                      6 responses
                      35 views
                      0 likes
                      Last Post JonesJoker  
                      Started by GussJ, 03-04-2020, 03:11 PM
                      12 responses
                      3,239 views
                      0 likes
                      Last Post Leafcutter  
                      Started by AveryFlynn, Today, 04:57 AM
                      0 responses
                      6 views
                      0 likes
                      Last Post AveryFlynn  
                      Working...
                      X