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

NinjaScript position exit error.

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

    NinjaScript position exit error.

    I'm running a ninja script strategy in which I coded a manual exit.

    The exit is as follows

    Code:
    if (Position.GetUnrealizedProfitLoss(PerformanceUnit. Ticks, Close[0]) >= Target)
    {
    ExitLong(Convert.ToInt32(Contracts));
    ExitShort(Convert.ToInt32(Contracts));
    Waiting = false; //after ghost target hit, setting waiting state = false = disarmed state, doesn't allow for unnecessary extra entries after exit.
    }
    It checks if unrealized PNL is > than a defined value in ticks, then given the position it exits the long or short position then switches a bool to false so the code to enter positions becomes disabled and doesn't enter new ones until that bool reverts back to true given certain conditions.

    My question is this.

    1) While running the strategy and editing code for different strategies in the code editor, does compiling code affect the currently running strategies? I've noticed this happen when I'm editing backup versions of strategies then recompiling them.

    When the strategy is supposed to exit with the code written above, the orders would get out of sync.

    This error is what happens in the execution window (for example a long position closing):

    E/X: Exit
    Position: - (denotes flat position)
    Name: Sell

    At the same time, this happens as a result of the wrong execution

    E/X: Entry
    Position: 1S (denotes short position)
    Name: Sell

    The correct outcome should be as follows.

    E/X: Exit
    Position: - (denotes flat position)
    Name: Close Position

    Then the position waits flat until a new signal triggers another trade.

    Given I'm trading this live, could there be an error or delay when the market order gets sent out to the exchange? The correct outcome should be just "sell to close".

    I'm deducing that NT;
    1. sends out 2 sell market orders that are out of sync one should be just "sell to close" but gets assigned as a sell short order and the sell to close becomes "1S" or a short position that shouldn't exist.
    2. I notice this because I keep an eye on the strategies tab as the take profit happens and the sync becomes false due to the unnecessary extra short position in this case. Then I have to right-click and synchronize the strategy and it closes the erroneous position.
    With that being said, this doesn't happen when I start NT, enable the strategies and let them run thru the session and do nothing else on the platform, no coding no compiling etc.

    Is there a way I can code something that checks the current position if not flat and forces a synchronization?

    For example:

    Code:
    if (Position.GetUnrealizedProfitLoss(PerformanceUnit. Ticks, Close[0]) >= Target)
    {
    ExitLong(Convert.ToInt32(Contracts));
    ExitShort(Convert.ToInt32(Contracts));
    Waiting = false; //after ghost target hit, setting waiting state = false = disarmed state, doesn't allow for unnecessary extra entries after exit.
    [B]Line of code that: 1 checks account if long/short and forces it to synchronize and go flat as the strategy position, is there a Synchronize command I can put in after the 3 previous lines are executed just to make sure that the account is in SYNC at all times so I don't have to check the strategy window and re-enable the strategy if it goes out of sync?[/B]
    }
    If you look at the code above, is it possible that the "ExitShort" gets misinterpreted?

    I prefer not to use the typical pending take profit orders as a means of exit since at times they either don't get filled, I'd rather get out at the market with guaranteed execution.

    Is there a way I can improve the exit code above and add an account synchronize command as I proposed in the bold section? I searched if such a command exists in the help guide, I presume there has to be a command that can be called to have the same effect as right-clicking in the strategies window and selecting "Synchronize all Strategies".


    Thanks in advance for your assistance.
    Last edited by TradingLoss; 03-31-2021, 10:42 AM.

    #2
    Hello TradingLoss,

    Thank you for your post.

    Is the strategy running OnEachTick or OnPriceChange?

    In a fast moving market it's very possible that a new price change could occur or a tick could come in before the initially submitted exit order is processed. Should this occur, you don't have anything in your conditions to keep from submitting a second order if the first hasn't been fully processed and the internal strategy position is updated. Since it doesn't yet know that it's in a flat position yet, and the conditions for exit are still true, the second order would get submitted and would likely cause an overfill scenario as you're seeing.

    What I would recommend here is to simply use a bool to control the exit that is reset when the internal strategy position is reported as flat again.

    So, something like:

    Code:
    // set as a class level variable
    private bool IsExitAllowed = true;
    
    if (Position.GetUnrealizedProfitLoss(PerformanceUnit. Ticks, Close[0]) >= Target
    && IsExitAllowed)
    {
    ExitLong(Convert.ToInt32(Contracts));
    ExitShort(Convert.ToInt32(Contracts));
    Waiting = false; //after ghost target hit, setting waiting state = false = disarmed state, doesn't allow for unnecessary extra entries after exit.
    IsExitAllowed = false;
    }
    
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    IsExitAllowed = true;
    
    }
    That would restrict it from sending multiple exits if the position has not yet been reported as flat.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Now that you mention it with regards to OnEachTick or OnPriceChange, I just found out...

      Code:
      Calculate                                    = Calculate.OnPriceChange; //switch back to OnBarClose if has varying effects with OnPriceChange
      A while back I was experimenting with OnPriceChange, I initially coded the strategy for OnBarClose hence the //comment about it.

      So yes currently I'm running it as OnPriceChhange, the next session I will run the strategy OnBarClose.

      I will also implement your recommendation by adding the extra bool to restrict multiple exits.

      I think this would fix the overfill I am experiencing.

      Will keep you updated, thanks for your help.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Skifree, Today, 03:41 AM
      1 response
      2 views
      0 likes
      Last Post Skifree
      by Skifree
       
      Started by usazencort, Today, 01:16 AM
      0 responses
      1 view
      0 likes
      Last Post usazencort  
      Started by kaywai, 09-01-2023, 08:44 PM
      5 responses
      603 views
      0 likes
      Last Post NinjaTrader_Jason  
      Started by xiinteractive, 04-09-2024, 08:08 AM
      6 responses
      23 views
      0 likes
      Last Post xiinteractive  
      Started by Pattontje, Yesterday, 02:10 PM
      2 responses
      23 views
      0 likes
      Last Post Pattontje  
      Working...
      X