{"id":720,"date":"2009-01-20T12:52:35","date_gmt":"2009-01-20T11:52:35","guid":{"rendered":"http:\/\/soci.hu\/blog\/?p=720"},"modified":"2009-01-20T22:48:29","modified_gmt":"2009-01-20T21:48:29","slug":"amikor-egy-metodus-nem-a-helyen-lakik","status":"publish","type":"post","link":"https:\/\/soci.hu\/blog\/index.php\/2009\/01\/20\/amikor-egy-metodus-nem-a-helyen-lakik\/","title":{"rendered":"Amikor egy met\u00f3dus nem a hely\u00e9n lakik"},"content":{"rendered":"<p>Az ut\u00f3bbi p\u00e1r h\u00e9tben rendk\u00edv\u00fcl intenz\u00edven egy programot \u00edrok &#8211; egy Automated Trading System-et, \u00edgy mondj\u00e1k ezeket angolul. Azaz egy olyan programot, amely real-time t\u0151zsdei adatok alapj\u00e1n automatikusan d\u00f6nt\u00e9seket hoz, ad-vesz. Persze a dolog a val\u00f3s\u00e1gban elk\u00e9peszt\u0151en bonyolult tud lenni, de programoz\u00e1si gyakorlatnak mindenk\u00e9ppen \u00e9rdekes.<\/p>\n<p>A rendszer r\u00e9szleteir\u0151l egyel\u0151re nem akarok \u00edrni, majd ha m\u0171k\u00f6dik, besz\u00e9lek r\u00f3la &#8211; b\u00e1r lehet, hogy pont akkor nem kellene, mi? :)<\/p>\n<p>No, a l\u00e9nyeg, hogy az ilyen rendszereket az ember csak akkor ind\u00edt el, ha nagyon hossz\u00fa m\u00faltbeli adatokon v\u00e9gigpr\u00f3b\u00e1lva el\u00e9g j\u00f3 a nyeres\u00e9g\/vesztes\u00e9g ar\u00e1nya, \u00e9s nem csin\u00e1l zsin\u00f3rban annyi vesztes\u00e9get, ami m\u00e1r pszichol\u00f3gialiag f\u00e1j, vagy ak\u00e1r le is null\u00e1zza az erre sz\u00e1nt p\u00e9nzt.<br \/>\nEzt a tesztel\u00e9st h\u00edvj\u00e1k backtest-nek. Ehhez adatok kellenek, amit persze p\u00e9nz\u00e9rt adnak, de ez m\u00e1r csak ilyen, ha ez ember p\u00e9nzt akar keresni, ahhoz be is kell fektetni.<br \/>\nNekem most m\u00e1r van t\u00f6bb mint 10 \u00e9vnyi perces adatom a legnagyobb likvidit\u00e1s\u00fa futures-\u00f6kh\u00f6z (S&#038;P 500 E-Mini, Nasdaq E-Mini, stb.).<br \/>\nJ\u00f6het a szakma. A backtest \u00edr\u00e1sakor kialakult a k\u00f6vetkez\u0151 k\u00f3dr\u00e9szlet:<\/p>\n<p>class BackTestSession<br \/>\n{<br \/>\n&#8230;<br \/>\n  for (int i = 0; i < bars.Count; i++)\n  {\n    foreach (TradingAlgorithm alg in algs)\n    {\n       StepOne(alg, i);\n    }\n  }\n\nprivate void StepOne(TradingAlgorithm alg, int i)\n{\n    if (!alg.InLong &#038;&#038; !alg.InShort)\n    {\n        decimal entryPrice;\n        TradeActionReason reason = alg.WantToBuyLong(i, out entryPrice);\n        if (reason != TradeActionReason.None)\n        {\n            return;\n        }\n\n        reason = alg.WantToSellShort(i, out entryPrice);\n        if (reason != TradeActionReason.None)\n        {\n            return;\n        }\n        return;\n    }\n\n    if (alg.InLong)\n    {\n        TradeActionReason reason = alg.WantToSellLong(i);\n        if (reason != TradeActionReason.None)\n        {\n            return;\n        }\n    }\n\n    if (alg.InShort)\n    {\n        TradeActionReason reason = alg.WantToCoverShort(i);\n        if (reason != TradeActionReason.None)\n        {\n            return;\n        }\n    }\n    return;\n}\n...\n}\n[\/source]\n\nEz a k\u00f3d messzir\u0151l b\u0171zlik. Mi a gond vele? Nem j\u00f3 helyen lakik. Figyelj\u00fck meg, hogy egyfolyt\u00e1ban egy m\u00e1sik t\u00edpuson (TradingAlgorithm) v\u00e9gez m\u0171veleteket, \u00e1tny\u00falk\u00e1l oda adatok\u00e9rt \u00e9s m\u0171veleteket v\u00e9gezve rajta.\nEz egy code smell a refactoringok h\u00e1zat\u00e1j\u00e1n, amire megold\u00e1s, ha elk\u00f6lt\u00f6ztetj\u00fck a met\u00f3dust a hely\u00e9re. Move method refactoring.\n\n[source='C#']\n\nclass BackTestSession\n{\n  for (int i = 0; i < bars.Count; i++)\n  {\n      foreach (TradingAlgorithm alg in algs)\n      {\n          alg.StepOne(i);\n      }\n  }\n}\n\nclass TradingAlgorithm\n{\n    public void StepOne(int i)\n    {\n        if (!InLong &#038;&#038; !InShort)\n        {\n            decimal entryPrice;\n            TradeActionReason reason = WantToBuyLong(i, out entryPrice);\n            if (reason != TradeActionReason.None)\n            {\n                return;\n            }\n\n            reason = WantToSellShort(i, out entryPrice);\n            if (reason != TradeActionReason.None)\n            {\n                return;\n            }\n            return;\n        }\n\n        if (InLong)\n        {\n            TradeActionReason reason = WantToSellLong(i);\n            if (reason != TradeActionReason.None)\n            {\n                return;\n            }\n        }\n\n        if (InShort)\n        {\n            TradeActionReason reason = WantToCoverShort(i);\n            if (reason != TradeActionReason.None)\n            {\n                return;\n            }\n        }\n        return;\n    }\n}\n[\/source]\n\nL\u00e1that\u00f3 a k\u00fcl\u00f6nbs\u00e9g? Sokkal direktebb lett a k\u00f3d, oda ker\u00fclt a met\u00f3dus, ahov\u00e1 val\u00f3. Err\u0151l sz\u00f3l az els\u0151 <a href=\"http:\/\/en.wikipedia.org\/wiki\/GRASP_(Object_Oriented_Design)\">GRASP pattern<\/a>, az Information Expert.<\/p>\n<p>K\u00f6vetkez\u0151 refactoring lenne az out param\u00e9terek kiirt\u00e1sa, lecser\u00e9l\u00e9se member v\u00e1ltoz\u00f3ra. Erre m\u00e9g a VS is ad refactoring t\u00e1mogat\u00e1st.<\/p>\n<p>\u00c9s most j\u00f6n a <a href=\"http:\/\/en.wiktionary.org\/wiki\/shameless_plug\">shameless plug<\/a> (b\u00e1r nem ez\u00e9rt kezdem el \u00edrni a cikket, de ki nem hagyn\u00e1m a lehet\u0151s\u00e9get :): A Netacademi\u00e1ban m\u00e1rciusban lesz \u00fajra <a href=\"http:\/\/www.netacademia.net\/course.aspx?id=DP\">Design Patterns tanfolyamom<\/a>, ahol az objektumorient\u00e1lt programoz\u00e1s praktik\u00e1it mutatom meg 4 napban, ilyen egyszer\u0171eket mint a fenti, \u00e9s sokkal bonyolultabbakat is. A r\u00e9gi tananyagot \u00e1t\u00edrom a k\u00f6vetkez\u0151 hetekben, kieg\u00e9sz\u00edtve .NET Fw. 3.5-\u00f6s p\u00e9ld\u00e1kkal (WCF-ben \u00e9s WPF-ben nagyon sz\u00e9p p\u00e9ld\u00e1kat l\u00e1tni a patternekre).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Az ut\u00f3bbi p\u00e1r h\u00e9tben rendk\u00edv\u00fcl intenz\u00edven egy programot \u00edrok &#8211; egy Automated Trading System-et, \u00edgy mondj\u00e1k ezeket angolul. Azaz egy olyan programot,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,13,34,69,3,4],"tags":[],"class_list":["post-720","post","type-post","status-publish","format-standard","hentry","category-net","category-design","category-felhivas","category-tozsde","category-elet","category-szakmai-elet"],"_links":{"self":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=720"}],"version-history":[{"count":6,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/720\/revisions"}],"predecessor-version":[{"id":729,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/720\/revisions\/729"}],"wp:attachment":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}