Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How do I set myTakeProfit [bool variable] when the "Profit target" order is filled?

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

    How do I set myTakeProfit [bool variable] when the "Profit target" order is filled?

    When the "Profit target" order is filled, I want to run the following to prevent "Stop loss" orders from filling immediately.

    For example:

    if (//my myTakeProfit condition)
    {
    myTakeProfit = true; // a boolean variable I previously created called myTakeProfit that was set to false.
    mySavedBar = CurrentBar; // an Int variable I previously created called mySavedBar to hold the bar number when the "Profit target" is reached.
    }

    later in the code:

    if (myTakeProfit && CurrentBar - mySavedBar >= 10) // wait for 10 bars
    {
    // Reset myTakeProfit Boolean
    myTakeProfit = false; // So this only executes once per condition.
    }


    #2
    Hello mg.watts, thanks for writing in.

    You can capture the exact moment the Profit Target fills by subscribing to the OnOrderUpdate event. The order.Name will come through as "Profit target" and you can check if order.OrderState == OrderState.Filled, then set your boolean.

    We have a full example demonstrating monitoring stop loss and profit target orders here:
    https://ninjatrader.com/support/help..._and_profi.htm

    Best regards,
    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hello,

      So I have it mostly working:
      5/10/2021 3:28:00 AM
      Filled
      orderId='bb8327901dd348e48b5625ea67a3f5b9' account='Sim101' name='Profit target' orderState=Filled instrument='NQ 06-21' orderAction=BuyToCover orderType='Limit' limitPrice=13652.5 stopPrice=0 quantity=1 tif=Gtc oco='4b521142d6f64fe38ce1a7ec7fa85a21' filled=1 averageFillPrice=13652.5 onBehalfOf='' id=4810 time='2021-05-10 03:29:16' gtd='2099-12-01' statementDate='2021-05-10'
      myTakeProfit Bool Value: True
      mySavedBar Int Value: 3015

      But the issue now is when I add the Code to OnBarUpdate to activate the Pause, it doesn't. Maybe I'm just not writing it correctly.

      Any Suggestions?

      Also, can I put OnOrderUpdate above OnBarUpdate in the Script? so it reads OnOrderUpdate before processing OnBarUpdate.

      Thank You,
      mg.watts
      Last edited by mg.watts; 05-10-2021, 08:29 AM.

      Comment


        #4
        Hello, thanks for your reply.

        The placement of an event method does not matter, OnOrderUpdate is called whenever an order changes its state/updates. If you use the Print() method to print out your variables, it will explain why it's not working in OnBarUpdate and help to debug the issue.

        Best regards,
        -ChrisL

        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by luxurious_04, Today, 10:55 PM
        0 responses
        1 view
        0 likes
        Last Post luxurious_04  
        Started by timmbbo, Today, 10:27 PM
        0 responses
        1 view
        0 likes
        Last Post timmbbo
        by timmbbo
         
        Started by 7robert, Today, 07:48 PM
        1 response
        14 views
        0 likes
        Last Post NinjaTrader_EricB  
        Started by saltminer, Today, 06:32 PM
        0 responses
        6 views
        0 likes
        Last Post saltminer  
        Started by caveat_lector, Today, 06:13 PM
        0 responses
        5 views
        0 likes
        Last Post caveat_lector  
        Working...
        X