| Overall Statistics |
|
Total Trades 6 Average Win 0.89% Average Loss -0.84% Compounding Annual Return 225.345% Drawdown 1.500% Expectancy 0.375 Net Profit 0.934% Sharpe Ratio 4.566 Loss Rate 33% Win Rate 67% Profit-Loss Ratio 1.06 Alpha -0.493 Beta -1.231 Annual Standard Deviation 0.173 Annual Variance 0.03 Information Ratio 6.704 Tracking Error 0.274 Treynor Ratio -0.643 Total Fees $6.00 |
namespace QuantConnect
{
public class MarketOnCloseAlgorithm : QCAlgorithm
{
//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{
SetStartDate(2016, 02, 22);
SetEndDate(2016, 02, 24);
SetCash(25000);
AddSecurity(SecurityType.Equity, "SPY", Resolution.Second);
// schedule event every day at 3:44pm to submit market on close orders
// for any open positions
Schedule.Event().EveryDay().At(15, 44).Run(() =>
{
foreach (var holding in Portfolio.Values)
{
if (holding.HoldStock)
{
MarketOnCloseOrder(holding.Symbol, -holding.Quantity, tag: "ScheduledEvent EOD Liquidate");
}
}
});
}
//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 (!Portfolio.HoldStock)
{
// the first day this will fire in the morning at 9:30:01am
// every following day it will fire at 4:00pm at market close
// after the MOC order fills and will be converted into a
// MarketOnOpen order and will fill at 9:30:00am the following
// trading day
SetHoldings("SPY", 1, tag: "SetHoldings SPY 1");
}
}
public override void OnEndOfDay(Symbol symbol)
{
var holdings = Portfolio[symbol];
if (holdings.HoldStock)
{
// this won't actually work because most exchanges require MarketOnClose
// orders to be submitted at least 15 minutes before the market closes,
// so the engine will flag this order as invalid
MarketOnCloseOrder(symbol, -holdings.Quantity, "MarketOnClose");
}
}
public override void OnOrderEvent(OrderEvent fill)
{
Log(Time + ":" + fill.ToString());
}
}
}