Overall Statistics
Total Trades
5
Average Win
0%
Average Loss
-0.42%
Compounding Annual Return
-0.936%
Drawdown
2.900%
Expectancy
-1
Net Profit
-1.790%
Sharpe Ratio
-0.399
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.01
Beta
-0.033
Annual Standard Deviation
0.019
Annual Variance
0
Information Ratio
0.194
Tracking Error
0.318
Treynor Ratio
0.226
Total Fees
$5.00
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);
	
					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") 
				{
	        		Transactions.CancelOpenOrders(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, "TM");
				var sell = StopMarketOrder(iSymbol, -iVolume, Securities[iSymbol].Price - 1, "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;
        }
    }
}