#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Enter the description of your strategy here")]
public class TestMarketExit : Strategy
{
#region Variables
// User defined variables (add any user defined variables below)
private IOrder entryOrder;
private IOrder stopOrder;
private IOrder profitOrder;
private int stopLossTicks = 22;
private int profitTargetTicks = 50;
#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()
{
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (Historical) return;
if (
Position.MarketPosition == MarketPosition.Flat
&& Close[0] > Open[0]
) {
entryOrder = EnterLong(DefaultQuantity, "entry");
}
if (
Position.MarketPosition == MarketPosition.Long
&& Close[0] < Open[0]
) {
ExitLong("exit", "entry");
}
}
protected override void OnOrderUpdate(IOrder order)
{
if (IsFilled(entryOrder, order)) {
double stop = order.AvgFillPrice - stopLossTicks * TickSize;
double profit = order.AvgFillPrice + profitTargetTicks * TickSize;
stopOrder = ExitLongStop(0, true, DefaultQuantity, stop, "exit", "entry");
profitOrder = ExitLongLimit(0, true, DefaultQuantity, profit, "profit", "entry");
}
}
private bool IsFilled(IOrder testOrder, IOrder order)
{
return
testOrder != null && testOrder == order
&& order.OrderState == OrderState.Filled;
}
#region Properties
#endregion
}
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
ExitLongStop + ExitLong = Overfill
Collapse
X
-
ExitLongStop + ExitLong = Overfill
Hello, how I can do secure ExitLong() if there is ExitLongStop placed?
It works well on historical... On live market when stop loss filled first (but I guess NT don't know about it yet) and then sent market exit... I have overfill... Is there any secure exit by market price?Code:Tags: None
-
Hello vadzim, and thank you for your question.
I would recommend, in this situation, setting up a boolean value that is only true once your targets are in place, such as the following :
Please let us know if there are any other ways we can help.Code:[FONT=Comic Sans MS] [FONT=Courier New] private bool targetsSet; /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { if (Historical) return; if ( Position.MarketPosition == MarketPosition.Flat && Close[0] > Open[0] ) { entryOrder = EnterLong(DefaultQuantity, "entry"); targetsSet = false; } if ( Position.MarketPosition == MarketPosition.Long && Close[0] < Open[0] && targetsSet ) { ExitLong("exit", "entry"); /* NOTE: Please consider cancelling your targets at this point */ } } protected override void OnOrderUpdate(IOrder order) { if (IsFilled(entryOrder, order)) { double stop = order.AvgFillPrice - stopLossTicks * TickSize; double profit = order.AvgFillPrice + profitTargetTicks * TickSize; stopOrder = ExitLongStop(0, true, DefaultQuantity, stop, "exit", "entry"); profitOrder = ExitLongLimit(0, true, DefaultQuantity, profit, "profit", "entry"); targetsSet = true; } } private bool IsFilled(IOrder testOrder, IOrder order) { return testOrder != null && testOrder == order && order.OrderState == OrderState.Filled; }[/FONT][/FONT]Jessica P.NinjaTrader Customer Service
-
I want enter in position... And place StopLoss and ProfitTarget... after that if some conditions true... I would like to do MarketExit... In most cases it works well (after MarketExit stop loss and profit target are canceled automaticly)... but very rare OverFill is happened... so, how securely do MarketExit, if StopLoss order exists?
Comment
-
Hello again vadzim,
Should the boolean switch I recommended earlier not suit your trading style, I would like to encourage you to use the SetProfitTarget and SetStopLoss methods. I am providing documentation for each.
You would set these up inside your Initialize routine, and they would place targets the same way ATM strategies do as soon as you enter a position, with no extra work on your part.Jessica P.NinjaTrader Customer Service
Comment
-
It doesn't matter... The same result with SetStopLoss/SetProfitTarget I did as simple as possible... Easy to reproduce it... is 10 Range bars... and stop loss 11 ticks... Connect to Simulated data feed... run strategy... make Up trend... wait long entry... and make Down trend...
Of course it's not all the time... in 90% it's ok... but still in 10% I have OverFill...
Code:namespace NinjaTrader.Strategy { /// <summary> /// Enter the description of your strategy here /// </summary> [Description("Enter the description of your strategy here")] public class TestMarketExit : Strategy { #region Variables // Wizard generated variables private int myInput0 = 1; // Default setting for MyInput0 // User defined variables (add any user defined variables below) private IOrder entryOrder; private IOrder stopOrder; private IOrder profitOrder; private int stopLossTicks = 11; private int profitTargetTicks = 50; #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() { CalculateOnBarClose = true; SetStopLoss(CalculationMode.Ticks, stopLossTicks); SetProfitTarget(CalculationMode.Ticks, profitTargetTicks); } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { if (Historical) return; if ( Position.MarketPosition == MarketPosition.Flat && Close[0] > Open[0] ) { entryOrder = EnterLong(); } if ( Position.MarketPosition == MarketPosition.Long && Close[0] < Open[0] ) { ExitLong(); } } protected override void OnOrderUpdate(IOrder order) { Print(order.ToString()); } #region Properties [Description("")] [GridCategory("Parameters")] public int MyInput0 { get { return myInput0; } set { myInput0 = Math.Max(1, value); } } #endregion } }Last edited by vadzim; 07-22-2016, 01:56 PM.
Comment
-
Code:Order='fd838b08b76b496e8173724a94ce6c0e/Sim101' Name='Buy' State=PendingSubmit Instrument='CL 09-16' Action=Buy Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=0 Fill price=0 Token='fd838b08b76b496e8173724a94ce6c0e' Gtd='01.01.0001 0:00:00' Order='fd838b08b76b496e8173724a94ce6c0e/Sim101' Name='Buy' State=Accepted Instrument='CL 09-16' Action=Buy Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=0 Fill price=0 Token='fd838b08b76b496e8173724a94ce6c0e' Gtd='01.01.0001 0:00:00' Order='fd838b08b76b496e8173724a94ce6c0e/Sim101' Name='Buy' State=Working Instrument='CL 09-16' Action=Buy Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=0 Fill price=0 Token='fd838b08b76b496e8173724a94ce6c0e' Gtd='01.01.0001 0:00:00' Order='fd838b08b76b496e8173724a94ce6c0e/Sim101' Name='Buy' State=Filled Instrument='CL 09-16' Action=Buy Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=1 Fill price=39,99 Token='fd838b08b76b496e8173724a94ce6c0e' Gtd='01.01.0001 0:00:00' Order='5d9b261a5fa8485caf8b7e18bb90f5bf/Sim101' Name='Stop loss' State=PendingSubmit Instrument='CL 09-16' Action=Sell Limit price=0 Stop price=39,88 Quantity=1 Type=Stop Tif=Gtc OverFill=False Oco='7620f5da0fca4758815ab7cf32f482bf-1051' Filled=0 Fill price=0 Token='5d9b261a5fa8485caf8b7e18bb90f5bf' Gtd='01.01.0001 0:00:00' Order='9bd60e25fd804a5f8ed01211a896c801/Sim101' Name='Profit target' State=PendingSubmit Instrument='CL 09-16' Action=Sell Limit price=40,49 Stop price=0 Quantity=1 Type=Limit Tif=Gtc OverFill=False Oco='7620f5da0fca4758815ab7cf32f482bf-1051' Filled=0 Fill price=0 Token='9bd60e25fd804a5f8ed01211a896c801' Gtd='01.01.0001 0:00:00' Order='5d9b261a5fa8485caf8b7e18bb90f5bf/Sim101' Name='Stop loss' State=Accepted Instrument='CL 09-16' Action=Sell Limit price=0 Stop price=39,88 Quantity=1 Type=Stop Tif=Gtc OverFill=False Oco='7620f5da0fca4758815ab7cf32f482bf-1051' Filled=0 Fill price=0 Token='5d9b261a5fa8485caf8b7e18bb90f5bf' Gtd='01.01.0001 0:00:00' Order='9bd60e25fd804a5f8ed01211a896c801/Sim101' Name='Profit target' State=Accepted Instrument='CL 09-16' Action=Sell Limit price=40,49 Stop price=0 Quantity=1 Type=Limit Tif=Gtc OverFill=False Oco='7620f5da0fca4758815ab7cf32f482bf-1051' Filled=0 Fill price=0 Token='9bd60e25fd804a5f8ed01211a896c801' Gtd='01.01.0001 0:00:00' Order='9bd60e25fd804a5f8ed01211a896c801/Sim101' Name='Profit target' State=Working Instrument='CL 09-16' Action=Sell Limit price=40,49 Stop price=0 Quantity=1 Type=Limit Tif=Gtc OverFill=False Oco='7620f5da0fca4758815ab7cf32f482bf-1051' Filled=0 Fill price=0 Token='9bd60e25fd804a5f8ed01211a896c801' Gtd='01.01.0001 0:00:00' Order='5d9b261a5fa8485caf8b7e18bb90f5bf/Sim101' Name='Stop loss' State=Working Instrument='CL 09-16' Action=Sell Limit price=0 Stop price=39,88 Quantity=1 Type=Stop Tif=Gtc OverFill=False Oco='7620f5da0fca4758815ab7cf32f482bf-1051' Filled=0 Fill price=0 Token='5d9b261a5fa8485caf8b7e18bb90f5bf' Gtd='01.01.0001 0:00:00' Order='6bc072cbced64846b512620c0fa4b42b/Sim101' Name='Sell' State=PendingSubmit Instrument='CL 09-16' Action=Sell Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=0 Fill price=0 Token='6bc072cbced64846b512620c0fa4b42b' Gtd='01.01.0001 0:00:00' Order='9bd60e25fd804a5f8ed01211a896c801/Sim101' Name='Profit target' State=PendingCancel Instrument='CL 09-16' Action=Sell Limit price=40,49 Stop price=0 Quantity=1 Type=Limit Tif=Gtc OverFill=False Oco='7620f5da0fca4758815ab7cf32f482bf-1051' Filled=0 Fill price=0 Token='9bd60e25fd804a5f8ed01211a896c801' Gtd='01.01.0001 0:00:00' Order='5d9b261a5fa8485caf8b7e18bb90f5bf/Sim101' Name='Stop loss' State=Filled Instrument='CL 09-16' Action=Sell Limit price=0 Stop price=39,88 Quantity=1 Type=Stop Tif=Gtc OverFill=False Oco='7620f5da0fca4758815ab7cf32f482bf-1051' Filled=1 Fill price=39,87 Token='5d9b261a5fa8485caf8b7e18bb90f5bf' Gtd='01.01.0001 0:00:00' Order='9bd60e25fd804a5f8ed01211a896c801/Sim101' Name='Profit target' State=Cancelled Instrument='CL 09-16' Action=Sell Limit price=40,49 Stop price=0 Quantity=1 Type=Limit Tif=Gtc OverFill=False Oco='7620f5da0fca4758815ab7cf32f482bf-1051' Filled=0 Fill price=0 Token='9bd60e25fd804a5f8ed01211a896c801' Gtd='01.01.0001 0:00:00' Order='6bc072cbced64846b512620c0fa4b42b/Sim101' Name='Sell' State=Accepted Instrument='CL 09-16' Action=Sell Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=0 Fill price=0 Token='6bc072cbced64846b512620c0fa4b42b' Gtd='01.01.0001 0:00:00' Order='6bc072cbced64846b512620c0fa4b42b/Sim101' Name='Sell' State=Working Instrument='CL 09-16' Action=Sell Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=0 Fill price=0 Token='6bc072cbced64846b512620c0fa4b42b' Gtd='01.01.0001 0:00:00' **NT** An over fill was detected on order 'Order='6bc072cbced64846b512620c0fa4b42b/Sim101' Name='Sell' State=Filled Instrument='CL 09-16' Action=Sell Limit price=0 Stop price=0 Quantity=1 Strategy='TestMarketExit' Type=Market Tif=Gtc Oco='' Filled=1 Fill price=39,87 Token='6bc072cbced64846b512620c0fa4b42b' Gtd='01.12.2099 0:00:00'' generated by strategy 'TestMarketExit/ed900c521c654f56a188e1a2882e3a94' : This strategy will be disabled and NinjaTrader will attempt to cancel/close any strategy generated orders and positions. Please check your account orders and positions and take any appropriate action. Order='6bc072cbced64846b512620c0fa4b42b/Sim101' Name='Sell' State=Filled Instrument='CL 09-16' Action=Sell Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=1 Fill price=39,87 Token='6bc072cbced64846b512620c0fa4b42b' Gtd='01.01.0001 0:00:00' Order='591e7ac7b58b453fae13acb6de1a77b3/Sim101' Name='Stop loss' State=PendingSubmit Instrument='CL 09-16' Action=BuyToCover Limit price=0 Stop price=39,98 Quantity=1 Type=Stop Tif=Gtc OverFill=False Oco='7ed08fc2546e47c1a1c144d2948dfdf0-1051' Filled=0 Fill price=0 Token='591e7ac7b58b453fae13acb6de1a77b3' Gtd='01.01.0001 0:00:00' Order='96f8bb1e61c74d28a44ba1facf0567f8/Sim101' Name='Profit target' State=PendingSubmit Instrument='CL 09-16' Action=BuyToCover Limit price=39,37 Stop price=0 Quantity=1 Type=Limit Tif=Gtc OverFill=False Oco='7ed08fc2546e47c1a1c144d2948dfdf0-1051' Filled=0 Fill price=0 Token='96f8bb1e61c74d28a44ba1facf0567f8' Gtd='01.01.0001 0:00:00' Order='591e7ac7b58b453fae13acb6de1a77b3/Sim101' Name='Stop loss' State=Accepted Instrument='CL 09-16' Action=BuyToCover Limit price=0 Stop price=39,98 Quantity=1 Type=Stop Tif=Gtc OverFill=False Oco='7ed08fc2546e47c1a1c144d2948dfdf0-1051' Filled=0 Fill price=0 Token='591e7ac7b58b453fae13acb6de1a77b3' Gtd='01.01.0001 0:00:00' Order='96f8bb1e61c74d28a44ba1facf0567f8/Sim101' Name='Profit target' State=Accepted Instrument='CL 09-16' Action=BuyToCover Limit price=39,37 Stop price=0 Quantity=1 Type=Limit Tif=Gtc OverFill=False Oco='7ed08fc2546e47c1a1c144d2948dfdf0-1051' Filled=0 Fill price=0 Token='96f8bb1e61c74d28a44ba1facf0567f8' Gtd='01.01.0001 0:00:00' Order='96f8bb1e61c74d28a44ba1facf0567f8/Sim101' Name='Profit target' State=Working Instrument='CL 09-16' Action=BuyToCover Limit price=39,37 Stop price=0 Quantity=1 Type=Limit Tif=Gtc OverFill=False Oco='7ed08fc2546e47c1a1c144d2948dfdf0-1051' Filled=0 Fill price=0 Token='96f8bb1e61c74d28a44ba1facf0567f8' Gtd='01.01.0001 0:00:00' Order='bac94d2ca9a94a688abaeced1c3e6d64/Sim101' Name='Close' State=PendingSubmit Instrument='CL 09-16' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=0 Fill price=0 Token='bac94d2ca9a94a688abaeced1c3e6d64' Gtd='01.01.0001 0:00:00' Order='bac94d2ca9a94a688abaeced1c3e6d64/Sim101' Name='Close' State=Accepted Instrument='CL 09-16' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=0 Fill price=0 Token='bac94d2ca9a94a688abaeced1c3e6d64' Gtd='01.01.0001 0:00:00' Order='bac94d2ca9a94a688abaeced1c3e6d64/Sim101' Name='Close' State=Working Instrument='CL 09-16' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=0 Fill price=0 Token='bac94d2ca9a94a688abaeced1c3e6d64' Gtd='01.01.0001 0:00:00' Order='bac94d2ca9a94a688abaeced1c3e6d64/Sim101' Name='Close' State=Filled Instrument='CL 09-16' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1 Type=Market Tif=Gtc OverFill=False Oco='' Filled=1 Fill price=39,85 Token='bac94d2ca9a94a688abaeced1c3e6d64' Gtd='01.01.0001 0:00:00'
Comment
-
Hello vadzim,
To clarify, I was suggesting using SetProfitTarget and SetStopLoss instead of the code you have in your OnOrderUpdate to set targets. With the boolean approach, my recommendation was for you to define some protections, so that you never encounter a situation in which you are placing two exit orders in a row. In order to allow you the flexibility to define strategies the way you see fit, we have to allow you a degree of freedom which enables you to do things which may result in an overfill condition.
Please let me know if I can clarify any of the above, or if there are any other ways we can help.Jessica P.NinjaTrader Customer Service
Comment
-
According to the documentation for SetProfitTarget,
Therefore, to accomplish this, you can do the following :Originally posted by http://ninjatrader.com/support/helpGuides/nt7/setprofittarget.htm
A profit target order is automatically cancelled if the managing position is closed by another strategy generated exit order
In Initialize
When you would like to cancel both of these along with your entry orderCode:protected override void Initialize() { SetProfitTarget(CalculationMode.Ticks, profitTargetTicks); SetStopLoss(CalculationMode.Ticks, profitTargetTicks); }
You will also want to remove your OnOrderUpdate method entirely.Code:CancelOrder(entryOrder);
If you would like to do things more complicated than this, you will need to use the unmanaged approach. I am providing a link for more information.
Please let us know if there are any other ways we can help.Jessica P.NinjaTrader Customer Service
Comment
-
In post #5 code with... SetStopLoss/SetProfitTarget and EnterLong/ExitLong
That's it.... And I can reproduce OverFill a lot of times with Simulated data feed... Using 10 Range bars and 11 ticks stop loss...Code:protected override void Initialize() { CalculateOnBarClose = true; SetStopLoss(CalculationMode.Ticks, stopLossTicks); SetProfitTarget(CalculationMode.Ticks, profitTargetTicks); } protected override void OnBarUpdate() { if (Historical) return; if ( Position.MarketPosition == MarketPosition.Flat && Close[0] > Open[0] //bar up ) { entryOrder = EnterLong(); } if ( Position.MarketPosition == MarketPosition.Long && Close[0] < Open[0] //bar down ) { ExitLong(); } }
Comment
-
Hello vadzim,
I was unable to generate overfill conditions with the attached strategy using 1 minute bars in the simulated data feed with the ES 09-16 contract. If you are able to do so, could you send your most recent files from your log and trace folders under (My) Documents\NinjaTrader 7 to platformsupport[at]ninjatrader[dot]com , referencing this unique ID? 1547904Attached FilesJessica P.NinjaTrader Customer Service
Comment
-
As I mentioned before 10 Range bars and 11 ticks stop loos... Trend max Up... entry is placed... trend Down... waiting stop loss or market exit...Originally posted by vadzim View PostThat's it.... And I can reproduce OverFill a lot of times with Simulated data feed... Using 10 Range bars and 11 ticks stop loss...
I will try send logs tomorrow...
Comment
-
Thank you vadzim. I was unable to use the attached strategy to produce overfills using the simulated data feed with my profit target and stop loss set to 11 ticks on range bars with a value of 10. When you do testing, please use the strategy I attached to my message previously.Jessica P.NinjaTrader Customer Service
Comment
-
Hello vadzim,
Thank you for your patience.
Overfills can occur in the case of an using two exit orders. It would be recommended to first cancel the ExitLongStop and ExitLongLimit using CancelOrder(stopOrder) and CancelOrder(profitOrder).
Another way you could do this is to adjust the ExitLongLimit to the current Ask price rather than submit a new order to exit. This would also mean cancelling the stopOrder as well.
Please let me know if you have any questions.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
576 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
334 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
101 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
553 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
551 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment