namespace NinjaTrader.Strategy
{
[Description("")]
public class aaaFXMMTest2 : Strategy
{
#region Variables
private double stopLoss = 10;
private double AccbalNewIR = 0;
private double AccountSize = 100000;
private double Riskf = 1;
private double Psize = 0;
private DataSeries lastProfit;
private DataSeries cumulativeLastProfit;
#endregion
protected override void Initialize()
{
CalculateOnBarClose = true;
lastProfit = new DataSeries(this, MaximumBarsLookBack.Infinite);
cumulativeLastProfit = new DataSeries(this, MaximumBarsLookBack.Infinite);
}
protected override void OnBarUpdate()
{
if (CrossAbove(SMA(10), SMA(20), 1))
{
PositionSize();
EnterLong((int)Psize,"");
SetStopLoss(CalculationMode.Ticks,stopLoss);
}
}
#region PositionSizeFunction
void PositionSize()
{
Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count-1];
lastProfit.Set(lastTrade.ProfitCurrency * lastTrade.Quantity / lastTrade.Exit);
cumulativeLastProfit.Set(cumulativeLastProfit[1] + lastProfit[0]);
AccbalNewIR = AccountSize + cumulativeLastProfit;
Psize = Math.Round(AccbalNewIR * Riskf / 100 / stopLoss / TickSize * lastTrade.Exit);
}
#endregion
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Strategy with Position Sizing for Indirect Forex Rates Help
Collapse
X
-
Strategy with Position Sizing for Indirect Forex Rates Help
Hi Guys, could somebody help me figure out what I am doing wrong in this strategy? Why am I getting the following error messages (see attached)?
Code:Tags: None
-
sburtt, the snippet below should compile for you -
One issue was not accessing any property of the lastTrade execution (.Price for example), the other trying to add a double to a dataseries, that would not work - you need to access particular double of the series and then you could add to it.
Code:private void PositionSize() { Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count-1]; lastProfit.Set(lastTrade.ProfitCurrency * lastTrade.Quantity / lastTrade.Exit.Price); cumulativeLastProfit.Set(cumulativeLastProfit[1] + lastProfit[0]); AccbalNewIR = AccountSize + cumulativeLastProfit[0]; Psize = Math.Round(AccbalNewIR * Riskf / 100 / stopLoss / TickSize * lastTrade.Exit.Price); }Last edited by NinjaTrader_Bertrand; 07-24-2013, 06:48 AM.
-
Bertrand, I appreciate your help on this matter. It is still not compiling as I suspect something is wrong in the bold part of the code highlighted above. The compiler is still giving me an error there.Originally posted by NinjaTrader_Bertrand View Postsburtt, the snippet below should compile for you -
One issue was not accessing any property of the lastTrade execution (.Price for example), the other trying to add a double to a dataseries, that would not work - you need to access particular double of the series and then you could add to it.
Code:private void PositionSize() { [B]Trade lastTrade = Performance.RealtimeTradesPerformance.RealtimeTrades.Count-1];[/B] [B] lastProfit.Set(lastTrade.ProfitCurrency * lastTrade.Quantity / lastTrade.Exit.Price);[/B] cumulativeLastProfit.Set(cumulativeLastProfit[1] + lastProfit[0]); AccbalNewIR = AccountSize + cumulativeLastProfit[0]; Psize = Math.Round(AccbalNewIR * Riskf / 100 / stopLoss / TickSize * lastTrade.Exit.Price); }
In the strategy I need to access the last trade profit and last trade exit price, as I need to convert the profit in USD amount by dividing by the exit price and store this value in a database.
Thanks for your help
Comment
-
please see my first attached, i still get the 1st and 3rd error message.Originally posted by NinjaTrader_Bertrand View PostWhich exact error would you still get?
The snippet I posted below will compile, were you attempted to reuse with the exact same syntax?
"Operator "/" cannot be applied to operands of type 'double' and 'NinjaTrader.Cbi.IExecution"
"Operator "*" cannot be applied to operands of type 'double' and 'NinjaTrader.Cbi.IExecution"
so exactly the second line of the highlighted bold code i posted earlier.
Furthermore, I think that also the 1st line has an error as there is a ] that maybe should removed, please check
Comment
-
Bertrand it's not compiling on my pc. Running through the code i see that you might be missing a square bracket, please see below:Originally posted by NinjaTrader_Bertrand View Post'please see my first attached, i still get the 1st and 3rd error message.'
Sorry, I don't follow - I've posted a corrected, compiling snippet with changes in for you - why would you not use that one then?
Trade lastTrade = Performance.RealtimeTradesPerformance.RealtimeTrad es.Count-1];
Comment
-
unfortunately it's still not compiling properly on my end. This is the error I get:Originally posted by NinjaTrader_Bertrand View PostMy apologies, please see my correct snippet in the post - there must have been one opening bracket missed copying this into the forums. Then I'm sure it would compile on your machine as well.
"Operator "/" cannot be applied to operands of type 'double' and 'NinjaTrader.Cbi.IExecution"
"Operator "*" cannot be applied to operands of type 'double' and 'NinjaTrader.Cbi.IExecution"
and this is the part of the code where the issue occurs:
lastProfit.Set(lastTrade.ProfitCurrency * lastTrade.Quantity / lastTrade.Exit.Price);
Comment
-
Bertrand, so basically now I am running this strategy, it compiles fine, but when I run this in StrategyAnalyser it doesn't generate any trade. This is weird as it should EnterLong on the SMA CrossAbove. I would appreciate if you could have time to look into this and tell me what you think could be the issue.Originally posted by NinjaTrader_Bertrand View PostPlease send me an email to support at ninjatrader dot com with the exact script you're using, as the snippet works fine here for me.
To help you I think it makes sense to explain you what I am trying to achieve with this strategy. I need the void PositionSize() to calculate the Quantity the strategy will Buy for each trade. This Quantity is function of the initial AccountSize(100,000USD) and the Cumulative Profit. However let’s assume I am trading USD/JPY, the Cumulative Profits calculated by NT7 are in JPY amount, not USD amount, hence I need to calculate the exact profit for each trade and divide it by the Exit.Price to convert that amount in USD, for that I have created the lastProfit and cumulativeLastProfit DataSeries to store this information. It all looks correctly coded to me, but I am no expert in programming and the strategy is not returning any trade in back testing, any idea why?
Code below:
Code:Namespace NinjaTrader.Strategy { [Description("")] public class aaaFXMMTest2 : Strategy { #region Variables private double AccbalNewIR = 0; private double AccountSize = 100000; private double Riskf = 1; private double Psize = 0; private DataSeries lastProfit; private DataSeries cumulativeLastProfit; #endregion protected override void Initialize() { CalculateOnBarClose = true; lastProfit = new DataSeries(this, MaximumBarsLookBack.Infinite); cumulativeLastProfit = new DataSeries(this, MaximumBarsLookBack.Infinite); } protected override void OnBarUpdate() { if (CrossAbove(SMA(10), SMA(20), 1)) { PositionSize(); EnterLong((int)Psize,""); SetStopLoss(CalculationMode.Ticks,50); SetProfitTarget(CalculationMode.Ticks,100); } } #region PositionSizeFunction private void PositionSize() { Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count-1]; lastProfit.Set(lastTrade.ProfitCurrency * lastTrade.Quantity / lastTrade.Exit.Price); cumulativeLastProfit.Set(cumulativeLastProfit[1] + lastProfit[0]); AccbalNewIR = AccountSize + cumulativeLastProfit[0]; Psize = Math.Round(AccbalNewIR * Riskf / 100 / stopLoss / TickSize * lastTrade.Exit.Price); } #endregion
Comment
-
Thanks for taking the time to look into this.Originally posted by NinjaTrader_Bertrand View PostThanks for getting in touch directly via email, I've replied to you already with my thoughts.
1.
So basically you suggest that adding this in the OnBarUpdate() method should make the trick?
2.Code:protected override void OnBarUpdate() { // Only run on real-time data if (Historical) return;
Would I need to add also?
3.Code:if (Performance.RealtimeTrades.Count < 1) return;
Finally, I am currently running the strategy for Back-test purpose, so not taking into account real time trade executed. Given this should I be using AllTrades rather than RealtimeTrades in my script?
Again thank you
Comment
-
Unfortunately it's not working, given I need this for BT purpose, I've sent you an email with the code I am running, I am not receiving any error message, but running the code on 12 month 1 min data I get not 1 trade, it's very strange.Originally posted by NinjaTrader_Bertrand View PostYour understanding is correct, you would need to have at least one realtime trade if using that snippet.
However for backtesting purposes that would not help you either, you need to work with AllTrades then.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
648 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
369 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
108 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
572 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
573 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment