Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
Partner 728x90
Collapse
NinjaTrader
Mutiple Position exit question
Collapse
X
-
Mutiple Position exit question
If I am using a strategy that opens 2 different positions, 1 in the eur/usd and 1 in the gbp/usd using BarsInProgress, and I get to a profit of 5 pips, what is the easiest way to simply exit the positions (prior to the close of the bar)???Tags: None
-
Bertrand,
I don't want to have a profit target of 5 pips for each one....what I want to do is, let's say that EUR is up 17 and GBP/USD is down 12, or if GBP/USD is up 44 and EUR/USD is down 39, or if EUR/USD is up 6 and GBP is down 1, etc., there is an overall profit of 5. Does that make sense? In other words, if my strategy opens up both positions using BarsArray, I want to exit the positions when there is an overall profit a 5. How can I achieve this? I tried if (Postion.AvgPrice * TickSize == 5), then close the positions, but it doesn't close them????
Comment
-
You would then need to calculate the combined PnL of your positions and then check if this is exceeding your target to get flat -
Code:double myPnl = Positions[0].GetProfitLoss(Closes[0][0], PerformanceUnit.Points) + Positions[1].GetProfitLoss(Closes[1][0], PerformanceUnit.Points);
BertrandNinjaTrader Customer Service
Comment
-
Bertrand.... Please be more specific! I just want to figure out how to program if I have a total profit of 5 with my 2 positions ...... close out. I tried what you said and have had no luck. Can you please be more specific? Help me out with the "double" thing please......
Comment
-
edgeliner, please post the exact code you attempted using - my snippet was just a sample how to get the total profit summed for both of your positions. You would need to construct a conditions out of this to exit your open positions once your threshhold is reached or exceeded.BertrandNinjaTrader Customer Service
Comment
-
Here it is Bertrand..... I just want to exit when the profit is 5 pips, not neccessarily at the end of the bar......
protected override void Initialize()
{
Add("$GBPUSD", PeriodType.Minute, 1);
TraceOrders = true;
CalculateOnBarClose = true;
}
/// <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 position 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 == 5)
{
ExitLong();
ExitShort();
}
Comment
-
Josh.....
If I compile this as is, it says "The name 'myPnl' does not exist in the current context
If I add:
public double myPnl
{
get { return myPnl; }
set { myPnl = Math.Max(1, value); }
}
to the #region Properties, it compiles fine, but when I try to load it to a chart, Ninjatrader crashes! Does that make sense?
Comment
-
Here is what my #region Properties looks like:
#region Properties
[Description("")]
[Category("Parameters")]
public int MyInput0
{
get { return myInput0; }
set { myInput0 = Math.Max(1, value); }
}
public double myPnl
{
get { return myPnl; }
set { myPnl = Math.Max(1, value); }
}
#endregion
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by DawnTreader, 05-08-2024, 05:58 PM
|
17 responses
62 views
0 likes
|
Last Post Today, 05:36 AM | ||
Started by ttrader23, 05-08-2024, 09:04 AM
|
10 responses
48 views
0 likes
|
Last Post Today, 05:21 AM | ||
Started by agclub, 04-21-2024, 08:57 PM
|
6 responses
41 views
0 likes
|
Last Post Today, 05:18 AM | ||
Started by thumper57, 05-11-2024, 04:30 PM
|
9 responses
28 views
0 likes
|
Last Post
by thumper57
Today, 05:16 AM
|
||
Started by Seneca, 08-25-2020, 08:31 AM
|
2 responses
5,945 views
0 likes
|
Last Post
by Tin34
Today, 05:03 AM
|
Comment