Overall Statistics |
Total Trades 20 Average Win 0.36% Average Loss -0.46% Compounding Annual Return -1.569% Drawdown 3.100% Expectancy -0.558 Net Profit -2.992% Sharpe Ratio -1.202 Loss Rate 75% Win Rate 25% Profit-Loss Ratio 0.77 Alpha -0.012 Beta 0.009 Annual Standard Deviation 0.011 Annual Variance 0 Information Ratio 0.185 Tracking Error 0.305 Treynor Ratio -1.358 Total Fees $23.35 |
using System.Drawing; using System.Threading; using System.Threading.Tasks; namespace QuantConnect { public partial class DailyMartin : QCAlgorithm { [Parameter] public decimal iStopPips = 5m; [Parameter] public decimal iVolume = 1m; [Parameter] public decimal iCommission = 1m; [Parameter] public string iSymbol = "PEP"; decimal iVolumeStep = 0; decimal iBalanceStep = 0; decimal iBalance = 10000m; string iChartName = "Deals"; public class iDeal { public int SL; public int TP; public int Price; public int Direction; }; Dictionary<int, iDeal> iDeals = new Dictionary<int, iDeal>(); public override void Initialize() { var resolution = Resolution.Daily; SetCash(iBalance); SetStartDate(2008, 1, 1); SetEndDate(2009, 12, 1); //SetStartDate(2017, 1, 1); //SetEndDate(DateTime.Now.Date); SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage); AddSecurity(SecurityType.Equity, iSymbol, resolution, true, 4m, false); var chart = new Chart(iChartName); var seriesStock = new Series("Stock", SeriesType.Line, 0) { Color = Color.Gray }; var seriesBuy = new Series("Buy", SeriesType.Scatter, 0) { Color = Color.Blue, ScatterMarkerSymbol = ScatterMarkerSymbol.Triangle }; var seriesSell = new Series("Sell", SeriesType.Scatter, 0) { Color = Color.Red, ScatterMarkerSymbol = ScatterMarkerSymbol.TriangleDown }; var seriesBalance = new Series("Balance", SeriesType.Line, 1) { Color = Color.Yellow }; chart.AddSeries(seriesStock); chart.AddSeries(seriesBuy); chart.AddSeries(seriesSell); chart.AddSeries(seriesBalance); iVolumeStep = iVolume; iBalanceStep = GetBalance(); AddChart(chart); } public override void OnOrderEvent(OrderEvent data) { var order = Transactions.GetOrderById(data.OrderId); if (order.Status == OrderStatus.Filled) { Log("FILL " + data.OrderId + " : " + order.Tag); if (order.Tag == "TM" || order.Tag == "BM") { //Transactions.CancelOpenOrders(iSymbol); foreach (var item in iDeals) { Transactions.CancelOrder(item.Key); } iDeals.Clear(); var price = Securities[iSymbol].Price; var direction = order.Direction == OrderDirection.Buy ? 1 : -1; var SL = StopMarketOrder(iSymbol, iVolumeStep * direction * -1, price - iStopPips * direction, "SL"); var TP = LimitOrder(iSymbol, iVolumeStep * direction * -1, price + 2 * iStopPips * direction, "TP"); } if (order.Tag == "SL" || order.Tag == "TP") { Liquidate(iSymbol); } } } public void OnData(TradeBars data) { Plot(iChartName, "Stock", data[iSymbol].Price); Plot(iChartName, "Balance", iBalanceStep); var setup = CanOpen(); if (setup == 1) { var buy = StopMarketOrder(iSymbol, iVolume, Securities[iSymbol].Price + 1.0m, "TM"); var sell = StopMarketOrder(iSymbol, -iVolume, Securities[iSymbol].Price - 1.0m, "BM"); Log("SAVE " + buy.OrderId + " : " + sell.OrderId); iDeals[buy.OrderId] = new iDeal { Price = buy.OrderId, Direction = 1 }; iDeals[sell.OrderId] = new iDeal { Price = sell.OrderId, Direction = -1 }; } } protected int CanOpen() { if (Portfolio.Invested == false && Transactions.GetOpenOrders(iSymbol).Count == 0) { var balance = GetBalance(); if (balance < iBalanceStep) { iVolumeStep = iVolumeStep * 2; } else { Plot(iChartName, "Balance", balance); iBalanceStep = balance; iVolumeStep = iVolume; } return 1; } return 0; } protected decimal GetBalance() { var balance = iBalance + Portfolio.TotalUnrealizedProfit + Portfolio.TotalProfit - Portfolio.TotalFees; return balance; } } }