Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
/*
 * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
 * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

namespace QuantConnect.Indicators
{
	
    public class ExponentialMovingAverageDeriv : Indicator
    {
        private readonly decimal _k;
        private readonly int _period;
        private List<decimal> EMA_list;

        public ExponentialMovingAverageDeriv(string name, int period)
            : base(name)
        {
            _period = period;
            _k = 2 / ((decimal)period + 1);
            EMA_list = new List<decimal>();
        }

        public ExponentialMovingAverageDeriv(string name, int period, decimal smoothingFactor)
            : base(name)
        {
            _period = period;
            _k = smoothingFactor;
        }

        public ExponentialMovingAverageDeriv(int period)
            : this("EMA" + period, period)
        {
        }

        public ExponentialMovingAverageDeriv(int period, decimal smoothingFactor)
            : this("EMA" + period, period, smoothingFactor)
        {
        }
        
        public decimal ReturnChangeInEMA(int period) {
        	if (EMA_list.Count > period) {
        		return EMA_list[EMA_list.Count-1] - EMA_list[EMA_list.Count-1-period];
        	} else {
        		return 0;
        	}
        }

        public override bool IsReady
        {
            get { return Samples > _period; }
        }

        protected override decimal ComputeNextValue(IndicatorDataPoint input)
        {
            // our first data point just return identity
            if (Samples == 1)
            {
                return input;
            }
            // Add to list
            EMA_list.Add(input*_k + Current*(1 - _k));
            
            return input*_k + Current*(1 - _k);
        }
    }
}
namespace QuantConnect 
{   
    /*
    *   Some tools
    */
    public class Aggressive_HFT_Algo : QCAlgorithm
    {
    	
        //Initialize the data and resolution
        public override void Initialize() 
        {
			// set up our analysis span
            SetStartDate(2009, 01, 01);
            SetEndDate(2009, 02, 01);
            SetCash(100000);

            // request SPY data with minute resolution
            AddSecurity(SecurityType.Equity, "F", Resolution.Minute);
        }
        
		public void OnData(TradeBars data)
        {
            //
        }
    }
}
namespace QuantConnect {

    public class ExchangeDataFilter : ISecurityDataFilter
    {
        private IAlgorithm _algo;

        //Save instance of algorithm namespace
        public ExchangeDataFilter(IAlgorithm algo)
        {
            _algo = algo;
        }

        public static class MarketCodesFilter
        {
            /// US Market Codes
            public static Dictionary<string, string> US = new Dictionary<string, string>() 
            {
                {"A", "American Stock Exchange"},
                {"B", "Boston Stock Exchange"},
                {"C", "National Stock Exchange"},
                {"D", "FINRA ADF"},
                {"I", "International Securities Exchange"},
                {"J", "Direct Edge A"},
                {"K", "Direct Edge X"},
                {"M", "Chicago Stock Exchange"},
                {"N", "New York Stock Exchange"},
                {"P", "Nyse Arca Exchange"},
                {"Q", "NASDAQ OMX"},
                {"T", "NASDAQ OMX"},
                {"U", "OTC Bulletin Board"},
                {"u", "Over-the-Counter trade in Non-NASDAQ issue"},
                {"W", "Chicago Board Options Exchange"},
                {"X", "Philadelphia Stock Exchange"},
                {"Y", "BATS Y-Exchange, Inc"},
                {"Z", "BATS Exchange, Inc"}
            };

            /// Canada Market Short Codes:
            public static Dictionary<string, string> Canada = new Dictionary<string, string>() 
            {
                {"T", "Toronto"},
                {"V", "Venture"}
            };

            // Allowed exchanges for this filter: top 4
            public static List<string> BannedExchanges = new List<string>() { 
            	"W", 
            	"u", 
            	"U", 
            	"D" 
            };
        }
        
        public bool Filter(Security asset, BaseData data)
        {
            // TRUE -->  Accept Tick
            // FALSE --> Reject Tick
            var tick = data as Tick;

            // This is a tick bar
            if (tick != null)
            {
                if (!MarketCodesFilter.BannedExchanges.Contains(tick.Exchange) &&
                	!tick.Suspicious == true &&
                	!tick.SaleCondition.Equals("W"))
                {
                    return true;
                }
            }

            //Only allow those exchanges through.
            return false;
        }
    }
}
namespace QuantConnect {

    public class RangeBars 
    {
    	private decimal TICK_SIZE = (decimal) 0.01;
    	private int barRange;
    	private List<RangeBar> BARS;
    	
