Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
-41.206%
Drawdown
7.500%
Expectancy
0
Net Profit
0%
Sharpe Ratio
-5.267
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.295
Beta
-0.226
Annual Standard Deviation
0.067
Annual Variance
0.004
Information Ratio
-4.948
Tracking Error
0.124
Treynor Ratio
1.563
Total Fees
$2.00
namespace QuantConnect.Algorithm.Examples
{
    /// <summary>

    /// </summary>
    public class QCUMovingAverageCross : QCAlgorithm
    {
        private const string symbol = "EURUSD";
        private ExponentialMovingAverage fast;
        private ExponentialMovingAverage slow;
        private AverageTrueRange atr;
        private SimpleMovingAverage ma50;
        private SimpleMovingAverage ma100;
        private OrderTicket orderLong = null;
        private OrderTicket orderShort = null;
        private decimal price;
        
        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(1000);
            AddSecurity(SecurityType.Forex, symbol, Resolution.Minute);
            SetRunMode(RunMode.Series);
            
            price = Securities[symbol].Price;
            fast = EMA(symbol, 15, Resolution.Minute);
            slow = EMA(symbol, 50, Resolution.Hour);
            
            ma100_h1 = SMA(symbol, 900, Resolution.Minute);
            ma100_m30 = SMA(symbol, 450, Resolution.Minute);
            ma100_m15 = SMA(symbol, 225, Resolution.Minute);
            ma50_m30 = SMA(symbol, 225, Resolution.Minute);
            
            
            atr = ATR(symbol, 450, MovingAverageType.Simple, Resolution.Minute);
        }

        private DateTime previous;
        public void OnData(TradeBars data)
        {
            if (!slow.IsReady) return;
            
            var qty = Portfolio[symbol].Quantity;
            //Debug("qty: " + qty);
            
            price = Securities[symbol].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())
            {
                Debug("long symbol " + Securities[symbol].Price + " SL: " + stopLoss + " TP: " + takeProfit);
                orderLong = StopLimitOrder(symbol, 1000, stopLoss, takeProfit, "test");
            }
            
            /*if (enterShort())
            {
                Debug("short symbol " + Securities[symbol].Price + " SL: " + stopLoss + " TP: " + takeProfit);
                orderShort = StopLimitOrder(symbol, -1000, stopLoss, takeProfit, "test");
            }*/
            
            
            if (orderLong != null)
            {
            	Debug(orderLong.Status.ToString());
            	
            	decimal stopPrice = orderLong.Get(OrderField.StopPrice);
            	var newStopPrice = stopPrice;
            	
            	//decimal limitPrice = orderLong.Get(OrderField.LimitPrice);
            	//var newLimitPrice = limitPrice;
            	
            	
            	if (stopPrice > stopLoss)
            	{
            		Debug("Price: " + price);
            		orderLong.Update(new UpdateOrderFields{StopPrice = newStopPrice});
            		Debug("orderLong modified - SL: " + stopPrice);
                }
            	// Liquidate();
            
            }


            previous = data.Time;
        }
        
        bool enterShort()
        {
        	if (orderShort != null) return false;
        	return true;
        }
        
        bool enterLong()
        {
        	const decimal tolerance = 0.00015m;
        	if (orderLong != null) return false;
        	//if (fast > slow * (1 + tolerance)) return true;
        	
        	if ((price > ma100_h1 * (1 + tolerance)) &&
        		(price > ma100_m30 * (1 + tolerance)) &&
        		(price > ma100_m15 * (1 + tolerance)) &&
        		(ma100_m30 > ma50_m30))
        	{
        		return true;
        		
        	}
        	
        	return false;
        }
    }
}
namespace QuantConnect {

    //
    //	Make sure to change "BasicTemplateAlgorithm" to your algorithm class name, and that all
    //	files use "public partial class" if you want to split up your algorithm namespace into multiple files.
    //

    //public partial class BasicTemplateAlgorithm : QCAlgorithm, IAlgorithm
    //{
    //  Extension functions can go here...(ones that need access to QCAlgorithm functions e.g. Debug, Log etc.)
    //}

    //public class Indicator 
    //{
    //  ...or you can define whole new classes independent of the QuantConnect Context
    //}

}
namespace QuantConnect {

    //
    //	Make sure to change "BasicTemplateAlgorithm" to your algorithm class name, and that all
    //	files use "public partial class" if you want to split up your algorithm namespace into multiple files.
    //

    //public partial class BasicTemplateAlgorithm : QCAlgorithm, IAlgorithm
    //{
    //  Extension functions can go here...(ones that need access to QCAlgorithm functions e.g. Debug, Log etc.)
    //}

    //public class Indicator 
    //{
    //  ...or you can define whole new classes independent of the QuantConnect Context
    //}

}