Advertisement
Guest User

ScaleTrading

a guest
Sep 16th, 2017
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.68 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.     /// Robert Lichello's Automated Investment Management and Braden Glett's Reverse Scale strategies
  20.     /// </summary>
  21.     [Description("Robert Lichello's Automated Investment Management and Braden Glett's Reverse Scale strategies")]
  22.     public class ScaleTrading : Strategy
  23.     {
  24.         #region Variables
  25.         // Wizard generated variables
  26.         //private bool reversed = false; // Set to true to use Reverse Scale, otherwise use AIM
  27.         private double inflationRate = 0.03; // Inflation rate for calendarEntryInterval time periods
  28.         private double interestRate = Math.Exp(Math.Log(1.01)/12)-1; // Interest rate for one time period
  29.         private int calendarEntryInterval = 12; // Invest every calendarEntryInterval time periods
  30.         private double calendarEntryQuantity = 3000; // Amount of new funds every calendarEntryInterval time periods
  31.         private double stockRatio = 0.5; // Ratio: stock / (stock + cash)
  32.         private double buySAFE = 0.1; // Stock Adjustment Factor Equalizer for entry
  33.         private double sellSAFE = 0.1; // Stock Adjustment Factor Equalizer for exit
  34.         private double buyMinQuantity = 10; // Minimum quantity for entry
  35.         private double sellMinQuantity = 10; // Minimum quantity for exit
  36.         // User defined variables (add any user defined variables below)
  37.         private int quantity,nTradesBuy,nTradesSell,nShares;
  38.         private double portfolioControl,totalCash,portfolioValue,cost,totalInvestment;
  39.         #endregion
  40.  
  41.         /// <summary>
  42.         /// This method is used to configure the strategy and is called once before any strategy method is called.
  43.         /// </summary>
  44.         protected override void Initialize()
  45.         {
  46.             CalculateOnBarClose = true;
  47.             IncludeCommission = false;
  48.             BarsRequired = 0;
  49.             EntriesPerDirection = 1000000;
  50.             ExitOnClose = false;
  51.         }
  52.  
  53.         /// <summary>
  54.         /// Set any variables or logic you wish to do only once at start of your indicator/strategy
  55.         /// </summary>
  56.         protected override void OnStartUp()
  57.         {
  58.             nTradesBuy = 0;
  59.             nTradesSell = 0;
  60.             nShares = 0;
  61.             portfolioControl = 0;
  62.             totalCash = 0;
  63.             totalInvestment = 0;
  64.             CalendarEntryQuantity /= (1+InflationRate);
  65.         }
  66.  
  67.         /// <summary>
  68.         /// Called on each bar update event (incoming tick)
  69.         /// </summary>
  70.         protected override void OnBarUpdate()
  71.         {
  72.             if (CurrentBar%CalendarEntryInterval == 0) {
  73.                 CalendarEntryQuantity *= (1+InflationRate);
  74.                 totalInvestment += CalendarEntryQuantity;
  75.                 quantity = (int)(CalendarEntryQuantity * StockRatio / Close[0]);
  76.                 if (quantity > BuyMinQuantity) {
  77.                     EnterLong(quantity, "Calendar Entry");
  78.                 }
  79.                 else {
  80.                     totalCash += CalendarEntryQuantity;
  81.                 }
  82.             }
  83.  
  84.             totalCash *= (1+InterestRate);
  85.             portfolioValue = Position.Quantity * Close[0];
  86.             if (CurrentBar > 0) {
  87.                 Print(ToDay(Time[1]).ToString()+"::  $"+Close[0].ToString()+",    Fund value: $"+portfolioValue.ToString()+", \tCash: $" + totalCash.ToString()+",    Control: $" + portfolioControl.ToString()+";    "+nTradesBuy.ToString()+" buys,    "+nTradesSell.ToString()+" sells,    "+nShares.ToString()+" shares.");
  88.             }
  89.  
  90.             if (portfolioValue < portfolioControl)
  91.             {
  92.                 quantity = (int) ((portfolioControl - portfolioValue*(1+BuySAFE))/Close[0]);
  93.                 if (quantity > BuyMinQuantity) {
  94.                     EnterLong(quantity, "AIM Entry");
  95.                 }
  96.             }
  97.             else if (portfolioValue > portfolioControl)
  98.             {
  99.                 quantity = (int) ((portfolioValue*(1-SellSAFE) - portfolioControl)/Close[0]);
  100.                 if (quantity > SellMinQuantity) {
  101.                     ExitLong(quantity);
  102.                 }
  103.             }
  104.         }
  105.  
  106.         /// <summary>
  107.         /// Generic execution logic not specific to a particular IOrder object
  108.         /// </summary>
  109.         protected override void OnExecution(IExecution execution)
  110.         {
  111.             // Remember to check the underlying IOrder object for null before trying to access its properties
  112.             if (execution.Order != null && execution.Order.OrderState == OrderState.Filled) {
  113.                 if (execution.Name == "AIM Entry") {
  114.                     nTradesBuy ++;
  115.                     nShares += execution.Quantity;
  116.                     cost = execution.Quantity*execution.Price + execution.Commission;
  117.                     portfolioControl += cost/2;
  118.                     totalCash -= cost;
  119.                     if (totalCash < 0) {
  120.                         Print("\r\nRun out of cash!");
  121.                         OnTermination();
  122.                     }
  123.                 }
  124.                 else if (execution.Name == "Calendar Entry") {
  125.                     nTradesBuy ++;
  126.                     nShares += execution.Quantity;
  127.                     cost = execution.Quantity*execution.Price + execution.Commission;
  128.                     portfolioControl += cost;
  129.                     totalCash += CalendarEntryQuantity - cost;
  130.                 }
  131.                 else {
  132.                     nTradesSell ++;
  133.                     nShares -= execution.Quantity;
  134.                     totalCash += execution.Quantity*execution.Price - execution.Commission;
  135.                 }
  136.             }
  137.         }
  138.  
  139.         /// <summary>
  140.         /// Clean up your resources here
  141.         /// </summary>
  142.         protected override void OnTermination()
  143.         {
  144.             portfolioValue = nShares * Close[0];
  145.             Print("\r\n====Summary: Automated Investment Management====\r\nTotal Investment: \t$"+totalInvestment.ToString()+", \t"+nTradesBuy.ToString()+" buys, \t"+nTradesSell.ToString()+" sells.\r\nPortfolio Value: \t$"+portfolioValue.ToString()+", \t"+nShares.ToString()+" shares;\r\nCash: \t\t$" + totalCash.ToString()+"\r\n");
  146.         }
  147.  
  148.         #region Properties
  149.         [Description("")]
  150.         [GridCategory("Parameters")]
  151.         public double InflationRate
  152.         {
  153.             get { return inflationRate; }
  154.             set { inflationRate = value; }
  155.         }
  156.  
  157.         [Description("")]
  158.         [GridCategory("Parameters")]
  159.         public double InterestRate
  160.         {
  161.             get { return interestRate; }
  162.             set { interestRate = Math.Max(0, value); }
  163.         }
  164.  
  165.         [Description("")]
  166.         [GridCategory("Parameters")]
  167.         public int CalendarEntryInterval
  168.         {
  169.             get { return calendarEntryInterval; }
  170.             set { calendarEntryInterval = Math.Max(1, value); }
  171.         }
  172.  
  173.         [Description("")]
  174.         [GridCategory("Parameters")]
  175.         public double CalendarEntryQuantity
  176.         {
  177.             get { return calendarEntryQuantity; }
  178.             set { calendarEntryQuantity = Math.Max(0, value); }
  179.         }
  180.  
  181.         [Description("")]
  182.         [GridCategory("Parameters")]
  183.         public double StockRatio
  184.         {
  185.             get { return stockRatio; }
  186.             set { stockRatio = Math.Min(1,Math.Max(0, value)); }
  187.         }
  188.  
  189.         [Description("")]
  190.         [GridCategory("Parameters")]
  191.         public double BuySAFE
  192.         {
  193.             get { return buySAFE; }
  194.             set { buySAFE = Math.Max(0.000, value); }
  195.         }
  196.  
  197.         [Description("")]
  198.         [GridCategory("Parameters")]
  199.         public double SellSAFE
  200.         {
  201.             get { return sellSAFE; }
  202.             set { sellSAFE = Math.Max(0.000, value); }
  203.         }
  204.  
  205.         [Description("")]
  206.         [GridCategory("Parameters")]
  207.         public double BuyMinQuantity
  208.         {
  209.             get { return buyMinQuantity; }
  210.             set { buyMinQuantity = Math.Max(0.000, value); }
  211.         }
  212.  
  213.         [Description("")]
  214.         [GridCategory("Parameters")]
  215.         public double SellMinQuantity
  216.         {
  217.             get { return sellMinQuantity; }
  218.             set { sellMinQuantity = Math.Max(0.000, value); }
  219.         }
  220.         #endregion
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement