| Overall Statistics |
|
Total Trades 0 Average Win 0% Average Loss 0% Compounding Annual Return 0% Drawdown 0% Expectancy 0 Net Profit 0% Sharpe Ratio 0 Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0 Beta 0 Annual Standard Deviation 0 Annual Variance 0 Information Ratio 0 Tracking Error 0 Treynor Ratio 0 Total Fees $0.00 |
//Added "SetBenchmark("SPY");", "SetBenchmark(_macd);"
// Add holdings of SPY
/*
* A simple moving average (SMA) is an arithmetic moving average calculated by adding the closing
* price of the security for a number of time periods and then dividing this total by the number of
* time periods.
*
* An exponential moving average (EMA) is a type of moving average that is similar to a
* simple moving average, except that more weight is given to the latest data. It's also known as the
* exponentially weighted moving average. This type of moving average reacts faster to recent price
* changes than a simple moving average.
*
* Moving average convergence divergence (MACD) is a trend-following momentum indicator that shows
* the relationship between two moving averages of prices. The MACD is calculated by
* subtracting the 26-day exponential moving average (EMA) from the 12-day EMA.
* A nine-day EMA of the MACD, called the "signal line", is then plotted on top of the MACD,
* functioning as a trigger for buy and sell signals.
*/
using System;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;
namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Uses daily data and a simple moving average cross to place trades and an ema for stop placement
/// </summary>
/// <meta name="tag" content="using data" />
/// <meta name="tag" content="indicators" />
/// <meta name="tag" content="trading and orders" />
public class DailyAlgorithm : QCAlgorithm
{
private DateTime _lastAction;
private MovingAverageConvergenceDivergence _macd;
private ExponentialMovingAverage _ema;
private readonly Symbol _cs = QuantConnect.Symbol.Create("CS", SecurityType.Equity, Market.USA);
private readonly Symbol _spy = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA);
/// <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(2013, 01, 01); //Set Start Date
SetEndDate(DateTime.Now); //Set End Date
SetCash(1700); //Set Strategy Cash
// Find more symbols here: http://quantconnect.com/data
AddSecurity(SecurityType.Equity, "CS", Resolution.Hour);
AddSecurity(SecurityType.Equity, "SPY", Resolution.Daily);
SetBenchmark("SPY");
//Wilders: The standard exponential moving average, using a smoothing factor of 1/n
// _macd = MACD(_spy, 12, 26, 9, MovingAverageType.Wilders , Resolution.Daily, Field.Close);
_macd = MACD(_spy, 12, 26, 9, MovingAverageType.Exponential , Resolution.Daily, Field.Close);
_ema = EMA(_cs, 15*6, Resolution.Hour, Field.SevenBar);
/*
*Leverage is the investment strategy of using borrowed money: specifically, the use of various financial
*instruments or borrowed capital to increase the potential return of an investment. Leverage can also
*refer to the amount of debt used to finance assets. When one refers to something (a company, a property
*or an investment) as "highly leveraged," it means that item has more debt than equity.
*/
Securities[_cs].SetLeverage(1.0m); //This is done to be able to short the security below
}
/// <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)
{
if (!_macd.IsReady) return;
if (!data.ContainsKey(_cs)) return;
if (_lastAction.Date == Time.Date) return;
_lastAction = Time;
var holding = Portfolio[_spy];
/*
* If there are no holdings of SPY, and if SPY's Moving Average Convergence Divergence
* is GREATER than its signal,
* AND if CS's price is ABOVE its exponential moving average, then buy CS on a long position.
*/
if (holding.Quantity <= 0 && _macd > _macd.Signal && data[_cs].Price > _ema)
{
// C#: If you want a numeric real literal to be treated as decimal, use the suffix m or M.
// Without the suffix m, the number is treated as a double, thus generating a compiler error.
SetHoldings(_cs, 0.25m);
SetHoldings(_spy, 0.75m);
}
/*
* Else if there are some or no holdings of SPY, and if SPY's Moving Average Convergence Divergence
* is LESS than its signal,
* AND if CS's price is BELOW its exponential moving average, then short CS.
*/
else if (holding.Quantity >= 0 && _macd < _macd.Signal && data[_cs].Price < _ema)
{
SetHoldings(_cs, -0.25m);
SetHoldings(_spy, 0.75m);
}
Plot("Portfolio", Portfolio.TotalPortfolioValue);
/*
* This example shows that
*/
}
}
}