Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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>
- /// A buy-and-hold strategy that buys a constant amount at a given frequency and when MACD exceeds its EMA by a threshhold
- /// </summary>
- [Description("A buy-and-hold strategy that buys a constant amount at a given frequency and when MACD exceeds its EMA by a threshhold")]
- public class CalendarAndMACDCrossOver : Strategy
- {
- #region Variables
- // Wizard generated variables
- private double inflationRate = Math.Exp(Math.Log(1.03)/52)-1; // Inflation rate for calendarEntryInterval time periods
- private int calendarEntryInterval = 1; // Invest every calendarEntryInterval time periods
- private double calendarEntryQuantity = 68; // Amount of new funds every calendarEntryInterval time periods
- private int mACDFast = 12; // Length of the fast moving average for MACD
- private int mACDSlow = 26; // Length of the slow moving average for MACD
- private int mACDSmooth = 9; // Length of the exponential moving average for MACD
- private double mACDThreshhold = 0; // Entry threshold; i.e., buy signal when EMA(MACD(MACDFast,MACDSlow),MACDSmooth) cross above MACD(MACDFast,MACDSlow)
- private double mACDEntryQuantity = 0; // Amount of new funds for MACD cross-over signals
- // User defined variables (add any user defined variables below)
- private int quantity,nTradesCalendar,nTradesMACD,nSharesCalendar,nSharesMACD;
- private double totalInvestmentCalendar,totalInvestmentMACD;
- #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;
- IncludeCommission = false;
- BarsRequired = 0;
- EntriesPerDirection = 1000000;
- ExitOnClose = false;
- }
- /// <summary>
- /// Set any variables or logic you wish to do only once at start of your indicator/strategy
- /// </summary>
- protected override void OnStartUp()
- {
- nTradesCalendar = 0;
- nTradesMACD = 0;
- nSharesCalendar = 0;
- nSharesMACD = 0;
- totalInvestmentCalendar = 0;
- totalInvestmentMACD = 0;
- CalendarEntryQuantity /= (1+InflationRate);
- MACDEntryQuantity /= (1+InflationRate);
- }
- /// <summary>
- /// Called on each bar update event (incoming tick)
- /// </summary>
- protected override void OnBarUpdate()
- {
- if (CurrentBar%CalendarEntryInterval == 0)
- {
- CalendarEntryQuantity *= (1+InflationRate);
- MACDEntryQuantity *= (1+InflationRate);
- quantity = (int)(CalendarEntryQuantity/Close[0]);
- if (quantity > 0) {
- EnterLong(quantity, "Calendar Entry");
- }
- }
- if (CrossAbove(MACD(MACDFast, MACDSlow, MACDSmooth).Diff, MACDThreshhold, 1))
- {
- quantity = (int)(MACDEntryQuantity/Close[0]);
- if (quantity > 0) {
- EnterLong(quantity, "MACD Entry");
- }
- }
- }
- /// <summary>
- /// Generic execution logic not specific to a particular IOrder object
- /// </summary>
- protected override void OnExecution(IExecution execution)
- {
- // Remember to check the underlying IOrder object for null before trying to access its properties
- if (execution.Order != null && execution.Order.OrderState == OrderState.Filled) {
- if (execution.Name == "Calendar Entry") {
- nTradesCalendar ++;
- nSharesCalendar += execution.Quantity;
- totalInvestmentCalendar += execution.Quantity*execution.Price + execution.Commission;
- }
- else if (execution.Name == "MACD Entry") {
- nTradesMACD ++;
- nSharesMACD += execution.Quantity;
- totalInvestmentMACD += execution.Quantity*execution.Price + execution.Commission;
- }
- }
- }
- /// <summary>
- /// Clean up your resources here
- /// </summary>
- protected override void OnTermination()
- {
- Print("\r\n====Summary: CalendarAndMACDCrossOver====\r\nCalendar: \t$" + totalInvestmentCalendar.ToString()+", \t"+nTradesCalendar.ToString()+" trades, \t"+nSharesCalendar.ToString()+" shares;\r\nMACD: \t\t$" + totalInvestmentMACD.ToString()+", \t"+nTradesMACD.ToString()+" trades, \t"+nSharesMACD.ToString()+" shares.\r\n");
- }
- #region Properties
- [Description("")]
- [GridCategory("Parameters")]
- public double InflationRate
- {
- get { return inflationRate; }
- set { inflationRate = value; }
- }
- [Description("")]
- [GridCategory("Parameters")]
- public int CalendarEntryInterval
- {
- get { return calendarEntryInterval; }
- set { calendarEntryInterval = Math.Max(1, value); }
- }
- [Description("")]
- [GridCategory("Parameters")]
- public double CalendarEntryQuantity
- {
- get { return calendarEntryQuantity; }
- set { calendarEntryQuantity = Math.Max(0, value); }
- }
- [Description("")]
- [GridCategory("Parameters")]
- public int MACDFast
- {
- get { return mACDFast; }
- set { mACDFast = Math.Max(1, value); }
- }
- [Description("")]
- [GridCategory("Parameters")]
- public int MACDSlow
- {
- get { return mACDSlow; }
- set { mACDSlow = Math.Max(2, value); }
- }
- [Description("")]
- [GridCategory("Parameters")]
- public int MACDSmooth
- {
- get { return mACDSmooth; }
- set { mACDSmooth = Math.Max(1, value); }
- }
- [Description("")]
- [GridCategory("Parameters")]
- public double MACDThreshhold
- {
- get { return mACDThreshhold; }
- set { mACDThreshhold = Math.Max(-1000, value); }
- }
- [Description("")]
- [GridCategory("Parameters")]
- public double MACDEntryQuantity
- {
- get { return mACDEntryQuantity; }
- set { mACDEntryQuantity = Math.Max(0, value); }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement