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

ExitOrder not working as expected

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

    ExitOrder not working as expected


    HTML Code:
    Disabling NinjaScript strategy 'EquityEdgeCrossoverUnlocked/312501154'
    12/13/2023 4:40:00 AM: Going Short at Current Ask 16607.75
    12/13/2023 4:45:00 AMPositionAvgPrice:16608.25= Adding ShortLimit Exit Order at 16588.25
    12/13/2023 5:15:00 AM: Going Short at Current Ask 16607
    12/13/2023 5:15:00 AMPositionAvgPrice:16608.25= Adding ShortLimit Exit Order at 16588.25
    12/13/2023 6:30:00 AM: Going Short at Current Ask 16609.75
    12/13/2023 6:30:00 AMPositionAvgPrice:16608.25= Adding ShortLimit Exit Order at 16588.25
    12/13/2023 6:55:00 AM: Going Short at Current Ask 16609.25
    12/13/2023 6:55:00 AMPositionAvgPrice:16608.25= Adding ShortLimit Exit Order at 16588.25
    12/13/2023 8:10:00 AM: Going Short at Current Ask 16621
    12/13/2023 8:10:00 AMPositionAvgPrice:16608.25= Adding ShortLimit Exit Order at 16588.25
    12/14/2023 5:05:00 AM: Going Short at Current Ask 16829.25
    12/14/2023 5:10:00 AMPositionAvgPrice:16829= Adding ShortLimit Exit Order at 16809
    12/14/2023 6:25:00 AM: Going Short at Current Ask 16820
    12/14/2023 6:25:00 AMPositionAvgPrice:16829= Adding ShortLimit Exit Order at 16809
    12/14/2023 7:05:00 AM: Going Short at Current Ask 16815.25
    12/14/2023 7:05:00 AMPositionAvgPrice:16829= Adding ShortLimit Exit Order at 16809
    12/14/2023 10:00:00 AM: Going Short at Current Ask 16735.25
    12/14/2023 10:05:00 AMPositionAvgPrice:16735.75= Adding ShortLimit Exit Order at 16715.75
    Enabling NinjaScript strategy 'EquityEdgeCrossoverUnlocked/312501154' : On starting a real-time strategy - StartBehavior=WaitUntilFlat AccountPosition=MNQ MAR24 1L EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On each tick IsUnmanaged=False MaxRestarts=4 in 5 minutes
    Disabling NinjaScript strategy 'EquityEdgeCrossoverUnlocked/312501154'
    12/13/2023 4:40:00 AM: Going Short at Current Ask 16607.75
    12/13/2023 4:45:00 AMPositionAvgPrice:16608.25= Adding ShortLimit Exit Order at 16588.25
    12/13/2023 5:15:00 AM: Going Short at Current Ask 16607
    12/13/2023 5:15:00 AMPositionAvgPrice:16608.25= Adding ShortLimit Exit Order at 16588.25
    12/13/2023 6:30:00 AM: Going Short at Current Ask 16609.75
    12/13/2023 6:30:00 AMPositionAvgPrice:16608.25= Adding ShortLimit Exit Order at 16588.25
    12/13/2023 6:55:00 AM: Going Short at Current Ask 16609.25
    12/13/2023 6:55:00 AMPositionAvgPrice:16608.25= Adding ShortLimit Exit Order at 16588.25
    12/13/2023 8:10:00 AM: Going Short at Current Ask 16621
    12/13/2023 8:10:00 AMPositionAvgPrice:16608.25= Adding ShortLimit Exit Order at 16588.25
    12/14/2023 5:05:00 AM: Going Short at Current Ask 16829.25
    12/14/2023 5:10:00 AMPositionAvgPrice:16829= Adding ShortLimit Exit Order at 16809
    12/14/2023 6:25:00 AM: Going Short at Current Ask 16820
    12/14/2023 6:25:00 AMPositionAvgPrice:16829= Adding ShortLimit Exit Order at 16809
    12/14/2023 7:05:00 AM: Going Short at Current Ask 16815.25
    12/14/2023 7:05:00 AMPositionAvgPrice:16829= Adding ShortLimit Exit Order at 16809
    12/14/2023 10:00:00 AM: Going Short at Current Ask 16735.25
    12/14/2023 10:05:00 AMPositionAvgPrice:16735.75= Adding ShortLimit Exit Order at 16715.75
    Enabling NinjaScript strategy 'EquityEdgeCrossoverUnlocked/312501154' : On starting a real-time strategy - StartBehavior=WaitUntilFlat AccountPosition=MNQ MAR24 1L EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On each tick IsUnmanaged=False MaxRestarts=4 in 5 minutes
    ​
    Code:
    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class EquityEdgeCrossoverUnlocked : Strategy
        {
            private int FirstEMA;
            private int SecondEMA;
    
            private EMA EMA1;
            private EMA EMA2;
            private bool PlacedExitShortLimitOrder;
            private bool PlacedExitLongLimitOrder;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "EquityEdgeCrossoverUnlocked";
                    Calculate                                    = Calculate.OnEachTick;
                    EntriesPerDirection                            = 1;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = true;
                    ExitOnSessionCloseSeconds                    = 30;
                    IsFillLimitOnTouch                            = false;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.Standard;
                    Slippage                                    = 0;
                    StartBehavior                                = StartBehavior.WaitUntilFlat;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = false;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                    StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                    BarsRequiredToTrade                            = 20;
                    // Disable this property for performance gains in Strategy Analyzer optimizations
                    // See the Help Guide for additional information
                    IsInstantiatedOnEachOptimizationIteration    = true;
                    EMALength1                    = 5;
                    EMALength2                    = 13;
                    LongTrailStop                    = 20;
                    ShortTrailStop                    = 20;
                    StrategyStartTime                        = DateTime.Parse("04:30", System.Globalization.CultureInfo.InvariantCulture);
                    StrategyEndTime                        = DateTime.Parse("13:45", System.Globalization.CultureInfo.InvariantCulture);
                    FirstEMA                    = 1;
                    SecondEMA                    = 1;
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {                
                    EMA1                = EMA(Close, Convert.ToInt32(EMALength1));
                    EMA2                = EMA(Close, Convert.ToInt32(EMALength2));
                    EMA1.Plots[0].Brush = Brushes.Green;
                    EMA2.Plots[0].Brush = Brushes.Red;
                    EMA2.Plots[0].Width = 2;
                    EMA1.Plots[0].Width = 2;
                    AddChartIndicator(EMA1);
                    AddChartIndicator(EMA2);
                    PlacedExitShortLimitOrder = false;
                    PlacedExitLongLimitOrder = false;
                }
            }
    
            protected override void OnBarUpdate()
            {
    //            if (BarsInProgress != 0)
    //                return;
    
                if (CurrentBars[0] < 1)
                    return;
    
                 // Set 1
                if ((CrossBelow(EMA1, EMA2, 1))
                     && (Times[0][0].TimeOfDay < StrategyEndTime.TimeOfDay)
                     && (Times[0][0].TimeOfDay > StrategyStartTime.TimeOfDay))
                {
                    Print(Time[0]  + ": Going Short at Current Ask " + GetCurrentAsk(0));
                    EnterShort(Convert.ToInt32(DefaultQuantity), @"");
                    PlacedExitShortLimitOrder = false;
                }
    
                 // Set 2
    //            if ((CrossAbove(EMA1, EMA2, 1))
    //                 && (Times[0][0].TimeOfDay < StrategyEndTime.TimeOfDay)
    //                 && (Times[0][0].TimeOfDay > StrategyStartTime.TimeOfDay))
    //            {
    //                Print(Time[0] + ": Going Long at Current Ask " + GetCurrentAsk(0));
    //                EnterLong(Convert.ToInt32(DefaultQuantity), @"Long");
    //                PlacedExitLongLimitOrder = false;
    //            }
    
                 //Set 3
    //            if (Position.MarketPosition == MarketPosition.Long && PlacedExitLongLimitOrder == false)
    //            {
    //                double PositionAvgPrice = Position.AveragePrice;
    //                double LongExitPrice = (Position.AveragePrice + (LongTrailStop));
    //                Print(Time[0]  + "PositionAvgPrice:" + PositionAvgPrice + "= Adding LongLimit Exit Order at " + LongExitPrice);
    //                ExitLongLimit(Convert.ToInt32(DefaultQuantity),  LongExitPrice, @"", @"Long");
    //                PlacedExitLongLimitOrder = true;
    //            }
    
                 // Set 4
                if (Position.MarketPosition == MarketPosition.Short && PlacedExitShortLimitOrder == false)
                {
                    double PositionAvgPrice = Position.AveragePrice;
                    double ShortExitPrice = (PositionAvgPrice - (ShortTrailStop));
                    Print(Time[0]  + "PositionAvgPrice:" + PositionAvgPrice + "= Adding ShortLimit Exit Order at " + ShortExitPrice);
                    ExitShortLimit(Convert.ToInt32(DefaultQuantity), ShortExitPrice, @"", @"");
                    PlacedExitShortLimitOrder = true;
                }
    
            }
    
            #region Properties
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="EMALength1", Order=1, GroupName="Parameters")]
            public int EMALength1
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="EMALength2", Order=2, GroupName="Parameters")]
            public int EMALength2
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="LongTrailStop", Order=3, GroupName="Parameters")]
            public int LongTrailStop
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="ShortTrailStop", Order=4, GroupName="Parameters")]
            public int ShortTrailStop
            { get; set; }
    
            [NinjaScriptProperty]
            [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKey")]
            [Display(Name="StrategyStartTime", Order=5, GroupName="Parameters")]
            public DateTime StrategyStartTime
            { get; set; }
    
            [NinjaScriptProperty]
            [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKey")]
            [Display(Name="StrategyEndTime", Order=6, GroupName="Parameters")]
            public DateTime StrategyEndTime
            { get; set; }
            #endregion
    
        }
    }
    ​



    Attached Files

    #2
    Hello futtrader,

    Thanks for your post.

    You mentioned your Exit order is not working as expected but you did not say exactly how you are expecting the order to behave.

    Ultimately, to understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    Also, enable TraceOrders which will let you know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_BrandonH View Post
      Hello futtrader,

      Thanks for your post.

      You mentioned your Exit order is not working as expected but you did not say exactly how you are expecting the order to behave.

      Ultimately, to understand why the script is behaving as it is, such as placing orders or not placing orders when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

      In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

      Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

      Also, enable TraceOrders which will let you know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

      Below is a link to a forum post that demonstrates how to use prints to understand behavior.
      https://ninjatrader.com/support/foru...121#post791121
      If you see my first order which is placed at 16608 as per the chart and I've also placed the ExitShortLimit at 20 point below the entry price but it was not executed ever.

      Comment


        #4
        Hello futtrader,

        Thanks for your notes.

        Debugging prints and Trace Orders should be used to determine exactly how your strategy orders behaved.

        Add debugging prints one line above the condition to place the exit orders that prints out each value used in your condition to place the exit order along with the time of the bar. Print out the Close price and the price the order is being submitted to to see if the Close price reaches the price your exit order is being placed at.

        Also, print the order object in OnOrderUpdate() to see how the order is behaving.

        And, enable TraceOrders which will let you know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.
        https://ninjatrader.com/support/foru...121#post791121

        OnOrderUpdate(): https://ninjatrader.com/support/help...rderupdate.htm
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by ageeholdings, Today, 07:43 AM
        0 responses
        7 views
        0 likes
        Last Post ageeholdings  
        Started by pibrew, Today, 06:37 AM
        0 responses
        4 views
        0 likes
        Last Post pibrew
        by pibrew
         
        Started by rbeckmann05, Yesterday, 06:48 PM
        1 response
        14 views
        0 likes
        Last Post bltdavid  
        Started by llanqui, Today, 03:53 AM
        0 responses
        6 views
        0 likes
        Last Post llanqui
        by llanqui
         
        Started by burtoninlondon, Today, 12:38 AM
        0 responses
        12 views
        0 likes
        Last Post burtoninlondon  
        Working...
        X