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

Multi-Instrament Strategy?

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

    Multi-Instrament Strategy?

    I have created a simple test strategy:

    protectedoverridevoid Initialize() {
    Add("VXX", PeriodType.Day, 1);
    CalculateOnBarClose = true;
    }

    protectedoverridevoid OnBarUpdate() {
    if (Time[0].Year < 2009) return;
    if (Time[0].DayOfYear <= 34) return;
    Print("");
    if (BarsInProgress == 0) {
    Print("BarsInProgress == 0");
    Print(string.Format("Opens[0][0] = {0}", Opens[0][0]));
    Print(string.Format("Opens[1][0] = {0}", Opens[1][0]));
    Print(string.Format("Closes[0][0] = {0}", Closes[0][0]));
    Print(string.Format("Closes[1][0] = {0}", Closes[1][0])); }
    elseif (BarsInProgress == 1) {
    Print("BarsInProgress == 1");
    Print(string.Format("Opens[0][0] = {0}", Opens[0][0]));
    Print(string.Format("Opens[1][0] = {0}", Opens[1][0]));
    Print(string.Format("Closes[0][0] = {0}", Closes[0][0]));
    Print(string.Format("Closes[1][0] = {0}", Closes[1][0])); }

    }

    I run the strategy in Strategy [FONT='Verdana','sans-serif']Analyzer[/font]. I choose Instrument=VXX, Type=Day, Value=1. This is the output I get:

    BarsInProgress == 0
    Opens[0][0] = 101.6
    Opens[1][0] = 98.25
    Closes[0][0] = 99.13
    Closes[1][0] = 99.74

    BarsInProgress == 1
    Opens[0][0] = 101.6
    Opens[1][0] = 101.6
    Closes[0][0] = 99.13
    Closes[1][0] = 99.13

    BarsInProgress == 0
    Opens[0][0] = 99
    Opens[1][0] = 101.6
    Closes[0][0] = 97.7
    Closes[1][0] = 99.13

    BarsInProgress == 1
    Opens[0][0] = 99
    Opens[1][0] = 99
    Closes[0][0] = 97.7
    Closes[1][0] = 97.7

    I would like to simply add the VXX as a filter to some of my strategies, but the numbers for its open and close seem to be lagging by 1.
    Is there no way to access the second bars Opens and Closes from the first bars OnBarUpdate? Or is there another way I can code my strategy so I can add an Instrument or two as filters?

    Thanks,
    Erik


    #2
    Erik, thanks for the post - please work closely alongside this reference sample - http://www.ninjatrader-support2.com/...ead.php?t=6652 and then make sure to make the ADDed series smaller than the primary one for proper sequencing. To access intrabar info of your daily chart then, please work with CalculateOnBarClose = false then in realtime.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      I reviewed that sample. I have some questions:

      Note: I am using a base class for my strategies. My strategies are complete and I am just trying to add another instrument as a filter. In this case VXX. I will be adding something like:

      protectedoverridevoid Initialize() {
      if (this.filterWithRSI_VXX != 0) {
      Print("Adding VXX");
      Add("VXX", PeriodType.Day, 1);
      }
      ...
      }

      So in this case VXX will be BarsArray[1].

      When I use the VXX I will use something like this:
      if (RSI(Closes[1], 14, 3)[0] > this.rsiVXXLimit) return;

      Question 1. I only have daily data no minute data. Lets say I want to go long on INTC. I would use something like:

      protectedvirtualvoid MyEnterLongLimit(double Price) {...
      EnterLongLimit(0, false, GetNumSharesToBuy(), Price, "");
      }
      protectedvirtualvoid MyEnterLongLimit() {...
      EnterLongLimit(0, false, GetNumSharesToBuy(), currClose, "");
      }
      protectedvirtualvoid MyEnterLongLimit(double limitPrice, string signalName) {...
      EnterLongLimit(0, false, GetNumSharesToBuy(), limitPrice, signalName);
      }
      protectedvirtualvoid MyEnterLongLimit(string signalName) {...
      EnterLongLimit(0, false, GetNumSharesToBuy(), currClose, signalName);
      }

      Using 0 as first parameter and using false as second parameter should cause orders to be entered that will only be good for the next bar (day) so long as I have TIF = Day?

      Question 2. if I only have daily data and at this point do not want to bother with minute data then this code will not work for me? Add(PeriodType.Minute, 1);

      Question 3. If the answer to 2 is true then I could do the following?

      leave CalculateOnBarClose = true; in Initialize()

      Add something like this to OnBarUpdate
      protectedoverridevoid OnBarUpdate() {
      if (this.filterWithRSI_VXX == 0) {
      if (BarsInProgress != 0) return;
      }
      else {
      if (BarsInProgress != 1) return;
      }

      ...
      }
      Make changes in my strategy:

      change:
      if (RSI(Close, 2, 3)[0] < entryOverBoughtLimit) {
      to:
      if (RSI(Closes[0], 2, 3)[0] < entryOverBoughtLimit) {

      change:
      currClose = Close[0];
      to:
      currClose = Closes[0][0];

      change:
      if (Position.MarketPosition != MarketPosition.Flat) return;
      to:
      if (Positions[0].MarketPosition != MarketPosition.Flat) return;

      etc...

      Any reasons why this would not work?

      Last edited by ErikHR; 07-24-2009, 10:38 AM.

      Comment


        #4
        Erik,

        1. True, orders would expire at the end of the bar then.

        2. The code should work in realtime as you would expect with CalculateOnBarClose set to false

        3. For backtesting, yes you would need to use more granular data.

        Adding one minute data would not help you getting last bar's open and close from the daily chart, you would need to access this info from the minute one then in backtesting.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Let me see if I understand this.

          So if I want to back test my strategy and have it work similar to real-time and do not have minute data then I will:

          1. Leave CalculateOnBarClose = true;
          2. Modify my original strategy so it never used properties like Open, High, Low, Close, Position, but instead uses the collections like Opens[0]
          3. instead of actually calling EnterLong, EnterShort when BarsInProgress == 0 I will just set a flag saying strategy should buy, then when BarsInProgress == 1 if I pass my filter test I will call EnterLong EnterShort making sure I pass in the barsInProgressIndex = 0

          Is this correct?

          Thanks,
          Erik

          Comment


            #6
            Erik, no if you want to simulate setting CalculateOnBarClose to false on a daily chart in realtime you would need to use more granular data in backtesting - http://www.ninjatrader-support2.com/...ead.php?t=6652
            BertrandNinjaTrader Customer Service

            Comment


              #7
              I am sorry I guess I was not very clear.

              I do not want to simulate setting CalculateOnBarClose to false on a daily chart in real-time. I simply want to add a filter to my daily strategy.

              I will not be looking at the charts during the day. I have a day job and will be looking at the charts in the morning when the markets opens and in the evening to see how I did and get ready for the next day.

              I only want to place orders at the begining of the day.

              So if I want to back test my strategy and have it work similar to real-time and do not have minute data then I will:

              1. Leave CalculateOnBarClose = true;
              2. Modify my original strategy so it never used properties like Open, High, Low, Close, Position, but instead uses the collections like Opens[0]
              3. instead of actually calling EnterLong, EnterShort when BarsInProgress == 0 I will just set a flag saying strategy should buy, then when BarsInProgress == 1 if I pass my filter test I will call EnterLong EnterShort making sure I pass in the barsInProgressIndex = 0

              Is this correct or am I still missing something?

              Thanks,
              Erik

              Comment


                #8
                Erik,

                I guess I still don't follow. When you are backtesting, the trades will have to go onto the NEXT bar. The only way you can get it to trade on the current bar is to add intrabar granularity. To do this, please see this reference sample: http://www.ninjatrader-support2.com/...ead.php?t=6652
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  I have already solved that problem, based on your answers to previous posts. I am doing the following:

                  1. at 10:00 pm after market data is available for current day back test empty strategy to get current days bars.
                  2. run small utility that:
                  a. backs-up NinjaTrader db
                  b. creates Mock daily bar text files for next day.
                  3. import Mock bars into NinjaTrader.
                  4. back test strategies and review charts and create order entry text files
                  5. using same small utility:
                  a. restore NinjaTrader db.
                  b. In the morning place orders to MB Trading Account using their API or open up the charts for the Intraments that back testing indicates will place trades.

                  Hopefully I have not left anything out this time

                  So if I want to add that daily Instrument for a new daily filter will what I mentioned below work.

                  FYI - my main problem I have been trying to solve is I am trading all NASDAQ stocks between 20 and 5 dollars that meet a specific volume filter ~500 stocks and I cannot open charts for all of them at once and there is no concept of running strategies for Instrument lists except for back testing and optimization.

                  This an exaple order file in backtesting ~5 to 10% of the orders get filled:

                  2009_07_22.txt

                  ALGN EnterLongLimit 100 8.7685
                  ALTH EnterLongLimit 200 7.106
                  AMCC EnterLongLimit 200 7.2675
                  ASCA EnterLongLimit 100 17.974
                  BARE EnterLongLimit 200 7.106
                  BDSI EnterLongLimit 200 5.1015
                  BJRI EnterLongLimit 100 14.991
                  BOOM EnterLongLimit 100 15.789
                  CRAY EnterLongLimit 200 7.0965
                  FHCO EnterLongLimit 200 5.415
                  HCBK EnterLongLimit 100 12.6445
                  HTGC EnterLongLimit 200 8.2175
                  HTLD EnterLongLimit 100 13.5185
                  IFSIA EnterLongLimit 200 5.624
                  MDAS EnterLongLimit 100 17.6605
                  MSCS EnterLongLimit 200 7.049
                  OVTI EnterLongLimit 100 11.4
                  PALM EnterLongLimit 100 13.167
                  SCHW EnterLongLimit 100 15.979
                  SEPR EnterLongLimit 100 14.535
                  SOLR EnterLongLimit 200 4.8925
                  TITN EnterLongLimit 100 10.7255
                  TLEO EnterLongLimit 100 15.504
                  TXRH EnterLongLimit 100 10.3835
                  VITA EnterLongLimit 200 5.092
                  VOLC EnterLongLimit 100 13.9365
                  WERN EnterLongLimit 100 16.359
                  ZOLT EnterLongLimit 100 8.645
                  Last edited by ErikHR; 07-24-2009, 12:19 PM.

                  Comment


                    #10
                    I know you have not replied to my last post yet but I have another question:

                    Regarding this item below:

                    b. In the morning place orders to MB Trading Account using their API or open up the charts for the Instruments that back testing indicates will place trades.

                    I would prefer to use NT and just open the charts once I have my list of Instruments that will trade the next day, that way it is done automatically for me and I do not have to manually run my utility that wraps the MB Trading api.

                    I am running a 64 bit os, with 4 gigs of ram and 4 monitors so the system should not have any performance or memory issues.

                    It looks like I will need ~30 charts open on any given day for each strategy I am running. With NT 6.5 approximately how many charts can I expect to open before I start having issues with the software? What about NT 7.0? I do not want to hold you to any numbers I am just looking for a best guess I realize there are many variables to take into account.

                    Thanks,
                    Erik

                    Comment


                      #11
                      Erik,

                      What I am understanding is that you are trying to generate signals for the very last bar on your daily charts and then writing that to some text file. Since the last bar of the chart can't be traded per se you add a fake daily bar for each instrument to be able to trade on that last bar. Then you want to write out to text file.

                      I do not think you need to do this though. If you are simply writing out to text file you already DO process signals on that last real daily bar. You can already write out to text file any information you want in regards to what trade you will want to take the next morning. The only thing is you won't actually see that trade in the Strategy Analyzer.

                      As far as the question on how many charts you can run, there are too many factors influencing. It depends on how many days back you are looking at, how many indicators you have running, how computationally intense your scripts are, how many bars are on your chart, how strong your machine is, etc. In general, 7 will reduce the memory footprint of bars significantly so you will be able to load up more charts, but it still comes down to your individual setup.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks for the information. I am confused. The reason I started adding the mock bar for the next day was so the OnBarUpdate would fire for the current days bar. I thought if I am doing Daily Bars the OnBarUpdate will not fire for today’s bar until tomorrows bar starts to paint. And since the morning is too late to do my analysis to figure out what Instrument charts to open I have been backing up the database adding the Mock bars and then restoring the database.

                        Are you saying this is not needed? I basically just need today’s OnBarUpdate event to fire in back testing (this evening) so I can filter my list of 500 Instruments to a manageable list I can trade the next morning.

                        Thanks,
                        Erik

                        Comment


                          #13
                          I see what you are saying now. Yes, you do need mock bars in then.
                          Josh P.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by joselube001, 05-10-2024, 12:17 PM
                          6 responses
                          27 views
                          0 likes
                          Last Post joselube001  
                          Started by bigc0220, 09-18-2018, 09:16 AM
                          6 responses
                          2,579 views
                          0 likes
                          Last Post NinjaTrader_ChelseaB  
                          Started by lorem, 04-25-2024, 09:18 AM
                          18 responses
                          78 views
                          0 likes
                          Last Post lorem
                          by lorem
                           
                          Started by DawnTreader, 05-08-2024, 05:58 PM
                          21 responses
                          81 views
                          0 likes
                          Last Post NinjaTrader_Gaby  
                          Started by doihaveto13, Today, 12:46 PM
                          2 responses
                          4 views
                          0 likes
                          Last Post doihaveto13  
                          Working...
                          X