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

OnPosition not immediately called or I do not Understand

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

    OnPosition not immediately called or I do not Understand

    I am running four instances of the same strategy with different settings for entry and exit. At times the account will be flat but the strats will be long and short. I've experienced the situation when in this state and the market moves quickly the strats will reverse or flat their positions and all strats end up in the same net position of long or short. However, OnPositionUpdate will not be immediately called for some of the strats. OnPositionUpdate is being called when each strat is at net zero but some will be called hours later when one of the other instances exit its position. Each strat is trading one contract. I log every entry into OnPositionUpdate().

    I looked through the Ninja trace log and the PositionUpdateCallback() entries match with my OnPositionUpdate() logs. I would expect a PositionUpdateCallback() for each ExecutionUpdateCallback() but that is not the case. I noticed several ExecutionUpdateCallback() entries before a PositionUpdateCallback().

    The strat will sometimes place a new market order while in the OnPositionUpdate() callback if the signal is still in effect. Is that OK or should I use a TriggerCustomEvent() to let the strat unwind and then place new orders?

    Also from the trace logs I see OnPositionChanged() entries with netPosition=1, then -1, then -3. Not a sequential move from 1,0,-1,-1,-2,-3. Any explanation will help. The docs state OnPositionUpdate will be called for every position change. That is true but the delay is not desired.

    #2
    Hello EWT John,

    Thank you for the post.

    I wanted to clear up some details on what you are trying here.

    Are you saying that the OnPositionUpdate in strategy 1 is not being called for each position update for the other strategies? Or are you saying that 1 instance of the strategy is missing/having delays in its own OnPositionUpdate events? If you are saying that you are not seeing updates from the other instances that would be expected.

    Do you have a test case that uses only 1 strategy to demonstrate your question or does this require multiple scripts to see the problem?

    If these are all applied to the same account and instrument, I would suggest to only enable 1 and test with 1 instance to see if that works as planned. it would be expected that there are some discrepancies in reporting between the different instances if they are running all at once on the same account and instrument.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Jesse,

      The issue is the OnPositionUpdate() being called 1.5 hr after the execution. Here is a brief summary:

      Four strats run at same time each react to the same long and short signals but place orders at different levels.

      Strat Instance 1 gets long from a long signal then sells and gets short from a short signal. OnPositionUpdate() is called three times as expected. Strat 1 will exit the short 1.5 hr later.

      Strat Instance 2 gets long from the long signal and sells and gets short from the short signal. OnPositionUpdate() is called three times but the third call is does not occur until strat 1 exits its short position 1.5 hr later.

      Strat Instance 3 gets long from the long signal and sells and gets short from the short signal. OnPositionUpdate() is called three times but the third call is does not occur until strat 1 exits its short position. Same as strat 2.

      Strat Instance 4 places order on the long signal but does not get an execution. It gets short on the short signal and OnPositionUpdate() is called once as expected.

      If you send me an email address I can send you my log snippets for each strat and the Ninja trace log for this period.
      My logging class uses a lock to manage writes. Each strategy instance has a separate log (and lock) as not to block each other. C# locks are also re-entrant so recursive calls within the same thread will not block.
      I also construct the time stamp prior to the lock so if I was blocking the time stamps in strat 2 and 3 OnPositionUpdate() would be close to the short execution and not 1.5 hr later.
      Last edited by EWT John; 08-22-2018, 08:54 PM.

      Comment


        #4
        Hello EWT John,

        Thank you for the reply and additional details.

        Based on what you have provided it sounds like the multiple instances are required to see the problem. With your additional notes, it also seems that creating a more simple test would likely be needed to see what is happening surrounding what you are trying.

        It sounds like the first script was working as planned in your first test, only after enabling the others you start to see a problem is this accurate? If so, have you further simplified your multi-script test to remove all extra logic from your tests? I would suggest making a test that is more simple and does not include your extra logging utilities or any other logic besides submitting an order and printing the position updates. This would help better understand if this is related to some logic you used, or if this is just a side effect of using more than one strategy at once for the same instrument/account.

        As far as sending an email to our support, you can use the platform support [at] ninjatrader.com address for support inquiries. The logging you are creating will only be relevant in contrast to your logic and without that it would be difficult to interpret the logging. Sending the logs for this specifically may not be the best approach here. For this, I would likely suggest what I did already or to make a heavily reduced example of the specific problem that we can work on together. Creating a series of scripts that wait for realtime and then submit an order in some way and then simply prints the position information would be a good starting place.

        If the intention is to have multiple levels of orders and the same account/instrument is to be used it could also be suggested to combine your logic into one script to avoid this all together.


        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by junkone, Today, 11:37 AM
        0 responses
        2 views
        0 likes
        Last Post junkone
        by junkone
         
        Started by quantismo, 04-17-2024, 05:13 PM
        5 responses
        34 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by proptrade13, Today, 11:06 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by love2code2trade, 04-17-2024, 01:45 PM
        4 responses
        34 views
        0 likes
        Last Post love2code2trade  
        Started by cls71, Today, 04:45 AM
        2 responses
        10 views
        0 likes
        Last Post eDanny
        by eDanny
         
        Working...
        X