Overall Statistics |
Total Trades 5 Average Win 0% Average Loss -4.95% Compounding Annual Return -95.43% Drawdown 39.900% Expectancy -1 Net Profit -39.92% Sharpe Ratio -8.429 Loss Rate 100% Win Rate 0% Profit-Loss Ratio 0 Alpha -3.115 Beta 0.529 Annual Standard Deviation 0.353 Annual Variance 0.125 Information Ratio -9.182 Tracking Error 0.353 Treynor Ratio -5.625 |
/* * QUANTCONNECT.COM - * Example custom transaction model override the underlying model */ using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using QuantConnect; using QuantConnect.Models; namespace QuantConnect.Securities { /******************************************************** * CLASS DEFINITIONS *********************************************************/ /// <summary> /// Custom Transaction Model Example /// </summary> public class CustomTransactionModel : SecurityTransactionModel, ISecurityTransactionModel { /******************************************************** * CLASS CONSTRUCTOR *********************************************************/ /// <summary> /// Custom transaction model class. /// </summary> public CustomTransactionModel() { } /******************************************************** * CLASS METHODS *********************************************************/ /// <summary> /// Perform neccessary check to see if the model has been filled, appoximate the best we can. /// </summary> /// <param name="vehicle">Asset we're working with</param> /// <param name="order">Order class to check if filled.</param> public override OrderEvent Fill(Security vehicle, Order order) { //Default order event to return. var fill = new OrderEvent(order); switch (order.Type) { case OrderType.Limit: fill = LimitFill(vehicle, order); break; case OrderType.StopMarket: fill = StopFill(vehicle, order); break; case OrderType.Market: fill = MarketFill(vehicle, order); break; } return fill; } /// <summary> /// Get the Slippage approximation for this order: /// </summary> public override decimal GetSlippageApproximation(Security security, Order order) { return 0; } /// <summary> /// Default market order model. Fill at last price /// </summary> /// <param name="security">Asset we're working with</param> /// <param name="order">Order to update</param> public override OrderEvent MarketFill(Security security, Order order) { //Default order event to return. var fill = new OrderEvent(order); //Set the order price order.Price = security.Price; order.Status = OrderStatus.Filled; //Set the order event fill: - Assuming 100% fill fill.FillPrice = security.Price; fill.FillQuantity = order.Quantity; fill.Status = order.Status; return fill; } /// <summary> /// Check if the model has stopped out our position yet: /// </summary> /// <param name="security">Asset we're working with</param> /// <param name="order">Stop Order to Check, return filled if true</param> public override OrderEvent StopFill(Security security, Order order) { //Default order event to return. var fill = new OrderEvent(order); //If its cancelled don't need anymore checks: if (fill.Status == OrderStatus.Canceled) return fill; //Check if the Stop Order was filled: opposite to a limit order switch (order.Direction) { case OrderDirection.Sell: //-> 1.1 Sell Stop: If Price below setpoint, Sell: if (security.Price < order.Price) { order.Status = OrderStatus.Filled; order.Price = security.Price; } break; case OrderDirection.Buy: //-> 1.2 Buy Stop: If Price Above Setpoint, Buy: if (security.Price > order.Price) { order.Status = OrderStatus.Filled; order.Price = security.Price; } break; } if (order.Status == OrderStatus.Filled || order.Status == OrderStatus.PartiallyFilled) { fill.FillQuantity = order.Quantity; fill.FillPrice = security.Price; //Stop price as security price because can gap past stop price. fill.Status = order.Status; } return fill; } /// <summary> /// Check if the price MarketDataed to our limit price yet: /// </summary> /// <param name="security">Asset we're working with</param> /// <param name="order">Limit order in market</param> public override OrderEvent LimitFill(Security security, Order order) { //Initialise; decimal marketDataMinPrice = 0; decimal marketDataMaxPrice = 0; var fill = new OrderEvent(order); //If its cancelled don't need anymore checks: if (fill.Status == OrderStatus.Canceled) return fill; //Depending on the resolution, return different data types: BaseData marketData = security.GetLastData(); if (marketData.DataType == MarketDataType.TradeBar) { marketDataMinPrice = ((TradeBar)marketData).Low; marketDataMaxPrice = ((TradeBar)marketData).High; } else { marketDataMinPrice = marketData.Value; marketDataMaxPrice = marketData.Value; } //-> Valid Live/Model Order: switch (order.Direction) { case OrderDirection.Buy: //Buy limit seeks lowest price if (marketDataMinPrice < order.Price) { //Set order fill: order.Status = OrderStatus.Filled; order.Price = security.Price; } break; case OrderDirection.Sell: //Sell limit seeks highest price possible if (marketDataMaxPrice > order.Price) { order.Status = OrderStatus.Filled; order.Price = security.Price; } break; } if (order.Status == OrderStatus.Filled || order.Status == OrderStatus.PartiallyFilled) { fill.FillQuantity = order.Quantity; fill.FillPrice = security.Price; fill.Status = order.Status; } return fill; } /// <summary> /// Example Override of Fee Model - $100 Per Trade Fee /// </summary> public override decimal GetOrderFee(decimal quantity, decimal price) { return 100; } } // End Algorithm Transaction Filling Classes } // End QC Namespace
/* * QUANTCONNECT.COM - * Example custom transaction model override the underlying model */ using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using QuantConnect; using QuantConnect.Models; namespace QuantConnect.Securities { /******************************************************** * CLASS DEFINITIONS *********************************************************/ /// <summary> /// Simple Custom Order Fee Only Model. /// </summary> public class CompactFeeOnlyModel : SecurityTransactionModel, ISecurityTransactionModel { /******************************************************** * CLASS CONSTRUCTOR *********************************************************/ /// <summary> /// Fee only transaction model. /// </summary> public CompactFeeOnlyModel() { } /******************************************************** * CLASS METHODS *********************************************************/ /// <summary> /// Example Override of Fee Model - $100 Per Trade Fee /// </summary> public override decimal GetOrderFee(decimal quantity, decimal price) { return 500; } } // End Algorithm Transaction Filling Classes } // End QC Namespace
using System; using System.Collections; using System.Collections.Generic; using QuantConnect.Securities; using QuantConnect.Models; namespace QuantConnect { // Name your algorithm class anything, as long as it inherits QCAlgorithm public class FillModelExample : QCAlgorithm { string _symbol = "MSFT"; DateTime _firedToday = new DateTime(); //Initialize the data and resolution you require for your strategy: public override void Initialize() { SetStartDate(new DateTime(2014,5, 1)); SetEndDate(new DateTime(2014, 6, 30)); SetCash(20000); AddSecurity(SecurityType.Equity, _symbol, Resolution.Minute); //SET THE FEE / SLIPPAGE / FILL MODEL PER SYMBOL. Securities[_symbol].Model = new CompactFeeOnlyModel(); } //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol. public void OnData(TradeBars data) { if (_firedToday.Date == Time.Date) return; if (Time.Day % 3 == 0 ) { Order(_symbol, 100 ); //Messaging. Debug("Debug Purchased: " + _symbol); Log("Log Purchased: " + _symbol); } if (Time.Day % 7 == 0) { Error("Fake Error to Demonstrate Errors: " + _symbol); Liquidate(); } _firedToday = Time; } } }