    	// Current last bar
    	private RangeBar lastBar;
	    
	    public RangeBars(int barRange) {
	    	this.barRange = barRange;
	    	BARS = new List<RangeBar>();
	    }
	    
	    public void AddTick(Tick tick) {
	    	// Round to cents
	    	decimal price = Math.Round(tick.Price, 2);
	    	// For debugging purposes:
	    	//Console.WriteLine("Price: "+price);
	    	
	    	if (lastBar == null) {
	    		// If this is the first bar of the series, create new last bar
	    		lastBar = new RangeBar(tick.Time, TICK_SIZE, price, barRange);
	    	} else {
	    		// Logic for the creation and completion of range bars
	    		decimal d_change = (decimal) (price - lastBar.startPrice) / (decimal) TICK_SIZE;
	    		int change = 0;
	    		if ((d_change % 1) == 0) {
	    			change = (int) Math.Ceiling((price - lastBar.startPrice) / (decimal) TICK_SIZE);
	    		} else {
	    			Console.WriteLine("The TICK_SIZE is apparently wrong: "+d_change);
	    		}
	    		// If outside of possible bounds
	    		if (change > lastBar.possDiffUp) {
	    			lastBar.highDiff = lastBar.possDiffUp;
	    			lastBar.CloseBar(lastBar.possDiffUp);
	    			BARS.Add(lastBar);	
	    			BarClosedSender(lastBar);	// Send to main thread
	    			decimal nPrice = lastBar.startPrice + ((decimal)(lastBar.possDiffUp)+1)*TICK_SIZE;
	    			lastBar = new RangeBar(tick.Time, TICK_SIZE, nPrice, barRange);
	    		} else if (change < lastBar.possDiffDown) {
	    			lastBar.lowDiff = lastBar.possDiffDown;
	    			lastBar.CloseBar(lastBar.possDiffDown);
	    			BARS.Add(lastBar);
	    			BarClosedSender(lastBar);	// Send to main thread
	    			decimal nPrice = lastBar.startPrice + ((decimal)(lastBar.possDiffDown)-1)*TICK_SIZE;
	    			lastBar = new RangeBar(tick.Time, TICK_SIZE, nPrice, barRange);
	    		} else {
	    			// Update current bar
	    			if (change > lastBar.highDiff) {
	    				lastBar.possDiffDown = lastBar.possDiffDown + (change - lastBar.highDiff);
	    				lastBar.highDiff = change;
	    			} else if (change < lastBar.lowDiff) {
	    				lastBar.possDiffUp = lastBar.possDiffUp - (lastBar.lowDiff - change);
	    				lastBar.lowDiff = change;
	    			}
	    		}
	    	}
	    	// Add current tick to last bar
	    	lastBar.AddTick(tick);
	    }
	    
	    public void BarClosedSender(RangeBar bar) {
	    	// Notify function in Main.cs that a new RangeBar has been closed
	    	// FUNCTION BELOW DOESN'T WORK ANYMORE (because RENKO is being used)
	    	//parentShadow.TRIGGER_CHART_UPDATER(this, bar);
	    }
	    
	    public void CleanMemory() {
			if (BARS.Count > 50) {
				int diff = BARS.Count - 50;
				BARS.RemoveRange(0, diff);
			}
		}
    }
}
namespace QuantConnect {

    public class RenkoBars
    {
    	private decimal TICK_SIZE = (decimal) 0.01;
    	public int BRICK_SIZE;
    	public List<RenkoBar> BARS;
    	public RenkoBar CurrentBar;
    	public decimal FirstValue;
    	
		public RenkoBars(int BRICK_SIZE) {
			this.BRICK_SIZE = BRICK_SIZE;
			BARS = new List<RenkoBar>();
		}
		
