Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy multithreading calling Entry/ExitLong and Entry/ExitShort

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

    Strategy multithreading calling Entry/ExitLong and Entry/ExitShort

    I have a strategy that I use Entry/ExitLong("MyStrategyName") and Entry/ExitShort("MyStrategyName").

    I have observed that since I am using OnBarUpdate to place the orders, if the market moves quickly, I can see a dozen Entry and Exit calls. This 95% of the time works ok and only one Entry or Exit is actually placed.

    Two things I have observed that are frustrating though...

    1.) Once in a while (I woke up this morning to the issue) on Exit, the Exit will have placed 2 orders and my strategy will be out of whack... Meaning, I'll be in a trade the opposite direction and it won't have been because my strategy called an Enter in that direction. How do I prevent this from happening? This leads me to my second observation...

    2.) The ExitLong/Short() calls do not call OnExecutionUpdate or OnOrderUpdate() functions so I cannot use those functions to set a variable to avoid placing multiple calls to ExitLong/Short().


    How do I properly avoid calling ExitLong/Short() in very very quick succession? Is there something my OnBarUpdate() should be calling to properly sync up the Position.MarketPosition?

    I'm using this code to check for Long and Short before exiting the trade. And I don't call a new Entry until the next iteration of OnBarUpdate. i.e. I don't call Exit and Entry in the same call to OnBarUpdate.


    Code:
                 if (Position.MarketPosition == MarketPosition.Short) {

    And here's an example from logging that shows how many times it's calling the Exit function because of OnBarUpdate() getting called so many times so quickly.

    Code:
    1/30/2020 2:00:28 AM - [ SilentThunder-ZBEG6N1K ] - didDotForm(): FALSE -- ToTime(dt): 224901 ToTime(Time[0]): 20028 ToTime(Time[1]): 20027
    1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28513 because price is at or below Take Profit at 28513
    1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28513 because price is at or below Take Profit at 28513
    1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28513 because price is at or below Take Profit at 28513
    1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - TakeScreenShotMethod(): Attempting to save screenshot with message (Exiting Short Trade (Realized Daily PnL: $355.00)) to: Screenshot-20200130-020029.jpg
    1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28512 because price is at or below Take Profit at 28513
    1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28512 because price is at or below Take Profit at 28513
    1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28511 because price is at or below Take Profit at 28513
    1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28511 because price is at or below Take Profit at 28513
    1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28511 because price is at or below Take Profit at 28513
    1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28511 because price is at or below Take Profit at 28513
    1/30/2020 2:00:29 AM - [ SilentThunder-ZBEG6N1K ] - OnBarUpdate(): Closing SHORT position at 28511 because price is at or below Take Profit at 28513


    In summary (TL/DR), is there something other than Position.MarketPosition I should be checking to ensure the order placement is synced between all current and future instances of OnBarUpdate() so that my strategy is not calling the Entry/Exit functions potentially dozens of times?


    Thanks,
    Timothy
    Last edited by timwelch; 01-30-2020, 09:15 AM.

    #2
    Hello Timothy,

    Thanks for your post.

    EnterLong and EnterShort do show up in OnOrderUpdate. (I've attached an example that you can use to test.)

    I would suggest to either of the following:

    1. Set a bool or similar variable in OnBarUpdate as you are submitting the order so it is only done once
    2. You could assign the entry order to an Order object in OnOrderUpdate, and only allow entry if the order object is null and the MarketPosition is flat.

    SampleOnOrderUpdate can describe how you can assign order objects and use them in your script, and also demonstrates how #2 can be performed.

    SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

    I look forward to assisting.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    90 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    135 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    120 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    69 views
    0 likes
    Last Post PaulMohn  
    Working...
    X