Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Hi..I am trying to write my first Ninja strategy..a simple SMA cross one ...but cant get it to work. Is there anyone who can point me to some site/forum where I can learn the basics.
What I have so far.....
#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>
/// Test SMA 5,13
/// </summary>
[Description("Test SMA 5,13")]
public class MyCustomStrategy1 : Strategy
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// 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 a 5 minute Bars object to the strategy
Add(PeriodType.Minute, 5);
// Add a 15 minute Bars object to the strategy
Add(PeriodType.Minute, 15);
// Note: Bars are added to the BarsArray and can be accessed via an index value
// E.G. BarsArray[1] ---> Accesses the 5 minute Bars object added above
// Add simple moving averages to the chart for display
// This only displays the SMA's for the primary Bars object on the chart
Add(SMA(5));
Add(SMA(13));
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// OnBarUpdate() will be called on incoming tick events on all Bars objects added to the strategy
// We only want to process events on our primary Bars object (index = 0) which is set when adding
// the strategy to a chart
if (BarsInProgress != 0)
return;
// Checks if the 5 period SMA is above the 50 period SMA on both the 5 and 15 minute time frames
if (SMA(BarsArray[1], 5)[0] > SMA(BarsArray[1], 13)[0] && SMA(BarsArray[2], 5)[0] > SMA(BarsArray[2], 13)[0])
{
// Checks for a cross above condition of the 5 and 50 period SMA on the primary Bars object and enters long
if (CrossAbove(SMA(5), SMA(13), 1))
EnterLong(1000, "SMA");
SendMail("NinjaTest", "[email protected]", "SMA Test", "SMA Test");
}
// Checks for a cross below condition of the 5 and 15 period SMA on the 15 minute time frame and exits long
if (CrossBelow(SMA(BarsArray[2], 5), SMA(BarsArray[2], 13), 1))
ExitLong(1000);
SendMail("NinjaTest", "[email protected]", "SMA Test", "SMA Test");
#region Properties
#endregion
}
}}
Can you help answer these questions from other members on NexusFi?
Firstly, make sure you have at lease .NET 2.0 or greater installed.
MS Kind of has had fun with this one, but first thing you are missing under declarations is
using syste.web.mail
Then in you strategy where you wish to send an e-mail you need this code
System.Web.Mail.SmtpMail.SmtpServer="SMTP Server Address";
System.Web.Mail.SmtpMail.Send(myMessage);
}
And then at the places where you wish to call this just call this method, i did not put try and catch into this, but as you Working with the system resources would be recommended to place some error handling.
#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>
/// Test SMA 5,13
/// </summary>
[Description("Test SMA 5,13")]
public class MyCustomStrategy1 : Strategy
{
#region Variables
// Wizard generated variables
private int myInput0 = 1; // Default setting for MyInput0
// 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 a 5 minute Bars object to the strategy
Add(PeriodType.Minute, 5);
// Add a 15 minute Bars object to the strategy
Add(PeriodType.Minute, 15);
// Note: Bars are added to the BarsArray and can be accessed via an index value
// E.G. BarsArray[1] ---> Accesses the 5 minute Bars object added above
// Add simple moving averages to the chart for display
// This only displays the SMA's for the primary Bars object on the chart
Add(SMA(5));
Add(SMA(13));
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// OnBarUpdate() will be called on incoming tick events on all Bars objects added to the strategy
// We only want to process events on our primary Bars object (index = 0) which is set when adding
// the strategy to a chart
if (BarsInProgress != 0) return;
bool msgSent = false;
// Checks if the 5 period SMA is above the 50 period SMA on both the 5 and 15 minute time frames
if (SMA(BarsArray[1], 5)[0] > SMA(BarsArray[1], 13)[0] && SMA(BarsArray[2], 5)[0] > SMA(BarsArray[2], 13)[0]) {
// Checks for a cross above condition of the 5 and 50 period SMA on the primary Bars object and enters long
if (CrossAbove(SMA(5), SMA(13), 1)) EnterLong(1000, "SMA");
//SendMail("NinjaTest", "[email protected]", "SMA Test", "SMA Test");
msgSent = SendMeAMessage();
}
// Checks for a cross below condition of the 5 and 15 period SMA on the 15 minute time frame and exits long
if (CrossBelow(SMA(BarsArray[2], 5), SMA(BarsArray[2], 13), 1)) {
ExitLong(1000);
//SendMail("NinjaTest", "[email protected]", "SMA Test", "SMA Test");
msgSent = SendMeAMessage();
}
}
Get error message
The type or namespace name 'system' could not be found (are you missing a using directive or an assembly reference?) CS0246 - click for info 8 7