Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Position Sizing During Backtest
Collapse
X
-
BertrandOriginally posted by NinjaTrader_Bertrand View Postsburtt, sorry do not recall any specifics now after those 2 yrs.
. 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
-
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 occuredOriginally posted by NinjaTrader_Bertrand View PostThere'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
Comment
-
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 -
Comment
-
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?Originally posted by NinjaTrader_Bertrand View PostSure, 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
Comment
-
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]);Originally posted by NinjaTrader_Bertrand View PostYou 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
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
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
88 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
48 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
31 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
34 views
0 likes
|
Last Post
|
||
|
Started by Mindset, 02-28-2026, 06:16 AM
|
0 responses
69 views
0 likes
|
Last Post
by Mindset
02-28-2026, 06:16 AM
|

Comment