		public void AddTick(Tick tick) {
			
			if (CurrentBar != null) {
				// Determine last bounds
				decimal HIGH = 0;	
				decimal LOW  = 0;
				RenkoBar LastBar;
				if (BARS.Count > 0) {
					LastBar 			= BARS[BARS.Count - 1];
					HIGH 				= LastBar.O > LastBar.C ? LastBar.O : LastBar.C;
					LOW 				= LastBar.O < LastBar.C ? LastBar.O : LastBar.C;
				} else {
					HIGH 				= FirstValue;
					LOW  				= FirstValue;
				}
				if (tick.Price > HIGH) {
					int u_change = (int) Math.Ceiling((tick.Price - HIGH) / TICK_SIZE);
					if (u_change > BRICK_SIZE) {
						// If null bar
						int nofNULL = (int) Math.Floor(((decimal)u_change) / ((decimal)BRICK_SIZE));
						for (int i = 0; i < nofNULL; i++) {
							CurrentBar.O 		= HIGH;
							CurrentBar.C 		= HIGH + ((decimal)BRICK_SIZE) * TICK_SIZE;
							CurrentBar.L 		= CurrentBar.O < CurrentBar.L ? CurrentBar.O : CurrentBar.L;
							CurrentBar.H 		= CurrentBar.C > CurrentBar.H ? CurrentBar.C : CurrentBar.H;
							if (i < nofNULL - 1) {
								// Not tradable!
								CurrentBar.Tradable = false;	
							} else {
								CurrentBar.Tradable = true;
							}
							BARS.Add(CurrentBar);
							BarClosedSender(CurrentBar);
							CurrentBar = new RenkoBar(BRICK_SIZE, tick.Time, CurrentBar.C);
							LastBar 	= BARS[BARS.Count - 1];
							HIGH 				= LastBar.O > LastBar.C ? LastBar.O : LastBar.C;
							LOW 				= LastBar.O < LastBar.C ? LastBar.O : LastBar.C;
						}
						// Add tick to current bar
						CurrentBar.AddTick(tick);
					} else {
						CurrentBar.H = tick.Price > CurrentBar.H ? tick.Price : CurrentBar.H;
						CurrentBar.AddTick(tick);
					}
				} else if (tick.Price < LOW) {
					int d_change = (int) Math.Ceiling((LOW - tick.Price) / TICK_SIZE);
					if (d_change > BRICK_SIZE) {
						int nofNULL = (int) Math.Floor(((decimal)d_change) / ((decimal)BRICK_SIZE));
						for (int i = 0; i < nofNULL; i++) {
							// Close bar and make new bar
							CurrentBar.O = LOW;
							CurrentBar.C = LOW - ((decimal)BRICK_SIZE) * TICK_SIZE;
							CurrentBar.L = CurrentBar.C < CurrentBar.L ? CurrentBar.C : CurrentBar.L;
							CurrentBar.H = CurrentBar.O > CurrentBar.H ? CurrentBar.O : CurrentBar.H;
							if (i < nofNULL - 1) {
								// Not tradable!
								CurrentBar.Tradable = false;	
							} else {
								CurrentBar.Tradable = true;
							}
							BARS.Add(CurrentBar);
							BarClosedSender(CurrentBar);
							CurrentBar = new RenkoBar(BRICK_SIZE, tick.Time, CurrentBar.C);
							LastBar 	= BARS[BARS.Count - 1];
							HIGH 				= LastBar.O > LastBar.C ? LastBar.O : LastBar.C;
							LOW 				= LastBar.O < LastBar.C ? LastBar.O : LastBar.C;
						}
						CurrentBar.AddTick(tick);
					} else {
						CurrentBar.L = tick.Price < CurrentBar.L ? tick.Price : CurrentBar.L;
						CurrentBar.AddTick(tick);
					}
				}
			} else {
				// If first bar
				CurrentBar = new RenkoBar(BRICK_SIZE, tick.Time, tick.Price);
				CurrentBar.AddTick(tick);
				FirstValue = tick.Price;
			}
		}
		
		public void BarClosedSender(RenkoBar bar) {
	    	// Notify function in Main.cs that a new RangeBar has been closed
	    	// ...
	    }
	    
	    public void CleanMemory() {
			if (BARS.Count > 50) {
				int diff = BARS.Count - 50;
				BARS.RemoveRange(0, diff);
			}
		}
    }

}
namespace QuantConnect {

    public class RenkoBar 
    {
    	private int BRICK_SIZE;
    	private List<Tick> TicksInBar;
    	
    	public bool Tradable;
    	public DateTime Time;
    	public decimal O;
    	public decimal H;
    	public decimal L;
    	public decimal C;
    	
      	public RenkoBar(int BRICK_SIZE, DateTime Time, decimal O) {
      		this.BRICK_SIZE = BRICK_SIZE;
      		this.Time = Time;
      		TicksInBar = new List<Tick>();
      		this.O = O;
      		this.H = O;
      		this.L = O;
      		this.C = O;
      	}
      	
		public void AddTick(Tick tick) {
    		TicksInBar.Add(tick);
    	}
    	
    	public bool IsTradable() {
    		return this.Tradable;
    	}
      	
    }

}
namespace QuantConnect {

    public class RangeBar 
    {
    	private decimal TICK_SIZE;
    	public DateTime 	Time;
    	public decimal 		startPrice;
    	public int			possDiffUp;
    	public int 			possDiffDown;
    	public int 			highDiff;
    	public int 			lowDiff;
    	public int 			endDiff;
    	public int			direction;
    	private List<Tick> 	ticksInBar;
    	
    	public decimal 		O;
    	public decimal		H;
    	public decimal 		L;
    	public decimal		C;
    	
    	public RangeBar(DateTime Time, decimal TICK_SIZE, decimal startPrice, int range) {
    		this.Time 			= Time;
    		this.TICK_SIZE		= TICK_SIZE;
    		this.startPrice		= startPrice;
    		this.possDiffUp 	= range;
    		this.possDiffDown 	= -1*range;
    		this.highDiff		= 0;
    		this.lowDiff		= 0;
    		this.ticksInBar 	= new List<Tick>();
    	}
    	
    	public void AddTick(Tick tick) {
    		ticksInBar.Add(tick);
    	}
    	
    	public void CloseBar(int endDiff) {
    		this.endDiff = endDiff;
    		//Console.WriteLine(ticksInBar.Count);

    		this.O = startPrice;
    		this.H = startPrice + this.highDiff*TICK_SIZE;
    		this.L = startPrice + this.lowDiff*TICK_SIZE;
    		this.C = startPrice + endDiff*TICK_SIZE;
    		
    		if (this.O < this.C) {
    			this.direction = 1;
    		} else if (this.C < this.O) {
    			this.direction = -1;
    		} else {
    			this.direction = 0;
    		}
    	}
    }

}
namespace QuantConnect {

    public class NormalBar
    {
		public DateTime Time;
		public decimal O;
		public decimal H;
		public decimal L;
		public decimal C;
		
		public NormalBar(DateTime T, decimal O, decimal H, decimal L, decimal C) {
			this.Time = T;
			this.O = O;
			this.H = H;
			this.L = L;
			this.C = C;
		}
    }

}
namespace QuantConnect {

    public class HeikinAshiBars 
    {
		private List<NormalBar> HA;
		
		public HeikinAshiBars() {
			HA = new List<NormalBar>();
		}
		
		public void ProcessNewBar(NormalBar bar) {
			if (HA.Count > 0) {
				// If not first bar
				NormalBar lastBar = HA[HA.Count - 1];
				
	    		decimal close = (bar.O + bar.H + bar.L + bar.C)/4;
	    		decimal open  = (lastBar.O + lastBar.C)/2;
	    		decimal high  = (new[] {bar.H, open, close}).Max();
	    		decimal low   = (new[] {bar.L, open, close}).Min();
	    		
	    		NormalBar temp = new NormalBar(bar.Time, open, high, low, close);
	    		HA.Add(temp);
	    		
	    	} else {
	    		// If first bar
	    		decimal close = (bar.O + bar.H + bar.L + bar.C)/4;
	    		decimal open  = (bar.O);
	    		decimal high  = (new[] {bar.H, open, close}).Max();
	    		decimal low   = (new[] {bar.L, open, close}).Min();
	    		
	    		NormalBar temp = new NormalBar(bar.Time, open, high, low, close);
	    		HA.Add(temp);
	    	}
		}
		
		public int GetLastColor() {
			NormalBar lastBar = HA[HA.Count - 1];
			if (lastBar.C > lastBar.O) {
				return 1;
			} else if (lastBar.C < lastBar.O) {
				return -1;
			} else {
				return 0;
			}
		}
		
		public int IsFirstColor() {
			if (HA.Count == 1) {
				NormalBar lastBar 		= HA[HA.Count - 1];
				int lastColour 			= lastBar.C > lastBar.O ? 1 : -1;
				return lastColour;
			} else {
				NormalBar secondLastBar = HA[HA.Count - 2];
				NormalBar lastBar 		= HA[HA.Count - 1];
				
				int secondLastColour 	= secondLastBar.C > secondLastBar.O ? 1 : -1;
				int lastColour 			= lastBar.C > lastBar.O ? 1 : -1;
				
				if (lastColour == 1 		&& lastColour != secondLastColour) {
					return 1;
				} else if (lastColour == -1 && lastColour != secondLastColour) {
					return -1;
				} else {
					return 0;
				}	
			}
		}
		
		public NormalBar GetLastBar() {
			return HA[HA.Count - 1];
		}
		
		public void CleanMemory() {
			if (HA.Count > 50) {
				int diff = HA.Count - 50;
				HA.RemoveRange(0, diff);
			}
		}
    }

}