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.



    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,



    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 swestendorf, Today, 11:14 AM
    2 responses
    5 views
    0 likes
    Last Post NinjaTrader_Kimberly  
    Started by xiinteractive, 04-09-2024, 08:08 AM
    4 responses
    13 views
    0 likes
    Last Post xiinteractive  
    Started by Mupulen, Today, 11:26 AM
    0 responses
    2 views
    0 likes
    Last Post Mupulen
    by Mupulen
     
    Started by Sparkyboy, Today, 10:57 AM
    1 response
    5 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by TheMarlin801, 10-13-2020, 01:40 AM
    21 responses
    3,917 views
    0 likes
    Last Post Bidder
    by Bidder
     
    Working...
    X