Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
strategy with world market data
Collapse
X
-
strategy with world market data
I am new to ninjascript and what I am trying to do is create a strategy where I go long if certain world markets are up and go short if certain world markets are down. But I only want to go long/short on stocks that have just crossed over their 9 and 18 period EMA's. I believe I have to add the world market data series to initialize. And then in onbarupdate state that the close > close of the day before for each world index. I have already added the world index data that I want into instrument manager, but I am having trouble developing the strategy to actually use the index's data. How would I setup this strategy for the world market data. Do I need to add multiple entry signals, or sync data series bars to the primary object, or do I need use barsinprogress to achieve what I want to do?Tags: None
-
Hi Wes, of all the options you were thinking of, I would suggest using BarsInProgress to achieve your goals. In addition, you might want to check out the reference page for Closes, the array that holds close prices for all data series. If you used Closes, you might not need to use BarsInProgress.
Its easy to start creating very complicated strategies when learning NinjaScript. Using and comparing values from, say, 5 different world markets at once can be challenging. I'd recommend starting with just two markets and build functionality on from there.AustinNinjaTrader Customer Service
-
I used BarsInProgress but every time I try to compile I get a lot of errors. And the majority of the errors are on parts of the program that obviously work because strategy wizard created those parts.
Here is what i have created
protected override void Initialize()
{
//Add the FTSE index to chart
Add("FTSE", PeriodType.Day, 1);
Add(EMA(Fast));
Add(EMA(Slow));
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
//use barsinprogress to check if the last close is greater than the close on the previous day
//if not return (strategy stops)
if (BarsInProgress == 1)
(
(Close[1][0] < Closes [1}[1])
return;
)
// If the above conditions are met then go long when the indicator conditions are met
else if (BarsInProgress == 0)
(CrossAbove(EMA(Fast), EMA(Slow), 1))
{
EnterLong(DefaultQuantity, "");
}
}
Comment
-
There are a couple of errors in the code. This:
has a few typos, and that code block doesn't really mean anything anyways because two values are just kind of put out there. There isn't an if-statement or anything, so no comparisons are done. Maybe you meant this?Code:if (BarsInProgress == 1) ( (Close[1][0] < Closes [1}[1]) return; )
You'll quickly find C#/NinjaScript are extremely picky about punctuation, spacing, capitalization, etc.Code:if (BarsInProgress == 1) ( if(Closes[1][0] < Closes[1][1]) return; )AustinNinjaTrader Customer Service
Comment
-
Sorry, I took too quick a look at the lower half of your code, but there was an error there too about where the brackets were. The CrossAbove was also missing an if statement. Try this as a complete OnBarUpdate() code block:
It is easiest to keep spacing and everything constant with tabs to ensure brackets and code blocks are opened and closed correctly.Code:protected override void OnBarUpdate() { //use barsinprogress to check if the last close is greater than the close on the previous day //if not return (strategy stops) if (BarsInProgress == 1) ( if(Closes[1][0] < Closes[1][1]) return; ) // If the above conditions are met then go long when the indicator conditions are met else if (BarsInProgress == 0) { if (CrossAbove(EMA(Fast), EMA(Slow), 1)) EnterLong(DefaultQuantity, ""); } }AustinNinjaTrader Customer Service
Comment
-
I made the changes exactly how you told me to, but I still get error messages.
I get these error messages and they have no codes for them:
1. ; expected
2. statement expected
3. )expected
I also get error codes on lines that have no typing on them.
In initialize i get the error: No overload for method 'Add' takes 4 arguments.
In reference to adding the ifs, i get the error message: invalid expression term if.
Here is what my strategy looks like as of right now.
#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>
/// Enter the description of your strategy here
/// </summary>
[Description("Enter the description of your strategy here")]
public class Worldtest2 : Strategy
{
#region Variables
// Wizard generated variables
private int fast = 9; // Default setting for Fast
private int slow = 18; // Default setting for Slow
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
//Add the FTSE index to chart
Add("FTSE", PeriodType.Day, 1);
Add(EMA(Fast));
Add(EMA(Slow));
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
//use barsinprogress to check if the last close is greater than the close on the previous day
//if not return (strategy stops)
if (BarsInProgress == 1)
(
if (Closes[1][0] < Closes[1][1])
return;
)
// If the above conditions are met then go long when the indicator conditions are met
if (BarsInProgress == 0)
(
if (CrossAbove(EMA(Fast), EMA(Slow), 1))
EnterLong(DefaultQuantity, "");
)
}
#region Properties
[Description("FastEMA")]
[Category("Parameters")]
public int Fast
{
get { return fast; }
set { fast = Math.Max(1, value); }
}
[Description("SlowEMA")]
[Category("Parameters")]
public int Slow
{
get { return slow; }
set { slow = Math.Max(1, value); }
}
#endregion
}
}
Comment
-
if-statements use { } NOT ( ).
Correct:
Incorrect:Code:if (some condition) [SIZE=5][COLOR=Red][B]{[/B][/COLOR][/SIZE] some action; [SIZE=5][COLOR=Red][B]}[/B][/COLOR][/SIZE]
Code:if (some condition) [SIZE=5][COLOR=Red][B]([/B][/COLOR][/SIZE] some action; [SIZE=5][COLOR=Red][B])[/B][/COLOR][/SIZE]Josh P.NinjaTrader Customer Service
Comment
-
that helped a lot but still am getting a few more errors. On the particular statement below:
{
//Add the FTSE index to chart
Add("FTSE", PeriodType.Day, 1);
I get the error code cs1501 "No overload for method 'Add' takes '4' arguments
On the line directly below this statement (which is a blank line)
Add(EMA(Fast));
Add(EMA(Slow));
Here ->
I get the error code cs1002 ";expected"
On these lines:
if (Closes[1][0] < Closes[1][1])
return;
i get three error messages: error cs1026 ") expected"
error cs1525 "Invalid expression term '{'
error cs1514 "{ expected"
Comment
-
Wes, I think many of those errors stem from problems with opening and closing brackets and parentheses. Make sure every { has a } and every ( has a )--in the right order as well.
If you'd like you could post the complete code here (in a code block) and I'll take a look, but this is getting close to us debugging for you, which is beyond the level of support we provide. To post code in a code block, wrap the code with [code] and [/*] (replace * with the word code) in your post.AustinNinjaTrader Customer Service
Comment
-
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> /// Enter the description of your strategy here /// </summary> [Description("Enter the description of your strategy here")] public class Worldtest2 : Strategy { #region Variables // Wizard generated variables private int fast = 9; // Default setting for Fast private int slow = 18; // Default setting for Slow // User defined variables (add any user defined variables below) #endregion /// <summary> /// This method is used to configure the strategy and is called once before any strategy method is called. /// </summary> protected override void Initialize() { //Add the FTSE index to chart Add("^FTSE", PeriodType.Day, 1); Add(EMA(Fast)); Add(EMA(Slow)); CalculateOnBarClose = true; } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { //use barsinprogress to check if the last close is greater than the close on the previous day //if not return (strategy stops) if (BarsInProgress == 1) { if (Closes[1][0] < Closes[1][1]) return; } // If the above conditions are met then go long when the indicator conditions are met if (BarsInProgress == 0) { if (CrossAbove(EMA(Fast), EMA(Slow), 1)) EnterLong(DefaultQuantity, ""); } } #region Properties [Description("FastEMA")] [Category("Parameters")] public int Fast { get { return fast; } set { fast = Math.Max(1, value); } } [Description("SlowEMA")] [Category("Parameters")] public int Slow { get { return slow; } set { slow = Math.Max(1, value); } } #endregion } }
Comment
-
Wes, I just copied and pasted the code you posted into a strategy and it compiled fine. I've attached it for reference.
Are you sure the errors are for the strategy you're working on? On the left side of the error console it'll tell you the file name. Make sure it is the correct file. If the error is highlighted with red, the error comes from a different file than the one you're working on.Attached FilesAustinNinjaTrader Customer Service
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
646 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
367 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
108 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
569 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
573 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment