• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

The Indicator Club

  • Indicators
    • All Indicators
    • Free Indicators
    • Join the Club!
    • Indicator TImeline
  • Blog
    • All
    • NinjaScript Tutorials
    • Trade Ideas
    • Free Strategies
    • Favorite Posts
  • FAQ
  • My Account
    • Activate License
    • API Keys
    • API Downloads
  • Contact
  • Cart

June 27, 2017 by The Indicator Club Leave a Comment

Declaring Variables with NinjaScript

Brief Introduction

We’ve put together a series of posts to help you in your journey to learn how to program from scratch, or edit existing NinjaTrader indicators and strategies. All of our posts are geared toward the non-programmer, so even though we will present a lot of information, for all of you more technical people out there, we will not go into all the technicalities of C#, NinjaTrader, or NinjaScript. It’s also important to note that even though we show you how to do something in NinjaTrader, that is by no way the only way to do it. We will present what has worked best for us and what will be most easily understood by someone learning how to program. Finally, for the more advance users, we are going to leave things out and over simplify certain parts because we are focused on learning what we need to so we can get started on our NinjaScript journey.

Variables are integral to programming indicators. Think of a variable as a name such as Period that holds a value (or something we want to keep track of and reference later), such as 8 (the value does not have to be a number). The value could also be a string such as Variable, it could contain a decimal 8.5354, it could be True, or it could even be Null (there are more options, but let’s stick with the basics). Variables help us easily reference a number, or calculation and they come in all different varieties.

Common Data Types

When we create (declare) a variable, we can declare that variable as one of many different data types. I will present a few of the most common and as we progress, I will introduce more through the use of examples (even though the following list looks intimidating, the most common Data Types we use are int, double, and string):

Table Source: https://msdn.microsoft.com/en-us/library/ms228360(v=vs.90).aspx

Short Name .NET Class Type Width Range (bits)
byte Byte Unsigned integer 8 0 to 255
sbyte SByte Signed integer 8 -128 to 127
int Int32 Signed integer 32 -2,147,483,648 to 2,147,483,647
uint UInt32 Unsigned integer 32 0 to 4294967295
short Int16 Signed integer 16 -32,768 to 32,767
ushort UInt16 Unsigned integer 16 0 to 65535
long Int64 Signed integer 64 -9223372036854775808 to 9223372036854775807
ulong UInt64 Unsigned integer 64 0 to 18446744073709551615
float Single Single-precision floating point type 32 -3.402823e38 to 3.402823e38
double Double Double-precision floating point type 64 -1.79769313486232e308 to 1.79769313486232e308
char Char A single Unicode character 16 Unicode symbols used in text
bool Boolean Logical Boolean type 8 True or false
object Object Base type of all other types
string String A sequence of characters
decimal Decimal Precise fractional or integral type that can represent decimal numbers with 29 significant digits 128 ±1.0 × 10e−28 to ±7.9 × 10e28

Variable Examples

I get it, my last table didn’t make that much sense. I only included it as reference for later, but the best way to demonstrate variables is through an example, but before we do that, we need to open a NinjaScript Output window. Go to your Control Center and navigate to New -> NinjaScript Output. A new window should appear that looks like this:

When we edit the indicator from Part 1, we will Print text that will appear in the NinjaScript Output window. So, let’s look at some examples of how to declare variables:

Before you add the above to a chart and see what happens in the NinjaScript Output window, you need to save and compile (see Part 1). After your indicator compiles (sound will play and no errors appear) and you load the indicator on a chart, you should see this in your NinjaScript Output window:

It’s As Simple As That

Declaring variables is as simple as that. Now it’s time to checkout Part 3 – If Statements.

