#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 dpMACD : Strategy
{
#region Variables
// Wizard generated variables
// User defined variables (add any user defined variables below)
private int fastlength = 12;
private int slowlength = 26;
private int smooth = 9;
private double stopamount = 3;
private double profittarget = 2;
private int maxdailylosses = 2;
private int maxdailytrades = 6;
private double starttime = 93000;
private double stoptime = 160000;
private bool tradingtime = false;
private DataSeries macd = null;
private DataSeries macdavg = null;
private int tradestoday = 0;
private int lossestoday = 0;
private IOrder StopOrder = null;
#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()
{
CalculateOnBarClose = true;
macd = new DataSeries(this);
macdavg = new DataSeries(this);
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
macd.Set(MACD(fastlength,slowlength,smooth)[0]);
macdavg.Set(MACD(fastlength,slowlength,smooth).Avg[0]);
// check for new day
if (ToDay(Time[0])!=ToDay(Time[1]))
{
tradestoday = 0;
lossestoday = 0;
}
tradingtime=ToTime(Time[0])>starttime && ToTime(Time[0])<stoptime;
if (tradingtime)
{
// entries
if (Positions[0].MarketPosition==MarketPosition.Flat && tradestoday<maxdailytrades && lossestoday<maxdailylosses)
{
if (macd[0]>macdavg[0] && macd[1]<=macdavg[1])//(CrossAbove(macd,macdavg,1))
{
EnterLong();
tradestoday++;
}
if (macd[0]<macdavg[0] && macd[1]>=macdavg[1])//(CrossBelow(macd,macdavg,1))
{
EnterShort();
tradestoday++;
}
}
// long trade risk management
if (Positions[0].MarketPosition==MarketPosition.Long)
{
// stop order
double stopprice = Positions[0].AvgPrice-stopamount;
if (Close[0]>stopprice)
StopOrder=ExitLongStop(stopprice);
else
StopOrder=ExitLong();
// profit order
double profitprice = Positions[0].AvgPrice+profittarget;
ExitLongLimit(profitprice);
}
// short trade risk management
if (Positions[0].MarketPosition==MarketPosition.Short)
{
// stop order
double stopprice = Positions[0].AvgPrice+stopamount;
if (Close[0]<stopprice)
StopOrder=ExitShortStop(stopprice);
else
StopOrder=ExitShort();
// profit order
double profitprice = Positions[0].AvgPrice-profittarget;
ExitShortLimit(profitprice);
}
}
else
{
if (Positions[0].MarketPosition==MarketPosition.Long)
ExitLong();
if (Positions[0].MarketPosition==MarketPosition.Short)
ExitShort();
}
}
/// <summary>
/// Called on each incoming execution
/// </summary>
protected override void OnExecution(IExecution execution)
{
if (StopOrder!=null && execution.Order.Token==StopOrder.Token)
lossestoday++;
}
#region Properties
[Description("MACD Fast Length")]
[Category("Parameters")]
public int FastLength
{
get { return fastlength; }
set { fastlength = value; }
}
[Description("MACD Slow Length")]
[Category("Parameters")]
public int SlowLength
{
get { return slowlength; }
set { slowlength = value; }
}
[Description("MACD Smooth Length")]
[Category("Parameters")]
public int Smooth
{
get { return smooth; }
set { smooth = value; }
}
[Description("Stop Loss Amount (Points)")]
[Category("Parameters")]
public double StopAmount
{
get { return stopamount; }
set { stopamount = value; }
}
[Description("Profit Target Amount (Points)")]
[Category("Parameters")]
public double ProfitTarget
{
get { return profittarget; }
set { profittarget = value; }
}
[Description("Maximum daily losses")]
[Category("Parameters")]
public int MaxDailyLosses
{
get { return maxdailylosses; }
set { maxdailylosses = value; }
}
[Description("Maximum daily trades")]
[Category("Parameters")]
public int MaxDailyTrades
{
get { return maxdailytrades; }
set { maxdailytrades = value; }
}
[Description("Time to start trading")]
[Category("Parameters")]
public double StartTime
{
get { return starttime; }
set { starttime = value; }
}
[Description("Time to stop trading")]
[Category("Parameters")]
public double StopTime
{
get { return stoptime; }
set { stoptime = value; }
}
#endregion
}
}

Comment