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>
- /// Robert Lichello's Automated Investment Management and Braden Glett's Reverse Scale strategies
- /// </summary>
- [Description("Robert Lichello's Automated Investment Management and Braden Glett's Reverse Scale strategies")]
- public class ScaleTrading : Strategy
- {
- #region Variables
- // Wizard generated variables
- //private bool reversed = false; // Set to true to use Reverse Scale, otherwise use AIM
- private double inflationRate = 0.03; // Inflation rate for calendarEntryInterval time periods
- private double interestRate = Math.Exp(Math.Log(1.01)/12)-1; // Interest rate for one time period
- private int calendarEntryInterval = 12; // Invest every calendarEntryInterval time periods
- private double calendarEntryQuantity = 3000; // Amount of new funds every calendarEntryInterval time periods
- private double stockRatio = 0.5; // Ratio: stock / (stock + cash)
- private double buySAFE = 0.1; // Stock Adjustment Factor Equalizer for entry
- private double sellSAFE = 0.1; // Stock Adjustment Factor Equalizer for exit
- private double buyMinQuantity = 10; // Minimum quantity for entry
- private double sellMinQuantity = 10; // Minimum quantity for exit
- // User defined variables (add any user defined variables below)
- private int quantity,nTradesBuy,nTradesSell,nShares;
- private double portfolioControl,totalCash,portfolioValue,cost,totalInvestment;
- #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()
- {
- nTradesBuy = 0;
- nTradesSell = 0;
- nShares = 0;
- portfolioControl = 0;
- totalCash = 0;
- totalInvestment = 0;
- CalendarEntryQuantity /= (1+InflationRate);
- }
- /// <summary>
- /// Called on each bar update event (incoming tick)
- /// </summary>
- protected override void OnBarUpdate()
- {
- if (CurrentBar%CalendarEntryInterval == 0) {
- CalendarEntryQuantity *= (1+InflationRate);
- totalInvestment += CalendarEntryQuantity;
- quantity = (int)(CalendarEntryQuantity * StockRatio / Close[0]);
- if (quantity > BuyMinQuantity) {
- EnterLong(quantity, "Calendar Entry");
- }
- else {
- totalCash += CalendarEntryQuantity;
- }
- }
- totalCash *= (1+InterestRate);
- portfolioValue = Position.Quantity * Close[0];
- if (CurrentBar > 0) {
- 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.");
- }
- if (portfolioValue < portfolioControl)
- {
- quantity = (int) ((portfolioControl - portfolioValue*(1+BuySAFE))/Close[0]);
- if (quantity > BuyMinQuantity) {
- EnterLong(quantity, "AIM Entry");
- }
- }
- else if (portfolioValue > portfolioControl)
- {
- quantity = (int) ((portfolioValue*(1-SellSAFE) - portfolioControl)/Close[0]);
- if (quantity > SellMinQuantity) {
- ExitLong(quantity);
- }
- }
- }
- /// <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 == "AIM Entry") {
- nTradesBuy ++;
- nShares += execution.Quantity;
- cost = execution.Quantity*execution.Price + execution.Commission;
- portfolioControl += cost/2;
- totalCash -= cost;
- if (totalCash < 0) {
- Print("\r\nRun out of cash!");
- OnTermination();
- }
- }
- else if (execution.Name == "Calendar Entry") {
- nTradesBuy ++;
- nShares += execution.Quantity;
- cost = execution.Quantity*execution.Price + execution.Commission;
- portfolioControl += cost;
- totalCash += CalendarEntryQuantity - cost;
- }
- else {
- nTradesSell ++;
- nShares -= execution.Quantity;
- totalCash += execution.Quantity*execution.Price - execution.Commission;
- }
- }
- }
- /// <summary>
- /// Clean up your resources here
- /// </summary>
- protected override void OnTermination()
- {
- portfolioValue = nShares * Close[0];
- 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");
- }
- #region Properties
- [Description("")]
- [GridCategory("Parameters")]
- public double InflationRate
- {
- get { return inflationRate; }
- set { inflationRate = value; }
- }
- [Description("")]
- [GridCategory("Parameters")]
- public double InterestRate
- {
- get { return interestRate; }
- set { interestRate = Math.Max(0, 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 double StockRatio
- {
- get { return stockRatio; }
- set { stockRatio = Math.Min(1,Math.Max(0, value)); }
- }
- [Description("")]
- [GridCategory("Parameters")]
- public double BuySAFE
- {
- get { return buySAFE; }
- set { buySAFE = Math.Max(0.000, value); }
- }
- [Description("")]
- [GridCategory("Parameters")]
- public double SellSAFE
- {
- get { return sellSAFE; }
- set { sellSAFE = Math.Max(0.000, value); }
- }
- [Description("")]
- [GridCategory("Parameters")]
- public double BuyMinQuantity
- {
- get { return buyMinQuantity; }
- set { buyMinQuantity = Math.Max(0.000, value); }
- }
- [Description("")]
- [GridCategory("Parameters")]
- public double SellMinQuantity
- {
- get { return sellMinQuantity; }
- set { sellMinQuantity = Math.Max(0.000, value); }
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement