Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Are order values saved?

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

    Are order values saved?

    Hello Ninjatrader Staff,

    I would like to create some sort of a safety mechanic in my code so that my trades will be closed if price decides to jump over the Profit/Loss targets during the Close and Open of the day gaps.

    Currently I have this piece of code that assigns the Profit (TP) and Loss (SL) targets to the L1 trade:

    if (L1 != null && L1 == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled)
    {
    TP = ExitLongLimit(0, true, 1, Close[1] + 1 * TickSize, "TP", "L1");
    SL = ExitLongStopLimit (0, true, 1, KyoriBands(TwentyFive, Fifty, SeventyFive).Low2[0], KyoriBands(TwentyFive, Fifty, SeventyFive).Low2[0], "SL", "L1");
    }
    }


    My question is whether it's possible to use the TP and SL values to create something like...

    if (Order is Long && Close[0] > TP)
    {
    ExitLong(L1);
    }


    I am looking forward to your reply and I thank you in advance for your time to answer my question.

    All the best,

    Law

    #2
    Hello GLFX005,

    Thank you for the post.

    Sure that is possible, you can retrieve the various values from the orders once they are not null.

    In your other code where you want to use the orders, you can form the condition like this:

    Code:
    if(TP != null && TP.OrderAction == OrderAction.BuyToCover && Close[0] > TP.StopPrice)
    This will entirely depend on the type of order as to what values or variables are being used, you can see the Order documentation for explanations of the properties of the Order object: https://ninjatrader.com/support/help...lightsub=Order


    I look forward to being of further assistance.

    Comment


      #3
      Hello Jesse,

      Thank you for your answer.

      Should the line of code you provided go into OnExecution or OnOrder update?

      And what is the difference between an OrderAction.Buy and the OrderAction.BuyToCover?
      Same for OrderAction.Sell and OrderAction.SellShort?
      How should I implement these 4 options in the code so that there's no confusion as to whether I am Long or Short at the time?

      Comment


        #4
        Hello GLFX005,

        This would go in OnBarUpdate or OnMarketData, somewhere that gets price updates as you are trying to compare the price movement against the order price. The OnOrderUpdate and OnExecutionUpdate events are only called for order events like changes in status or fills.

        For the order action, these are just standard trading terms. I would suggest looking at some external resources to help with this question, one resource you could use here is investopedia:


        The buy to cover will be seen when exiting a short position and sell short is seen when exiting a long position. The type of order will be determined by the situation, you can use a Print to better understand each order you observe:

        Code:
        if(TP != null)
        {
            Print(TP.ToString());
        }
        If you need more information on any of the types of orders being used, there will be a lot more information in external trading information sites that are not specifically targeting NinjaTrader/NinjaScript.

        How should I implement these 4 options in the code so that there's no confusion as to whether I am Long or Short at the time?
        If you specifically want to know if you are currently long or short you can likely just use the Position object for that rather than trying to figure out what type of order was seen.


        Another observation here would be the way your code is structured. If you used the variable L1 and I assume S1 for the short, then only one of these should be not null at a time so that would be another way to determine which exit to call.

        Code:
        if(L1 != null)
        {
            //you are long
        }
        if(S1 != null)
        {
            //you are short
        }
        I look forward to being of further assistance.

        Comment


          #5
          Hello Jesse,

          Thank you for your answer.

          With the information you have provided me I have created the following code.

          if (Position.MarketPosition == MarketPosition.Short)
          {
          if (TP != null && TP.OrderAction == OrderAction.BuyToCover && Close[0] < TP.StopPrice)
          {
          ESR = ExitShort("ESR", "");
          }

          if (SL != null && SL.OrderAction == OrderAction.BuyToCover && Close[0] > SL.StopPrice)
          {
          ESR = ExitShort("ESR", "");
          }
          }


          I assume that using a "BuyToCover" for both the TP and the SL is appropriate since they are both going to exit a Sell position?

          Comment


            #6
            Hello GLFX005,

            In this case I would suggest using Prints to identify what you are going to need to use as that is always good practice. I had just used BuyToCover in my example to show that in a condition, depending on the types of orders and position you are targeting you may see other types of orders happening as well.

            You can use a Print like the following to guide you so you can customize this for your use case:

            Code:
            if(TP != null)
            {
                Print(TP);
            }
            Alternatively just print the order events directly:

            Code:
            protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
            {
                    Print(order);
            }
            You should get some output similar to:

            orderId='a2d2300fec34492ab2f4361ca1752e2e' account='Sim101' name='TP' orderState=Working instrument='ES 06-20' orderAction=BuyToCover orderType='Limit' limitPrice=2803.25 stopPrice=0 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=933 time='2020-04-30 08:24:20' gtd='2099-12-01' statementDate='2020-04-30'
            So in this situation if you have a Short position and have used the TP code shown earlier but targeting the short instead you would see orderAction=BuyToCover.

            I assume that using a "BuyToCover" for both the TP and the SL is appropriate since they are both going to exit a Sell position?
            Yes, this is what the print looks like for that:
            orderId='54f2d2c0215e44108fe32478db2e467c' account='Sim101' name='SL' orderState=Submitted instrument='ES 06-20' orderAction=BuyToCover orderType='Stop Limit' limitPrice=2808.25 stopPrice=2808 quantity=1 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=940 time='2020-04-30 08:27:20' gtd='2099-12-01' statementDate='2020-04-30'

            I look forward to being of further assistance.

            Comment


              #7
              Thank you for your help, I have been able to achieve the goal I aimed for.
              I will let you know if I have any further questions.

              Comment


                #8
                Hello Jesse,

                I am back with additional questions related to the topic we've discussed before.

                Currently I have this piece of code which acts as a safety mechanism to exit a trade in case Price should jump +10 ticks over my Stop target (SL).

                if (SL != null && SL.OrderAction == OrderAction.BuyToCover && Close[0] > SL.StopPrice + 10 * TickSize)
                {
                ESR = ExitShort("ESR", "");
                CancelOrder(SL);
                CancelOrder(TP);
                }


                However, it seems that the code is ignoring the "+ 10 * TickSize" part and continues to execute ESR in combination with SL, meaning that there's a double Buy to exit a Short position resulting in the strategy being in a faulty Long position. Please see the attached image.

                Another question I have is that I am using the "SL.StopPrice" for Stop targets, but whether I should be using "TP.LimitPrice" for Profit targets instead?

                I am looking forward to your reply.
                Attached Files

                Comment


                  #9
                  Hello GLFX005,

                  Thank you for the post.

                  When you say that it is ignoring that part, do you mean the condition is becoming true at the wrong time? The given syntax does not look incorrect, you would likely need to use a Print here to identify what values are being passed to the connection at this time so we can better understand why that happened.

                  Another question I have is that I am using the "SL.StopPrice" for Stop targets, but whether I should be using "TP.LimitPrice" for Profit targets instead?
                  This is going to depend on the type of order you are targeting, If you had a stop order you would want its stop price and for the target you would likely need a limit price.

                  I look forward to being of further assistance.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by NullPointStrategies, Today, 05:17 AM
                  0 responses
                  38 views
                  0 likes
                  Last Post NullPointStrategies  
                  Started by argusthome, 03-08-2026, 10:06 AM
                  0 responses
                  124 views
                  0 likes
                  Last Post argusthome  
                  Started by NabilKhattabi, 03-06-2026, 11:18 AM
                  0 responses
                  64 views
                  0 likes
                  Last Post NabilKhattabi  
                  Started by Deep42, 03-06-2026, 12:28 AM
                  0 responses
                  41 views
                  0 likes
                  Last Post Deep42
                  by Deep42
                   
                  Started by TheRealMorford, 03-05-2026, 06:15 PM
                  0 responses
                  46 views
                  0 likes
                  Last Post TheRealMorford  
                  Working...
                  X