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
Stop orders rejected
Collapse
X
-
Stop orders rejected
Is there a way to solve the problem of stop order rejection in NT8? This can be done in RTrader Pro with a market if order rejected option. I want to do something similar in ninja traderTags: None
- Likes 1
-
Yes, but you'll have to write all the code to catch the rejected order
and submit the recovery order completely by yourself.
NinjaScript does not provide an automatic way to do what you
ask -- but it provides all the facilities you need to do it yourself.
Are you using Managed or Unmanaged orders?
-
Hello caacapital,
Thank you for your post.
You could use RealtimeErrorHandling to determine the behavior of a strategy when the strategy places an order that is returned "Rejected". The default behavior is to stop the strategy, cancel any remaining working orders, and then close any open positions.
RealtimeErrorHandling could be set to IgnoreAllErrors to ignore any order errors received or StopCancelCloseIgnoreRejects to perform the default strategy behavior on all errors except order rejections. Please note that setting this property value to IgnoreAllErrors can have serious adverse effects on a running strategy unless you have programmed your own order rejection handling in the OnOrderUpdate() method. To do this you could trap the rejected order by checking if the OrderState is Rejected within OnOrderUpdate() followed by defining your own order rejection handling behavior for the rejected order.
Please see the example in the help guide link below that demonstrates using RealtimeErrorHandling and trapping a rejected order in OnOrderUpdate().
RealtimeErrorHandling — https://ninjatrader.com/es/support/h...orhandling.htm
Please let us know if we may assist further.Brandon H.NinjaTrader Customer Service
Comment
-
is there a template already created using ninjascript to auto close a position that had a stop rejected? What i came up with doesnt seem to be working its not even handling event updates. i tried in replay mode i was able to get a stop can not be placed below/above market.
complied as a strategy and loaded the strategy to the chart
Code:#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.Indicators; using NinjaTrader.NinjaScript.DrawingTools; #endregion //This namespace holds Strategies in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Strategies { public class stopRejected : Strategy { private int position_direction; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Automatically close position when a stop is rejected"; Name = "stopRejected"; Calculate = Calculate.OnEachTick; EntriesPerDirection = 1; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = true; ExitOnSessionCloseSeconds = 30; IsFillLimitOnTouch = false; MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; OrderFillResolution = OrderFillResolution.Standard; Slippage = 0; StartBehavior = StartBehavior.ImmediatelySubmit; TimeInForce = TimeInForce.Gtc; TraceOrders = false; RealtimeErrorHandling = RealtimeErrorHandling.StopCancelCloseIgnoreRejects ; StopTargetHandling = StopTargetHandling.PerEntryExecution; BarsRequiredToTrade = 1; // Disable this property for performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = true; position_direction = 0; } else if (State == State.Configure) { } } protected override void OnBarUpdate() { //Add your custom strategy logic here. } protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, C bi.MarketPosition marketPosition) { if (position.MarketPosition == MarketPosition.Flat) { // reset current_position position_direction = 0; } else if(position.MarketPosition == MarketPosition.Short) { position_direction = -1; Log("short quantity = %d", LogLevel.Alert); } else if(position.MarketPosition == MarketPosition.Long) { position_direction = 1; } } protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time) { } protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError) { double closePrice; if (order.OrderState == OrderState.Rejected) { Log("Reject captured.", LogLevel.Alert); // assume this is from a stop rejection emergency stop if(position_direction == 1) { closePrice = GetCurrentAsk(); ExitLongStopMarket(closePrice); } else if(position_direction == -1) { closePrice = GetCurrentBid(); ExitShortStopMarket(closePrice); } } } } }
Last edited by caacapital; 10-17-2020, 11:21 AM.
Comment
-
Hello caacapital,
Thank you for your note.
You could see the attached example strategy that demonstrates how to trap a rejected order in OnOrderUpdate() and exits a position. In the script, we enter a long position followed by placing a stop-loss order above the current Ask to get a rejected order error message. Then in OnOrderUpdate() we check if the order state is rejected and exit our current position.
When RealtimeErrorHandling is set to IgnoreAllErrors or StopCancelCloseIgnoreRejects, the example strategy will exit the position when an order is rejected and the strategy will continue running. If RealtimeErrorHandling is set to StopCancelClose, the example strategy will stop, any remaining working orders will be canceled, and any open positions managed by the strategy will be closed.
Please let us know if we may assist further.Attached FilesBrandon H.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by f.saeidi, Today, 11:02 AM
|
1 response
1 view
0 likes
|
Last Post Today, 11:08 AM | ||
Started by geotrades1, Today, 10:02 AM
|
4 responses
12 views
0 likes
|
Last Post
by geotrades1
Today, 11:08 AM
|
||
Started by rajendrasubedi2023, Today, 09:50 AM
|
3 responses
15 views
0 likes
|
Last Post Today, 10:57 AM | ||
Started by lorem, Today, 09:18 AM
|
2 responses
11 views
0 likes
|
Last Post Today, 10:55 AM | ||
Started by geddyisodin, Today, 05:20 AM
|
4 responses
30 views
0 likes
|
Last Post
by geddyisodin
Today, 10:45 AM
|
Comment