Overall Statistics
Total Trades
382
Average Win
8.31%
Average Loss
-2.75%
Compounding Annual Return
43.747%
Drawdown
35.200%
Expectancy
0.620
Net Profit
1436.849%
Sharpe Ratio
1.096
Probabilistic Sharpe Ratio
41.913%
Loss Rate
60%
Win Rate
40%
Profit-Loss Ratio
3.02
Alpha
0.161
Beta
0.265
Annual Standard Deviation
0.319
Annual Variance
0.102
Information Ratio
-0.688
Tracking Error
0.53
Treynor Ratio
1.323
Total Fees
$329455.99
Estimated Strategy Capacity
$600000.00
Lowest Capacity Asset
BTCUSD E3
namespace QuantConnect
{
    public class CreativeFluorescentOrangeMosquito : QCAlgorithm
    {
    	private OrderTicket orderTicket;

        private decimal stoploss = 0m;
        private string ticker = "BTCUSD";
        public decimal price;
        public decimal usd;

        int fastPeriod = 50;
        int mediumPeriod = 120;
        int slowPeriod = 200;

        ExponentialMovingAverage fastMA;
        ExponentialMovingAverage mediumMA;
        ExponentialMovingAverage slowMA;
        RelativeStrengthIndex rsi;
        
        public override void Initialize()
        {
			SetTimeZone("Etc/GMT");
            SetStartDate(2014, 01, 01);
            SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Cash);
            SetCash(100000);

            var _crypto = AddCrypto(ticker, Resolution.Hour, Market.Bitfinex);

            fastMA = EMA(ticker, fastPeriod, Resolution.Hour);
            mediumMA = EMA(ticker, mediumPeriod, Resolution.Hour);
            slowMA = EMA(ticker, slowPeriod, Resolution.Hour);
            rsi = RSI(ticker, 14, MovingAverageType.Wilders, Resolution.Hour);

            SetBenchmark(ticker);

            SetWarmUp(200);
        }

        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// Slice object keyed by symbol containing the stock data
        public override void OnData(Slice data)
        {
			//if (!Portfolio.Invested )
            //{
            //	SetHoldings(ticker, 1);
            //}
            //return;

            if (!slowMA.IsReady)
            {
                return;
            }

            price = data[ticker].Price;

            stoploss = Math.Round(mediumMA.Current.Value * 0.995m, 2);

            //usd = Portfolio.CashBook["USD"].Amount * 0.98m;
            //usd = Portfolio.GetBuyingPower("USD", 0);

            //var holdings = Portfolio[ticker].Quantity;

            //Log(Portfolio.GetBuyingPower("USD", 0));

            //var fastMaVal = fastMA.Current.Value;
            //var distancePerc = ((price - fastMaVal) / fastMaVal) * 100;

            if (!Portfolio.Invested)
            {

                //&& rsi.Current.Value >= 70
                if (fastMA > mediumMA && mediumMA > slowMA && rsi.Current.Value >= 70)
                {
                    var usdHeld = Portfolio.CashBook["USD"].Amount;
                    var quantity = (usdHeld / price) * 0.95m;
                    MarketOrder(ticker, quantity);
                    //SetHoldings(ticker, 0.95);
                }
            }
            else
            {
                if (orderTicket.Status != OrderStatus.Filled)
                {
                    orderTicket.UpdateStopPrice(stoploss);
                }
                

                //if (fastMA < mediumMA)
                //{
                //    Liquidate();
                //}

            }

            Plot("Candles", "Open", data.Bars[ticker].Open);
            Plot("Candles", "High", data.Bars[ticker].High);
            Plot("Candles", "Low", data.Bars[ticker].Low);
            Plot("Candles", "Close", data.Bars[ticker].Close);

            Plot("ChartIndicators", "FastMA", fastMA);
            Plot("ChartIndicators", "MediumMA", mediumMA);
            Plot("ChartIndicators", "SlowMA", slowMA);
            Plot("ChartIndicators", "StopLoss", stoploss);

            Plot("Info", "TotalHoldingsValue", Portfolio.TotalHoldingsValue);
            Plot("Info", "TotalPortfolioValue", Portfolio.TotalPortfolioValue);
        }
        
        
        public override void OnOrderEvent(OrderEvent orderEvent)
        {
            if (orderEvent.Status.IsFill())
            {

                if (orderEvent.FillQuantity > 0)
                {
                    orderTicket = StopMarketOrder(ticker, -orderEvent.FillQuantity, stoploss);
                }

                //Debug($"Purchased Stock: {orderEvent.Symbol}");
            }
        }
    }
    
      
}