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

Strategy that trades another DataSeries

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

    Strategy that trades another DataSeries

    Hi!

    Please assist with strategy that does analysis on one instrument (DataSeries)
    but trades (send order) another one.

    Best Regards,
    Makhmout

    #2
    Hello makhmout,

    To do that you would need to manually code the strategy. In a manually coded strategy you can use AddDataSeries to add another insturment for the strategy to use.



    After adding the secondary series you can submit orders to that series by specifing the BarsInProgress when submitting an order. A simple example would be to make a condition using the first instrument and submit it to the second instrument. BarsInProgress is the series that is currently processing OnBarUpdate, 0 is the primary series. Each of the order methods have a specific overload set that lets you specify which series to submit the order to. In the following sample this is saying if the primary bars are processing check the condition Close greater than Open, if that is true submit an order to the secondary series.

    Code:
    if(BarsInProgress == 0)
    {
        if(Close[0] > Open[0])
       {
          EnterLong(1, 1, "MyEntryName");
       }
    }
    In the help gude you can see the overload for each order method that takes a BarsInProcess, that looks like the following:
    Code:
    EnterLong(int barsInProgressIndex, int quantity, string signalName)


    https://ninjatrader.com/support/help...hlightsub=mult i

    JesseNinjaTrader Customer Service

    Comment


      #3
      Exactly what I needed.
      One question more: if I don't specify BarsInProgress in analysis what DataSeries will be used by default? Main?

      Comment


        #4
        Hello makhmout,

        When you don't make conditions to contain the code for a specific BarsInProgress that code will be executed for all series. If we assume you have two instruments the OnBarUpdate method is going to be called for both instruments. The price series like Close[0] represents the close price of whatever series called OnBarUpdate at that time.

        here is a simple way to see what that processing looks like:

        Code:
        protected override void OnBarUpdate()
        {
            Print(BarsInProgress + " " + Close[0]);
        }
        If you run this it would output similar to the following, the prices are just made up here:

        Code:
        0 4000
        1 500
        0 4001
        1 501
        As each instrument/series closes a bar it calls OnBarUpdate. The BarsInProgress tells you which series called OnBarUpdate and the Close is mapped to the close of that instrument.

        If you want to run code just for 1 series its best to use a condition to constrain that code to isolate its execution:

        Code:
        if(BarsInProgress == 0)
        {​
        
        }
        If you want to run the code for all series but target specific data from a specific instrument you can use the plural series names such as Closes or Opens which lets you specify the BarsInProgress of where to get data like the following:

        Code:
        protected override void OnBarUpdate()
        {
            if(Closes[0][0] > Opens[0][0])
            {
                EnterLong(1, 1, "MyEntry");
            }
        }​
        This is saying check the prices of the primary series in the condition, submit the order to the secondary series. Both the primary and secondary series would call this code.


        JesseNinjaTrader Customer Service

        Comment


          #5
          Best explanation.
          Thanks a lot. Have a good day!

          Comment


            #6
            Hi again!

            I've coded all I need. Now it is running in simulation mode for check errors.
            One question more:
            then my strategy references to two DataSeries(instruments) I see in Strategies Page in NT that running strategy includes two lines - main DataSeries and Additional one.
            So in Realized colomn within additional series I've got message "In order to display realized PnL per instrument the strategy developer mast enable the strategy to include trade history".
            So, will this message disappear later after fisrt trade happen with second instrument or I have make changes within my code? And will realized PnL for second instrument be included in Strategy Perfomance analysis?

            Best Regards,
            Makhmout

            Comment


              #7
              Hello makhmout,

              You can enable trade history by using IncludeTradeHistoryInBacktest, that is normally by default true except when running a strategy from the control center. If so specify this in your code then it will always be true and should remove that message.

              JesseNinjaTrader Customer Service

              Comment


                #8
                Hello!

                All is good it seems.
                Have to check one moment.
                My strategy enters two times by limit order at different levels.​ So total position size is two.
                I 've set up entries per direction value in strategy properties at 2.
                But sometimes I'm catching that strategy's total position is 4.
                So, I can't understand what entries per direction value is correct - one or two.
                If I set it as 1, will my strategy enter both limit orders until total position size is two or not?
                What is the true way here?

                Best Regards,
                Makhmout

                Comment


                  #9
                  Hello makhmout,

                  Entries per direction only blocks entries that are submitted at the exact same time, for example you have it set to 2 but submit 3 orders at once. If your entry condition remains true and continues to submit orders later that will be allowed, you need to make sure your condition is true only for the initial entries.

                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Got it. Thanks a lot!

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by kujista, Today, 12:39 AM
                    0 responses
                    2 views
                    0 likes
                    Last Post kujista
                    by kujista
                     
                    Started by tonynt, 05-21-2019, 06:27 AM
                    11 responses
                    534 views
                    1 like
                    Last Post NinjaTrader_Jason  
                    Started by fitspressotablettry, Today, 12:36 AM
                    0 responses
                    1 view
                    0 likes
                    Last Post fitspressotablettry  
                    Started by Lancer, 01-29-2020, 10:15 PM
                    7 responses
                    468 views
                    1 like
                    Last Post NinjaTrader_Jason  
                    Started by gravdigaz6, Today, 12:05 AM
                    0 responses
                    1 view
                    0 likes
                    Last Post gravdigaz6  
                    Working...
                    X