Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to make sure that only one position can exist

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

    How to make sure that only one position can exist

    I use several instances of the same strategy for different instruments. How can I make sure that at any given time, only one position can exist over all instruments and directions?
    I.e. if any of the different instances of my strategy has triggered a position for a certain instrument in a certain direction, I want to make sure that none of the other instances can enter a position for any instrument in any direction, until the existing position has been closed.

    #2
    Hello rafe0304,

    Thank you for writing in.

    You can utilize the UserDefinedMethods script and declare a static public boolean, for example.

    Then, you'll want to check if the boolean is true or false in all of your strategies.

    The UserDefinedMethods script can be accessed by clicking on Tools -> Edit NinjaScript -> Strategy.

    As a simple example:
    Code:
    //User Defined Methods script
    partial class Strategy
    {
         static public bool inPosition = false;
    }
    
    // Strategy script
    protected override void OnBarUpdate()
    {
         if (!inPosition)
         {
              EnterLong();
              inPosition = true;
         }
    }
    In my example, I set inPosition to true once I enter. In other scripts, when checking for inPosition, this variable is already true because my example strategy entered and set the variable to true. Because of this, the other strategy will not enter.

    Remember to place logic to switch that variable back to false when you're out of a position.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Cant this be handled by the manached approach, by just setting a variable that makes sure, that on any given account only one position can exist at a time?

      Comment


        #4
        Hello rafe0304,

        Strategies do not communicate with each other. One strategy will not know what position another one is in.

        You will need to create a static variable that each strategy can check and/or modify as I have sampled in my previous post.
        Zachary G.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by algospoke, 04-17-2024, 06:40 PM
        3 responses
        26 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by bmartz, 03-12-2024, 06:12 AM
        3 responses
        28 views
        0 likes
        Last Post NinjaTrader_Zachary  
        Started by Aviram Y, Today, 05:29 AM
        2 responses
        9 views
        0 likes
        Last Post Aviram Y  
        Started by gentlebenthebear, Today, 01:30 AM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by cls71, Today, 04:45 AM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X