NexusFi: Find Your Edge


Home Menu

 





Convert custom Thinkscript into Ninjascript


Discussion in NinjaTrader

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




Closed Thread
 
Search this Thread

Convert custom Thinkscript into Ninjascript

  #1 (permalink)
 JBottum 
Omaha + Nebraska/USA
 
Experience: Intermediate
Platform: TOS
Trading: ES
Posts: 8 since Jul 2018
Thanks Given: 1
Thanks Received: 6

Hello.

I'm fluent in Thinkscript but am still learning ninjascript.

I would like an indicator that I've built in ToS to be converted to Ninjatrader.
I'll post the thinkscript below. It's an oscillator that gets it's data off of 5 different indicators.

The end goal would be an indicator that can be plotted onto the chart and can be built into strategy builder to test for trading.

It's a trend following indicator where you would go long when the 21 ema of the oscillator is going up for two consecutive bars. Then go short when the 21 ema of the oscillator is going down for two consecutive bars.

Labels aren't necessary in the conversion.

Thanks for your help.

Justin Bottum

Thinkscript code:

#Indicator Name Current_State

declare lower;

plot zeroline = 0;

def ema1 = ExpAverage(length = 3, data = close);
def ema2 = ExpAverage(length = 8, data = close);

def location = if if ema1 < ema2 then close > ema1 else close > ema2 then 1 else -1;

DefineGlobalColor("Bullish", Color.GREEN);
DefineGlobalColor("Sideways", Color.YELLOW);
DefineGlobalColor("Bearish", Color.RED);

def EMA34High = ExpAverage (high, 34); #was ema1_34
def EMA34Low = ExpAverage (low, 34); # was ema3_34

def EMA8 = ExpAverage(close, 8); #JT Changed var name from EMA1 to EMA8

def EMA21 = ExpAverage(close, 21); #JT Changed var name from EMA2 to EMA21
def EMA13 = ExpAverage(close, 13);

def bullish = (EMA8 > EMA13) and (EMA13 > EMA21);
def bearish = (EMA8 < EMA13) and (EMA13 < EMA21);

def WaveState = {default SW, Bullish, Bearish}; #SW = sideways
WaveState = if (EMA8 > EMA13) and (EMA13 > EMA21) and (EMA21 > EMA34High)
then WaveState.Bullish
else if (EMA8 < EMA13) and (EMA13 < EMA21) and (EMA21 < EMA34Low)
then WaveState.Bearish
else WaveState.SW;

def IsBullishWave = WaveState == WaveState.Bullish;
def IsBearishWave = WaveState == WaveState.Bearish;
def IsSidewaysWave = WaveState == WaveState.SW;

def bullbear = if bullish then 1 else if bearish then -1 else 0;

def wave = if IsBullishWave then 1 else if IsBearishWave then -1 else 0;
def JTmulti = if IsNaN(bullbear) then 0 else bullbear + if IsNaN(wave) then 0 else wave;


def momentum = TTM_Squeeze();
def zero = if momentum > 0 then 1 else -1;


def BolKelDelta_Low = reference BollingerBands("num_dev_up" = 2, "length" = 20 )."upperband" - KeltnerChannels("factor" = 2, "length" = 20)."Upper_Band";
def BolKelDeltaMIDLPlot = BolKelDelta_Low;

def volexpanding = if BolKelDeltaMIDLPlot > BolKelDeltaMIDLPlot[1] then 1 else 0;
#def momorising = if momentum > momentum[1] then 1 else 0;
def direction = if momentum > momentum[1] then 1 + volexpanding else if momentum < momentum[1] then -1 - volexpanding else 0;

#MajorStoch
def ProLength = 28;
def k3v = Max(-100, Min(100, (StochasticFull(KPeriod = 17, slowing_period = 5, averageType = AverageType.EXPONENTIAL))) - 50) / 50.01;

rec f3 = CompoundValue(1, if IsNaN(0.5 * (Log((1 + k3v) / (1 - k3v)) + f3[1])) then f3[1]
else 0.5 * (Log((1 + k3v) / (1 - k3v)) + f3[1]), 0);


def Major = if IsNaN(close) then Double.NaN else f3;

def majordir = if Major > Major[1] then 1 else -1;

#adx

def DMIlength = 14;
def ADXThreshold = 20;
def ADX = DMI(length = DMIlength)."ADX";
def DIm = DMI(length = DMIlength)."DI-";
def DIp = DMI(length = DMIlength)."DI+";
def long = DIp > DIm and ADX > ADXThreshold;
def short = DIm > DIp and ADX > ADXThreshold;

