| Overall Statistics |
|
Total Trades 860 Average Win 0.37% Average Loss -0.60% Compounding Annual Return -98.909% Drawdown 89.800% Expectancy -0.883 Net Profit -89.773% Sharpe Ratio -7.857 Loss Rate 93% Win Rate 7% Profit-Loss Ratio 0.62 Alpha -3.504 Beta 0.033 Annual Standard Deviation 0.446 Annual Variance 0.199 Information Ratio -7.413 Tracking Error 0.471 Treynor Ratio -104.935 Total Fees $860.00 |
namespace QuantConnect
{
/*
Designed as a "first use" algorithm by a trader new to robotic trading, with a cash
trading account of minimal size. Does not use leverage or margin and waits 4 days from
sell date to next purchase date to allow for funds settlement by the broker. Does not
make a lot of money, but does not lose. It may however leave you holding stock for a long
time if you choose a stock that is falling, never to come back (oops!).
Happy trading!
*/
public class BeginnerAlgo : QCAlgorithm
{
private string symbol = "AAPL";
private ExponentialMovingAverage slow;
private ExponentialMovingAverage fast;
private decimal _old_price = 100000;
private int _traded_dir;
private int _current_dir;
private Chart plotter;
public override void Initialize()
{
// set up our analysis span
SetStartDate(2015, 6, 1);
SetEndDate(2015, 12, 1);
SetCash(1000);
// request "symbol's" data with minute resolution
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
// create a 20 minute simple moving average
slow = EMA(symbol, 40, Resolution.Minute);
// create a 5 minute exponential moving average
fast = EMA(symbol, 10, Resolution.Minute);
plotter = new Chart("MAs", ChartType.Overlay);
plotter.AddSeries(new Series("Price", SeriesType.Line));
plotter.AddSeries(new Series("SMA", SeriesType.Line));
plotter.AddSeries(new Series("EMA", SeriesType.Line));
AddChart(plotter);
}
public void OnData(TradeBars data)
{
// wait for our slow SMA to fully initialize
if (!slow.IsReady) return;
var holdings = Portfolio[symbol].Quantity;
decimal currentPrice = data[symbol].Close;
// indicator logic
if (fast > slow) {
_current_dir = 1;
} else {
_current_dir = -1;
}
// only trade when: currently in no trade && direction is up && first trade since indicator change && minimal distance
if (!Portfolio.HoldStock && _current_dir == 1 && _current_dir != _traded_dir && Math.Abs(fast-slow) > 0.02M)
{
int quantity = (int) Math.Floor(Portfolio.Cash/currentPrice);
Order(symbol, quantity);
_traded_dir = 1;
_old_price = currentPrice;
}
// only trade when: currently in no trade && direction is down && first trade since indicator change && minimal distance
if (!Portfolio.HoldStock && _current_dir == -1 && _current_dir != _traded_dir && Math.Abs(fast-slow) > 0.02M)
{
int quantity = (int) Math.Floor(Portfolio.Cash/currentPrice);
Order(symbol, -quantity);
_traded_dir = -1;
_old_price = currentPrice;
}
// calculate percentages (didn't use it, but you could)
decimal pps = ((currentPrice - _old_price)/_old_price)*100;
// simple exit logic based on reversion
if (_traded_dir == 1 && _current_dir == -1) {
Liquidate(symbol);
_traded_dir = 0;
}
if (_traded_dir == -1 && _current_dir == 1) {
Liquidate(symbol);
_traded_dir = 0;
}
// Update plot
Plot("MAs", "Price", currentPrice);
Plot("MAs", "SMA", slow);
Plot("MAs", "EMA", fast);
}
}
}