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

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.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Christopher_R, Today, 12:29 AM
    0 responses
    6 views
    0 likes
    Last Post Christopher_R  
    Started by sidlercom80, 10-28-2023, 08:49 AM
    166 responses
    2,234 views
    0 likes
    Last Post sidlercom80  
    Started by thread, Yesterday, 11:58 PM
    0 responses
    3 views
    0 likes
    Last Post thread
    by thread
     
    Started by jclose, Yesterday, 09:37 PM
    0 responses
    7 views
    0 likes
    Last Post jclose
    by jclose
     
    Started by WeyldFalcon, 08-07-2020, 06:13 AM
    10 responses
    1,414 views
    0 likes
    Last Post Traderontheroad  
    Working...
    X