Overall Statistics
Total Trades
10
Average Win
40.35%
Average Loss
-8.91%
Compounding Annual Return
39.457%
Drawdown
22.100%
Expectancy
2.318
Net Profit
114.196%
Sharpe Ratio
1.052
Loss Rate
40%
Win Rate
60%
Profit-Loss Ratio
4.53
Alpha
0.462
Beta
-9.323
Annual Standard Deviation
0.296
Annual Variance
0.088
Information Ratio
0.998
Tracking Error
0.296
Treynor Ratio
-0.033
Total Fees
$0.00
using System;
using System.Linq;
using QuantConnect.Indicators;
using QuantConnect.Models;
using QuantConnect.Data.Consolidators;

namespace QuantConnect.Algorithm.Examples
{

    
    
    public class QCUMovingAverageCross : QCAlgorithm
    {
        private const string Symbol = "XPDUSD";
        
        private ExponentialMovingAverage fast;
        private SimpleMovingAverage slow;
        private SimpleMovingAverage[] ribbon;
        
        int Qty = 4;

        public override void Initialize()
        {
            
            SetStartDate(2016, 01, 01);
            SetEndDate(2018, 04, 15 );
            SetBrokerageModel(BrokerageName.OandaBrokerage);
            SetCash(1200);

            
            AddSecurity(SecurityType.Cfd, Symbol, Resolution.Minute);

            
            fast = EMA(Symbol, 25, Resolution.Daily); 
 
            
            slow = SMA(Symbol, 99, Resolution.Daily);

         
            int ribbonCount = 7;
            int ribbonInterval = 15*8;
            ribbon = new SimpleMovingAverage[ribbonCount];
            
            for(int i = 0; i < ribbonCount; i++) 
            {
                ribbon[i] = SMA(Symbol, (i + 1)*ribbonInterval, Resolution.Hour);
            }
        }

        private DateTime previous;
        public void OnData(Slice data)
        {
            

           
            if (!slow.IsReady) return;

            
            if (previous.Date == data.Time.Date) return;

            
            const decimal tolerance = 0.00015m;
            var holdings = Portfolio[Symbol].Quantity;

           


            if (holdings == 0 )
            {
                
                if (fast > slow * (1 + tolerance))
                {
                    Log("BUY  >> " + Securities[Symbol].Price);
                    Order(Symbol, Qty);
                }
            }

            
            if (holdings > 0 && fast <= slow)
            {
                Log("SELL >> " + Securities[Symbol].Price);
                Liquidate(Symbol);    
            }
            Plot(Symbol, "Price", data[Symbol].Price);
            Plot("Ribbon", "Price", data[Symbol].Price);
            
            
            Plot(Symbol, fast, slow);
            Plot("Ribbon", ribbon);

            previous = data.Time;
        }
    }
}