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
Probabilistic 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
namespace QuantConnect.Algorithm.CSharp
{
    public class VentralVerticalContainmentField : QCAlgorithm
    {
		
		string[] tickers = {"TSLA", "AAPL", "FB", "XOM", "JNJ", "AMD"};
		Dictionary<Symbol, RollingWindow<decimal>> closingData = new Dictionary<Symbol, RollingWindow<decimal>>();
		
		
        public override void Initialize()
        {
            SetStartDate(2020, 3, 1);  //Set Start Date
            SetCash(100000);             //Set Strategy Cash
            
            foreach(var ticker in tickers){
            	var symbol = AddEquity(ticker, Resolution.Hour).Symbol;
            	closingData.Add(symbol, new RollingWindow<decimal>(50));
            }
            
            
            SetWarmUp(50);

        }

        
        public override void OnData(Slice data)
        {
            
            foreach(var symbol in data.Keys){
            	if(closingData.ContainsKey(symbol)){
            		closingData[symbol].Add(data[symbol].Close);
            	}
            }
            
            if(IsWarmingUp){
            	return;
            }
            
            foreach(var symbol in closingData.Keys){
            	var window = closingData[symbol];
            	if(window.IsReady){
            		var supports = GetSupports(window);
            		var resistances = GetResistances(window);
            		
            		Log($"Symbol: {symbol.Value} , Supports: {String.Join(", ", supports.ToArray())} , Resistances: {String.Join(", ", resistances.ToArray())}");
            		
            		
            	}
            }
            
            
            
        }
        
        
        
        public List<decimal> GetSupports(RollingWindow<decimal> series, double variation = 0.01, int h = 3){
        	List<decimal> minima = new List<decimal>();
        	List<decimal> supports = new List<decimal>();
        	
        	for(int i = h; i < series.Size - h; i++){
        		if(series[i] < series[i - h] && series[i] < series[i + h]){
        			minima.Add(series[i]);
        		}
        	}
        	
        	
        	foreach(var low1 in minima){
        		var r = low1 * (decimal)variation;
        		List<decimal> commonLevel = new List<decimal>();
        		
        		foreach(var low2 in minima){
        			if(low2 > low1 - r && low2 < low1 + r){
        				commonLevel.Add(low2);
        			}
        		}
        		
        		if(commonLevel.Count > 1){
        			var level = commonLevel.Min(price => price);
        			if(!supports.Contains(level)){
        				supports.Add(level);
        			}
        		}
        	}
        	
        	
        	return supports;
        }	
        
        
        public List<decimal> GetResistances(RollingWindow<decimal> series, double variation = 0.01, int h = 3){
        	List<decimal> maxima = new List<decimal>();
        	List<decimal> resistances = new List<decimal>();
        	
        	for(int i = h; i < series.Size - h; i++){
        		if(series[i] > series[i - h] && series[i] > series[i + h]){
        			maxima.Add(series[i]);
        		}
        	}
        	
        	
        	foreach(var high1 in maxima){
        		var r = high1 * (decimal)variation;
        		List<decimal> commonLevel = new List<decimal>();
        		
        		foreach(var high2 in maxima){
        			if(high2 > high1 - r && high2 < high1 + r){
        				commonLevel.Add(high2);
        			}
        		}
        		
        		if(commonLevel.Count > 1){
        			var level = commonLevel.Max(price => price);
        			if(!resistances.Contains(level)){
        				resistances.Add(level);
        			}
        		}
        	}
        	
        	
        	return resistances;
        }	

    }
}