NexusFi: Find Your Edge


Home Menu

 





Ninja Strategy


Discussion in NinjaTrader

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




 
Search this Thread

Ninja Strategy

  #1 (permalink)
432860Steve
Australia
 
Posts: 2 since May 2011
Thanks Given: 1
Thanks Received: 0

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
}
}}

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
Futures True Range Report
The Elite Circle
NexusFi Journal Challenge - April 2024
Feedback and Announcements
Exit Strategy
NinjaTrader
ZombieSqueeze
Platforms and Indicators
 
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
27 thanks
GFIs1 1 DAX trade per day journal
19 thanks
The Program
18 thanks
  #3 (permalink)
 
sinisa's Avatar
 sinisa 
Melbourne, Australia
 
Experience: Intermediate
Platform: NinjaTrader
Broker: IG Markets
Trading: EUR?USD,AU/USD
Posts: 38 since Apr 2011
Thanks Given: 10
Thanks Received: 42


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.MailMessage myMessage=new System.Web.Mail.MailMessage();

//Provide information for server authetication, as by default MS has assumed that you do not need to authenticate
//to your mail server, funny that

myMessage.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1 );
myMessage.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername","SmtpHostUserName" );
myMessage.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword","SmtpHostPassword" );

myMessage.From="from e-mail";
myMessage.To="to e-mail";
myMessage.Subject="Message Subject";
myMessage.Body="Message Body";

System.Web.Mail.SmtpMail.SmtpServer="SMTP Server Address";
System.Web.Mail.SmtpMail.Send(myMessage);

I would recommend putting above code into seprate method like

bool SendMeAMailNotification () {
System.Web.Mail.MailMessage myMessage=new System.Web.Mail.MailMessage();

//Provide information for server authetication, as by default MS has assumed that you do not need to authenticate
//to your mail server, funny that

myMessage.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1 );
myMessage.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername","SmtpHostUserName" );
myMessage.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword","SmtpHostPassword" );

myMessage.From="from e-mail";
myMessage.To="to e-mail";
myMessage.Subject="Message Subject";
myMessage.Body="Message Body";

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.

Regards

Markets are logical and
Reply With Quote
Thanked by:
  #4 (permalink)
 
sinisa's Avatar
 sinisa 
Melbourne, Australia
 
Experience: Intermediate
Platform: NinjaTrader
Broker: IG Markets
Trading: EUR?USD,AU/USD
Posts: 38 since Apr 2011
Thanks Given: 10
Thanks Received: 42

#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();
}
}

private bool SendMeAMessage() {
try {
System.Web.Mail.MailMessage myMessage=new System.Web.Mail.MailMessage();

//Provide information for server authetication, as by default MS has assumed that you do not need to authenticate
//to your mail server, funny that

myMessage.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate",1 );
myMessage.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendusername","SmtpHostUserName" );
myMessage.Fields.Add( "http://schemas.microsoft.com/cdo/configuration/sendpassword","SmtpHostPassword" );

myMessage.From="from e-mail";
myMessage.To="to e-mail";
myMessage.Subject="Message Subject";
myMessage.Body="Message Body";

System.Web.Mail.SmtpMail.SmtpServer="SMTP Server Address";
System.Web.Mail.SmtpMail.Send(myMessage);

} catch ( Exception ex ) {
//do something when error
return false;
}
return true;


}

Hope it helps

Markets are logical and
Reply With Quote
Thanked by:
  #5 (permalink)
432860Steve
Australia
 
Posts: 2 since May 2011
Thanks Given: 1
Thanks Received: 0

Still having trouble Sinisa.

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

Reply With Quote




Last Updated on May 10, 2011


© 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