Overall Statistics
namespace QuantConnect 
    	Title:	Very Simple ADX DI+ DI- Crossing algorithm
    	Author:	Johannes Ferner, johannes@ferner.me
    	Date: 	Sat 4. Feb. 2017
    	The Idea is to trade the crossings of DI+ and DI- through a ADX minimum level
    	Actions are only taken if the current ADX is above a certain minimum level
    	- Only open new Positions if ADX Value > 20 (_minADXlevel)
    	- Open Sell Position if DI- crossed through _minADXlevel and DI+ < _minADXlevel
    	- Open Buy Position if DI+ crossed through _minADXlevel and DI- < _minADXlevel
    	Next Steps:
    	- Initial StopLoss
    	- Trailing StopLoss based on some ATR Magic
    public class BasicTemplateAlgorithm : QCAlgorithm
    	string _symbol = "EURUSD";
    	AverageDirectionalIndex _adx;
    	QuoteBarConsolidator _consolidator;
    	decimal _minADXlevel = 20;
    	bool _firstRun = true;
    	decimal _beforeADXlevel, _beforeNegativeDirectionalIndex, _beforePositiveDirectionalIndex;

        public override void Initialize() 
            //Start and End Date range for the backtest:
            SetStartDate(2017, 2, 1);         
            //Cash allocation
            // EURUSD 1:100 Leverage 1 Minute Data
            AddForex(_symbol, Resolution.Minute, Market.FXCM, true, 100m);
			//AddForex(string ticker, Resolution resolution = Resolution.Minute, string market = Market.FXCM, bool fillDataForward = true, decimal leverage = 0m)

			// Set ADX Indicator
            _adx = ADX(_symbol, 14, Resolution.Minute);

			// Setup the FiveMinutesCallback            
            _consolidator = new QuoteBarConsolidator( TimeSpan.FromMinutes(5) );
            _consolidator.DataConsolidated += FiveMinutesCallback;
            SubscriptionManager.AddConsolidator(_symbol, _consolidator);
		public void Buy(TradeBars data){
			// Check for Buy-Oportunity
			Log("Current ADX: "+_adx.Current.Value + " minAdxLevel " + _minADXlevel);
        	if(_minADXlevel < _adx.Current.Value){
        		Log("Should BUY EURUSD!");
        		int quantity = 100; // TODO this should be something more sophisticated
                MarketOrder("EURUSD", 100);

                //Order function places trades: enter the string symbol and the quantity you want:
                Order(_symbol,  quantity); // TODO Why is this not executed?!
                MarketOrder("EURUSD", 100);
                //Debug sends messages to the user console: "Time" is the algorithm time keeper object 
                Debug("Purchased "+_symbol+" on " + Time.ToShortDateString());
		public void Sell(TradeBars data){
			// Check for Sell-Oportunity
        //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
        public void OnData(TradeBars data) 
        	Log("1M ADX Current "+ _adx.Current);

        	 // Set the current data to "Before"-Data of next iteration
        	_beforeADXlevel = _adx.Current.Value;
        	_beforePositiveDirectionalIndex = _adx.PositiveDirectionalIndex;
        	_beforeNegativeDirectionalIndex = _adx.NegativeDirectionalIndex;
			// Set firstRun to false in order to execute Strategy on Next Data Call
            _firstRun = false;
        	Log("EURUSD: "+Portfolio[_symbol]);
        	if (Portfolio[_symbol].Invested) 
            	Log("Portfolio holds a EURUSD share!");
        	if (!Portfolio[_symbol].Invested) 
				Log("Portfolio holds NO EURUSD share!");
         public void FiveMinutesCallback(Object o, QuoteBar bar) 
        	// TODO check if this is Minute-Data or 5-Minute Data
            Log("5Minutes!"+Time.ToString("u") + "ADX: "+ _adx.Current+"Negative:"+ _adx.NegativeDirectionalIndex+" Positive:"+ _adx.PositiveDirectionalIndex);