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

Rewriting Indicator EMA to use Ninjascript Base but not working

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

    Rewriting Indicator EMA to use Ninjascript Base but not working

    Hi,

    I want to write an add-on that can be used like an indicator from a strategy. I believe i cant pass the Indicator object to my classes - where my classes use indicators. So that means that I must rewrite the indicators that my classes use to use Ninjascript base.

    I use the EMA indicator in my classes, so i decided to rewrite that, and came up with the following code below, but it doesnt seem to work.. what am I doing incorrectly..

    Thank you in advance.

    Code:
    namespace Khaos_Elements
    {
    	public class Khaos_EMA
    	{
    		
    		private NinjaScriptBase NinjaScriptBase;
    		
    		private int Period;
    		
    		private double constant1;
    		private double constant2;
    		
    		public Series<double> Value { get; private set; }
    		
    		
    		
    		public Khaos_EMA(NinjaScriptBase NinjaScriptBase)
    		{
    			this.NinjaScriptBase = NinjaScriptBase;
    			this.Period = Period;
    			constant1 = 2.0 / (1 + Period);
    			constant2 = 1 - (2.0 / (1 + Period));
    			
    			 Value = new Series<double>(NinjaScriptBase);
    		}
    		
    		
    		public Series<double> TickTock(int Period)
    		{
    			
    			
    			try
    			{
    			Value[0] = (NinjaScriptBase.CurrentBar == 0 ? NinjaScriptBase.Input[0] : NinjaScriptBase.Input[0] * constant1 + constant2 * Value[1]);
    			
    			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;
    			}				
    		}
    	}
    }

    #2
    Hello KhaosTrader, and thank you for your question. The attached add-on compiles properly for me. If you could let us know which errors you are receiving when you try to use it during runtime, or I could answer any questions about the operation of NinjaTrader add-ons, please let us know.
    Attached Files
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      There is some sort of error, its not calculating properly, One thing I noted was that there was one error which i fixed, but still it does not yield the proper result when calling TickTock

      Here is the latest code below.. Thank you for your patience with me on this, I really cant figure why its not calculating properly.

      Code:
      	public class Khaos_EMA
      	{
      		
      		private NinjaScriptBase NinjaScriptBase;
      		
      		private int Period;
      		
      		private double constant1;
      		private double constant2;
      		
      		public Series<double> Value { get; private set; }
      		
      		
      		
      		public Khaos_EMA(NinjaScriptBase NinjaScriptBase)
      		{
      			this.NinjaScriptBase = NinjaScriptBase;
      
      			
      			 Value = new Series<double>(NinjaScriptBase);
      		}
      		
      		
      		public Series<double> TickTock(int Period)
      		{
      			
      			
      			try
      			{
      			this.Period = Period;
      			constant1 = 2.0 / (1 + Period);
      			constant2 = 1 - (2.0 / (1 + Period));
      			
      			Value[0] = (NinjaScriptBase.CurrentBar == 0 ? NinjaScriptBase.Input[0] : NinjaScriptBase.Input[0] * constant1 + constant2 * Value[1]);
      				
      			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;
      			}				
      		}
      	}
      }
      Last edited by KhaosTrader; 12-06-2016, 09:59 AM.

      Comment


        #4
        I did some more testing and it seems like the above function does indeed work, thank you for your assistance on this..

        Comment


          #5
          I am glad you were able to determine what happened. I am pasting my original unedited reply for the benefit of other users. Please let us know if there are any other ways we can help.

          I am happy to help set up a test which will let us know exactly what is happening and why. Before we can do so, we must ensure your code is being executed at all. If you have not already, please follow these instructions to load your code into the Visual Studio editor, so we may have breakpoints available.



          Once we have done so, please add a breakpoint to your TickTock method on the line which sets this.Period, to ensure this section is being executed. Please also add a breakpoint to your constructor where it sets NinjaScriptBase .

          If you are unable to reach either debug point, for the time being, please use the add-on namespace rather than your own namespace. I would recommend copying your code body into a new add-on you create through the NinjaScript editor, with add-on template code, to change namespaces. If you are still unable to reach an execution point, you will need to experiment further before I can assist through the forums beyond answering basic questions about Ninja.

          Once you are able to ensure you are reaching your code, please give us a complete scenario we can use to test with. For example, to test the line where Value[0] is set, we would need to pick a known Input[0], constant1, constant2, and Value[1] value. It is preferable to set these both directly and at the highest level possible, to determine if we need to write more tests. In other words, if you know of a circumstance that would set Input[0] for us, we should specify that circumstance as well. Finally, we need an expected value for Value[0].

          With this information, we should definitely be able to get your add-on working properly. I look forward to assisting further.
          Jessica P.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by llanqui, Today, 03:53 AM
          0 responses
          2 views
          0 likes
          Last Post llanqui
          by llanqui
           
          Started by burtoninlondon, Today, 12:38 AM
          0 responses
          10 views
          0 likes
          Last Post burtoninlondon  
          Started by AaronKoRn, Yesterday, 09:49 PM
          0 responses
          14 views
          0 likes
          Last Post AaronKoRn  
          Started by carnitron, Yesterday, 08:42 PM
          0 responses
          11 views
          0 likes
          Last Post carnitron  
          Started by strategist007, Yesterday, 07:51 PM
          0 responses
          14 views
          0 likes
          Last Post strategist007  
          Working...
          X