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!
See more
See less

Partner 728x90

Collapse

Stop orders rejected

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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 trader

    #2
    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?

    Comment


      #3
      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


        #4
        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.
        rejected stop

        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


          #5
          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 Files
          Brandon H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by PaulMohn, Today, 12:36 PM
          0 responses
          2 views
          0 likes
          Last Post PaulMohn  
          Started by love2code2trade, 04-17-2024, 01:45 PM
          4 responses
          38 views
          0 likes
          Last Post love2code2trade  
          Started by alifarahani, Today, 09:40 AM
          2 responses
          14 views
          0 likes
          Last Post alifarahani  
          Started by junkone, Today, 11:37 AM
          3 responses
          20 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by frankthearm, Yesterday, 09:08 AM
          12 responses
          44 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Working...
          X