Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
How to set 2 or more profit targets?
Collapse
X
-
Entering multiple limit orders
Hi i want to enter two separate limit orders when one set of conditions are meet to be able to sclae out of the position with different targets but only the first of my limit order in the code gets filled. Could you please assist me with this.
my code;
protected override void OnBarUpdate()
{
// Long Entry
if (Position.MarketPosition == MarketPosition.Flat && entrysignal)
{
EnterLongLimit(1, EntryPrice, "firsttrade");
EnterLongLimit(1, EntryPrice, "secondtrade");
}
// Long Target
if (Position.MarketPosition == MarketPosition.Long)
{
ExitLongLimit(TargetPrice1, "firsttrade");
ExitLongLimit(TargetPrice2, "secondtrade");
}
{
Im not sure what to do to enter 2 separate limit orders at the same condition, it seems that ninja only enters the first order in the code
Comment
-
I have a similar strategy.
My strategy places three orders each tagged uniquely. By example, order 1 for 404 shares, order 2 for 1,200 shares and order 3 for another 404. I set profit targets for 2 of them at 8% and 12%. When the strategy runs, my broker (IB) shows 2 sell/limit orders, both for the full size of my total position ( 2,008). The limit orders are set at the right profit target. If they both execute I would sell out my total position into a short position. Why are the limit orders not placed for 404 and 1,200 i.e. the size of the two uniquely placed orders? Very simple logic.
I also have a question about syncing my strategy position with my broker position when I start the strategy in the morning. I have set the parameter on NinjaScripts to "immediately submit live working historical orders". When my strategy position is 2,008, for example, I manually buy 2,008 on TWS. Does it make a difference if I start the strategy first or should I place the order then start my strategy. As soon as the strategy runs, it places the two limit orders for profit targets albeit for the full quantity of the position I hold. Thanks in advance for your comments.
protected override void Initialize()
{
TimeInForce = Cbi.TimeInForce.Gtc;
Add(EMA(25));
Add(SMA(28));
EMA(Fast).Plots[0].Pen.Color = Color.Blue;
SMA(Slow).Plots[0].Pen.Color = Color.Green;
ExitOnClose = false;
EntriesPerDirection = 3;
EntryHandling = EntryHandling.UniqueEntries;
IncludeCommission = true;
SetProfitTarget ("XYZ 1", CalculationMode.Percent, .08);
SetProfitTarget ("XYZ 2", CalculationMode.Percent, .12);
SetProfitTarget ("XYZ 4", CalculationMode.Percent, .08);
SetProfitTarget ("XYZ 5", CalculationMode.Percent, .12);
TraceOrders = true;
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (CrossAbove(EMA(Fast), SMA(Slow), 1))
{
ExitShort();
computeAmountToTrade();
SharesToBuy = (int) Math.Round(AmountToTrade / Close[0]);
EnterLong((int) Math.Round(SharesToBuy * .2), "XYZ 1");
EnterLong((int) Math.Round(SharesToBuy * .6), "XYZ 2");
EnterLong((int) Math.Round(SharesToBuy * .2), "XYZ 3");
}
else if (CrossBelow(EMA(Fast), SMA(Slow), 1))
{
ExitLong();
computeAmountToTrade();
SharesToBuy = (int) Math.Round(AmountToTrade / Close[0]);
EnterShort((int) Math.Round(SharesToBuy * .2), "XYZ 4");
EnterShort((int) Math.Round(SharesToBuy * .6), "XYZ 5");
EnterShort((int) Math.Round(SharesToBuy * .2), "XYZ 6");
}
}
// functions
void computeAmountToTrade()
{
AmountToTrade = AccountCashBalance;
}
#region Properties
Comment
-
sharpie, are you using this script with order handling set to 'PerEntryExecution' or 'ByStrategyPosition'? http://www.ninjatrader-support.com/H...romAChart.html
As far as the code goes, there should be no need for the ExitLong() and ExitShort() as the Enter() methods would reverse your position as well.
Comment
-
Thanks, one problem solved, but another one has surfaced.
On the last bar yesterday one of my two profit targets was met and the limit order for that target was executed reducing my position from 2020S to 1616S. I just restarted NT this morning. When I start the chart it shows the trade execution. Then I initiate the strategy and the chart no longer shows the order being placed, two working orders are placed for my profit targets with IB. I now have a position of 2020S with my strategy and 1616S with IB. I am out of sync. What will happen once the market opens? It seems to me that if and when my position is cleared I will end up flat in the strategy and 404L at my broker.
Comment
-
Last question, I hope.
On my crossover (occured before the market opened) my strategy generated an order "buy to cover" my existing short AND a "buy" close position. Had I left the orders intact I would have bought "twice". This may be because I have an "ExitShort" in front of my "EnterLongs" in my strategy. You mentioned that the "Exit" was redundant, but I have not yet removed them. Could this be the reason I am generating 2 buy orders? If not, why?
Thanks for your help in getting me going Bertrand. I don't like the idea that I may be out of sync every morning, but understand that will be rectified in NT7.
Comment
-
And absolutely do not, I repeat, DO NOT FORGETOriginally posted by NinjaTrader_Josh View Postbkout,
What needs to be done to setup scale outs is to have separate entry signal names. When you have separate names then you can tie the appropriate percentage target/stops to the positions.
Originally posted by NinjaTrader_Josh View PostRoss,
To scale-out would require you to scale-in first and then tie each scale-out order to the appropriate scale-in counter part. Be sure to be running with PerEntryExecution for your Stop & Target Handling too.
And if you want to make sure your strategy is ALWAYS SET TO THIS NO MATTER WHAT! Then put this in your properties region in your code. You will need to unlock your code, so this is for advanced users only. It is not supported by NT.
My hope is that this helps someone.Code:[SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][Description([/FONT][/SIZE][/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"Stop & Target Submission"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])][/SIZE][/FONT] [SIZE=2][FONT=Courier New][Category([/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"Order Handling"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])][/SIZE][/FONT] [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] StopTargetHandling StopTargetHandling[/SIZE][/FONT] [SIZE=2][FONT=Courier New]{[/FONT][/SIZE] [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]get[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] { [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] StopTargetHandling.PerEntryExecution; }[/SIZE][/FONT] [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]set[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] { [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]base[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2].StopTargetHandling=StopTargetHandling.PerEntryExecution;}[/SIZE][/FONT] [SIZE=2][FONT=Courier New]}[/FONT][/SIZE] [/SIZE][/FONT]
Keywords: Advanced Order Handling, SetStopLoss, Live Til Cancelled, PerEntryExecution
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
104 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
52 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
34 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
38 views
0 likes
|
Last Post
|
||
|
Started by Mindset, 02-28-2026, 06:16 AM
|
0 responses
74 views
0 likes
|
Last Post
by Mindset
02-28-2026, 06:16 AM
|

Comment