Advertisement
Guest User

CalendarAndMACDCrossOver.cs

a guest
Sep 16th, 2017
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.49 KB | None | 0 0
  1. #region Using declarations
  2. using System;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Xml.Serialization;
  8. using NinjaTrader.Cbi;
  9. using NinjaTrader.Data;
  10. using NinjaTrader.Indicator;
  11. using NinjaTrader.Gui.Chart;
  12. using NinjaTrader.Strategy;
  13. #endregion
  14.  
  15. // This namespace holds all strategies and is required. Do not change it.
  16. namespace NinjaTrader.Strategy
  17. {
  18.     /// <summary>
  19.     /// A buy-and-hold strategy that buys a constant amount at a given frequency and when MACD exceeds its EMA by a threshhold
  20.     /// </summary>
  21.     [Description("A buy-and-hold strategy that buys a constant amount at a given frequency and when MACD exceeds its EMA by a threshhold")]
  22.     public class CalendarAndMACDCrossOver : Strategy
  23.     {
  24.         #region Variables
  25.         // Wizard generated variables
  26.         private double inflationRate = Math.Exp(Math.Log(1.03)/52)-1; // Inflation rate for calendarEntryInterval time periods
  27.         private int calendarEntryInterval = 1; // Invest every calendarEntryInterval time periods
  28.         private double calendarEntryQuantity = 68; // Amount of new funds every calendarEntryInterval time periods
  29.         private int mACDFast = 12; // Length of the fast moving average for MACD
  30.         private int mACDSlow = 26; // Length of the slow moving average for MACD
  31.         private int mACDSmooth = 9; // Length of the exponential moving average for MACD
  32.         private double mACDThreshhold = 0; // Entry threshold; i.e., buy signal when EMA(MACD(MACDFast,MACDSlow),MACDSmooth) cross above MACD(MACDFast,MACDSlow)
  33.         private double mACDEntryQuantity = 0; // Amount of new funds for MACD cross-over signals
  34.         // User defined variables (add any user defined variables below)
  35.         private int quantity,nTradesCalendar,nTradesMACD,nSharesCalendar,nSharesMACD;
  36.         private double totalInvestmentCalendar,totalInvestmentMACD;
  37.         #endregion
  38.  
  39.         /// <summary>
  40.         /// This method is used to configure the strategy and is called once before any strategy method is called.
  41.         /// </summary>
  42.         protected override void Initialize()
  43.         {
  44.             CalculateOnBarClose = true;
  45.             IncludeCommission = false;
  46.             BarsRequired = 0;
  47.             EntriesPerDirection = 1000000;
  48.             ExitOnClose = false;
  49.         }
  50.  
  51.         /// <summary>
  52.         /// Set any variables or logic you wish to do only once at start of your indicator/strategy
  53.         /// </summary>
  54.         protected override void OnStartUp()
  55.         {
  56.             nTradesCalendar = 0;
  57.             nTradesMACD = 0;
  58.             nSharesCalendar = 0;
  59.             nSharesMACD = 0;
  60.             totalInvestmentCalendar = 0;
  61.             totalInvestmentMACD = 0;
  62.             CalendarEntryQuantity /= (1+InflationRate);
  63.             MACDEntryQuantity /= (1+InflationRate);
  64.         }
  65.  
  66.         /// <summary>
  67.         /// Called on each bar update event (incoming tick)
  68.         /// </summary>
  69.         protected override void OnBarUpdate()
  70.         {
  71.             if (CurrentBar%CalendarEntryInterval == 0)
  72.             {
  73.                 CalendarEntryQuantity *= (1+InflationRate);
  74.                 MACDEntryQuantity *= (1+InflationRate);
  75.                 quantity = (int)(CalendarEntryQuantity/Close[0]);
  76.                 if (quantity > 0) {
  77.                     EnterLong(quantity, "Calendar Entry");
  78.                 }
  79.             }
  80.  
  81.             if (CrossAbove(MACD(MACDFast, MACDSlow, MACDSmooth).Diff, MACDThreshhold, 1))
  82.             {
  83.                 quantity = (int)(MACDEntryQuantity/Close[0]);
  84.                 if (quantity > 0) {
  85.                     EnterLong(quantity, "MACD Entry");
  86.                 }
  87.             }
  88.         }
  89.  
  90.         /// <summary>
  91.         /// Generic execution logic not specific to a particular IOrder object
  92.         /// </summary>
  93.         protected override void OnExecution(IExecution execution)
  94.         {
  95.             // Remember to check the underlying IOrder object for null before trying to access its properties
  96.             if (execution.Order != null && execution.Order.OrderState == OrderState.Filled) {
  97.                 if (execution.Name == "Calendar Entry") {
  98.                     nTradesCalendar ++;
  99.                     nSharesCalendar += execution.Quantity;
  100.                     totalInvestmentCalendar += execution.Quantity*execution.Price + execution.Commission;
  101.                 }
  102.                 else if (execution.Name == "MACD Entry") {
  103.                     nTradesMACD ++;
  104.                     nSharesMACD += execution.Quantity;
  105.                     totalInvestmentMACD += execution.Quantity*execution.Price + execution.Commission;
  106.                 }
  107.             }
  108.         }
  109.  
  110.         /// <summary>
  111.         /// Clean up your resources here
  112.         /// </summary>
  113.         protected override void OnTermination()
  114.         {
  115.             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");
  116.         }
  117.  
  118.         #region Properties
  119.         [Description("")]
  120.         [GridCategory("Parameters")]
  121.         public double InflationRate
  122.         {
  123.             get { return inflationRate; }
  124.             set { inflationRate = value; }
  125.         }
  126.  
  127.         [Description("")]
  128.         [GridCategory("Parameters")]
  129.         public int CalendarEntryInterval
  130.         {
  131.             get { return calendarEntryInterval; }
  132.             set { calendarEntryInterval = Math.Max(1, value); }
  133.         }
  134.  
  135.         [Description("")]
  136.         [GridCategory("Parameters")]
  137.         public double CalendarEntryQuantity
  138.         {
  139.             get { return calendarEntryQuantity; }
  140.             set { calendarEntryQuantity = Math.Max(0, value); }
  141.         }
  142.  
  143.         [Description("")]
  144.         [GridCategory("Parameters")]
  145.         public int MACDFast
  146.         {
  147.             get { return mACDFast; }
  148.             set { mACDFast = Math.Max(1, value); }
  149.         }
  150.  
  151.         [Description("")]
  152.         [GridCategory("Parameters")]
  153.         public int MACDSlow
  154.         {
  155.             get { return mACDSlow; }
  156.             set { mACDSlow = Math.Max(2, value); }
  157.         }
  158.  
  159.         [Description("")]
  160.         [GridCategory("Parameters")]
  161.         public int MACDSmooth
  162.         {
  163.             get { return mACDSmooth; }
  164.             set { mACDSmooth = Math.Max(1, value); }
  165.         }
  166.  
  167.         [Description("")]
  168.         [GridCategory("Parameters")]
  169.         public double MACDThreshhold
  170.         {
  171.             get { return mACDThreshhold; }
  172.             set { mACDThreshhold = Math.Max(-1000, value); }
  173.         }
  174.  
  175.         [Description("")]
  176.         [GridCategory("Parameters")]
  177.         public double MACDEntryQuantity
  178.         {
  179.             get { return mACDEntryQuantity; }
  180.             set { mACDEntryQuantity = Math.Max(0, value); }
  181.         }
  182.         #endregion
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement