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

Profit Target Triggered But Not Executed

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

    Profit Target Triggered But Not Executed

    Hello,

    I am running into this situation during market playback where my profit target (PT) is activated but not executed because the market stop is placed way too high. I calculated my PT to be 108.593, and the sell limit order was correctly placed at 108.593. The bizarre part is that the stop market order for the PT has been placed at 109.009 (I did not calculated this value). Problem 1 is that for some reason the stop market order has been placed 40pips over my commanded PT. Problem 2 is that the stop limit order is not even triggered when the price falls below my limit of 108.593! Problem 3 Where did the value 109.00 come from? I did not send or generate this value.

    Here is what I am doing in code:

    Code:
    string longEntryName = "Enter-Long-1";
    double profitTarget = 108.593;
    double stopLoss = 107.00;
    EnterLong(quantity, longEntryName);
    SetProfitTarget(longEntryName, CalculationMode.Price, profitTarget, true);
    SetStopLoss(longEntryName, CalculationMode.Price, stopLoss, true);
    If I understand correctly, using SetProfitTarget() with SetStopLoss() should generate an OCO (this is confirmed by my orders tab). When the price reaches my PT, the limit order is converted to a market order.

    What I don't understand is why and how my PT was converted to a price (109.00) FORTY pips above my PT (108.593) or why the price was allowed to fall THIRTY pips below my PT after my PT has been triggered and accepted. Someone please explain what I am missing here.
    Attached Files

    #2
    Hello jflaggs,

    Are you always using hard coded values?

    Is the stop loss always set to 107.00?

    Is the signal name always the same for the entry order?

    You are setting the stop loss after the entry is placed. This means it will initially be placed with the old value, and then will attempt to immediately be updated to this price.

    When using SetStopLoss() or SetProfitTarget() these do not get unset. This is why you can set them once in OnStartUp when State == State.DataLoaded or after, and it will continue using that value anytime an new entry is made (with that signal name).

    I would recommend setting the stop loss before the entry is placed.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks so much Chelsea. I'm sure that is my problem. Makes sense. I am not always using 107 or 108 for my stops. I just wanted to show my simplified code.

      With that said, I would like to dynamically change my stops after entering the trade. If I update SetStopLoss() or SetProfitTarget in OnBarUpdate, will my stops be updated accordingly? Is this the recommended way to dynamically change my stops (using the same order/signal name)? I have not tried these methods yet, but I don't want to go down the wrong path. Thanks for your help.

      Comment


        #4
        Hello jflaggs,

        Yes, it is fine to update a stoploss or profit target before placing an entry, or update these after they are working to change their price.

        Below is a link to an example.

        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I looked over these examples. Thank you for these.

          It seems that in "StopStrategySetMethodsOnMarketData.cs" you are using the Set() methods in OnBarUpdate() to manage stops after your order is working. But it looks like you are doing the achieving the same goal in "ProfitChaseStopTrailExitOrdersExample.cs" but by managing Order objects within OnBarUpdate(), OnOrderUpdate(), and OnExecutionUpdate().

          Can you speak to why one would use either of these methods over the other? The code using Set() methods seems simpler and easier to maintain since there's no need to build logic for managing order objects-- so I am not sure why one would prefer to manipulate order objects directly.

          Comment


            #6
            Hello jflaggs,

            How you code the logic and which tools you use is up to you.

            OnMarketData does not trigger historically and would not work in a backtest (unless TickReplay is enabled and even then the behavior is slightly different).

            For me personally, its easier for me in my mind to track orders by order variable to reference price, orderstate, etc..

            Some make their scripts simpler by just checking the position.

            I've made multiple scripts to demonstrate different ways one could go about their script. You may imagine something completely different than anything I've shown.


            However, take one example.

            The position can take a few moments to fill. If your script simply waits for a position to change before taking an action like ceasing to send an order, the next tick may generate a second order as the position has not updated for the first order.
            (A race condition)

            Or instead, by using a variable, I can submit the first order, and then check that the variable is null before allowing another. If its not null, then we know an order is doing something and may not have filled or may have filled and the position has not yet been updated.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Great information and resources. I'll be able to work with this information. I'll get back to you if I run into trouble. Thanks again.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by andrewtrades, Today, 04:57 PM
              1 response
              5 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by chbruno, Today, 04:10 PM
              0 responses
              3 views
              0 likes
              Last Post chbruno
              by chbruno
               
              Started by josh18955, 03-25-2023, 11:16 AM
              6 responses
              436 views
              0 likes
              Last Post Delerium  
              Started by FAQtrader, Today, 03:35 PM
              0 responses
              7 views
              0 likes
              Last Post FAQtrader  
              Started by rocketman7, Today, 09:41 AM
              5 responses
              19 views
              0 likes
              Last Post NinjaTrader_Jesse  
              Working...
              X