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

Sample using Multi-Instruments in strategy

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

    Sample using Multi-Instruments in strategy

    Hello Ninjas
    I coded an example to understand the use of several instruments in a strategy.

    I noticed that there are only two instruments to trade at a time. I do not understand why ?

    I have attached the file for you to try and tell me your comments and if there is a setting or problem that needs to be resolved.

    I await your feedback

    THANKS​
    Attached Files

    #2
    Hello MGHORBEL,

    When using multiple instruments you need to use the Positions object to check the other instruments position, the Position object references only the primary series position. Beyond that you would need to add Print statements into your entry conditions to make sure that those conditions are becoming true. If they are not you can add a Print outside each of the conditions to print out the value being used in the condition to see why it was not true.


    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse
      Thank you for your comments and advice.
      I changed as you recommended.
      I forced the strategy so that there will be one position in each instrument but I always find two positions in total.​
      Attached Files

      Comment


        #4
        Hello MGHORBEL,

        I see that you added prints for your conditions, are you seeing that the values being printed would lead to the condition to trade becoming true?
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello Jesse
          with this condition :

          if(Positions[BarsInProgress].MarketPosition == MarketPosition.Flat)
          {
          sR = sRandom();

          ​​ cdt = sR<1;
          Print("Entry condition : " + cdt.ToString() + " _ " + Instrument.FullName + " _ " + DateTime.Now.ToString());
          if(cdt)
          { EnterLong(BarsInProgress, appliedQuantity, Instrument.FullName + " _ " + DateTime.Now.ToString()); }
          else
          { EnterShort(BarsInProgress, appliedQuantity, Instrument.FullName + " _ " + DateTime.Now.ToString()); }
          }
          else
          {
          Print(Instrument.FullName + " _ " + DateTime.Now.ToString() + " _ OnBarUpdate BarsInProgress = " + BarsInProgress);
          Print("\t _ Position is " + Positions[BarsInProgress].MarketPosition);
          }


          I must have a position in each instrument.

          Comment


            #6
            Hello MGHORBEL,

            I understand that you want a position on each instrument, did that condition become true? You have prints that should be outputting the value of your condition, was the condition to trade true when you ran the script?
            JesseNinjaTrader Customer Service

            Comment


              #7
              from what I see the condition is not true for all instruments

              The image shows only two instruments with which the condition is true

              Click image for larger version

Name:	image.png
Views:	33
Size:	453.3 KB
ID:	1294363

              Comment


                #8
                I applied the strategy on the graphic CL

                When I restart the strategy, other instruments which have the condition true but only two at a time.

                if you try the strategy and you will notice the same thing

                Comment


                  #9
                  Hello MGHORBEL,

                  For the purpose of testing I would suggest making your entry condition a simple bool value so you can be sure all conditions become true when the OnBarUpdate event is called for that series. Using the position and removing your other condition would be a easy way to do that.

                  Code:
                  if(Positions[BarsInProgress].MarketPosition == MarketPosition.Flat)
                  {​
                     EnterLong(); 
                  }
                  JesseNinjaTrader Customer Service

                  Comment


                    #10

                    I used this condition and it's not good because only one instrument have position. You can test it​


                    if(Positions[BarsInProgress].MarketPosition == MarketPosition.Flat)
                    {
                    //EnterLong(BarsInProgress, appliedQuantity, Instrument.FullName + " _ " + DateTime.Now.ToString());
                    EnterLong();
                    }
                    else
                    {
                    Print(Instrument.FullName + " _ " + DateTime.Now.ToString() + " _ OnBarUpdate BarsInProgress = " + BarsInProgress);
                    Print("\t _ Position is " + Positions[BarsInProgress].MarketPosition);
                    }

                    Comment


                      #11
                      Hello MGHORBEL,

                      I would suggest to simplify your test further, I see this works on my end. You can do the following as a test:


                      Code:
                      protected override void OnStateChange()
                      {
                         if (State == State.SetDefaults)
                         {
                            Calculate = Calculate.OnEachTick;
                         }
                         else if (State == State.Configure)
                         {
                            AddDataSeries("YM 03-24", BarsPeriodType.Minute, 5);
                            AddDataSeries("NQ 03-24", BarsPeriodType.Minute, 5);
                            AddDataSeries("RTY 03-24", BarsPeriodType.Minute, 5);
                         }
                      }
                      
                      protected override void OnBarUpdate()
                      {
                         if(State == State.Historical) return;
                         Print(BarsInProgress + " " + Positions[BarsInProgress].MarketPosition);
                         if(Positions[BarsInProgress].MarketPosition == MarketPosition.Flat)
                         {
                            EnterLong();
                         }
                      }
                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        The Output is :

                        1 Flat
                        0 Long
                        2 Flat
                        1 Flat
                        2 Flat
                        2 Flat
                        0 Long
                        3 Flat
                        2 Flat
                        2 Flat​

                        Comment


                          #13
                          Can you send me your strategy file, please

                          Comment


                            #14
                            Hello MGHORBEL,

                            The code I posted is the full file, did you make any changes or did you try to use that in an existing strategy? For the purpose of testing create a new empty strategy and then use only that code. The only settings change you need to make to the new file is the Calculate setting to OnEachTick so that each instrument is called in realtime immediately. Additionally make sure you are using a chart for an instrument that is not being used with AddDataSeries so you have 4 different instruments.

                            I see that all instruments have a position opened when using that code.
                            JesseNinjaTrader Customer Service

                            Comment


                              #15
                              I attached the file used and the result is :
                              2 Flat
                              0 Long
                              1 Flat
                              3 Flat
                              2 Flat
                              3 Flat
                              2 Flat
                              2 Flat
                              1 Flat
                              2 Flat
                              2 Flat
                              2 Flat
                              1 Flat
                              0 Long
                              2 Flat​

                              I will watch again and I will come back to you tomorrow​
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by pibrew, Today, 06:37 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post pibrew
                              by pibrew
                               
                              Started by rbeckmann05, Yesterday, 06:48 PM
                              1 response
                              14 views
                              0 likes
                              Last Post bltdavid  
                              Started by llanqui, Today, 03:53 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post llanqui
                              by llanqui
                               
                              Started by burtoninlondon, Today, 12:38 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post burtoninlondon  
                              Started by AaronKoRn, Yesterday, 09:49 PM
                              0 responses
                              16 views
                              0 likes
                              Last Post AaronKoRn  
                              Working...
                              X