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

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 DayTradingDEMON, Today, 09:28 AM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by cmtjoancolmenero, Yesterday, 03:58 PM
        8 responses
        31 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by helpwanted, Today, 03:06 AM
        2 responses
        22 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by navyguy06, Today, 09:28 AM
        0 responses
        5 views
        0 likes
        Last Post navyguy06  
        Started by rjbtrade1, 11-30-2023, 04:38 PM
        2 responses
        77 views
        0 likes
        Last Post DavidHP
        by DavidHP
         
        Working...
        X