Filed Under: Tutorial Tagged With: Learn, NinjaScript

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Top Shelf Trader October 6, 2017
  • Use Any Indicator with Our Frameworks: Universal Inputs September 21, 2017
  • NinjaTrader Training: Part 10: Coloring Plots and Backgrounds September 1, 2017
  • NinjaTrader Training: Part 9 – AddPlot and Plot Variables August 31, 2017
  • NinjaTrader Training: Part 8 – Referencing Indicators August 30, 2017

Post Tags

Advance Internals Automated Backtest Day Trading Detrend Forex Trading Free Futures Trading Learn Moving Average MTF NinjaScript NinjaTrader 7 NInjaTrader 8 Oscillator Pivot Points RSI Stock Trading Strategy Vix Trend Volume

Footer

WHO WE ARE

The Indicator Club is made up of normal traders and professional software developers who want to share their indicators for a fraction of the cost it would take to code just one!

The Indicator Club

Purchase & Support

To get access to all of our current and future indicators and strategies, check out our Complete Package.

If you need support please email [email protected], or head over to our contact page.

New Releases

  • NinjaTrader 8 Osc Universal Input Osc Universal Input $497.00
  • NinjaTrader 8 MA Universal Input MA Universal Input $497.00
  • NinjaTrader 8 Bar Orders Bar Orders $297.00
  • NinjaTrader 8 Tick Offset Tick Offset $0.00

Recent Posts

  • Top Shelf Trader
  • Use Any Indicator with Our Frameworks: Universal Inputs
  • NinjaTrader Training: Part 10: Coloring Plots and Backgrounds
  • NinjaTrader Training: Part 9 – AddPlot and Plot Variables

Copyright ©2020 | All Rights Reserved | By Using This Site you Agree to our Terms and Conditions, Risk Disclosures, and Software Agreement

NinjaTrader Disclosure: NinjaTrader® is a registered trademark of NinjaTrader Group, LLC. No NinjaTrader company has any affiliation with the owner, developer, or provider of the products or services described herein, or any interest, ownership or otherwise, in any such product or service, or endorses, recommends or approves any such product or service.

RISK DISCLOSURE: Futures and forex trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or life style. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results.

HYPOTHETICAL PERFORMANCE DISCLAIMER: HYPOTHETICAL PERFORMANCE RESULTS HAVE MANY INHERENT LIMITATIONS, SOME OF WHICH ARE DESCRIBED BELOW. NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT WILL OR IS LIKELY TO ACHIEVE PROFITS OR LOSSES SIMILAR TO THOSE SHOWN; IN FACT, THERE ARE FREQUENTLY SHARP DIFFERENCES BETWEEN HYPOTHETICAL PERFORMANCE RESULTS AND THE ACTUAL RESULTS SUBSEQUENTLY ACHIEVED BY ANY PARTICULAR TRADING PROGRAM. ONE OF THE LIMITATIONS OF HYPOTHETICAL PERFORMANCE RESULTS IS THAT THEY ARE GENERALLY PREPARED WITH THE BENEFIT OF HINDSIGHT. IN ADDITION, HYPOTHETICAL TRADING DOES NOT INVOLVE FINANCIAL RISK, AND NO HYPOTHETICAL TRADING RECORD CAN COMPLETELY ACCOUNT FOR THE IMPACT OF FINANCIAL RISK OF ACTUAL TRADING. FOR EXAMPLE, THE ABILITY TO WITHSTAND LOSSES OR TO ADHERE TO A PARTICULAR TRADING PROGRAM IN SPITE OF TRADING LOSSES ARE MATERIAL POINTS WHICH CAN ALSO ADVERSELY AFFECT ACTUAL TRADING RESULTS. THERE ARE NUMEROUS OTHER FACTORS RELATED TO THE MARKETS IN GENERAL OR TO THE IMPLEMENTATION OF ANY SPECIFIC TRADING PROGRAM WHICH CANNOT BE FULLY ACCOUNTED FOR IN THE PREPARATION OF HYPOTHETICAL PERFORMANCE RESULTS AND ALL WHICH CAN ADVERSELY AFFECT TRADING RESULTS.