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
Probabilistic 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
using QuantConnect;

namespace QuantConnect.Algorithm.CSharp
{
    public class IndicatorTest : QCAlgorithm {
    	private static readonly string VERSION = "1.0";
    	
        private readonly string _ticker = "ETHUSD";
		private readonly int _startingCash = 2000;
		private readonly int _period = 40;
		private readonly Resolution _resolution = Resolution.Hour;
		private string _baseSymbol;
		private SimpleMovingAverage _sma;
		private bool _isWarmUpFinished = false;
		private bool _updatedPeriod = false;
		private TradeBarConsolidator _consolidator;
		private RollingWindow<TradeBar> _priceRollingWindow;

        public override void Initialize()
        {
            SetStartDate(2018, 1, 1);  //Set Start Date
            SetEndDate(2018, 1, 2);    //Set End Date
            SetCash(_startingCash);    //Set Strategy Cash

            QuantConnect.Securities.Crypto.Crypto crypto = AddCrypto(_ticker, _resolution);

            _baseSymbol = crypto.BaseCurrencySymbol;
            SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Cash);
            
            _consolidator = new TradeBarConsolidator(TimeSpan.FromHours(2));
            SubscriptionManager.AddConsolidator(_ticker, _consolidator); 
            _consolidator.DataConsolidated += OnCustomHandler;
			
			_priceRollingWindow = new RollingWindow<TradeBar>(_period);

			// how to change the period of this indicator on runtime and feed it with historical data?
            _sma = new SimpleMovingAverage(_period);

            SetWarmUp(_period);
        }
        
        public override void OnWarmupFinished(){
        	_isWarmUpFinished = true;
        }
        
        public void OnCustomHandler(object sender, TradeBar data)
        {
			_priceRollingWindow.Add(data);
			_sma.Update(data.EndTime, data.Close);

            if (!_sma.IsReady || !_isWarmUpFinished || !_priceRollingWindow.IsReady) {
        	 	return;
        	}
        	
        	if (!_updatedPeriod) {
        		_updatedPeriod = false;
        		_sma = UpdateSmaPeriod(_period - 1);
        	}
        }

        public override void OnData(Slice data) {
			// nothing to do
        }
        
        private SimpleMovingAverage UpdateSmaPeriod(int period) {
        	SimpleMovingAverage sma = new SimpleMovingAverage(period);
            for (int i = _period - 1; i >= 0; i --) {
                sma.Update(_priceRollingWindow[i].EndTime, _priceRollingWindow[i].Close);
            }

			return sma;
        }
       
    }
}