Overall Statistics
Total Trades
5
Average Win
0.13%
Average Loss
-0.08%
Compounding Annual Return
0.712%
Drawdown
0.300%
Expectancy
0.293
Net Profit
0.044%
Sharpe Ratio
1.361
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
1.59
Alpha
0.005
Beta
0
Annual Standard Deviation
0.004
Annual Variance
0
Information Ratio
1.583
Tracking Error
0.084
Treynor Ratio
23.608
Total Fees
$5.00
namespace QuantConnect 
{   
    /*
    *   QuantConnect University: Full Basic Template:
    *
    *   The underlying QCAlgorithm class is full of helper methods which enable you to use QuantConnect.
    *   We have explained some of these here, but the full algorithm can be found at:
    *   https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Algorithm/QCAlgorithm.cs
    */
    public class BasicTemplateAlgorithm : QCAlgorithm
    {
    	private decimal lastClose = -1m;
    	private decimal lastBuy = -1m;
    	private string symbol = "AAPL";
    	
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize() 
        {
            //Start and End Date range for the backtest:
            SetStartDate(2015, 7, 15);
            SetEndDate(DateTime.Now.Date.AddDays(-1));
            
            //Cash allocation
            SetCash(25000);
            
            //Add as many securities as you like. All the data will be passed into the event handler:
            AddSecurity(SecurityType.Equity, symbol, Resolution.Minute, true, 1, true);
        }

        //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) 
        {   
            // "TradeBars" object holds many "TradeBar" objects: it is a dictionary indexed by the symbol:
            // 
            //  e.g.  data["MSFT"] data["GOOG"]
            TradeBar d = data[symbol];
            
            // Record the previous closing price
            if (d.Time.Hour == 15) {
            	lastClose = d.Close;
            	lastBuy = -1m;
            }
            
            // Buy on a 1% dip in extended hours trading, keep buying as long as it keeps going down
            if (d.Time.Hour >= 16 && lastClose > 0m) {
            	if (d.Low < lastClose * 0.99m) {
            		if (lastBuy < 0m || d.Low < lastBuy) {
            			LimitOrder(symbol, 10, d.Low);
            			lastBuy = d.Low;
            		}
            	}
            }
            
            // Liquidate at the next day open
            if (d.Time.Hour == 9 && d.Time.Minute >=30) {
            	if (Portfolio[symbol].HoldStock) {
            		Liquidate(symbol);
            	}
            }
            if (Time.Minute == 0)
            {
        		Plot("holdings", symbol, Portfolio[symbol].Quantity);
            }
        }
        
        public override void OnOrderEvent(OrderEvent fill)
        {
        	Log(fill.ToString());
        }
    }
}