NexusFi: Find Your Edge


Home Menu

 





Need help to figure out why this SMA class wont work in Ninjatrader.. its wierd..


Discussion in NinjaTrader

Updated
    1. trending_up 1,985 views
    2. thumb_up 0 thanks given
    3. group 2 followers
    1. forum 1 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
 KhaosTrader 
San Jose
 
Experience: Intermediate
Platform: NinjaTrader, Esignal
Trading: Stocks
Posts: 107 since Jan 2012
Thanks Given: 40
Thanks Received: 21

Hi,

I wrote an SMA class, by basically moving the built in SMA Indicator code that comes with Ninjatrader into an object that uses no Indicator.

I am having an issue though as the output is not correct. I was able to successfully do the EMA, but the SMA is not working.

Any help would be appreciated... Thank you in advance..

Here is the code...

 
Code
#region Khaos_Virtual_Indicator_SMAz
	public class Khaos_Virtual_Indicator_SMAz
	{
		
		private NinjaScriptBase NinjaScriptBase;
		private int Period;
		
		private double priorSum;
		private double sum;

		public Series<double> Value { get; private set; }
		
		
		
		public Khaos_Virtual_Indicator_SMAz(NinjaScriptBase NinjaScriptBase, int Period)
		{
			this.NinjaScriptBase = NinjaScriptBase;
			this.Period = Period;
			Value = new Series<double>(NinjaScriptBase);
			
			priorSum	= 0;
			sum			= 0;
		}
		
		
		public Series<double> TickTock(ISeries <double> Input)
		{
			
			
			try
			{
			
			if (NinjaScriptBase.BarsArray[0].BarsType.IsRemoveLastBarSupported)
			{
				if (NinjaScriptBase.CurrentBar == 0)
					Value[0] = Input[0];
				else
				{
					double last = Value[1] * Math.Min(NinjaScriptBase.CurrentBar, Period);

					if (NinjaScriptBase.CurrentBar >= Period)
						Value[0] = (last + Input[0] - Input[Period]) / Math.Min(NinjaScriptBase.CurrentBar, Period);
					else
						Value[0] = ((last + Input[0]) / (Math.Min(NinjaScriptBase.CurrentBar, Period) + 1));
				}
			}
			else
			{
				if (NinjaScriptBase.IsFirstTickOfBar)
					priorSum = sum;

				sum = priorSum + Input[0] - (NinjaScriptBase.CurrentBar >= Period ? Input[Period] : 0);
				Value[0] = sum / (NinjaScriptBase.CurrentBar < Period ? NinjaScriptBase.CurrentBar + 1 : Period);
			}
			
			return Value;
				
			}
			
			catch (Exception e)
			{
				NinjaScriptBase.Log ("Cought Error Khaos Moving Average = " + e.ToString(), NinjaTrader.Cbi.LogLevel.Warning);
				NinjaScriptBase.Print(NinjaScriptBase.Time[0] +  " " + e.ToString());
				return Value;
			}				
		}
	}
	#endregion
Here is a project that invokes the class


 
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;
using Khaos_Elements;
#endregion

//This namespace holds Indicators in this folder and is required. Do not change it. 
namespace NinjaTrader.NinjaScript.Indicators.KhaosIndicators
{
	public class Test : Indicator
	{
	//	private  Khaos_Virtual_Indicator_QQE_v1 Khaos_Virtual_Indicator_QQE;
		private Khaos_Virtual_Indicator_SMAz Khaos_Virtual_Indicator_SMAz;	
		protected override void OnStateChange()
		{
			if (State == State.SetDefaults)
			{
				Description									= @"test";
				Name										= "Test";
				Calculate									= Calculate.OnEachTick;
				IsOverlay									= false;
				DisplayInDataBox							= true;
				DrawOnPricePanel							= true;
				DrawHorizontalGridLines						= true;
				DrawVerticalGridLines						= true;
				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					= false;
				AddPlot(Brushes.Orange, "Signal");

			}
			else if (State == State.Configure)
			{

				Khaos_Virtual_Indicator_SMAz = new Khaos_Virtual_Indicator_SMAz((NinjaScriptBase)this,20);
			}
		}

		protected override void OnBarUpdate()
		{
			if (CurrentBar < 15) {return;}
			Khaos_Virtual_Indicator_SMAz.TickTock(Close);
			Signal[0] = Khaos_Virtual_Indicator_SMAz.Value[0];

		}

		#region Properties

		[Browsable(false)]
		[XmlIgnore]
		public Series<double> Signal
		{
			get { return Values[0]; }
		}

		#endregion

	}
}

#region NinjaScript generated code. Neither change nor remove.

namespace NinjaTrader.NinjaScript.Indicators
{
	public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
	{
		private KhaosIndicators.Test[] cacheTest;
		public KhaosIndicators.Test Test()
		{
			return Test(Input);
		}

		public KhaosIndicators.Test Test(ISeries<double> input)
		{
			if (cacheTest != null)
				for (int idx = 0; idx < cacheTest.Length; idx++)
					if (cacheTest[idx] != null &&  cacheTest[idx].EqualsInput(input))
						return cacheTest[idx];
			return CacheIndicator<KhaosIndicators.Test>(new KhaosIndicators.Test(), input, ref cacheTest);
		}
	}
}

namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
	public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
	{
		public Indicators.KhaosIndicators.Test Test()
		{
			return indicator.Test(Input);
		}

		public Indicators.KhaosIndicators.Test Test(ISeries<double> input )
		{
			return indicator.Test(input);
		}
	}
}

namespace NinjaTrader.NinjaScript.Strategies
{
	public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
	{
		public Indicators.KhaosIndicators.Test Test()
		{
			return indicator.Test(Input);
		}

		public Indicators.KhaosIndicators.Test Test(ISeries<double> input )
		{
			return indicator.Test(input);
		}
	}
}

#endregion


Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Pakistan Mediator in Tehran as Hormuz Normalization Coll …
Prediction Markets & Event Contracts
UMA Votes Tonight: Polymarkets $80M Strategy Bitcoin Bat …
Prediction Markets & Event Contracts
Penalties in Budapest, Peace Deadline in Tehran: Arsenal …
Prediction Markets & Event Contracts
April FOMC Minutes: Most Divided Fed Since 1992 -- Many …
Traders Hideout
Saylors 41-Month HODL Breaks: Strategy Sells 32 BTC as $ …
Prediction Markets & Event Contracts
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
24 thanks
2026 Jlab journal
10 thanks
Lady Vols Primer: Trading Volatility Journal
7 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Trying to learn Volume and price action correlation
5 thanks




Last Updated on December 27, 2016


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts