Overall Statistics
using System;
using System.Collections.Generic;
using NodaTime;
using QuantConnect.Data;
using QuantConnect.Data.Custom;

namespace QuantConnect.Algorithm.CSharp
{
    /// <summary>
    /// Daily Fx demonstration to call on and use the FXCM Calendar API
    /// </summary>
    
    public class DailyFxAlgorithm : QCAlgorithm
    {
        /// <summary>
        /// Add the Daily FX type to our algorithm and use its events.
        /// </summary>
        private string symbol = "EURUSD";
        private OrderTicket buyOrder;
        private OrderTicket sellOrder;
       	decimal stop = 0.0005m;
       	decimal limit = 0.0002m;
        int buyQty = 10000;
        int sellQty = -10000;
        
        decimal price;
        decimal limitPrice;
        decimal stopPrice;
        decimal trailPercent;
        decimal stopLimitBuy;
        decimal stopLossBuy;
        decimal stopLimitSell;
        decimal stopLossSell;


        // int buyOrder;
        // int sellOrder;
        
        public override void Initialize()
        {
        	SetTimeZone(TimeZones.Utc);
            SetStartDate(2018, 6, 25);  //Set Start Date
            SetEndDate(2018, 07, 05);    //Set End Date
            SetCash(1000);             //Set Strategy Cash
            AddData<DailyFx>("DFX", Resolution.Tick, DateTimeZone.Utc);
            AddForex(symbol, Resolution.Tick, Market.Oanda);
            SetBrokerageModel(BrokerageName.OandaBrokerage);
            
            // Schedule.On(DateRules.EveryDay(), TimeRules.Every(TimeSpan.FromMinutes(60)), () =>
            // {
            //     Transactions.CancelOpenOrders(symbol);	
            // });
            // Schedule.On(DateRules.EveryDay(), TimeRules.Every(TimeSpan.FromMinutes(60)), () =>
            // {
            //     Liquidate(symbol);	
            // });            
        }

        public override void OnData(Slice QuoteBars)
        {
            //
        }

        /// <summary>
        /// Trigger an event on a complete calendar event which has an actual value.
        /// </summary>
        public void OnData(DailyFx calendar)
        {

			price = Securities[symbol].Close;

		
        	// Trigger for order
        	if(calendar.Importance != FxDailyImportance.High) // return;
            {
            	if(!Portfolio.Invested)
            	{
            		if(calendar.Meaning == FxDailyMeaning.Better)
            		{
		 	        	OrderTicket buyOrder = LimitOrder(symbol, buyQty, price + limit);
			        	stopLossBuy = price - stop;
			        	Log("BUY: Price: "+price+", Limit: "+(price + limit)+", Ticket: "+buyOrder+"Stoploss 1: "+stopLossBuy);
	            		// Transactions.CancelOpenOrders(symbol);		        	
			        	//Log("BUY: StopLimit: "+stopLimitBuy+"StopLoss: "+stopLossBuy);            			
            		}
					else if(calendar.Meaning == FxDailyMeaning.Worse)
					{
			        	OrderTicket sellOrder = LimitOrder(symbol, sellQty, price - limit);
			        	stopLossSell = price + stop;
			        	Log("SELL: Price: "+price+", Limit: "+(price - limit)+", Ticket: "+buyOrder+"Stoploss: "+stopLossSell); 
			        	// Transactions.CancelOpenOrders(symbol);						
					}
            	}
            }
			// var newBuyStop = buyOrder.Get(OrderField.StopPrice) - stop;            
			stopLossBuy = Math.Max(stopLossBuy, price - stop);
			Log("Stop;oss 2: "+stopLossBuy);
			stopLossBuy = stopLossBuy;
			Log("Stoploss 3: "+stopLossBuy);
			// var newSellStop = buyOrder.Get(OrderField.StopPrice) - stop;			
			stopLossSell = Math.Min(stopLossSell, price + stop);
			stopLossSell = stopLossSell;

            if(Portfolio.Invested)
            {
            	Transactions.CancelOpenOrders(symbol);
            	// Update StopLossSell & stopLossBuy
            	// https://www.quantconnect.com/forum/discussion/1072/setting-a-stop-loss-and-take-profit
            	if(Portfolio[symbol].Quantity > 0)
            	{
            		if(price < stopLossBuy)
            		{
            			Liquidate(symbol);
            			Log("BUY Close: "+price);
            		}
            	}
     //       	if(Portfolio[symbol].Quantity < 0)
     //       	{
					// if(price > stopLossSell)
					// {
					// 	Liquidate(symbol);
     //       			Log("SELL Close: "+price);						
					// }
     //       	}
            }
           
        }
    }
}