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 |
using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using QuantConnect.Data.Fundamental; using QuantConnect.Data.Market; using QuantConnect.Data.UniverseSelection; //https://github.com/QuantConnect/Lean/blob/master/Algorithm.CSharp/EmaCrossUniverseSelectionAlgorithm.cs namespace QuantConnect.Algorithm.CSharp { /// <summary> /// 1. adding SMA , IN LINQ /// </summary> public class FindValue1 : QCAlgorithm { private const int _numberOfSymbolsCoarse = 100; private const int _numberOfSymbolsFine = 5; private const decimal _investmentAmt = .2m; private const int _fastPeriod = 20; private const int _slowPeriod = 50; private const decimal _tolerance = 0.01m; private SecurityChanges _changes = SecurityChanges.None; //private Dictionary<string, SymbolData> _symbolDataBySymbol; private readonly ConcurrentDictionary<Symbol, SelectionData> _averages = new ConcurrentDictionary<Symbol, SelectionData>(); // class used to improve readability of the coarse selection function private class SelectionData { public readonly SimpleMovingAverage Fast; public readonly SimpleMovingAverage Slow; public SelectionData() { Fast = new SimpleMovingAverage(_fastPeriod); Slow = new SimpleMovingAverage(_slowPeriod); } // computes an object score of how much large the fast is than the slow public decimal ScaledDelta { get { return (Fast - Slow)/((Fast + Slow)/2m); } } // updates the indicators, returning true when they're both ready public bool Update(DateTime time, decimal value) { return Fast.Update(time, value) && Slow.Update(time, value); } } public override void Initialize() { UniverseSettings.Resolution = Resolution.Daily; SetStartDate(2018, 04, 01); SetEndDate(2018, 06, 01); SetCash(25000); AddUniverse(CoarseSelectionFunction, FineSelectionFunction); } public IEnumerable<Symbol> CoarseSelectionFunction(IEnumerable<CoarseFundamental> coarse) { return (from cf in coarse // grab the SelectionData instance for this symbol let avg = _averages.GetOrAdd(cf.Symbol, sym => new SelectionData()) where avg.Update(cf.EndTime, cf.AdjustedPrice) where cf.HasFundamentalData where cf.Price < avg.Slow orderby cf.Price - avg.Slow select cf.Symbol).Take(_numberOfSymbolsCoarse); } // sort the data by P/E ratio and take the top 'NumberOfSymbolsFine' public IEnumerable<Symbol> FineSelectionFunction(IEnumerable<FineFundamental> fine) { // sort descending by P/E ratio var ordered = fine.OrderByDescending(x => x.ValuationRatios.PERatio); // take the top entries from our sorted collection var filtered = ordered.Take(_numberOfSymbolsFine); // we need to return only the symbol objects return filtered.Select(x => x.Symbol); } //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) { // if we have no changes, do nothing if (_changes == SecurityChanges.None) return; // liquidate removed securities foreach (var security in _changes.RemovedSecurities) { if (security.Invested) { Liquidate(security.Symbol); Debug("Liquidated Stock: " + security.Symbol.Value); } } // allocate each security in our universe if Price < SLOW SMA foreach (var security in _changes.AddedSecurities) { SetHoldings(security.Symbol, _investmentAmt); Debug("Purchased Stock: " + security.Symbol.Value); } _changes = SecurityChanges.None; } // this event fires whenever we have changes to our universe public override void OnSecuritiesChanged(SecurityChanges changes) { _changes = changes; if (changes.AddedSecurities.Count > 0) { Debug("Securities added: " + string.Join(",", changes.AddedSecurities.Select(x => x.Symbol.Value))); } if (changes.RemovedSecurities.Count > 0) { Debug("Securities removed: " + string.Join(",", changes.RemovedSecurities.Select(x => x.Symbol.Value))); } } } public class SymbolData { public Security Security { get; set; } public Symbol Symbol => Security.Symbol; public SimpleMovingAverage Fast { get; set; } public SimpleMovingAverage Slow { get; set; } //public bool FastIsOverSlow { get; set; } //public bool SlowIsOverFast => !FastIsOverSlow; } }