Overall Statistics
Total Trades
2
Average Win
0%
Average Loss
-0.03%
Compounding Annual Return
-2.443%
Drawdown
0.000%
Expectancy
-1
Net Profit
-0.034%
Sharpe Ratio
-6.481
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.015
Beta
0.009
Annual Standard Deviation
0.002
Annual Variance
0
Information Ratio
-3.699
Tracking Error
0.025
Treynor Ratio
-1.674
Total Fees
$4.00
/*
* TODO dont reload data for first minutes if same contract?
 *
*/

using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Brokerages;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;
using QuantConnect.Orders;
using QuantConnect.Securities;

namespace QuantConnect.Algorithm.CSharp
{
    /// <summary>
    /// in up trend, plays BB20 rebound
    /// we exit all at 2BB top
    /// we exit half at 1std up
    /// we stop at trailing pull level once profit taken(running lower band or mid band)
    /// we stop at pull level before

    /// once profit taken, exit logic is we go back below one of the thresholds 2std
    /// stop loss logic is once a threshold(u, m, d) is crossed we set it to 1std below threshold

    /// </summary>
    public class AaPullbackFx : QCAlgorithm
    {
        Security _contract = null;

        private decimal _stopLoss = 0;
        private int _trade_count = 0;
        private decimal _portfolio_ref_value = 100000;

		
		private string SYMBOL = "EURUSD";
		
        public override void Initialize()
        {
            SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin);
            SetStartDate(year:2017, month:9, day:6);
            SetEndDate(year: 2017, month: 9, day: 10);
            
            SetCash(_portfolio_ref_value);
            _contract = AddSecurity(SecurityType.Forex, SYMBOL, Resolution.Minute);
           
        }

        public override void OnData(Slice slice)
        {
            var tickSize = Securities[SYMBOL].SymbolProperties.MinimumPriceVariation;
            var cBar = slice.Bars[SYMBOL];
			Log("bar - H: " + cBar.High.ToString("#.00000") + "  L: " + cBar.Low.ToString("#.00000") + " C: " + cBar.Close.ToString("#.00000"));

            if (!Portfolio.Invested && _trade_count == 0)
            {
                //Log("Pullback recovery trigger: price @ " + futureBar.Close.ToString("#.00000") + " BBD @ " + _bdown[1].ToString("#.00000") + " - std @" + std.ToString("#.00000") );
                SetHoldings(SYMBOL, 1);
                _trade_count++;
                
                _stopLoss = 1.1908m;
                Log("Buying: Price is @ " + cBar.Close.ToString("#.00000") + " STOP is @ " + _stopLoss.ToString("#.00000") );
            }
            
            //we wait for all orders to be filled
            if (Portfolio[Securities[SYMBOL].Symbol].Quantity>0 && Transactions.GetOpenOrders().Count == 0)
            {
            	
                var orders = Transactions.GetOpenOrders(SYMBOL);
                if (orders.Count == 0)
                {
                    decimal qty = Portfolio[SYMBOL].Quantity;
                    Log("Setting stop loss @ " + _stopLoss.ToString());
                    StopMarketOrder(SYMBOL, -qty, _stopLoss);
                }
            }
        }

        public override void OnEndOfDay()
        {
            //Plot("Indicator Signal", "EOD", IsDownTrend ? -1 : IsUpTrend ? 1 : 0);
            Log("EOD***********************");
            _portfolio_ref_value = Portfolio.TotalPortfolioValue;
        }

        public override void OnOrderEvent(OrderEvent orderEvent)
        {
            var order = Transactions.GetOrderById(orderEvent.OrderId);
            string msg = "OrderEvent: " + order.Status.ToString() + ": " + order.Direction.ToString() + " " + order.Quantity.ToString("#.0") + " " + order.Symbol;
            if (order.Type == OrderType.StopMarket)
            {
                var o = ((QuantConnect.Orders.StopMarketOrder) order);
                msg += " - STOP @ " + o.StopPrice.ToString("#.00000");                
            }
            else if (order.Type == OrderType.Market)
            {
                
            }
            else if (order.Type == OrderType.Limit)
            {
                var o = ((QuantConnect.Orders.LimitOrder)order);
                msg += " - LIMIT @ " + o.LimitPrice.ToString("#.00000");
            }
            if (order.Status == OrderStatus.Filled)
            {
            	msg += " - filled @ " + order.Price.ToString("#.00000");
            }
            Log(msg);
        }
    }
}