if (Performance.AllTrades.Count > 1)
{
Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
double lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity;
Print("The last trade profit is " + lastProfit);
}
// Condition set 1
if (
lastProfit > 0
&& Close[0] == High[0]
)
{
EnterLongLimit(1, Close[0] + 1 * TickSize, "LongEntry");
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
lastTrade
Collapse
X
-
lastTrade
I am attempting to implement some money management approaches within my strategy but can not seem to get it. For simplicity, let's say I just want to check whether the last trade was a winner or looser. What is wrong with this code?
Code:Tags: None
-
Hello Tdschulz,
The trade collection will add an object to it only when you have a trade. However a trade will never occur since your coding filter specifies lastProfit > 0 (which will never happens unless there is a trade).
Its like a circular reference, and thus you are witnessing the scenario.
Please note, if any trade results in a loss you will not have any further trade since lastProfit > 0.Code:double lastProfit = 0; if (Performance.AllTrades.Count >= 1) { Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1]; lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity; Print("The last trade profit is " + lastProfit); } // Condition set 1 if ((Performance.AllTrades.Count == 0 || lastProfit > 0) && Close[0] == High[0] ) { EnterLongLimit(1, Close[0] + 1 * TickSize, "LongEntry"); }JoydeepNinjaTrader Customer Service
-
Sorry. I didn't set the double = 0; at the beginning. I have it now.
Next issue. The lastProfit is only recording the profit from a single contract rather that the amount for the entire quantity. Do you see anything wrong here?
Code:protected override void OnBarUpdate() { double lastProfit = 0; if (CurrentBar < 250) { return; } if (Performance.AllTrades.Count >= 1) { Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1]; lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity; } Print("The last trade profit is " + lastProfit); // Condition set 1 if ((Performance.AllTrades.Count == 0 || lastProfit > 0) && Close[0] == High[0] ) { EnterLongLimit(1, Close[0] + 1 * TickSize, "LongEntry"); } if ((lastProfit < 0 && lastProfit > -140) && Close[0] == High[0] ) { EnterLongLimit(2, Close[0] + 1 * TickSize, "LongEntry"); } if ((lastProfit < -140) && Close[0] == High[0] ) { EnterLongLimit(6, Close[0] + 1 * TickSize, "LongEntry"); }
Comment
-
Full fills. It's occurring in replay and SIM. Maybe there is a better way to accomplish my goal. I want to trade one contract unless I get a looser. If a looser occurs when trading one contract, then trade two on the next trade. If that looses, trade 6 contracts. I was attempting to do that using dollar loss amounts but maybe you know of a better way.
Comment
-
It might help if you posted the bit of code that you're having trouble with.Originally posted by Tdschulz View PostI tried Printing lastTrade.Quantity and I receive an error stating 'lastTrade' does not exist in the current context. It compiles fine when used in the lastProfit statement but gives an error when trying to print it. Any ideas?
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
651 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
370 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
109 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
574 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
577 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment