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

Code to generate variable for Ratio of two stocks in a pairs trade

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

    Code to generate variable for Ratio of two stocks in a pairs trade

    I need 2 simultaneous orders to use as close to the same nominal cash value as each other in the morning. I coded my entries but I have no experience coding. The formula would be something like (HigherPricedStock/LowerPriceStock)=Variable, then sell short 1000 of the Higherpricestock and 1000 X Variable of the Lower Price stock so that the cash value of the positions is as close to equal as possible. Does anyone have the basic code that would generate this variable? I want it calculated byt the 9:30 price.

    #2
    Hello Vt2009,

    Thank you for your post.

    You can use principles from the help guide article below to create a multiseries strategy.



    Closes is used to reference the last price of a secondary series.


    You can get the time stamp of a bar using ToTime(Time[0]). The reference sample below can help with working with DateTime in NinjaScript.

    http://www.ninjatrader-support2.com/...ad.php?t=19292

    The snippet below will capture the value of the primary series / secondary series if time stamp is 9:30.

    Code:
     
    if ((ToTime(Time[0]) == 93000))
    myVariable = (Closes[0][0] / Closes[1][0]);
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Very Helpful, Another Dumb ?

      That code looks great, how do I set the 1 and 0 to define the two stocks I wish to use to generate the variable?

      Ex. SKF/UYG = (Closes [0][0]/Closes [1][0])

      =myVariable

      then EnterShort SKF, 1000
      EnterLong UYG 1000*myVariable

      Comment


        #4
        The primary series([0] index) is the chart that you'e running the strategy against or the instrument selected when running the strategy from the control center.

        All other series are added manually. Process is outlined in the link below:
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          so:

          if((ToTime(Time[0])==93000))
          myVariable=(Closes[0][SKF]/Closes[1][UYG]);

          The strategy is run on UYG
          so... that needs to be the primary series?

          Comment


            #6
            That's not how it works. You need to add a secondary series in the initialize method.

            Code:
             
             Add("SKF", PeriodType.Minute, 1);
            That adds a one minute SKF series.

            Then this secondary series is accessed with index [1] . The current close of this series is Closes[1][0]

            http://www.ninjatrader-support.com/HelpGuideV6/MultiTimeFrameInstruments.html

            That link is relevant to Multiseries strategies and your question. As Tim suggested in your other thread you can see a sample multi instrument strategy by clicking Tools > Edit NinjaScript > Strategy and viewing the code for Sample Multi Instrument.

            Please post your questions only once so that we remain efficient and aren't duplicating efforts.
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Thanks

              Sorry for posting multiple questions- thanks for all the help:

              Would this probably work then?


              Add("SKF",PeriodType.Minute, 1)

              if ((ToTime(Time[0])==930000))
              myVariable= (Closes[1][0]/Closes[0][0])


              Since the Primary series is UYG (Closes[0][0]) and I want SKF/UYG to be the variable?

              Comment


                #8
                Make sure you have the "Add" statement in the initialize method and include the missing semicolon ;

                Then that code should return SKF / UYG.

                You can work with print statements and view the output window to verify the results are what you expect.

                Print (myVariable);
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  Placement in Code

                  This is how I tried to enter this code, could yall help me clean it up a little bit?

                  private
                  int shortRatio = 1; // Default setting for ShortRatio
                  // User defined variables (add any user defined variables below)

                  protectedoverridevoid Initialize ()
                  {
                  Add(
                  "skf",PeriodType.Minute, 1)
                  }
                  {
                  if ((ToTime(Time[0]==93000));
                  myVariable=(Closes[
                  1][0]/Closes[0][0])
                  }
                  #endregion
                  ///<summary>

                  Comment


                    #10
                    Here's some items I noticed:
                    • Never see variable shortRatio used in code.
                    • shortRatio is declared as an int so should not be used for prices. Use type double for this.
                    • Might need to capitalize SKF
                    • Still missing Semicolon on Add statement
                    • Semicolon doesn't belong at end of if statement
                    Not clear what methods your code is contained in.
                    • Variables are declared in variables region.
                    • Adding series is done in the Initialize() method.
                    • Calculation logic should be in OnBarUpdate() method.
                    Last edited by NinjaTrader_RyanM1; 03-24-2010, 10:14 AM. Reason: noticed additional items
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #11
                      Multi instruments

                      I'm not able to find correct syntax for BarsSinceEntry () == 0
                      for multi instruments. Wihout this the strategy works. I put the condition
                      if (BarsInProgress == 0) but doesnt hels.
                      Can you please write down how it should be.
                      BarsSinceEntry (0,,) == 0 Doesnt works.
                      Thanks

                      BarsSinceEntry(int barsInProgressIndex, string signalName, int entriesAgo)

                      Comment


                        #12
                        Hello Smkal,

                        You use the advanced overload that you posted, only it needs values for each parameter expected.

                        BarsSinceEntry(0, "", 0)
                        Ryan M.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by reynoldsn, Today, 04:40 PM
                        0 responses
                        4 views
                        0 likes
                        Last Post reynoldsn  
                        Started by Philippe56140, 04-27-2024, 02:35 PM
                        6 responses
                        54 views
                        0 likes
                        Last Post bltdavid  
                        Started by ETFVoyageur, Yesterday, 06:05 PM
                        7 responses
                        45 views
                        0 likes
                        Last Post ETFVoyageur  
                        Started by betsuni_123, Today, 04:20 PM
                        0 responses
                        12 views
                        0 likes
                        Last Post betsuni_123  
                        Started by Aquila_Welokk, 04-29-2024, 01:14 PM
                        2 responses
                        22 views
                        0 likes
                        Last Post Aquila_Welokk  
                        Working...
                        X