Overall Statistics
Total Trades
2
Average Win
0%
Average Loss
0%
Compounding Annual Return
-59.305%
Drawdown
14.800%
Expectancy
0
Net Profit
0%
Sharpe Ratio
-4.61
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.503
Beta
-0.502
Annual Standard Deviation
0.129
Annual Variance
0.017
Information Ratio
-4.506
Tracking Error
0.173
Treynor Ratio
1.187
Total Fees
$4.00
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;
using QuantConnect.Orders;
using System;
using System.Linq;

namespace QuantConnect.Algorithm.CSharp
{
    /// <summary>

    /// </summary>
    public class QCUMovingAverageCross : QCAlgorithm
    {
        int cnt = 0;
        const decimal tolerance = 0.00015m;
        private const string ticker = "EURUSD";
        Symbol symbol;
        private ExponentialMovingAverage fast;
        private ExponentialMovingAverage slow;
        private AverageTrueRange atr;

        //private decimal price;

        //private OrderTicket orderLong = null;
        private OrderTicket orderShort = null;

        //private SimpleMovingAverage ma50;
        //private SimpleMovingAverage ma100;
        private SimpleMovingAverage ma100_h1;
        private SimpleMovingAverage ma100_m30;
        private SimpleMovingAverage ma100_m15;
        private SimpleMovingAverage ma50_m30;

        public override void Initialize()
        {
            // set up our analysis span
            SetStartDate(2016, 11, 01);
            SetEndDate(2016, 12, 01);
            SetCash(10000);

            symbol = AddForex(ticker, market: Market.Oanda).Symbol;
            //AddSecurity(SecurityType.Forex, ticker, Resolution.Minute);
            
            //price = Securities[ticker].Price;
            fast = EMA(symbol, 15, Resolution.Minute);
            slow = EMA(symbol, 50, Resolution.Hour);

            ma100_h1 = SMA(symbol, 100, Resolution.Hour);

            ma100_m15 = new SimpleMovingAverage(100);

            ma50_m30 = new SimpleMovingAverage(50);
            ma100_m30 = new SimpleMovingAverage(100);

            var fifteenMinuteConsolidator = new QuoteBarConsolidator(TimeSpan.FromMinutes(15));
            SubscriptionManager.AddConsolidator(symbol, fifteenMinuteConsolidator);
            fifteenMinuteConsolidator.DataConsolidated += FifteenMinuteConsolidator_DataConsolidated; ;

            var thirtyMiutneConsolidator = new QuoteBarConsolidator(TimeSpan.FromMinutes(30));
            SubscriptionManager.AddConsolidator(symbol, thirtyMiutneConsolidator);
            thirtyMiutneConsolidator.DataConsolidated += ThirtyMiutneConsolidator_DataConsolidated; ;

            atr = ATR(symbol, 450, MovingAverageType.Simple, Resolution.Minute);

            SetWarmUp(TimeSpan.FromDays(5));
        }

        private void FifteenMinuteConsolidator_DataConsolidated(object sender, QuoteBar e)
        {
            ma100_m15.Update(new IndicatorDataPoint { Time = e.Time, Value = e.Value });
        }

        private void ThirtyMiutneConsolidator_DataConsolidated(object sender, QuoteBar e)
        {
            ma50_m30.Update(new IndicatorDataPoint { Time = e.Time, Value = e.Value });
            ma100_m30.Update(new IndicatorDataPoint { Time = e.Time, Value = e.Value });
        }

        //private DateTime previous;

        public override void OnData(Slice data)
        {
            if (IsWarmingUp || !data.ContainsKey(symbol)) return;
            var qty = Portfolio[symbol].Quantity;
            //Debug("qty: " + qty);

            //price = Securities[ticker].Price;

            // define a small tolerance on our checks to avoid bouncing

            var atrLoss = atr * 0.3m;
            var atrProfit = atr * 0.15m;
            var stopLoss = Securities[symbol].Price - atrLoss;
            var takeProfit = Securities[symbol].Price + atrProfit;

            if (enterLong() && !Portfolio[symbol].Invested)
            {
                Debug("long symbol " + Securities[symbol].Price + " SL: " + stopLoss + " TP: " + takeProfit);
                var asd = StopLimitOrder(symbol, 10000, stopLoss, takeProfit, "test");
            }

            /*if (enterShort())
            {
                Debug("short symbol " + Securities[symbol].Price + " SL: " + stopLoss + " TP: " + takeProfit);
                orderShort = StopLimitOrder(symbol, -1000, stopLoss, takeProfit, "test");
            }*/


            // Here you can use LINQ to select the tickets you need.
            var openLongOrderTicket = Transactions.GetOrderTickets(o => o.Status == OrderStatus.Filled && o.Quantity > 0)
                                              .FirstOrDefault();

            if (openLongOrderTicket != null)
            {
                var stopPrice = openLongOrderTicket.Get(OrderField.StopPrice);
                // Bug?
                //var newStopPrice = stopPrice;
                var newStopPrice = stopLoss;

                //decimal limitPrice = orderLong.Get(OrderField.LimitPrice);
                //var newLimitPrice = limitPrice;

                if (stopPrice > stopLoss)
                {
                    Debug("Price: " + data[symbol].Price);
                    openLongOrderTicket.Update(new UpdateOrderFields { StopPrice = newStopPrice });
                    Debug("orderLong modified - SL: " + newStopPrice);
                }
                // Liquidate();
            }

            //previous = data.Time;
            cnt++;
        }

        public override void OnOrderEvent(OrderEvent orderEvent)
        {
            switch (orderEvent.Status)
            {
                case OrderStatus.New:
                    break;
                case OrderStatus.Submitted:
                    break;
                case OrderStatus.PartiallyFilled:
                    break;
                case OrderStatus.Filled:
                    break;
                case OrderStatus.Canceled:
                    break;
                case OrderStatus.None:
                    break;
                case OrderStatus.Invalid:
                    break;
                case OrderStatus.CancelPending:
                    break;
                default:
                    break;
            }
        }

        public override void OnEndOfDay()
        {
            Log("EOD " + Time.ToShortDateString());
        }

        private bool enterShort()
        {
            if (orderShort != null) return false;
            return true;
        }

        private bool enterLong()
        {
            var price = Securities[symbol].Price;

            if ((price > ma100_h1 * (1 + tolerance)) &&
                (price > ma100_m30 * (1 + tolerance)) &&
                (price > ma100_m15 * (1 + tolerance)) &&
                (ma100_m30 > ma50_m30))
            {
                return true;
            }

            return false;
        }
    }
}