| Overall Statistics |
|
Total Trades 154 Average Win 2.00% Average Loss -1.65% Compounding Annual Return 92.384% Drawdown 9.400% Expectancy 0.321 Net Profit 47.276% Sharpe Ratio 2.369 Loss Rate 40% Win Rate 60% Profit-Loss Ratio 1.21 Alpha 0.582 Beta -0.146 Annual Standard Deviation 0.241 Annual Variance 0.058 Information Ratio 1.687 Tracking Error 0.294 Treynor Ratio -3.901 Total Fees $1232.00 |
namespace QuantConnect
{
public class QCUParameterizedAlgorithm : QCAlgorithm
{
//Parameter attribute can be applied to any variable in the algorithm.
//If no parameter is set, it uses the default specified here (2013).
//[Parameter("StartDate")]
public DateTime StartDateParameter = new DateTime(2015, 12, 10);
//[Parameter("EndDate")]
public DateTime EndDateParameter = new DateTime(2016, 07, 12);
//[Parameter]
public string Ticker = "XIV";
private SimpleMovingAverage SMAobj;
private bool bStopTrading = false;
private int countOvernightHold = 0;
//By default we use the name of the property if no name specified.
//[Parameter]
public decimal StartingCash = 100000;
//[Parameter]
public decimal StopLoss = 0;
//[Parameter]
public decimal MinProfit = 0;
//[Parameter]
public int smaPeriod = 19;
// Initialize the algorithm using our parameters
public override void Initialize()
{
Resolution res = Resolution.Minute;
//Resolution res = Resolution.Second;
//if (LiveMode) res = Resolution.Second;
//Using parameters for starting cash
SetCash(StartingCash);
//Using parameters for start and end date
SetStartDate(StartDateParameter);
SetEndDate(EndDateParameter);
AddSecurity(SecurityType.Equity, Ticker, res);
Securities[Ticker].TransactionModel = new ConstantFeeTransactionModel(8);
// create a 19 day simple moving average
SMAobj = SMA(Ticker, smaPeriod, Resolution.Daily);
// schedule event every day at 3:44pm to submit market on close orders
// for any open positions
Schedule.Event().EveryDay().At(15, 44).Run(() =>
{
// If we are going to lose more than X or we are already holding overnight...
if (Portfolio.TotalUnrealizedProfit < -100 || countOvernightHold == 1)
{
// Increment the countOvernightHold counter.
countOvernightHold++;
}
foreach (var holding in Portfolio.Values)
{
if (holding.HoldStock && countOvernightHold != 1)
{
MarketOnCloseOrder(holding.Symbol, -holding.Quantity, tag: "ScheduledEvent EOD Liquidate");
}
}
bStopTrading = true;
});
Schedule.Event().EveryDay().At(23,55).Run(() =>
{
bStopTrading = false;
});
Schedule.Event().EveryDay().At(08,55).Run(() =>
{
bStopTrading = false;
});
}
private int numShares;
private decimal instantSMA;
public void OnData(TradeBars data)
{
// wait for our slow ema to fully initialize
// only once per day
if (!Portfolio.HoldStock && bStopTrading == false)
{
if (!SMAobj.IsReady)
{
Debug("SMA not ready.");
// Log("SMA not ready.");
return;
}
instantSMA = ((SMAobj*19) + data[Ticker].Price) / 20;
Log("Calculated instantSMA: ((" + SMAobj + " * 19) + " + Securities[Ticker].Price + ") / 20 = " + instantSMA);
if (data[Ticker].Price >= (instantSMA - (decimal)0.30))
{
Debug("Price: " + data[Ticker].Price);
Debug("SMA(20): " + instantSMA);
if (data[Ticker].Price >= (instantSMA - (decimal)0.10))
{
//numShares = (int)(Portfolio.Cash / Securities[Ticker].Price);
numShares = (int)(100000 / Securities[Ticker].Price);
Order(Ticker, numShares, tag: "SMA Buy Trigger - " + instantSMA);
Log("BUY >> " + numShares + " @ " + Securities[Ticker].Price);
// Sell at the close of the day.
}
bStopTrading = true;
}
}
else if (countOvernightHold == 1 && Portfolio.TotalUnrealizedProfit >= 0)
{
Liquidate(Ticker);
}
else if (countOvernightHold != 1 && Portfolio.TotalUnrealizedProfit <= -1500)//-(Portfolio.TotalHoldingsValue * StopLoss))
/*|| Portfolio.TotalUnrealizedProfit > (Portfolio.TotalHoldingsValue * MinProfit))*/
{
// Liquidate(Ticker);
}
else if (Portfolio.TotalUnrealizedProfit >= 4000)
{
//Liquidate(Ticker);
}
else if (countOvernightHold != 1 && data[Ticker].Price <= (instantSMA - (decimal)1.50))
{
Liquidate(Ticker);
}
else
{
//Log("ONLY TRADE ONCE A DAY.");
}
}
}
}