| Overall Statistics |
|
Total Trades 564 Average Win 0.53% Average Loss -0.46% Compounding Annual Return -1.429% Drawdown 19.400% Expectancy -0.023 Net Profit -6.935% Sharpe Ratio -0.16 Loss Rate 55% Win Rate 45% Profit-Loss Ratio 1.16 Alpha 0.023 Beta -0.245 Annual Standard Deviation 0.073 Annual Variance 0.005 Information Ratio -0.75 Tracking Error 0.206 Treynor Ratio 0.048 Total Fees $3680.35 |
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Data.Consolidators;
namespace QuantConnect.Algorithm.Examples
{
/// <summary>
/// Algorithm that detects over night gaps
/// </summary>
public class GapAlgorithm : QCAlgorithm
{
// these are open/close minute bars
// we'll set the open at the beginning of each day to detect gaps
TradeBar open;
// we'll set the close at the end of each day
TradeBar close;
/// <summary>
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
/// </summary>
public override void Initialize()
{
SetStartDate(2010, 05, 03);
SetEndDate(2015, 04, 30);
AddSecurity(SecurityType.Equity, "SPY");
}
/// <summary>
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// </summary>
/// <param name="data">TradeBars IDictionary object with your stock data</param>
public void OnData(TradeBars data)
{
// populate our opening price variable
if (open == null || open.Time.Date != Time.Date)
{
// when TryGetValue succeeds it will populate the 'open'
// variable with our first minute bar of the day (at 9:31, the bar that spans from 9:30->9:31)
// if it fails then 'open' will have a value of null
data.TryGetValue("SPY", out open);
if (open != null && close != null && open.Time.Date != close.Time.Date)
{
// The close of yesterday is greater than the open today.
// Gap_Down = Close[1] > Open[0]
bool gapDown = close.Close > open.Open;
if (gapDown)
{
// The difference in percentage.
// Gap_Change = (Open[0]/Close[1] -1)
decimal gapChange = open.Open/close.Close - 1m;
Console.WriteLine(Time + " - GapDown: " + gapChange.ToString("0.000"));
SetHoldings("SPY", -.45);
}
}
}
// we get our last minute bar at 4:00, market is closed,
// save it into our 'close' variable
if (Time.TimeOfDay.TotalHours == 16)
{
// when TryGetValue succeeds it will populate the 'close'
// variable with our final minute bar of the day (at $:00)
// if it fails then 'close' will have a value of null
data.TryGetValue("SPY", out close);
}
// at 3:58 liquidate
if (Portfolio.Invested && Time.TimeOfDay == new TimeSpan(15, 58, 0))
{
Liquidate();
}
}
}
}