GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Lean/Algorithm.CSharp/DropboxUniverseSelectionAlgorithm.cs at master · nuswgg/Lean Expand file tree/ Copy pathDropboxUniverseSelectionAlgorithm.csMore file actions 181 lines (164 loc) · 7.87 KB / Copy pathDropboxUniverseSelectionAlgorithm.csFile metadata and controls181 lines (164 loc) · 7.87 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 /* * 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. */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using QuantConnect.Data; using QuantConnect.Data.UniverseSelection; using QuantConnect.Interfaces; namespace QuantConnect.Algorithm.CSharp { /// <summary> /// In this algortihm we show how you can easily use the universe selection feature to fetch symbols /// to be traded using the AddUniverse method. This method accepts a function that will return the /// desired current set of symbols. Return Universe.Unchanged if no universe changes should be made /// </summary> /// <meta name="tag" content="using data" /> /// <meta name="tag" content="universes" /> /// <meta name="tag" content="custom universes" /> public class DropboxUniverseSelectionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition { // the changes from the previous universe selection private SecurityChanges _changes = SecurityChanges.None; // only used in backtest for caching the file results private readonly Dictionary<DateTime, List<string>> _backtestSymbolsPerDay = new Dictionary<DateTime, List<string>>(); /// <summary> /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized. /// </summary> /// <seealso cref="QCAlgorithm.SetStartDate(System.DateTime)"/> /// <seealso cref="QCAlgorithm.SetEndDate(System.DateTime)"/> /// <seealso cref="QCAlgorithm.SetCash(decimal)"/> public override void Initialize() { // this sets the resolution for data subscriptions added by our universe UniverseSettings.Resolution = Resolution.Daily; // set our start and end for backtest mode SetStartDate(2013, 01, 01); SetEndDate(2013, 12, 31); // define a new custom universe that will trigger each day at midnight AddUniverse("my-dropbox-universe", Resolution.Daily, dateTime => { // handle live mode file format if (LiveMode) { // fetch the file from dropbox var file = Download(@"https://www.dropbox.com/s/2az14r5xbx4w5j6/daily-stock-picker-live.csv?dl=1"); // if we have a file for today, break apart by commas and return symbols if (file.Length > 0) return file.ToCsv(); // no symbol today, leave universe unchanged return Universe.Unchanged; } // backtest - first cache the entire file if (_backtestSymbolsPerDay.Count == 0) { // No need for headers for authorization with dropbox, these two lines are for example purposes var byteKey = Encoding.ASCII.GetBytes($"UserName:Password"); // The headers must be passed to the Download method as list of key/value pair. var headers = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("Authorization", $"Basic ({Convert.ToBase64String(byteKey)})") }; var file = Download(@"https://www.dropbox.com/s/rmiiktz0ntpff3a/daily-stock-picker-backtest.csv?dl=1", headers); // split the file into lines and add to our cache foreach (var line in file.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)) { var csv = line.ToCsv(); var date = DateTime.ParseExact(csv[0], "yyyyMMdd", null); var symbols = csv.Skip(1).ToList(); _backtestSymbolsPerDay[date] = symbols; } } // if we have symbols for this date return them, else specify Universe.Unchanged List<string> result; if (_backtestSymbolsPerDay.TryGetValue(dateTime.Date, out result)) { return result; } return Universe.Unchanged; }); } /// <summary> /// Event - v3.0 DATA EVENT HANDLER: (Pattern) Basic template for user to override for receiving all subscription data in a single event /// </summary> /// <code> /// TradeBars bars = slice.Bars; /// Ticks ticks = slice.Ticks; /// TradeBar spy = slice["SPY"]; /// List{Tick} aaplTicks = slice["AAPL"] /// Quandl oil = slice["OIL"] /// dynamic anySymbol = slice[symbol]; /// DataDictionary{Quandl} allQuandlData = slice.Get{Quand} /// Quandl oil = slice.Get{Quandl}("OIL") /// </code> /// <param name="slice">The current slice of data keyed by symbol string</param> public override void OnData(Slice slice) { if (slice.Bars.Count == 0) return; if (_changes == SecurityChanges.None) return; // start fresh Liquidate(); var percentage = 1m/slice.Bars.Count; foreach (var tradeBar in slice.Bars.Values) { SetHoldings(tradeBar.Symbol, percentage); } // reset changes _changes = SecurityChanges.None; } /// <summary> /// Event fired each time the we add/remove securities from the data feed /// </summary> /// <param name="changes"></param> public override void OnSecuritiesChanged(SecurityChanges changes) { // each time our securities change we'll be notified here _changes = changes; } /// <summary> /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm. /// </summary> public bool CanRunLocally { get; } = true; /// <summary> /// This is used by the regression test system to indicate which languages this algorithm is written in. /// </summary> public Language[] Languages { get; } = { Language.CSharp, Language.Python }; /// <summary> /// This is used by the regression test system to indicate what the expected statistics are from running the algorithm /// </summary> public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string> { {"Total Trades", "66"}, {"Average Win", "1.06%"}, {"Average Loss", "-0.50%"}, {"Compounding Annual Return", "18.511%"}, {"Drawdown", "7.100%"}, {"Expectancy", "0.810"}, {"Net Profit", "18.511%"}, {"Sharpe Ratio", "1.439"}, {"Loss Rate", "42%"}, {"Win Rate", "58%"}, {"Profit-Loss Ratio", "2.12"}, {"Alpha", "-0.011"}, {"Beta", "0.673"}, {"Annual Standard Deviation", "0.1"}, {"Annual Variance", "0.01"}, {"Information Ratio", "-1.065"}, {"Tracking Error", "0.081"}, {"Treynor Ratio", "0.213"}, {"Total Fees", "$193.75"} }; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |