NexusFi: Find Your Edge


Home Menu

 





(Kaufman adaptive moving average + ROC) Strategy Transcode


Discussion in NinjaTrader

Updated
    1. trending_up 3,568 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)
 
Pharmakon's Avatar
 Pharmakon 
Paris France
 
Experience: Beginner
Platform: TradingView
Broker: CQG
Trading: ES1!, CL1!, DY1!
Posts: 45 since Jan 2016
Thanks Given: 119
Thanks Received: 28

Hi everybody !

Firstly, I'd like to give many thanks to everybody working hard to give this community a lot of value. It's very helpful to find almost one post about everything I'm looking to know about trading methodology.

I'm very new to strategy programming but I'd really like to make progress on it. I've only done the BigMike Tutorial. Thank you Big Mike. And, I've understood that I won't be able to progress alone or in a very slow way. So I come here looking to share ideas and help if I can but I don't want to flood the forum. I'm trying to give the most value to my post.

I've found a nice working strategy on TradingView website. It works very well on many securities (see pictures at the bottom of the post) with this platform trading simulator. I give the link to the strategy just to respect sourcing.

https://www.tradingview.com/script/Y49ipujG-Kama-VS-HeikinAshi-Strategy-synapticex/

It's a combination of Kaufman Adaptive Moving Average and ROC.

Source code is here (TradingView Proprietary Language) :

 
Code
//@version=2
//synapticex.com
kamaPeriod = input(8, minval=1) 
ROCLength=input(4, minval=1) 

kama(length)=>
    volatility = sum(abs(close-close[1]), length)
    change = abs(close-close[length-1])
    er = iff(volatility != 0, change/volatility, 0)
    sc = pow((er*(0.666666-0.064516))+0.064516, 2)
    k = nz(k[1])+(sc*(hl2-nz(k[1])))
    
kamaEntry = security(tickerid,period,kama(kamaPeriod))

plot(kamaEntry, color=gray, title="Kama",transp=0, trackprice=false, style=line)
roc = roc(close, ROCLength)

strategy("Kama VS HeikinAshi", overlay=true, pyramiding=0, calc_on_every_tick=true, calc_on_order_fills=true)

buyEntry =  kamaEntry[0]>kamaEntry[1] and roc[1]<0 and roc >0
sellEntry = kamaEntry[0]<kamaEntry[1] and roc[1]<0 and roc <0

buyExit = kamaEntry<kamaEntry[1] or (roc[1]>0 and roc<0)
sellExit =  kamaEntry>kamaEntry[1] or (roc[1]<0 and roc>0)

if (buyEntry)
    strategy.entry("KAMAL", strategy.long, comment="KAMAL")
else
    strategy.close("KAMAL", when=buyExit)

if (sellEntry)
    strategy.entry("KAMAS", strategy.short, comment="KAMAS")
else
    strategy.close("KAMAS", when=sellExit)

I'm trying to transcode it to NinjaTrader to be able to share it to do a small real backtest. But I'm confronted with some issues on coding it. I'm stuck ! I don't how to transcode two of these function :

- nz : Not a Number function (NaN)
- security : which returns another series of the input data given.

I've already started to code it. I hope it will help :

 
Code
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion

// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
    /// <summary>
    /// 20160428 My first money maker strategy based with SMA, EMA, HMA
    /// </summary>
    [Description("20160428 My first money maker strategy based with SMA, EMA, HMA")]
    public class MyMoneyMaker : Strategy
    {
        #region Variables
        // Wizard generated variables
        private int myInput0 = 1; // Default setting for MyInput0
        // User defined variables (add any user defined variables below)
        #endregion
		
		private int		kamaPeriod 		= 8;		//variables en minuscules
		private int		rocLength 		= 4;
		
        /// <summary>
        /// This method is used to configure the strategy and is called once before any strategy method is called.
        /// </summary>
        protected override void Initialize()
        {
            CalculateOnBarClose = true;
			EntryHandling		= EntryHandling.UniqueEntries; //Unique entry if you have multiple longs signal at same time
			
        }		
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
			EntryHandling		= EntryHandling.UniqueEntries;
			
			if (Position.MarketPosition != MarketPosition.Flat) return;
			if (CurrentBar < kamaPeriod) return;
			
			//length = kamaPeriod
			
			double 		volatility 		= 0;
			double 		change			= 0;
			double		er				= 0;
			double		sc				= 0;
			double		k				= 0;
			double		roc				= 0;
			
			for (int barsAgo = 0; barsAgo < kamaPeriod; barsAgo++)
			{
				volatility = volatility + Math.Abs(Close[barsAgo] - Close[barsAgo+1]);
			}
			
			change = Math.Abs(Close[0] - Close[kamaPeriod - 1]);
			
			if (volatility != 0)
				er = change/volatility;
			else
				er = 0;
			
			sc = Math.Pow((er*(0.666666-0.064516))+0.064516, 2);
			
			// left: 
			// security ?
			// k : NaN function ?
			// Plot ?
			
			//ROC
			
			roc = ROC(Close[0],rocLength);
			
			
			// Long/Sell entry orders
			
			
			// exit orders
			
			
			
			
		
        }

        #region Properties
        [Description("")]
        [GridCategory("Parameters")]
        public int KamaPeriod	//Fonction 1ere lettre en Majuscule ?
        {
            get { return kamaPeriod; }
            set { kamaPeriod = Math.Max(1, value); }
        }        
		[Description("")]
        [GridCategory("Parameters")]
		public int RocLength
        {
            get { return rocLength; }
            set { rocLength = Math.Max(1, value); }
        }        
        #endregion
    }
}


 
Code







Regards,
M.


Follow me on X Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Synapse Pro - Trade Copier with topstep and MT5 integrat …
NinjaTrader
SEC and CFTC Unlock Customer Cross-Margining for Treasur …
Treasury Notes and Bonds
Irans Answer Due Today: Peace Surges to 33.5%, Invasion …
Prediction Markets & Event Contracts
Prop Firms Are Banning Gold Trading as Record Prices Mak …
Funded Trading Evaluation Firms
April FOMC Minutes: Most Divided Fed Since 1992 -- Many …
Traders Hideout
 
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 April 29, 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