def volumeAveragingLength = 20;
def volumePercentThreshold = 50;
def showVolumeStrengthOnYellow = No;
def aVol = Average(volume, volumeAveragingLength);
def pVol = 100 * ((volume - aVol[1]) / aVol[1]);
def pDot = pVol >= volumePercentThreshold;
def volumeStrength = if pDot and (long or short or showVolumeStrengthOnYellow) then hl2 else Double.NaN;

def scan = if long then 1 + if pDot then 1 else 0 else if short then -1 + if pDot then -1 else 0 else 0;

plot currentstate = location + JTmulti + direction + majordir + scan;
plot ema3p = ExpAverage(currentstate, 21);

ema3p.AssignValueColor(if ema3p > ema3p[1] then Color.GREEN else if ema3p < ema3p[1] then Color.RED else Color.GRAY);
currentstate.AssignValueColor(if currentstate > currentstate[1] then Color.GREEN else if currentstate < currentstate[1] then Color.RED else Color.GRAY);

AssignPriceColor(if ema3p > ema3p[1] then Color.GREEN else if ema3p < ema3p[1] then Color.RED else Color.GRAY);

AddLabel(1,"38:"+location,Color.WHITE);
AddLabel(1,"JT:"+JTMulti,Color.WHITE);
AddLabel(1,"Mo:"+direction,Color.WHITE);
AddLabel(1,"Stoch:"+majordir,Color.WHITE);
AddLabel(1,"adx:"+scan,Color.WHITE);

Started this thread

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
Futures True Range Report
The Elite Circle
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
38 thanks
NexusFi site changelog and issues/problem reporting
26 thanks
GFIs1 1 DAX trade per day journal
19 thanks
The Program
18 thanks
  #2 (permalink)
 
cory's Avatar
 cory 
virginia
 
Experience: Intermediate
Platform: ninja
Trading: NQ
Posts: 6,098 since Jun 2009
Thanks Given: 877
Thanks Received: 8,090


JBottum View Post
Hello.

I'm fluent in Thinkscript but am still learning ninjascript.

;

check out my code, WaveTrend.cs
I convert it from pine script, it calls other indicators, basically, you specify other indicators in the beginning then just refer to them later.
Also check out download section here and on ninja https://ninjatraderecosystem.com/user-app-share/ there should be some codes you can use.

Thanked by:
  #3 (permalink)
 JBottum 
Omaha + Nebraska/USA
 
Experience: Intermediate
Platform: TOS
Trading: ES
Posts: 8 since Jul 2018
Thanks Given: 1
Thanks Received: 6



cory View Post
check out my code, WaveTrend.cs
I convert it from pine script, it calls other indicators, basically, you specify other indicators in the beginning then just refer to them later.
Also check out download section here and on ninja https://ninjatraderecosystem.com/user-app-share/ there should be some codes you can use.

Thanks, I also posted in that string for more visibility. Any websites or guides you used to become more fluent in Ninjascript?

Started this thread
  #4 (permalink)
 
bobwest's Avatar
 bobwest 
Western Florida
Site Moderator
 
Experience: Advanced
Platform: Sierra Chart
Trading: ES, YM
Frequency: Several times daily
Duration: Minutes
Posts: 8,162 since Jan 2013
Thanks Given: 57,341
Thanks Received: 26,267


JBottum View Post
Thanks, I also posted in that string for more visibility. Any websites or guides you used to become more fluent in Ninjascript?

Hi @JBottum, glad you saw that thread. I was going to suggest it.

As to NinjaScript, you'll need a little acquaintance with C#, and then just with the particular ways that NT allows you to call the NT functionality. The C# language is not a big deal to get used to, although you may need to learn about how it uses classes, which I don't think Thinkscript uses (I could be wrong about that; it's been a long time since I did anything in it.)

Your best bet may be going though the Help topics, as well as just opening up the code for particular indicators and reading through them. Source code is included for all the built-NT indicators. Eventually it should start to make sense for you. You can post questions in the forum, including in the free indicator thread, and there will be people who can help out.

I'm going to close this thread as a duplicate because we try to keep the forum fairly organized, and the better place for your indicator conversion question is in the thread that @cory directed you to, the free NT indicator thread.

Good luck in your learning, and I hope you find what you need.

Bob.

When one door closes, another opens.
-- Cervantes, Don Quixote

Closed Thread



Last Updated on September 12, 2021


© 2024 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 - Privacy Policy - Downloads - Top
no new posts