• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Reverse Direction when Profit Target or Stop Loss Occurs

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

    Reverse Direction when Profit Target or Stop Loss Occurs

    Hello all, I have a strategy that reverses directions when either a Profit Target or Stop Loss occurs.

    The strategy looks for "Profit target" or "Stop loss" events in OnOrderUpdate()

    If the event is detected, flags are set in OnOrderUpdate() and an attempt to reverse the position with SetProfitTarget(), SetStopLoss(), EnterLongLimit() or EnterShortLimit() is done in OnBarUpdate().

    The problem is sometimes my order gets ignored according to this link.

    https://ninjatrader.com/support/help...antedPositions

    So I am thinking of inserting a small 100ms delay (I'm guessing) within OnBarUpdate() right after the event is detected and right before the flag is set to initiated the reverse position in OnBarUpdate()

    So I use the following code in OnOrderUpdate()

    Code:
    var t = Task.Run(async delegate
                 		{
                   		     await Task.Delay(100);
    			});	
    			
                            t.Wait();
    When testing with Playback data, I notice a freeze if I set the delay to a big number ex. 5000ms. I was hoping OnOrderUpdate() ran outside of the UI thread. Soooo, how do I put in a small delay so I get "normal" execution with OnOrderUpdate() or without affecting other threads with this freeze? Is there a better way to program this without the delay? Any help is greatly appreciated!
    Last edited by geekodude; 06-23-2018, 12:29 AM.

    #2
    Hello geekodude,

    Thank you for your note.

    Using a timer for this is neither supported nor recommended.

    Under the managed approach your order is going to be ignored for internal handling rules, as your link indicates.

    It would be best you use the unmanaged approach to do this, submitting a profit target with twice the quantity as the existing position. I have an attached a sample strategy using the Unmanaged approach.

    See the unmanaged approach section of our helpguide,
    https://ninjatrader.com/support/help...sunmanaged.htm
    https://ninjatrader.com/support/help...runmanaged.htm

    Another approach would be to have the strategy submit an ATM. I put together a sample which will enter long and reverse on profit target or stop loss. Before you can use this you should create an ATM Template called ATMReverse, with the parameters in the screen shot.

    Please let us know if you need further assistance.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Atilla, Today, 04:05 PM
    0 responses
    15 views
    0 likes
    Last Post Atilla
    by Atilla
     
    Started by Displacer, Today, 02:57 PM
    2 responses
    13 views
    0 likes
    Last Post Displacer  
    Started by SAILIKITY, 05-22-2019, 05:41 PM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_PaulH  
    Started by rmillar, 01-09-2019, 06:02 PM
    6 responses
    74 views
    1 like
    Last Post eDanny
    by eDanny
     
    Started by mlarocco, Today, 11:39 AM
    4 responses
    23 views
    0 likes
    Last Post mlarocco  
    Working...
    X