• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Creating daily turnover on multi instrument and multi timeframe

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

    Creating daily turnover on multi instrument and multi timeframe

    Hi,

    I am coding a strategy to enter positions on a list of stocks entering on a 1 minute timeframe but with rules that also use daily values. The list of stocks changes every day.

    How do I calculate a 20 day moving average of turnover SMA(Close * Volume,20) in a daily time frame that I can access in 1 minute time frame for each stock.

    I got this far:
    Code:
    			else if (State == State.Configure)
    			{
    				ClearOutputWindow();
    				
    				AddDataSeries(Data.BarsPeriodType.Day, 1);
    				
    				AddDataSeries("AAL", Data.BarsPeriodType.Minute, 1);
    				AddDataSeries("AAL", Data.BarsPeriodType.Day, 1);				
    				AddDataSeries("AAPL", Data.BarsPeriodType.Minute, 1);
    				AddDataSeries("AAPL", Data.BarsPeriodType.Day, 1);	
    				AddDataSeries("ADBE", Data.BarsPeriodType.Minute, 1);
    				AddDataSeries("ADBE", Data.BarsPeriodType.Day, 1);	
    				AddDataSeries("ADI", Data.BarsPeriodType.Minute, 1);
    				AddDataSeries("ADI", Data.BarsPeriodType.Day, 1);	
    				AddDataSeries("ADP", Data.BarsPeriodType.Minute, 1);
    				AddDataSeries("ADP", Data.BarsPeriodType.Day, 1);	
    				AddDataSeries("ADSK", Data.BarsPeriodType.Minute, 1);
    				AddDataSeries("ADSK", Data.BarsPeriodType.Day, 1);	
    				AddDataSeries("AKAM", Data.BarsPeriodType.Minute, 1);
    				AddDataSeries("AKAM", Data.BarsPeriodType.Day, 1);	
    						
    			}
    			
    				else if (State == State.DataLoaded)
    			{
    				turnover = new Series<double>(this, MaximumBarsLookBack.Infinite);
    			}
    			
    		}
    
    		
    		protected override void OnBarUpdate()
    		{
    
    			if (BarsInProgress%2 == 0) 
    			{
    				if (CurrentBar < 2000)	return;
    			}
    				
    			
    			if (BarsInProgress%2 > 0) 
    			{
    				if (CurrentBar < 21)	return;
    				
    			}
    			
    
    				if(BarsInProgress >= 2 && BarsInProgress%2 == 0)//Only run on 1 minute data series
    				{
    					sma5 = SMA(BarsArray[BarsInProgress+1],5)[0];//Yesterdays SMA5 daily
    					turnover[0] = Closes[BarsInProgress+1][0] * Volumes[BarsInProgress+1][0];//Yesterdays turnover	
    }
    The code above will calculate the daily turnover of the last day, however how do I create a 20 period simple moving average on daily values of the turnover for each of the stocks in the list noting that one day there may be 5 stocks in the list and the next day 50 so it needs to be dynamic.

    Thanks.

    Numbo

    #2
    Hello Numbo,

    It is not supported to dynamically add instruments to a script.

    From the help guide:
    "Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result into an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner."
    http://ninjatrader.com/support/helpG...dataseries.htm

    To supply an added series to an indicator for calculations, pass the BarsArray index of that series as the input series parameter to the indicator method. Do this is the BarsInProgress of the primary series after determining both series have enough bars to prevent an error.

    BarsArray - http://ninjatrader.com/support/helpG.../barsarray.htm
    BarsInProgress - http://ninjatrader.com/support/helpG...inprogress.htm

    Below is a link to an example:
    http://ninjatrader.com/support/forum...ad.php?t=94639
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      I am a little confused by your reply, perhaps this comes through the use of the word "dynamic". The code posted above shows that the data series are hard coded using AddDataSeries, not added dynamically into a variable. I will update the strategy code on a daily basis to reflect the stocks the strategy will trade on hence it will alsways be hard coded. Or am I missing something?

      What I want to be able to do is code this:
      SMA20 = SMA(Close[2[]0] * Volume[2][0],20);

      The 2 is for the barsinprogress of the daily data series and 0 for current bar. However SMA does not allow me to do this. If I use Barsarray[2] it only references the close of this data series. In the code posted I will be referencing the result from the minute data series.

      I hope this clarifies the question.

      Numbo.

      Comment


        #4
        Hello Numbo,

        Thanks for your reply.

        To accomplish your goal of "SMA20 = SMA(Close[2][0] * Volume[2][0],20);"

        You would need to create a double data series to hold the results of the math calculation of close * volume. Reference: http://ninjatrader.com/support/helpG...s/?seriest.htm

        You also would need to synchronize the calculation results data series to the added bars data series (to create the same number of "slots" as there are bars in the secondary data series. This is discussed and shown here in this reference sample: http://ninjatrader.com/support/forum...ead.php?t=3572

        To collect the data you would need to use something like:

        myCustomDataseries[0] = Closes[1][0] * Volumes[1][0]; // note the use of the plurals of closes and volumes
        References: http://ninjatrader.com/support/helpG...us/?closes.htm
        http://ninjatrader.com/support/helpG...es_volumes.htm

        To finally obtain the 20 period SMA:

        SMA20 = SMA(myCustomDataseries, 20)[0];
        Last edited by NinjaTrader_ChelseaB; 07-25-2017, 08:20 AM.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hi Paul,

          Thanks for the reply. As the strategy is trading over multiple instruments, potentially up to 50+, is it required to add one custom data series for each instrument (and timeframe), or can one custom series be created for all instruments and then referenced using BarsInProgress during the OnBarUpdate method?

          If I need to create the custom data series needs for each instrument (and timeframe) this will be impractical as I update the hardcoded list of instruments in the strategy each day and would like to minimise any coding changes other than changing the data series that are added to reflect the changes in the symbols being traded. I thought that another way to do it would be to create a jagged array and then store the Closes * Volumes values in the array using something like this:

          Code:
          				else if (State == State.DataLoaded)
          			{	
          		private double[][] SMAturnoverArray = new double[100][];
          				for (var i = 0; i < CurrentBars.Length; i++)
          				{	
          					for (var j = 0; j < 30; j++)
          					{	
          						SMAturnoverArray[i][j] =  Closes[i][j] * Volumes[i][j];	
          					}
          					
          				}				
          			}
          The SMA could be calculated manually thereafter. However this gives the following error when running:
          Error on calling 'OnStateChange' method: Object reference not set to an instance of an object.

          What is the best way to calculate the SMA on multiple instruments given the above. Also why does the code above give this error?

          Thanks.

          Numbo

          Comment


            #6
            Hello Numbo,

            Thanks for your reply.

            Regarding the error, you would need to declare the array as private at the class level and then initialize in State.DataLoaded, however, you would not be able to use a jagged array as an input to the SMA as it requires a dataseries input.

            We recommend using the custom data series. By default, these are limited to 256 data points. From the helpguide: Note: By default NinjaTrader limits the number of values stored for Series<T> objects to 256 from the current bar being processed. This drastically improves memory performance by not holding onto old values that are generally not needed. Should you need more values than the last 256 please be sure to create the Series<T> object so that it stores all values instead through the use of the MaximumBarsLookBack property. Reference: http://ninjatrader.com/support/helpG...s/?seriest.htm
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Hi Paul,

              If I use a custom data series in a multi instrument script, I assume that I need to declare a custom data series for each instrument that I pass into the SMA. Is this correct?
              e.g. to store the turnover of AAPL and MSFT (two shares):
              turnoverMSFT = new Series<double>(this, MaximumBarsLookBack.TwoHundredFiftySix);
              turnoverAAPL = new Series<double>(this, MaximumBarsLookBack.TwoHundredFiftySix);

              And then input these data series to the SMA.
              Is this correct? Or is there a way to store multiple data series (for multiple instruments) in on new custom data series?

              Thanks.

              Numbo

              Comment


                #8
                Hello Numbo,

                Thanks for your reply.

                Correct, you would need to create a data series for each instrument that you wish to store the calculation results of volume times close to input to the SMA.

                An alternative you may wish to consider is to create a separate indicator that calculates the SMA values and writes the instrument name and the current SMA values to a data file. Your strategy could then read the file and pick the data values according to the instrument name. You can see an example of reading/writing in these three reference samples:
                http://ninjatrader.com/support/forum...ead.php?t=3475
                http://ninjatrader.com/support/forum...ead.php?t=3476
                http://ninjatrader.com/support/forum...ead.php?t=3477
                Paul H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by pulpiwapi, 03-04-2019, 11:18 PM
                11 responses
                143 views
                0 likes
                Last Post music_p13  
                Started by indy73, Yesterday, 10:52 PM
                3 responses
                17 views
                0 likes
                Last Post sledge
                by sledge
                 
                Started by priceisking, Today, 12:51 PM
                1 response
                18 views
                0 likes
                Last Post NinjaTrader_PaulH  
                Started by chartish, Today, 10:33 AM
                3 responses
                26 views
                0 likes
                Last Post chartish  
                Started by casabella, Today, 11:51 AM
                1 response
                10 views
                0 likes
                Last Post NinjaTrader_PaulH  
                Working...
                X