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

Intraday OHLC / Multitime

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

    Intraday OHLC / Multitime

    Hello,

    I am porting an indicator to NT8 that relies upon a range of OHLC data (5 min, 240 min, daily) and uses this data to incorporate into a machine learning algo.

    What I have now:

    Code:
    public double GetDiff()
    		{
    				previous = (PriorDayOHLC().PriorOpen[0] + PriorDayOHLC().PriorHigh[0] + PriorDayOHLC().PriorLow[0] + PriorDayOHLC().PriorClose[0])/4;
    				now = (CurrentDayOHL().CurrentOpen[0] + CurrentDayOHL().CurrentHigh[0] + CurrentDayOHL().CurrentLow[0] + PriorDayOHLC().PriorClose[0])/4;
    			
    				change = (now - previous);
    				percentage = Math.Tanh(change / previous);
    				return percentage;
    		}
    I also have added a new data series:

    Code:
    				if(useMTF)
    				{
    				   AddDataSeries((Data.BarsPeriodType)barType, barPeriod);
    
    				}
    As a construct, I am able to get the indicator to work, however, when I adjust my barPeriod, the getDifference result is the same, almost as if it is not changing my resolution.

    Any thoughts?

    #2
    Hello rdavido,

    Is AddDataSeries being called in State.Configure?

    Where are you attempting to use this added series?

    Are you supplying BarsArray[1] (the second series) as the input series to an indicator?
    (You can also use Closes[1], Highs[1], etc)


    Are you checking BarsInProgress to ensure you are processing during the series you want to be processing?


    Are you ensuring there are enough bars for each series before processing?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      My code is attached below. Several issues:

      - I am not sure I have my plot correctly setup. For example, I want to ultimately plot "l3_0"
      - I would like to use a 5 minute chart, but have a bar array of 120 minutes and then obtain the open, high, low, close of said bars and compare now - previous.

      Note: some code missing (midde layers of a neural network) for propriety

      Code:
      #region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Gui.SuperDom;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion
      
      //This namespace holds Indicators in this folder and is required. Do not change it. 
      namespace NinjaTrader.NinjaScript.Indicators
      {
      	public class ANNIndicator : Indicator
      	{
      	#region Variables
      	double l0_0;
      	double l0_1;
      	double l0_2;
      	double l0_3;
      	double l0_4;
      	double l0_5;
      	double l0_6;
      	double l0_7;
      	double l0_8;
      	double l0_9;
      	double l0_10;
      	double l0_11;
      	double l0_12;
      	double l0_13;
      	double l0_14;
      	
      	double l1_0 ;
      	double l1_1 ;
      	double l1_2 ;
      	double l1_3 ;
      	double l1_4 ;
      	double l1_5 ;
      	double l1_6 ;
      	double l1_7 ;
      	double l1_8 ;
      	double l1_9 ;
      	double l1_10 ;
      	double l1_11 ;
      	double l1_12 ;
      	double l1_13 ;
      	double l1_14 ;
      	double l1_15 ;
      	double l1_16 ;
      	double l1_17 ;
      	double l1_18 ;
      	double l1_19 ;
      	double l1_20 ;
      	double l1_21 ;
      	double l1_22 ;
      	double l1_23 ;
      	double l1_24 ;
      	double l1_25 ;
      	double l1_26 ;
      	double l1_27 ;
      	double l1_28 ;
      	double l1_29 ;
      	
      	double l2_0 ;
      	double l2_1 ;
      	double l2_2 ;
      	double l2_3 ;
      	double l2_4 ;
      	double l2_5 ;
      	double l2_6 ;
      	double l2_7 ;
      	double l2_8 ;
      	double l3_0 ;
      		
      	double Threshold_Long;
      	double Threshold_Short;
      	
      	double week;	
      	double previous ;
      	double now ;
      	double change ;
      	double percentage ;
      		
      	private int  barIndex  = 0;
      	
      	#endregion
      		
      		
      		
      		protected override void OnStateChange()
      		{
      			if (State == State.SetDefaults)
      			{
      				Description									= @"Enter the description for your new custom Indicator here.";
      				Name										= "ANNIndicator";
      				Calculate									= Calculate.OnEachTick;
      				useMTF										= true;
      				barType										= barType.Minute;
      				barPeriod 									= 120;
      				IsOverlay									= false;
      				DisplayInDataBox							= true;
      				DrawOnPricePanel							= true;
      				DrawHorizontalGridLines						= true;
      				DrawVerticalGridLines						= true;
      				Threshold_Long								= -0.05;
      				Threshold_Short								= 0.05;
      				PaintPriceMarkers							= true;
      				
      				
      				ScaleJustification							= NinjaTrader.Gui.Chart.ScaleJustification.Right;
      				//Disable this property if your indicator requires custom values that cumulate with each new market data event. 
      				//See Help Guide for additional information.
      				IsSuspendedWhileInactive					= true;
      				Period										= l3_0;
      				
      				AddPlot(new Stroke(Brushes.DodgerBlue, 2), PlotStyle.Bar,  "l3_0");
      				AddPlot(new Stroke(Brushes.Green, 1), PlotStyle.Line, "Threshold_Long");
      				AddPlot(new Stroke(Brushes.Red, 1), PlotStyle.Line, "Threshold_Short");
      			}
      			else if (State == State.Configure)
      			{
      //				   AddDataSeries((Data.BarsPeriodType)barType, barPeriod);
      					AddDataSeries(Data.BarsPeriodType.Minute, 120);
      			}
      		}
      		
      		private double GetDiff()
      		{
      //				previous = (Opens[1][0] + Highs[1][0] + Lows[1][0] + Closes[1][0])/4;
      //				now = (Opens[0][1] + Highs[0][1] + Lows[0][1] + Closes[0][1])/4;
      			
      				previous = (PriorDayOHLC(BarsArray[1]).Open[1] + PriorDayOHLC(BarsArray[1]).High[1] + PriorDayOHLC(BarsArray[1]).Low[1] + PriorDayOHLC(BarsArray[1]).Close[1]);
      				now = (CurrentDayOHL(BarsArray[1]).Open[0] + CurrentDayOHL(BarsArray[1]).High[0] + CurrentDayOHL(BarsArray[1]).Low[0]);
      			
      				change = (now - previous);
      				percentage = (change / previous);
      				return percentage;
      		}
      		
      		protected override void OnBarUpdate()
      		{
      		    // Checks to ensure all Bars objects contain enough bars before beginning
          		// If this is a strategy, use BarsRequiredToTrade instead of BarsRequiredToPlot
          		if (CurrentBars[0] <= BarsRequiredToPlot || CurrentBars[1] <= BarsRequiredToPlot || CurrentBars[2] <= BarsRequiredToPlot)
              	return;
      			
      		l0_0 = GetDiff();
      		l0_1 = GetDiff();
      		l0_2 = GetDiff();
      		l0_3 = GetDiff();
      		l0_4 = GetDiff();
      		l0_5 = GetDiff();
      		l0_6 = GetDiff();
      		l0_7 = GetDiff();
      		l0_8 = GetDiff();
      		l0_9 = GetDiff();
      		l0_10 = GetDiff();
      		l0_11 = GetDiff();
      		l0_12 = GetDiff();
      		l0_13 = GetDiff();
      		l0_14 = GetDiff();
      	 
      		l3_0 = Math.Tanh(l2_0*0.385061186 + l2_1*0.6218531956 + l2_2*-0.7790340983 + l2_3*0.1413078332 + l2_4*0.1857010624 + l2_5*-0.1769456351 + l2_6*-0.3242337911 + l2_7*-0.503944883 + l2_8*0.1540568869);
      		Values[0][0] = l3_0;
      		}
      
      		#region Properties
      		[Range(-1, double.MaxValue), NinjaScriptProperty]
      		[Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
      		public double Period
      		{ get; set; }
      		
      		[NinjaScriptProperty]
      		[Display(Name = "Use MTF", GroupName = "NinjaScriptParameters", Order = 0)]
      		public bool useMTF
      		{ get; set; }
      		
      		[NinjaScriptProperty]
      		[Display(Name = "Bar Type", GroupName = "NinjaScriptParameters", Order = 1)]
      		public barType barType
      		{ get; set; }
      		
      		[NinjaScriptProperty]
      		[Range(1, int.MaxValue)]
      		[Display(Name = "Bar Period", GroupName = "NinjaScriptParameters", Order = 2)]
      		public int barPeriod
      		{ get; set; }
      
      		#endregion
      
      	}
      }
      
      public enum barType
      {
      	Day     	= BarsPeriodType.Day,
      	Minute  	= BarsPeriodType.Minute,
      	Range   	= BarsPeriodType.Range,
      	Second  	= BarsPeriodType.Second,
      	Tick    	= BarsPeriodType.Tick,
      	Volume  	= BarsPeriodType.Volume,
      	Renko   	= BarsPeriodType.Renko
      }
      
      #region NinjaScript generated code. Neither change nor remove.
      
      namespace NinjaTrader.NinjaScript.Indicators
      {
      	public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
      	{
      		private ANNIndicator[] cacheANNIndicator;
      		public ANNIndicator ANNIndicator(double period, bool useMTF, barType barType, int barPeriod)
      		{
      			return ANNIndicator(Input, period, useMTF, barType, barPeriod);
      		}
      
      		public ANNIndicator ANNIndicator(ISeries<double> input, double period, bool useMTF, barType barType, int barPeriod)
      		{
      			if (cacheANNIndicator != null)
      				for (int idx = 0; idx < cacheANNIndicator.Length; idx++)
      					if (cacheANNIndicator[idx] != null && cacheANNIndicator[idx].Period == period && cacheANNIndicator[idx].useMTF == useMTF && cacheANNIndicator[idx].barType == barType && cacheANNIndicator[idx].barPeriod == barPeriod && cacheANNIndicator[idx].EqualsInput(input))
      						return cacheANNIndicator[idx];
      			return CacheIndicator<ANNIndicator>(new ANNIndicator(){ Period = period, useMTF = useMTF, barType = barType, barPeriod = barPeriod }, input, ref cacheANNIndicator);
      		}
      	}
      }
      
      namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
      {
      	public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
      	{
      		public Indicators.ANNIndicator ANNIndicator(double period, bool useMTF, barType barType, int barPeriod)
      		{
      			return indicator.ANNIndicator(Input, period, useMTF, barType, barPeriod);
      		}
      
      		public Indicators.ANNIndicator ANNIndicator(ISeries<double> input , double period, bool useMTF, barType barType, int barPeriod)
      		{
      			return indicator.ANNIndicator(input, period, useMTF, barType, barPeriod);
      		}
      	}
      }
      
      namespace NinjaTrader.NinjaScript.Strategies
      {
      	public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
      	{
      		public Indicators.ANNIndicator ANNIndicator(double period, bool useMTF, barType barType, int barPeriod)
      		{
      			return indicator.ANNIndicator(Input, period, useMTF, barType, barPeriod);
      		}
      
      		public Indicators.ANNIndicator ANNIndicator(ISeries<double> input , double period, bool useMTF, barType barType, int barPeriod)
      		{
      			return indicator.ANNIndicator(input, period, useMTF, barType, barPeriod);
      		}
      	}
      }
      
      #endregion
      Attached Files
      Last edited by rdavido; 04-11-2017, 05:12 PM.

      Comment


        #4
        Hello rdavido,

        Just heads up, the line that uses barPeriod is commented out. Currently you have hard coded a additional series of 120 minute.

        If you comment the line with the hard coded 120 minute and uncomment the line that uses barPeriod in the AddDataSeries() call, does this allow the script to produce different values?
        As a heads up, it is not supported to dynamically add series using variables in this manner.

        From the help guide:

        "Arguments supplied to AddDataSeries() should be hard-coded 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.".


        Currently, the script is not set to plot any thing. Instead of the Values[plotIndex][barIndex] being set, there are double variables being set.

        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by chartchart, 05-19-2021, 04:14 PM
        3 responses
        577 views
        1 like
        Last Post NinjaTrader_Gaby  
        Started by bsbisme, Yesterday, 02:08 PM
        1 response
        15 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by prdecast, Today, 06:07 AM
        0 responses
        3 views
        0 likes
        Last Post prdecast  
        Started by i019945nj, 12-14-2023, 06:41 AM
        3 responses
        60 views
        0 likes
        Last Post i019945nj  
        Started by TraderBCL, Today, 04:38 AM
        2 responses
        18 views
        0 likes
        Last Post TraderBCL  
        Working...
        X