| Overall Statistics |
|
Total Trades 214 Average Win 1.32% Average Loss -0.89% Compounding Annual Return 4.223% Drawdown 14.100% Expectancy 0.045 Net Profit 3.495% Sharpe Ratio 0.323 Loss Rate 58% Win Rate 42% Profit-Loss Ratio 1.49 Alpha 0.043 Beta -0.041 Annual Standard Deviation 0.131 Annual Variance 0.017 Information Ratio 0.082 Tracking Error 0.197 Treynor Ratio -1.037 Total Fees $214.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, 1, 1);
[Parameter("EndDate")]
public DateTime EndDateParameter = new DateTime(2015, 3, 31);
[Parameter]
public string Ticker;
private SimpleMovingAverage SMA;
private bool bStopTrading = false;
//By default we use the name of the property if no name specified.
[Parameter]
public decimal StartingCash = 25000;
[Parameter]
public decimal StopLoss = 0;
[Parameter]
public decimal MinProfit = 0;
[Parameter]
public int smaPeriod;
// Initialize the algorithm using our parameters
public override void Initialize()
{
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(1);
// create a 20 day simple moving average
SMA = 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(() =>
{
foreach (var holding in Portfolio.Values)
{
if (holding.HoldStock)
{
MarketOnCloseOrder(holding.Symbol, -holding.Quantity, tag: "ScheduledEvent EOD Liquidate");
}
}
bStopTrading = true;
});
Schedule.Event().EveryDay().At(23,55).Run(() =>
{
bStopTrading = false;
});
}
private DateTime previous;
private int numShares;
public void OnData(TradeBars data)
{
// wait for our slow ema to fully initialize
if (!SMA.IsReady)
{
Debug("SMA not ready.");
return;
}
// only once per day
if (!Portfolio.HoldStock && !(previous.Date == data.Time.Date) && bStopTrading == false)
{
if (data[Ticker].Price > SMA)
{
//numShares = (int)(Portfolio.Cash / Securities[Ticker].Price);
numShares = (int)(100000 / Securities[Ticker].Price);
Order(Ticker, numShares, tag: "SMA Buy Trigger - " + SMA);
Log("BUY >> " + numShares + " @ " + Securities[Ticker].Price);
// Sell at the close of the day.
}
Debug("Price: " + data[Ticker].Price);
Debug("SMA(20): " + SMA);
bStopTrading = true;
}
else if (Portfolio.TotalUnrealizedProfit <= -1500)//-(Portfolio.TotalHoldingsValue * StopLoss))
/*|| Portfolio.TotalUnrealizedProfit > (Portfolio.TotalHoldingsValue * MinProfit))*/
{
Liquidate(Ticker);
}
}
}
}