private double myPnl;
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Mutiple Position exit question
Collapse
X
-
Everything is working great, except.......Why do you suppose this doesn't exit the positions when the overall profit is 2 pips???
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
private double myPnl;
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
Add("$GBPUSD", PeriodType.Tick, 1);
TraceOrders = true;
CalculateOnBarClose = false;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
{
if (Historical)
return;
}
// Checks if OnBarUpdate() is called from an update on EUR/USD minute Bars
if (BarsInProgress == 0)
{
EnterLong(DefaultQuantity, "EUR/USD Long");
}
// Checks if OnBarUpdate() is called from an update on GBP/USD minute Bars
if (BarsInProgress == 1)
{
EnterShort(DefaultQuantity, "GBP/USD Short");
}
// Positions[0] is the posotion of the EUR/USD and Positions[1] is that of the GBP/USD
myPnl = Positions[0].GetProfitLoss(Closes[0][0], PerformanceUnit.Points) + Positions[1].GetProfitLoss(Closes[1][0], PerformanceUnit.Points);
if (myPnl > 1)
{
ExitLong();
ExitShort();
}
Comment
-
I am talking about your exit orders. You need to follow the TraceOrders and see if the exits were ever sent. You need to use Print statements also and follow if that if-statement of yours was ever evaluated to true. You should also print the value of your variable and run calculations by hand.Josh P.NinjaTrader Customer Service
Comment
-
OK...I just did that and it didn't really make sense.......
I added Print ("Total Profit/Loss is" + myPnl); to my code and when the EUR/USD and GBP/USD were at -$8.00 and +$1.00 for a total of -$7.00, the print statement in the output window said:
Total Profit/Loss is-0.000300000000000189
???????
When I was at -$9.00 and +$4.00 for a total of -$5.00, it read:
Total Profit/Loss is-0.000100000000000211
???????
Comment
-
It appears to me that the EUR/USD (Primary) is working properly, but the GBP/USD is not.
I redid the print statement and when the EUR/USD was down 4 and the GBP was down 5, it printed:
EUR/USD is at-0.000400000000000178
GBP/USD is at9.9999999999989E-05
Total Profit/Loss is-0.000300000000000189
My code (listed previously here) uses the GBP/USD at (BarsInProgress == 1)
which is why I believe the Positions[1] is correct???
Shouldn't Print ("GBP/USD is at" + Positions[1].GetProfitLoss(Closes[1][0], PerformanceUnit.Points)); give me the correct profit/loss??
Comment
-
Josh/Bertrand.....
Attached is my code. Maybe this will make it easier for you...... Why is it not returning the correct myPnl and therefore not exiting when the overall profit is 2??? Thanks a bunch!Attached Files
Comment
-
Edgeliner, lets take a couple of steps back. The code Bertrand provided will give you the net P/L in points, which are equal to individual pips in this case (1 pip or 1 point for 6E = .0001).
If you're interested in getting the P/L in ticks or pips, you could work with TickSize to get to the right value:
The extra decimals that don't seem to make any sense at all are a result of rounding errors and such. They can probably be ignored for now.Code:// lets say a certain variable 'x' has a value of 0.0003 (like what you've been seeing) // another assumption we'll make here, for converting to ticks, that you're trading 6E (with a TickSize of 0.0001) double profitInTicks = x / TickSize; // now profitInTicks has a value of 3.
From what I can tell, you're looking for the net P/L in dollars. This code should do the trick:
Code:double myPnl = Positions[0].GetProfitLoss(Closes[0][0], PerformanceUnit.Currency) + Positions[1].GetProfitLoss(Closes[1][0], PerformanceUnit.Currency);
AustinNinjaTrader Customer Service
Comment
-
Edgeliner, just saw you posted a reply while I was replying.
I'll sum it up:
The code Bertrand gave you will give you the p/l in points, like 0.0003 or 0.0231, not in $.
Basically,
needs to be changed to:Code:PerformanceUnit.Points
Code:PerformanceUnit.Currency
AustinNinjaTrader Customer Service
Comment
-
Thanks a bunch Austin! I thought perhaps that was the case, but couldn't find any help in the help section on it. I changed it, and it is working great for the EUR/USD, but the GBP/USD is not! It says that the GBP/USD total profit/loss is 0 at all times???? Could you look at the code again and see if you can make sense of it??? Thanks again!!! I really appreciate your help!
Comment
-
Sure Bertrand....here it is......let me know if you want me to upload it .........
#region Variables
// Wizard generated variables
privateint myInput0 = 1; // Default setting for MyInput0
privatedouble myPnl;
// User defined variables (add any user defined variables below)
#endregion
///<summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
///</summary>
protectedoverridevoid Initialize()
{
Add("$GBPUSD", PeriodType.Tick, 1);
TraceOrders = true;
CalculateOnBarClose = false;
}
///<summary>
/// Called on each bar update event (incoming tick)
///</summary>
protectedoverridevoid OnBarUpdate()
{
{
if (Historical)
return;
}
// Checks if OnBarUpdate() is called from an update on EUR/USD minute Bars
if (BarsInProgress == 0)
{
EnterLong(DefaultQuantity, "EUR/USD Long");
}
// Checks if OnBarUpdate() is called from an update on GBP/USD minute Bars
if (BarsInProgress == 1)
{
EnterShort(DefaultQuantity, "GBP/USD Short");
}
// Positions[0] is the posotion of the EUR/USD and Positions[1] is that of the GBP/USD
// I tried adding this here at 1 point to see if it would help, but it didn't seem to .......... if (BarsInProgress == 0)
{
myPnl = ((Positions[0].GetProfitLoss(Closes[0][0], PerformanceUnit.Currency) + Positions[1].GetProfitLoss(Closes[1][0], PerformanceUnit.Currency)));
Print ("EUR/USD is at " + Positions[0].GetProfitLoss(Closes[0][0], PerformanceUnit.Currency));
Print ("GBP/USD is at " + Positions[1].GetProfitLoss(Closes[1][0], PerformanceUnit.Currency));
Print ("Total Profit/Loss is " + myPnl);
if (myPnl > 1)
{
ExitLong();
}
if (myPnl > 1)
{
ExitShort();
}
}
}
#region Properties
[Description("")]
[Category("Parameters")]
publicint MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
#endregion
}
}
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
639 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
366 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
107 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
569 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
572 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment