protected override void OnBarUpdate()
{
// Submit an entry limit order if we currently don't have an entry order open
if (entryOrder == null && Close[0] > Open[0])
{
entryOrder = EnterLongLimit(0, true, 1, Median[0] + -2 * TickSize, "MyEntry");
}
if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Position.AvgPrice + (7 * (TickSize / 2)))
{
// Checks to see if our Stop Order has been submitted already
if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice)
{
// Modifies stop-loss to breakeven
stopOrder = ExitLongStop(0, true, stopOrder.Quantity, Position.AvgPrice, "MyStop", "MyEntry");
}
}
}
/// <summary>
/// Called on each incoming order event
/// </summary>
protected override void OnOrderUpdate(IOrder order)
{
// Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
if (entryOrder != null && entryOrder.Token == order.Token)
{
// Reset the entryOrder object to null if order was cancelled without any fill
if (order.OrderState == OrderState.Working
&& order.LimitPrice < GetCurrentBid() - 4 * TickSize)
{
CancelOrder(entryOrder);
entryOrder = null;
}
}
}
/// <summary>
/// Called on each incoming execution
/// </summary>
protected override void OnExecution(IExecution execution)
{
if (entryOrder != null && entryOrder.Token == execution.Order.Token)
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
// Stop-Loss order 4 ticks below our entry price
stopOrder = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 4 * TickSize, "MyStop", "MyEntry");
// Target order 8 ticks above our entry price
targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 8 * TickSize, "MyTarget", "MyEntry");
// Resets the entryOrder object to null after the order has been filled or partially filled
if (execution.Order.OrderState != OrderState.PartFilled)
{
entryOrder = null;
}
}
}
// Reset our stop order and target orders' IOrder objects after our position is closed.
if ((stopOrder != null && stopOrder.Token == execution.Order.Token) || (targetOrder != null && targetOrder.Token == execution.Order.Token))
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
{
stopOrder = null;
targetOrder = null;
}
}
}
/// <summary>
/// Called on each incoming position event
/// </summary>
protected override void OnPositionUpdate(IPosition position)
{
// Print our current position to the lower right hand corner of the chart
DrawTextFixed("MyTag", position.ToString(), TextPosition.BottomRight);
}
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Orders not cancelling...
Collapse
X
-
Orders not cancelling...
Having an issue with a vers of SampleOnOrderUpdate which I've modified to use Limit Orders... but am having issues with the CancelOrder() command... my orders don't seem to be cancelling... I've tried TraceOrders but still am lost... appreciate any help... I'm calling the CancelOrders() from OnOrderUpdate()
Code:Tags: None
-
Then your bid is staying the same. Please use other conditions to check for to cancel your bar with. Please remember, in OnOrderUpdate() you only receive one event for the working state. It is not like OnOrderUpdate() is constantly checking for what your limit order price is vs the bid.Josh P.NinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_Josh View PostThen your bid is staying the same. Please use other conditions to check for to cancel your bar with. Please remember, in OnOrderUpdate() you only receive one event for the working state. It is not like OnOrderUpdate() is constantly checking for what your limit order price is vs the bid.
i guess this is where i'm hitting my wall in terms of getting around it...
any suggestion in terms of something to try?
Comment
-
when I put in the cancel logic in OnBarUpdate()... in the Log tab I get the following error...
"Error on calling 'OnBarUpdate' method for strategy "SampleOnOrderUpdate'. Object reference not set to an instance of an object.
Code:protected override void OnBarUpdate() { // Submit an entry limit order if we currently don't have an entry order open if (entryOrder == null && Close[0] > Open[0]) { entryOrder = EnterLongLimit(0, true, 1, Median[0] + -2 * TickSize, "MyEntry"); } if (entryOrder.OrderState == OrderState.Working && entryOrder.LimitPrice < Median[0] - 4 * TickSize) { CancelOrder(entryOrder); entryOrder = null; } if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Position.AvgPrice + (7 * (TickSize / 2))) { // Checks to see if our Stop Order has been submitted already if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice) { // Modifies stop-loss to breakeven stopOrder = ExitLongStop(0, true, stopOrder.Quantity, Position.AvgPrice, "MyStop", "MyEntry"); } } }
Comment
-
You still need to check for null first. http://www.ninjatrader-support2.com/...ead.php?t=4226Josh P.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
633 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
364 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
105 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
567 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
568 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|


Comment