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/FuzzyInferenceAlgorithm.cs at master · nuswgg/Lean Expand file tree/ Copy pathFuzzyInferenceAlgorithm.csMore file actions 174 lines (148 loc) · 6.79 KB / Copy pathFuzzyInferenceAlgorithm.csFile metadata and controls174 lines (148 loc) · 6.79 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 /* * 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 QuantConnect.Data.Market; using QuantConnect.Indicators; using Accord.Fuzzy; namespace QuantConnect.Algorithm.CSharp { /// <summary> /// Demonstration of the Accord Fuzzy Logic library in CSharp. Using Accord to do fuzzy inference for making decisions on indicators. /// </summary> /// <meta name="tag" content="strategy example" /> /// <meta name="tag" content="indicators" /> /// <meta name="tag" content="machine learning" /> /// <meta name="tag" content="fuzzy logic" /> public class FuzzyInferenceAlgorithm : QCAlgorithm { //Indicators private RelativeStrengthIndex _rsi; private Momentum _mom; private string _symbol = "SPY"; // // With Accord v3.3.0, we need Accord.Math referenced in other projects that use // this. By placing a hard reference to an Accord.Math type, the compiler // will properly copy the required dlls into other project bin directories. // Without this, consuming projects would need to hard reference the Accord dlls, // which is less than perfect. This seems to be the better of two evils // Accord.Math.Matrix3x3 _matrix = new Accord.Math.Matrix3x3(); //Fuzzy Engine private FuzzyEngine _engine; public override void Initialize() { SetStartDate(2015, 01, 01); //Set Start Date SetEndDate(2015, 06, 30); //Set End Date SetCash(100000); //Set Strategy Cash AddEquity(_symbol, Resolution.Daily); _rsi = RSI(_symbol, 14, MovingAverageType.Simple, Resolution.Daily); _mom = MOM(_symbol, 10, Resolution.Daily, Field.Close); _engine = new FuzzyEngine(); } public void OnData(TradeBars data) { if (_rsi.IsReady && _mom.IsReady) { try { var signal = _engine.DoInference((float)_mom.Current.Value, (float)_rsi.Current.Value); if (!Portfolio.Invested) { if (signal > 30) { var quantity = decimal.ToInt32(Portfolio.MarginRemaining / data[_symbol].Price); Buy(_symbol, quantity); Debug("Purchased Stock: " + quantity + " shares"); } } else { if (signal < -10) { var quantity = decimal.ToInt32(Portfolio[_symbol].Quantity); Sell(_symbol, quantity); Debug("Sold Stock: " + quantity + " shares"); } } } catch (Exception ex) { Debug("Ex: " + ex.Message); Debug("## rsi: " + _rsi + " mom: " + _mom); } } } } public class FuzzyEngine { private InferenceSystem IS; public FuzzyEngine() { // Linguistic labels (fuzzy sets) for Momentum var momDown = new FuzzySet("Down", new TrapezoidalFunction(-20, 5, 5, 5)); var momNeutral = new FuzzySet("Neutral", new TrapezoidalFunction(-20, 0, 0, 20)); var momUp = new FuzzySet("Up", new TrapezoidalFunction(5, 20, 20, 20)); // Linguistic labels (fuzzy sets) for RSI var rsiLow = new FuzzySet("Low", new TrapezoidalFunction(0, 30, 30, 30)); var rsiMedium = new FuzzySet("Medium", new TrapezoidalFunction(0, 50, 50, 100)); var rsiHigh = new FuzzySet("High", new TrapezoidalFunction(70, 100, 100, 100)); // MOM (Input) var lvMom = new LinguisticVariable("MOM", -20, 20); lvMom.AddLabel(momDown); lvMom.AddLabel(momNeutral); lvMom.AddLabel(momUp); // RSI (Input) var lvRsi = new LinguisticVariable("RSI", 0, 100); lvRsi.AddLabel(rsiLow); lvRsi.AddLabel(rsiMedium); lvRsi.AddLabel(rsiHigh); // Linguistic labels (fuzzy sets) that compose the Signal var fsShort = new FuzzySet("Sell", new TrapezoidalFunction(-100, 0, 0, 00)); var fsHold = new FuzzySet("Hold", new TrapezoidalFunction(-50, 0, 0, 50)); var fsLong = new FuzzySet("Buy", new TrapezoidalFunction(0, 100, 100, 100)); // Output var lvSignal = new LinguisticVariable("Signal", -100, 100); lvSignal.AddLabel(fsShort); lvSignal.AddLabel(fsHold); lvSignal.AddLabel(fsLong); // The database var fuzzyDB = new Database(); fuzzyDB.AddVariable(lvMom); fuzzyDB.AddVariable(lvRsi); fuzzyDB.AddVariable(lvSignal); // Creating the inference system IS = new InferenceSystem(fuzzyDB, new CentroidDefuzzifier(1000)); // Rules IS.NewRule("Rule 1", "IF RSI IS Low AND MOM IS Down THEN Signal IS Buy"); IS.NewRule("Rule 2", "IF RSI IS Medium AND MOM IS Down THEN Signal IS Buy"); IS.NewRule("Rule 3", "IF RSI IS High AND MOM IS Down THEN Signal IS Hold"); IS.NewRule("Rule 4", "IF RSI IS Low AND MOM IS Neutral THEN Signal IS Buy"); IS.NewRule("Rule 5", "IF RSI IS Medium AND MOM IS Neutral THEN Signal IS Hold"); IS.NewRule("Rule 6", "IF RSI IS High AND MOM IS Neutral THEN Signal IS Sell"); IS.NewRule("Rule 7", "IF RSI IS Low AND MOM IS Up THEN Signal IS Hold"); IS.NewRule("Rule 8", "IF RSI IS Medium AND MOM IS Up THEN Signal IS Sell"); IS.NewRule("Rule 9", "IF RSI IS High AND MOM IS Up THEN Signal IS Sell"); } public double DoInference(float mom, float rsi) { // Setting inputs IS.SetInput("MOM", mom); IS.SetInput("RSI", rsi); // Setting outputs double signal = IS.Evaluate("Signal"); return signal; } } } |
| 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. |