Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Running multiple strategy's on same instrument

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

    Running multiple strategy's on same instrument

    I would like to run multiple strategys on the same instrument but would need it to not put on a trade if it is in one already from the other strategy.

    Is this possible to do?

    Thanks.

    #2
    Hello brucelevy, and thank you for your question.

    NinjaTrader 7 provides the file (My) Documents\NinjaTrader 7\bin\Custom\Strategy\UserDefinedMethods.cs . Any static values you create here become part of every Strategy, and as such, can be used as semaphores for inter-strategy communication.

    "Semaphore" is a fancy way of saying that you can set up a flag variable (semaphore communication is a language like morse code for communicating by waving flags, thus the name) so that one strategy tests it inside an if statement inside OnBarUpdate, and another strategy sets its value, thus allowing the two strategies to "talk" to each other.

    I am including two publicly available links, one covering static member variables in C#, and one covering using semaphores for inter-process communication.

    Static classes can't be instantiated in C#. You access the members of a static class by using the class name itself.



    In your case, I recommend adding the following to UserDefinedVariables inside the Strategy partial class :

    Code:
    [FONT=Courier New]public static MarketPosition semaphore;[/FONT]
    You can then set the variable semaphore to whatever MarketPosition you happen to be in in your first strategy, and check it in your second strategy to tell whether your first strategy is flat or not.

    Please let us know if there are any other ways we can help.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Ok, I've added "public static MarketPosition semaphore;" to the UserDefinedMethods.cs. I am not clear on what the next step is. I do want it to check if the instrument is flat.

      Comment


        #4
        Hello brucelevy,

        Now that this exists, in one of your strategies, you can call

        Code:
        [FONT=Courier New]      semaphore = Position.MarketPosition;[/FONT]
        to make that strategy's market position something you can check from other strategies.

        From another strategy, you can call

        Code:
        [FONT=Courier New]      if (semaphore == MarketPosition.Long)
              {
                Print("This is only called if the 1st strategy is in a Long position");
              }[/FONT]
        to execute conditionally based on the 1st strategy's position.
        Jessica P.NinjaTrader Customer Service

        Comment


          #5
          Thanks Jessica,

          Would this example work to check that there are no open position in the same instrument?

          And this will check the instrument regardless if its a different strategy, or even the same strategy on a different time-frame, correct?


          Thanks.

          if (Close[0] > Open[1]
          && semaphore == MarketPosition.Flat)
          {
          EnterLong("LongEntry");
          }

          Comment


            #6
            Hello brucelevy,

            Your code would work well for checking that variable. You can check from as many strategies as you would like, but the way this is currently set up, only a single strategy can set your semaphore value. In other words, this variable is "write from one, read from many"

            From your single running strategy you would like to track, you would then use this code :

            semaphore = Position.MarketPosition;
            Jessica P.NinjaTrader Customer Service

            Comment


              #7
              And I would put that where? In the Variables section of the main strategy?

              Thanks.

              Comment


                #8
                Both of these should actually be handled in each strategy's OnBarUpdate method.
                Jessica P.NinjaTrader Customer Service

                Comment


                  #9
                  Would a semaphore be able to be used with 2 strategies NOT on the same instrument. I'd like to check the state of certain variables on a strategy running on YM from a strategy running ES.

                  Thanks

                  Comment


                    #10
                    Hello All4theticks,

                    Globals you define in UserDefinedMethods.cs are available from any strategy or indicator. This publicly available link goes into more detail on statics and globals in C#. http://www.dotnetperls.com/global-variable . Please let us know if there are any other ways we can help.
                    Jessica P.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by bmartz, 03-12-2024, 06:12 AM
                    4 responses
                    31 views
                    0 likes
                    Last Post bmartz
                    by bmartz
                     
                    Started by Aviram Y, Today, 05:29 AM
                    4 responses
                    12 views
                    0 likes
                    Last Post Aviram Y  
                    Started by algospoke, 04-17-2024, 06:40 PM
                    3 responses
                    28 views
                    0 likes
                    Last Post NinjaTrader_Jesse  
                    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