using System;
namespace QuantConnect.Algorithm.CSharp
{
public class EMACrossExample : QCAlgorithm
{
//USER VARIABLES
private string _Ticker = "BCHUSD";
int _MaxPosition = 500;
int _MinPosition = 100;
int _FastPeriod = 12;
int _SlowPeriod = 26;
// PROGRAM VARIABLES
public decimal _Price;
public decimal _Holding;
public string _BaseSymbol;
public decimal _USD;
ExponentialMovingAverage _FastEMA;
ExponentialMovingAverage _SlowEMA;
// INITIALIASE BLOCK
public override void Initialize()
{
SetStartDate(2017, 1, 1);
SetEndDate(2019, 3, 8);
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);
}
// ONDATA BLOCK
public override void OnData(Slice data)
{
_Price = data[_Ticker].Price;
_USD = Portfolio.CashBook["USD"].Amount;
if (!Portfolio.Invested && _USD > _MinPosition)
{
if(_FastEMA > _SlowEMA)
{
decimal _Quantity = Math.Round(_MaxPosition / _Price, 6);
MarketOrder(_Ticker, _Quantity);
}
}
if(Portfolio.Invested)
{
_Holding = Portfolio.CashBook[_BaseSymbol].Amount;
if(_FastEMA < _SlowEMA)
{
Sell(_Ticker, _Holding);
}
}
}
}
}