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

Force Profit Target Reset after 30 seconds

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

    Force Profit Target Reset after 30 seconds

    I have a strategy I have written where we get long or short in OnBarUpdate()

    We then go to OnOrderUpdate() and SetProfitTarget which is

    Code:
    			if (orderState == OrderState.Filled
    				&& Position.MarketPosition == MarketPosition.Long)
    			{
    				averagePriceLong = Position.AveragePrice > 0 ? Position.AveragePrice : averageFillPrice;
    				takeProfitLong = (averagePriceLong) //////+ (some hidden calculations);
    				
    
    				SetProfitTarget("Long", CalculationMode.Price, takeProfitLong, true);
    				
    			}
    What I need though is for the "takeProfitLong" to reset after 30 seconds. I think this is possible, but I'm not sure what the code is. Is there some sample code you could direct me to that would show how to force a target reset after so many seconds?

    Also I assume I'll need a new if statement asking for that reset, if you have any advice on that would appreciate. Thanks!
    -EC_Chris-
    NinjaTrader Ecosystem Vendor - Emergent Cybernetics

    #2
    Hello EC_Chris,

    Thanks for your inquiry.

    We would advise to use SetProfitTarget() and SetStopLoss() to prep NinjaTrader so it submits profit targets and stop losses for when an Entry method gets filled, so we would not recommend using this in OnOrderUpdate() on the fill of an order. Instead, we would advise setting it before the Entry method is fired and then you may update it as you wish by calling the Set methods once in position. Please refer to the documentation for all notes and advisories.

    If you wish to use OnOrderUpdate() and OnExecutionUpdate() I may refer you to the SampleOnOrderUpdate strategy for a complete example.

    Since OnBarUpdate() updates based on incoming data, if you are looking to do things based off of time based events, I would suggest to use a timer in your strategy. An example for implementing a timer in NinjaScripts can be found in the SampleLevel2Book example.

    Documentation and links to the resources mentioned can be found below.


    Please let us know if you have any additional questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      Thanks Jim,

      The reason I didn't set the profit target in OnBarUpdate is I need the averagePrice of the entry to set the target. Is there a way to accurately set that in OnBarUpdate? From what I understand I need to use OnOrderUpdate to get the accurate averagePrice of the entry.

      The strategy is actually working fine as I have it. I just can't reset the Target. I understand I have more options in OnBarUpdate to reset the target, but I can't get the averagePrice of the entry.

      I looked at the SampleLevel2Book and I didn't see anything on a force reset.

      Would a fore refresh https://ninjatrader.com/support/help...rcerefresh.htm

      or Trigger custom event work? https://ninjatrader.com/support/help...ustomevent.htm
      -EC_Chris-
      NinjaTrader Ecosystem Vendor - Emergent Cybernetics

      Comment


        #4
        Hello EC_Chris,

        Since the Set methods are for prepping exits with NinjaTrader, I may suggest to use a CalculationMode of CalculationMode.Ticks to prep the target and stop in association to the average entry price for the execution of the Entry method.

        Placing the Set methods in OnOrderUpdate() on a fill creates a potential race condition where a previous stop or target could be rejected before your Set method for the new position is fired. If it works fine for you and you aren't seeing issues, feel free to use it. However, I would recommend attempting the above or using your own Exit methods to control your exits and submit those orders in OnExecutionUpdate(). There is also the Position object that can tell you the average position price, and Order.AverageFillPrice which will give you the average fill price of an Order object.

        SampleLevel2Book incorporates a timer. That is the tool that would be used for time based actions. The sample is an indicator and does not take trades, so the timer would need to be extracted for your purposes.

        ForceRefresh() tells the chart it needs to re-render when it checks every 250ms.

        TriggerCustomEvent() would be used within the timer event handler so you can properly reference objects within the NinjaScript since it synchronizes all internal pointers.

        Position.AveragePrice - https://ninjatrader.com/support/help...erageprice.htm

        Order (See AverageFillPrice) - https://ninjatrader.com/support/help...n-us/order.htm

        I've also included the Advanced Order Handling docs. I highly recommend to review this material and the SampleOnOrderUpdate strategy to develop more robust strategies and accomplish your goal.

        Advanced Order Handling - https://ninjatrader.com/support/help...r_handling.htm

        If there is anything else we can do to assist, please let us know.
        Last edited by NinjaTrader_Jim; 02-21-2018, 01:07 PM.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by ender_wiggum, Today, 09:50 AM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by rajendrasubedi2023, Today, 09:50 AM
        1 response
        11 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by geotrades1, Today, 10:02 AM
        0 responses
        4 views
        0 likes
        Last Post geotrades1  
        Started by bmartz, Today, 09:30 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Erick  
        Started by geddyisodin, Today, 05:20 AM
        3 responses
        26 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X