using System;
namespace QuantConnect.Algorithm.CSharp
{
public class EMACrossExample : QCAlgorithm
{
//USER VARIABLES
private string _Ticker = "BTCUSD";
int _MaxPosition = 500;
int _MinPosition = 100;
int _FastPeriod = 12;
int _SlowPeriod = 26;
// PROGRAM VARIABLES
public decimal _Close;
public decimal _Open;
public decimal _Low;
public decimal _High;
public decimal _Volume;
public decimal _Holding;
public string _BaseSymbol;
public decimal _USD;
ExponentialMovingAverage _FastEMA;
ExponentialMovingAverage _SlowEMA;
AccumulationDistribution _AD;
// INITIALIASE BLOCK
public override void Initialize()
{
SetStartDate(2018, 11, 10);
SetEndDate(2018, 11, 20);
SetCash(5000);
var _Crypto = AddCrypto(_Ticker, Resolution.Hour);
_BaseSymbol = _Crypto.BaseCurrencySymbol;
SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash);
_FastEMA = EMA(_Ticker, _FastPeriod, Resolution.Hour);
_SlowEMA = EMA(_Ticker, _SlowPeriod, Resolution.Hour);
_AD = AD(_Ticker, Resolution.Hour);
}
// ONDATA BLOCK
public override void OnData(Slice data)
{
_Close = data[_Ticker].Close;
_Open = data[_Ticker].Open;
_Low = data[_Ticker].Low;
_High = data[_Ticker].High;
_Volume = data[_Ticker].Volume;
_USD = Portfolio.CashBook["USD"].Amount;
Plot("Price Open", _Open);
Plot("Price Low", _Low);
Plot("Price High", _High);
Plot("Price Close", _Close);
Plot("Volume", _Volume);
Plot("Accumulation Distribution", _AD);
if (!Portfolio.Invested && _USD > _MinPosition)
{
if(_FastEMA > _SlowEMA)
{
decimal _Quantity = Math.Round(_MaxPosition / _Close, 6);
MarketOrder(_Ticker, _Quantity);
}
}
if(Portfolio.Invested)
{
_Holding = Portfolio.CashBook[_BaseSymbol].Amount;
if(_FastEMA < _SlowEMA)
{
Sell(_Ticker, _Holding);
}
}
}
}
}