Overall Statistics
using System;

namespace QuantConnect.Algorithm.CSharp
{

    public class TestEURJPY : QCAlgorithm
    {
    	//USER VARIABLES
		private string _AccountCurrency = "USD";
    	private string _Symbol = "EURJPY";
    	private decimal _StartingCash = 100000;
    	int _MaxPosition = 50000;
    	int _MinPosition = 1000;
    	int _FastPeriod = 20;
    	int _SlowPeriod = 50;
    	int _TradeCount = 0;


		// PROGRAM VARIABLES
		public decimal _Price;
		public decimal _Quantity;
		public string _BaseSymbol;
		public string _QuoteSymbol;
		public decimal _AvailableMargin;
		public decimal _LotSize;
		public decimal _ConversionRate;
		public decimal _EntryPrice;
		public decimal _ExitPrice;
		public decimal _EntryConversionRate;
		public decimal _ExitConversionRate;
		public decimal _ExpectedPnL;
		public decimal _ResidualBaseQuantity;
		public decimal _ResidualQuoteQuantity;
			
		ExponentialMovingAverage _FastEMA;
		ExponentialMovingAverage _SlowEMA;
		
		
        // INITIALIASE BLOCK
        public override void Initialize()
        {
        	SetStartDate(2016, 01, 01);
            SetEndDate(2017, 12, 31);
			SetAccountCurrency(_AccountCurrency);
            SetCash(_StartingCash);

            var _Forex = AddForex(_Symbol, Resolution.Daily, Market.Oanda, true, 20m);
            _BaseSymbol = _Forex.BaseCurrencySymbol;
            
			SetBrokerageModel(BrokerageName.OandaBrokerage, AccountType.Margin);
            SetWarmUp(TimeSpan.FromDays(200));

            _FastEMA = EMA(_Symbol, _FastPeriod, Resolution.Daily);
            _SlowEMA = EMA(_Symbol, _SlowPeriod, Resolution.Daily);
        }
        
        // ONDATA BLOCK
        public override void OnData(Slice data)
        {
        	_Price = data[_Symbol].Close;
        	_AvailableMargin = Portfolio.MarginRemaining;
			_LotSize = Portfolio.Securities[_Symbol].SymbolProperties.LotSize;
			_ConversionRate = Portfolio.Securities[_Symbol].QuoteCurrency.ConversionRate;

        	if (!Portfolio.Invested && _AvailableMargin > _MinPosition && _TradeCount < 1)
            {
                _Quantity = Math.Round((_MaxPosition / (_Price * _ConversionRate)) / _LotSize, 0) * _LotSize;
                if(_FastEMA > _SlowEMA)
                {
                	var _EntryTicket = MarketOrder(_Symbol, _Quantity);
                	if (_EntryTicket.Status == OrderStatus.Filled)
                	{
                		_EntryPrice = _EntryTicket.AverageFillPrice;
                		_EntryConversionRate = _ConversionRate;
                		_TradeCount++;
                		_QuoteSymbol = Portfolio.Securities[_Symbol].SymbolProperties.QuoteCurrency;
                	}
                }
            }
             
            if(Portfolio.Invested)
            {
            	_Quantity = Portfolio[_Symbol].Quantity;
            	if(_Price > _EntryPrice * 1.05m || _Price < _EntryPrice * 0.95m)
           		{
	        		var _ExitTicket = MarketOrder(_Symbol, -_Quantity);
                	if (_ExitTicket.Status == OrderStatus.Filled)
                	{
                		_ExitPrice = _ExitTicket.AverageFillPrice;
                		_ExitConversionRate = _ConversionRate;
			            _ExpectedPnL = (_ExitPrice - _EntryPrice) * _Quantity * _ExitConversionRate;
			            Log($"Expected PnL in {_AccountCurrency} is {Math.Round(_ExpectedPnL,2)}");
			            Log($"Expected ending Equity in {_AccountCurrency} is {Math.Round(_StartingCash + _ExpectedPnL,2)}");
            			_ResidualBaseQuantity = Portfolio.CashBook[_BaseSymbol].Amount;
            			_ResidualQuoteQuantity = Portfolio.CashBook[_QuoteSymbol].Amount;
            			//MarketOrder(_AccountCurrency+_QuoteSymbol, _ResidualQuoteQuantity);
            			_ResidualQuoteQuantity = Portfolio.CashBook[_QuoteSymbol].Amount;
                	}
                }
            }
            
        }
    }
}