استراتيجيات التداول رمز r


استراتيجيات تداول رمز R
الحصول على فيا أب ستور قراءة هذه المشاركة في التطبيق لدينا!
R: إعادة اختبار استراتيجية التداول. مبتدئين إلى كوانتمود و R.
أنا جديدة جدا ل R ومحاولة ل باكتست استراتيجية لقد برمجت بالفعل في ويالثلاب.
عدة أشياء لا أفهمها (ولا تعمل بشكل واضح :)
أنا لا تحصل على أسعار قريبة بشكل جيد في ناقلات. أو نوع من ناقلات ولكن يبدأ مع بنية وأنا لا أفهم حقا ما هذه الوظيفة لا. ولهذا السبب قد لا تعمل دعوتي [1] على الأرجح.
n & لوت؛ - نرو (سيريز) لا يعمل إما، ولكن أنا بحاجة إلى ذلك ل حلقة.
لذلك أعتقد إذا حصلت على هذه الأسئلة 2 أجاب استراتيجيتي يجب أن تعمل. أنا ممتن جدا لأي مساعدة .. R يبدو معقدا جدا حتى مع تجربة البرمجة في لغات أخرى.
بدءا من السؤال الثاني.
حتى إذا كنت ترغب في العمل على كائن شتس الفعلي تحتاج إلى استخدام الحصول على.
حول سؤالك الأول - لا أعتقد أنك حقا بحاجة إلى سحب البيانات كناقلات - كائن شتس هو صفيف مفهرسة حسب التاريخ وأنه من السهل أن تعمل مع. إذا كنت لا تزال ترغب في الحصول على البيانات التي يمكنك استخدامها.
الآن، للحصول على انك بدأت مع عودة اختبار بسيط من الاستراتيجيات وسوف أقترح العمل في الخطوات التالية.
تحديد الاستراتيجية الخاصة بك. 2. إنشاء مصفوفة أو إضافة عمود إلى كائن شتس الخاص بك الذي سيمثل موقفك لكل يوم. 1 لفترة طويلة، 0 لأي موقف و -1 قصيرة (في وقت لاحق يمكنك أن تلعب مع عدد للرافعة المالية). 3. مضاعفة كل أيام العودة مع الموقف وستحصل على استراتيجية ناقلات العودة الخاص بك. 4. فحص النتائج - توصيتي هي بيرفورمانساناليتيكش.
استراتيجية بسيطة - شراء عند إغلاق SMA20، بيع تحت.

تجارة خوارزمية في R تعليمي.
في هذا المنصب، وسوف تظهر كيفية استخدام R لجمع الأسهم المدرجة على loyal3، والحصول على البيانات التاريخية من ياهو ومن ثم تنفيذ استراتيجية التداول خوارزمية بسيطة. على طول الطريق، سوف تتعلم بعض كشط على شبكة الإنترنت، وظيفة ضرب أبي المالية و هتملويدجيت لجعل الرسم البياني سلسلة زمنية تفاعلية.
بالنسبة إلى هذه الوظيفة، يتم تعريف الغرامة التجارية بأنها مجموعة من القواعد التي تؤدي إلى حدث شراء أو بيع بدلا من نموذج تنبؤي أو توقعات سلسلة زمنية. هذا هو أبسط أنواع التداول ألغو، ولكن إذا كنت ترغب في حفر أعمق في التمويل مع R، وأود أن أشجعكم على اتخاذ مسار DataCampЂЂ ™ في نمذجة استراتيجية التداول الكمي في R.
خلفية.
في عام 2018، بدأت الاستثمار قليلا في الولاء 3. خدمتهم غير عادية ومكان عظيم لبدء رحلة الاستثمار الخاص. بدلا من توجيه الاتهام للمستثمر في الصفقات، الموالين 3 يتهم الشركات على قائمة على منصة. الفرضية هي أن الناس الذين يحبون خدمة company'Ђ ™ سوف أيضا شراء الأسهم وفي القيام بذلك تصبح قوية المدافعين عن العلامة التجارية. جعل المنصة أكثر إقناعا هو أنه يمكنك شراء أسهم كسور. لذلك، يمكنك الحصول على أن 800 $ الأمازون الأسهم فقط 10 $ وشراء آخر 10 $ الكسور في كل مرة لديك قليلا من النقود الإضافية في نهاية الشهر. بالتأكيد هناك تكاليف الاحتكاك منذ لديك للتداول في ويندوز ومحفظة كامل الخاص بك يقتصر على.
70 أسهم ولكن loyal3 يمثل وسيلة ممتعة ومنخفضة التكلفة لاستكشاف التدريب على الإنصاف. يمكنك وضع الجلد الحقيقي في اللعبة لمدة لا تقل عن 10 $!
أن تكون واضحة، لدي حسابات التقاعد والتقاعد نموذجية ولكن أنا أحب Lyn3†™ ق واجهة نظيفة على التطبيق وعدم وجود رسوم. أنا في نهاية المطاف فحص بلدي محفظة loyal3 متعة في كثير من الأحيان من صناديق الاستثمار بلدي ببساطة لأنه من السهل ومسلية لمعرفة أداء الأسهم اخترت مباشرة.
الأسهم المتاحة في الولاء 3.
إعداد مساحة العمل الخاصة بك.
للبدء، تحميل المكتبات في البيئة الخاصة بك. أنا دائما تقريبا استخدام رفيست لشبكة الإنترنت كشط هذه الأيام. هناك حزم أخرى التي تعمل بما في ذلك رسلينيوم، ولكن أنا أحب كيف يمكن تنفيذ رفيست سهلة.
الحزمة الثانية، بابابلي، اختيارية لأنها ببساطة إضافة شريط التقدم إلى وظائف تطبيق. منذ كنت يمكن أن يكون كشط مئات من صفحات الويب شريط التقدم يمكن أن تكون مفيدة لتقدير الوقت.
بعد ذلك، تر هي الحزمة التي بدأت للتو لاستكشاف. يتم استخدام المكتبة لبناء Ђњ قواعد التداول الفنية Ђќ. على الرغم من أنك سوف تتعلم ألغو التداول بسيطة في هذا المنصب، حزمة تر يمكن أن تؤدي العمليات الحسابية أكثر تطورا ويستحق التعلم.
مكتبة ديغرافس عبارة عن مجمع لمكتبة جافا سكريبت مفتوحة المصدر للمخططات. وهي واحدة من هتملويدجيتس التي تجعل R رسم أكثر ديناميكية وجزء من ملف هتمل بدلا من صورة ثابتة. وأخيرا، يتم استخدام حزمة لوبريديت للتلاعب تاريخ سهل.
جمع البيانات.
يتم إدراج جميع الأسهم الموالية 3 في صفحة واحدة. قبل أن تتمكن من البحث عن أسعار الأسهم اليومية الفردية لبناء خوارزمية التداول الخاص بك، تحتاج إلى جمع كل ما تبذلونه من المخزون ستوكر المتاحة. أول شيء يجب القيام به هو إعلان stock. list كسلسلة ورل. استخدم التالي read_html () بحيث تقوم جلسة R بإنشاء جلسة إنترنت وجمع كل معلومات هتمل على الصفحة كمجموعة عقدة شمل. تحتوي صفحة كس على معرف يسمى “pany-nameЂќЂќ. استخدم هذا كمعلمة عند استدعاء html_nodes () لتحديد بيانات شمل المرتبطة بهذه العقدة فقط. وأخيرا، استخدم html_text () بحيث يتم جمع قيم النص الفعلية لأسماء الشركة.
لفحص الأسهم المتوفرة على loyal3، يمكنك طباعة الكائن stock. names إلى وحدة التحكم الخاصة بك. يؤدي هذا إلى إرجاع اسم الشركة كمتجه نصي.
من أجل البحث في أسعار الأسهم، تحتاج إلى الحصول على رمز شريط أولا. عندما كنت على موقع loyal3، يمكنك النقر على البلاط الشركة لتحميل صفحة مع رمز شريط ومعلومات الشركة الأخرى.
باستخدام html_nodes () على الأسهم، يمكنك سحب كافة العقد ملحوظ مع “a. ” في هتمل & لوت؛ a & غ؛ علامة الارتباط التشعبي الذي يستخدم لربط نموذج صفحة واحدة إلى أخرى. ضمن علامة الارتباط التشعبي، يشير “hrefв to إلى عنوان ورل الدقيق. لذا سوف يستخرج html_attr () عنوان ورل لكل الروابط الموجودة في الصفحة إذا كنت تمر في “href”.
بعد القيام ببعض التفتيش اليدوي، وجدت 54 إلى 123rd الروابط على الصفحة تمثل صفحات الشركة أحتاج من أجل كشط المعلومات شريط. يستخدم السطر الأخير paste0 () لتوصيل سلسلة عنوان ورل الأساسي †™ loyal3` إلى صفحات الشركة المحددة، مثل “ / WALMART”. على سبيل المثال، loyal3 / والمارت:
على كل صفحة من صفحات الشركة هناك وصف، وسعر الإغلاق الأخير و شريط. ويتم تنظيم جميع صفحات الشركة نفسها بحيث يمكن استخدام وظيفة مخصصة get. ticker () لاستخراج رمز شريط.
داخل صفحة ويب company' s s هناك جدول يسمى “ticker-price”. ستنتقل الدالة إلى صفحة شركة، وتحدد الجدول المناسب، وتستخرج النص باستخدام html_text (). وأخيرا، باستخدام سوب () جنبا إلى جنب مع التعبير العادي ^ ([[: ألفا:]] *). * و \\ 1 سوف تحتفظ بجميع الأحرف الأبجدية. والنتيجة هي أن أي أحرف خاصة، مثل $، وأية أحرف رقمية، مثل سعر الإغلاق، تتم إزالة. كما يقرأ وظيفة كل من 70 صفحة، فإنه سيتم جمع فقط شريط الأسهم.
صفحة الأسهم 3 ل علي بابا، حيث ترى الجدول الذي يحتوي على شريط الأسهم، ™ بابا، هو أقل من النص عريض.
المسلحة مع وظيفة مخصصة الخاص بك، استخدام بلابلي () لتطبيقه على كل من stock. links التي تحتوي على كل صفحة companyЂЂ ™ ق. الكائن الناتج، stock. tickers، هو قائمة من الأوراق المالية الفردية مع كل عنصر المقابلة لشركة فردية.
طريقة واحدة لتغيير قائمة العناصر في كائن مسطح مع do. call (). هنا، كنت تطبيق ربيند إلى صف ربط كل عنصر القائمة في متجه واحد. وأخيرا، يمكنك إنشاء إطار بيانات مع الرمز ومعلومات اسم الشركة.
لكي تكون متسقة في تحليلك، قد ترغب في تحديد مقدار المعلومات التاريخية التي تجمعها على كل سهم. ستقوم الدالة Sys. Data () بتخزين عنصر تاريخ كسنة وشهر ثم يوم. استخدام سنوات مع عدد صحيح هو طريقة واحدة لطرح كمية معينة من الوقت من كائن start. date.
للحصول على بيانات مالية ياهو، يجب تغيير كائن التاريخ إلى كائنات أحرف بسيطة بدون شرطة. استخدام الدالة الاستبدال العالمية غسوب () على حد سواء start. date و end. date سيغير الطبقة و في نفس الوقت إزالة الشرطات. داخل غسوب ()، تمر في نمط حرف للبحث عن، ثم أحرف استبدال. في هذه الحالة نمط الاستبدال هو حرف فارغ بين علامات الاقتباس. المعلمة الأخيرة هي الكائن الذي سيتم تطبيق غسوب () على.
الدالة تر () جيتياهوداتا () تقبل رمز الأسهم، وتاريخ البدء والانتهاء. ترجع الدالة إطار بيانات يحتوي على معلومات سلسلة زمنية. كل صف هو تاريخ والأعمدة تحتوي على معلومات مثل سعر “Open”، “High”، “Low” و “Closingв for للأسهم. منذ كنت تبحث عن شركات متعددة، يمكنك استخدام لابلي () أو بلابلي (). تمر في ناقلات رموز الشركة، ثم وظيفة، جيتياهوداتا ()، ومن ثم معلومات التاريخ. يتم إعادة تدوير الكائنات التاريخ المعلمات في كل مرة يتم تطبيق جيتياهوداتا () على رمز السهم.
لجعل اختيار قائمة عاد، stock. ts، وأسهل للتنقل يمكنك إضافة أسماء إلى عناصر القائمة. استخدام أسماء مع الكائن stock. ts تعلن أسماء كمتجه رمز الأصلي $.
عند العمل مع القوائم الكبيرة، أود أن فحص الكائن الناتج للتأكد من أن النتيجة هي ما كنت أتوقع. الآن بعد أن العناصر لها أسماء، يمكنك الرجوع إليها مباشرة. في هذا المثال، كنت تدرس الصفوف الأولى 6 ل أمك الترفيه القابضة (أمك). سيؤدي استخدام رأس () في القائمة أثناء الرجوع إلى أمك $ إلى إعادة جزء من السلاسل الزمنية لهذا المخزون:
فحص بيانات المخزون.
عندما أستمع إلى معلقين الأخبار المالية غالبا ما تشير إلى الرسوم البيانية. على الرغم من التداول عالية التردد وإدارة نشطة يؤديها آخرون، العديد من المستثمرين الصغار لا تزال تشير إلى الرسوم البيانية للحصول على البصيرة. يمكن عرض الكائن سلسلة الوقت بسرعة باستخدام مؤامرة. مرر في القائمة التي تشير إلى العنصر المسمى مثل أمك $ ثم العمود الذي تريد عرضه، وهنا إغلاق $.
المؤامرة السابقة هي ثابتة وغير مثيرة جدا للاهتمام.
استخدام مكتبة جافا سكريبت لجعل الرسم البياني يمكنك استكشاف. في مقتطف الشفرة هذا، قد تلاحظ عامل التشغيل Ђњ٪ & غ؛٪ ” أو الأنبوب. مشغل الأنابيب هو وسيلة جيدة لكتابة رمز موجزة. فإنه يحيل كائن إلى الدالة التالية دون إجبارك على إعادة كتابة اسم كائن كما فعلت في وقت سابق في هذا المنصب.
في هذا المثال، يمكنك إنشاء ديغراف يشير إلى الأسهم تويتر، $ توتر، ثم العمود الذي تريد مؤامرة، $ إغلاق. داخل ديغراف، يضيف الرئيسي عنوان الذي تم تحديده بين علامات الاقتباس. باستخدام “٪ & غ؛٪ ” يتم إعادة توجيه هذا الكائن بأكمله إلى الدالة التالية ديرانجيسليكتور (). يمكنك تحديد نطاق تاريخ افتراضي باستخدام c () مع سلسلة تاريخ البدء والنهاية. كائن هتمل الناتج هو سلسلة زمنية ديناميكية لمخزون Twitter†™ مع شريط تمرير التاريخ في الأسفل.
تذكر، لتغيير الأسهم المعروضة، تغيير رمز شريط في قائمة stock. ts ثم عنوان الرسم البياني.
هذا هو ديغراف الأساسية لمخزون Twitter†™ ل.
استراتيجية التداول بسيطة: الاتجاه التالي.
تجار عالية التردد وصناديق التحوط استخدام نماذج متطورة والقواعد القائمة على نهج لتنفيذ الصفقات. إذا كنت ترغب في معرفة المزيد أقترح زيارة كوانتوبيان للنهج المتقدمة. لتبدأ نهج أبسط مع هذه الصفحة في إنفستوبيديا.
في التعليمات البرمجية أدناه، سوف تصور استراتيجية التداول الزخم بسيطة. في الأساس، كنت ترغب في حساب المتوسطات المتحركة ل 200 يوم و 50 يوم لسعر السهم. في أي يوم من الأيام أن المتوسط ​​المتحرك لمدة 50 يوم فوق المتوسط ​​المتحرك ل 200 يوم، يمكنك شراء أو الاحتفاظ بموقفك. في الأيام التي يكون فيها المتوسط ​​200 يوم أكثر من المتوسط ​​المتحرك ل 50 يوم، ستبيع أسهمك. وتسمى هذه الاستراتيجية استراتيجية الاتجاه التالي. تمثل الطبيعة الإيجابية أو السلبية بين المتوسطين الزمنيين الزخم قوة الزخم.
وتوفر الحزمة تر سما () لحساب المتوسط ​​المتحرك البسيط. في مقتطف الشفرة هذا، فإنك تدرس القيم الستة الأولى لمتوسطات متحركة ل 200 و 50 يوم. سما () يعمل عن طريق تمرير في سلسلة البيانات الزمنية لمخزون وعمود معين مثل إغلاق. هذا هو متجه واحد لأسعار إغلاق الأسهم توتر. والمعلمة الثانية هي عدد صحيح يمثل عدد المشاهدات للمتوسط ​​المتحرك. بدون استخدام الرأس () ستقوم الدالة سما () بإرجاع كل القيم.
الآن بعد أن كنت قد فحصت المتوسط ​​المتحرك وظيفة بالتفصيل، تحتاج إلى تطبيق على كل من 70 أسهم. stock. ts هي قائمة من 70 إطار البيانات التي تحتوي على بيانات المخزون الفردية. يحتوي العمود الرابع من كل إطار بيانات على سعر الإغلاق الذي نريد استخدامه للمتوسطات المتحركة.
تقبل الدالة المخصصة mov. avgs () إطار بيانات مخزون واحد لحساب المتوسطات المتحركة. السطر الأول يختار أسعار الإغلاق لأنه فهرسة [، 4] لإنشاء stock. close. بعد ذلك، تستخدم الدالة إفيلز للتحقق من عدد الصفوف في إطار البيانات. على وجه التحديد إذا كان نرو في إطار البيانات أقل من (2 * 260)، ثم وظيفة إنشاء إطار بيانات للمتوسطات المتحركة مع “NA”.
لقد اخترت هذا الرقم لأن هناك حوالي 250 أيام التداول في السنة لذلك هذا سوف تحقق من أن سلسلة زمنية حوالي 2 سنوات أو أكثر في الطول. يمكن لويال 3 في بعض الأحيان الحصول على الاكتتابات العامة وإذا كان الأسهم العامة الجديدة لن تكون هناك بيانات كافية لمتوسط ​​المتحرك 200 يوم. ومع ذلك، إذا كانت قيمة نرو أكبر من 2 * 260 ثم وظيفة إنشاء إطار بيانات مع البيانات الأصلية جنبا إلى جنب مع 200 و 50 يوم المتوسطات المتحركة كأعمدة جديدة. باستخدام الأسماء، أعلن أسماء الأعمدة. يستخدم الجزء الأخير من الدالة كومبليت. يتم التحقق من القيم في العمود المتوسط ​​المتحرك ل 200 يوم. يتم إسقاط أية صفوف لا تحتوي على قيمة في النتيجة النهائية.
المسلحة مع هذه الوظيفة mov. avgs () يمكنك استخدام بلابلي () لإضافة حسابات المتوسط ​​المتحرك إلى كل من إطارات البيانات 70.
استخدم الرمز أدناه لتصور المتوسطات المتحركة StockЂ s ™ باستخدام ديغراف. مرة أخرى، يستخدم هذا الرمز عامل التشغيل “٪ & غ؛٪ в to لإعادة توجيه الكائنات. و ديغراف () وظيفة يقبل الأسهم. $ فوكس إطار البيانات. على وجه التحديد، يتم فهرسة إطار البيانات حسب اسم العمود مع c ('sma_200'، 'sma_50'). يتم تمرير هذا الكائن إلى ديسريز () في السطرين التاليين. يمكنك الرجوع إلى عمود بالاسم حتى ديسيريز () كل مؤامرة سطر للقيم “sma_50” و “sma_200в in في السطرين 2 و 3. يتم إعادة توجيه هذا الكائن مرة أخرى إلى ديرانجيسليكتور () لضبط ارتفاع المحدد. وأخيرا، أضفت بعض التظليل لتحديد فترات عندما كنت ترغب في شراء أو الاحتفاظ الأسهم وفترة عندما كنت قد باعت أسهمك أو بقي بعيدا اعتمادا على موقفكم.
هنا هي النتيجة النهائية في سلسلة زمنية تفاعلية.
المتوسطات المتحركة فوكس مع المناطق المظللة لشراء / عقد مقابل بيع.
استنتاج.
كما المتداول الخوارزمية الناشئة، لا تحتاج إلى مؤامرة جميع 70 سهم. بدلا من ذلك، كنت ترغب في تشغيل التعليمات البرمجية كل يوم وإضافة طريقة برنامجية لتحديد الأسهم التي تناسب الطريقة القائمة على القاعدة، “buy إذا كان المتوسط ​​المتحرك لمدة 50 يوم فوق المتوسط ​​المتحرك ل 200 يومЂќЂќ. كما كنت مراجعة الرسم البياني السابق، القسم الأخضر هو الوقت الذي كنت شراء الأسهم فوكس. يمثل القسم الأحمر الوقت لبيع أسهمك وعدم إعادة الدخول.
نظرا لأن الرسم البياني تفاعلي، يمكنك استخدام شريط التمرير لتغيير حجم العنصر المرئي. وبناء على هذا النهج التداول ألغو بسيط، والآن قد يكون وقتا طيبا لشراء فوكس! كان 30 ديسمبر 2018 يوم تداول حيث تحرك المتوسط ​​المتحرك ل 50 يوم 0.01 دولار أعلى من المتوسط ​​المتحرك ل 200 يوم!
قسم التكبير من فوكس الأسهم.
وبطبيعة الحال، تذكر جميع الاستثمارات يمكن أن تفقد القيمة. لمعرفة المزيد عن التمويل والتداول ألغو، В تحقق من الدورات DataCamp†™ ل هنا.

و التاجر R.
استخدام R والأدوات ذات الصلة في التمويل الكمي.
تصور بيانات سلسلة الوقت في R.
أنا مسرور جدا أن أعلن بلدي داتاكامب بالطبع على تصور البيانات سلسلة الوقت في R. هذا بالطبع هو أيضا جزء من سلسلة الوقت مع R المهارات المسار. لا تتردد في الحصول على نظرة، والفصل الأول هو حر!
وصف المساقات.
وكما يقول المثل، "الرسم البياني يستحق ألف كلمة". هذا هو السبب في التصور هو الطريقة الأكثر استخداما وقوية للحصول على فهم أفضل للبيانات الخاصة بك. بعد هذا بالطبع سيكون لديك نظرة عامة جيدة جدا من R سلسلة الوقت قدرات التصور، وسوف تكون قادرة على اتخاذ قرار أفضل النموذج الذي يختار للتحليل اللاحق. سوف تكون قادرة على نقل الرسالة التي تريد تسليم بطريقة فعالة وجميلة.
بالطبع مخطط.
الفصل 1: R التصور سلسلة الوقت أدوات.
هذا الفصل سوف أعرض لكم الأساسية R سلسلة الوقت أدوات التصور.
الفصل 2: ​​سلسلة زمنية أحادية المتغير.
وقد صممت األراضي املتعددة املتغريات لتعلم قدر اإلمكان عن التوزيع والتوجه املركزي وانتشار البيانات املتوفرة. في هذا الفصل سوف يتم تقديمك مع بعض الأدوات البصرية المستخدمة لتشخيص سلسلة أحادية المتغيرات.
الفصل الثالث: سلسلة زمنية متعددة المتغيرات.
ماذا تفعل إذا كان لديك للتعامل مع سلاسل زمنية متعددة المتغيرات؟ في هذا الفصل، سوف تتعلم كيفية تحديد أنماط في التوزيع، والاتجاه المركزي وانتشار على أزواج أو مجموعات من البيانات.
الفصل الرابع: دراسة حالة: اختيار بصريا الأسهم التي تحسن المحفظة الحالية.
اسمحوا & # 8217؛ ق وضع كل ما تعلمته حتى الآن في الممارسة العملية! تخيل أنك تملك بالفعل محفظة من الأسهم وكان لديك بعض النقود الاحتياطية للاستثمار، وكيف يمكنك بحكمة اختيار الأسهم الجديدة للاستثمار النقدية الخاصة بك إضافية؟ ويعد تحليل الخصائص الإحصائية للأسهم الفردية مقابل المحفظة القائمة وسيلة جيدة لمعالجة المشكلة.
ربط R إلى إقفيد مع حزمة كوانتولس.
يوفر إقفيد تدفق خدمات البيانات والحلول التجارية التي تغطي السوق الزراعي والطاقة والمالية. بل هو معروف ومعترف به مزود تغذية البيانات الموجهة نحو مستخدمي التجزئة والمؤسسات الصغيرة. يبدأ سعر الاشتراك في حوالي 80 $ / الشهر.
وقد وضعت ستانيسلاف كوفاليفسكي حزمة تسمى كوانتولس. بل هو حزمة في كل واحدة تهدف إلى تعزيز النمذجة التداول الكمي. فإنه يسمح لتحميل وتنظيم بيانات السوق التاريخية من مصادر متعددة مثل ياهو، جوجل، فينام، موكس و إكيفيد. الميزة التي تهمني أكثر هي القدرة على ربط إكفيد ل R. أنا & # 8217؛ لقد تم استخدام إكفيد لبضع سنوات وأنا & # 8217؛ م سعيد معها (أنا & # 8217؛ م لا ينتمي إلى الشركة في أي الطريق). ويمكن الاطلاع على مزيد من المعلومات هنا. أنا & # 8217؛ كنت تبحث عن التكامل داخل R لفترة من الوقت وهنا هو. ونتيجة لذلك، بعد أن ركضت بعض الاختبارات، انتقلت التعليمات البرمجية التي كانت لا تزال في بيثون إلى R. مجرد اكتمال، وهنا & # 8217؛ ق رابط يشرح كيفية تحميل البيانات التاريخية من إكفيد باستخدام بايثون.
كوانتولس يقدم أربع وظائف رئيسية هي: الحصول على بيانات السوق، مخزن / استرداد بيانات السوق، مؤامرة البيانات سلسلة الوقت والاختبار مرة أخرى.
تأكد أولا من أن إقفيد مفتوح. يمكنك إما تحميل البيانات اليومية أو خلال اليوم. أدناه رمز التنزيلات الأسعار اليومية (المفتوحة، عالية، منخفضة، إغلاق) ل سبي من 1 يناير 2017 إلى 1 يونيو 2017.
أدناه رمز التنزيلات البيانات اللحظية من 1 مايو 2017 إلى 3 مايو 2017.
لاحظ معلمة الفترة. يمكن أن تأخذ أي من القيم التالية: القراد، 1min، 5min، 10min، 15min، 30min، ساعة، يوم، أسبوع، شهر، اعتمادا على التردد الذي تحتاجه.
كوانتولس يجعل عملية إدارة وتخزين بيانات سوق القراد سهلة. كنت فقط الإعداد معلمات التخزين وكنت على استعداد للذهاب. المعلمات هي حيث، منذ التاريخ والرموز التي ترغب في أن يتم تخزينها. في أي وقت يمكنك إضافة المزيد من الرموز وإذا لم تكن موجودة في التخزين، كوانتولس يحاول الحصول على البيانات من تاريخ البدء المحدد. سيقوم الرمز أدناه بحفظ البيانات في الدليل التالي: & # 8220؛ C: / وسرس / أرنو / دوكومينتس / ماركيت داتا / إكفيد & # 8221 ؛. هناك مجلد فرعي واحد من قبل أداة والبيانات هو أفيد في ملفات. rds.
يمكنك أيضا تخزين البيانات بين تواريخ محددة. استبدل السطر الأخير من الشفرة أعلاه بأحد الخيارات التالية.
الآن إذا كنت ترغب في الحصول على العودة بعض البيانات التي قمت بتخزينها، مجرد تشغيل شيء مثل:
لاحظ أن القراد فقط معتمد في التخزين المحلي لذلك يجب أن تكون الفترة & # 8216؛ علامة & # 8217؛
كوانتولس يوفر وظيفة plot_ts لرسم البيانات سلسلة الوقت دون عطلة نهاية الأسبوع، والعطلات والثغرات بين عشية وضحاها. في المثال أدناه، أنا أولا استرداد البيانات المخزنة أعلاه، ثم حدد أول 100 الملاحظات السعر وأخيرا رسم المخطط.
أمران أن نلاحظ: الجاسوس الأول هو كائن data. table وبالتالي بناء الجملة أعلاه. للحصول على لمحة سريعة عن قدرات data. table لها نظرة على هذه الورقة الغش ممتازة من داتاكامب. ثانيا المعلمة المحلية ترو كما يتم استرجاع البيانات من وحدة التخزين الداخلية.
كوانتولس يسمح لكتابة استراتيجية التداول الخاصة بك باستخدام C ++ أبي. أنا & # 8217؛ م لن نتحدث عن هذا لأن هذا هو أساسا C ++ التعليمات البرمجية. يمكنك الرجوع إلى قسم الأمثلة على موقع كوانتولس.
عموما أجد حزمة مفيدة للغاية وموثقة بشكل جيد. الشيء الوحيد المفقود هو تغذية حية بين R و إقفيد والتي سوف تجعل حزمة نهاية حقيقية لإنهاء الحل.
كالمعتاد أي تعليقات موضع ترحيب.
بيرت: الوافد الجديد في اتصال R إكسيل.
قبل بضعة أشهر قارئ يشير لي من هذه الطريقة الجديدة لربط R و إكسيل. أنا لا أعرف كم من الوقت كان هذا حولها، ولكن أنا لم تأتي عبر ذلك وأنا & # 8217؛ لم أر أي مشاركة بلوق أو مقالة حول هذا الموضوع. لذلك قررت أن أكتب وظيفة كأداة حقا يستحق ذلك وقبل أن يسأل أي شخص، أنا & # 8217؛ م لا علاقة للشركة بأي شكل من الأشكال.
يقف بيرت لمجموعة أدوات إكسيل R الأساسية. إنه مجاني (مرخص بموجب غل v2) وقد تم تطويره من قبل ستروتوريد داتا ليك. في وقت كتابة النسخة الحالية من بيرت هو 1.07. ويمكن الاطلاع على مزيد من المعلومات هنا. من منظور أكثر تقنية، تم تصميم بيرت لدعم تشغيل وظائف R من خلايا جداول البيانات إكسل. في عبارات إكسيل، فإنه يتم كتابة المهام التي يحددها المستخدم (أودفس) في R.
في هذا المنصب أنا & # 8217؛ م لن تظهر لك كيف R و إكسيل التفاعل عبر بيرت. هناك دروس جيدة جدا هنا، هنا وهنا. بدلا من ذلك أريد أن تظهر لك كيف استخدمت بيرت لبناء & # 8220؛ برج التحكم & # 8221؛ لتداول بلدي.
يتم إنشاء إشارات التداول الخاصة بي باستخدام قائمة طويلة من الملفات R ولكن أنا بحاجة إلى مرونة إكسيل لعرض النتائج بسرعة وكفاءة. كما هو مبين أعلاه بيرت يمكن أن تفعل هذا بالنسبة لي ولكن أريد أيضا أن خياط التطبيق لاحتياجاتي. من خلال الجمع بين قوة شمل، فبا، R و بيرت يمكنني إنشاء تطبيق جيد حتى الآن قوية في شكل ملف إكسيل مع الحد الأدنى من التعليمات البرمجية فبا. في نهاية المطاف لدي ملف اكسل واحد جمع كل المهام اللازمة لإدارة محفظتي: تحديث قاعدة البيانات، توليد إشارة، تقديم الطلبات الخ & # 8230؛ ويمكن تقسيم نهجي في الخطوات الثلاث التالية:
استخدم شمل لإنشاء قوائم وأزرار محددة من قبل المستخدم في ملف إكسيل. القوائم المذكورة أعلاه وأزرار هي أساسا يدعو إلى وظائف فبا. تلك الوظائف فبا هي التفاف حول وظائف R المعرفة باستخدام بيرت.
مع هذا النهج يمكنني الحفاظ على تمييز واضح بين جوهر بلدي رمز الاحتفاظ بها في R، سكل وبيثون وكل ما يستخدم لعرض وتنسيق النتائج أبقى في إكسيل، فبا & أمب؛ XML. في الأقسام التالية أقدم الشرط الأساسي لتطوير مثل هذا النهج ودليل خطوة بخطوة يوضح كيف يمكن استخدام بيرت لمجرد تمرير البيانات من R إلى إكسيل مع الحد الأدنى من التعليمات البرمجية فبا.
1 & # 8211؛ تحميل وتثبيت بيرت من هذا الرابط. بمجرد اكتمال التثبيت يجب أن يكون لديك قائمة الوظائف الإضافية الجديدة في إكسيل مع الأزرار كما هو موضح أدناه. هذه هي الطريقة التي تحققت بيرت في إكسيل.
2 & # 8211؛ تنزيل وتثبيت محرر واجهة مستخدم مخصص: يسمح محرر واجهة المستخدم المخصص بإنشاء قوائم وأزرار محددة من قبل المستخدم في شريط إكسيل. يتوفر إجراء خطوة بخطوة هنا.
1 & # 8211؛ R كود: وظيفة R أدناه هي قطعة بسيطة جدا من التعليمات البرمجية لأغراض التوضيح فقط. ويحسب ويعيد البقايا من الانحدار الخطي. هذا هو ما نريد استرداد في إكسيل. حفظ هذا في ملف يسمى myRCode. R (أي اسم آخر على ما يرام) في دليل من اختيارك.
2 & # 8211؛ functions. R في بيرت: من إكسيل حدد الوظائف الإضافية - & غ؛ الصفحة الرئيسية الدليل وفتح الملف يسمى functions. R. في هذا الملف قم بلصق التعليمة البرمجية التالية. تأكد من إدراج المسار الصحيح.
هذا هو مجرد مصادر في بيرت ملف R قمت بإنشائه أعلاه. ثم حفظ وإغلاق الملف functions. R. إذا كنت تريد إجراء أي تغيير على ملف R الذي تم إنشاؤه في الخطوة 1 سيكون لديك لإعادة تحميله باستخدام زر بيرت & # 8220؛ تحديث ملف بدء التشغيل & # 8221؛ من القائمة الوظائف الإضافية في إكسيل.
3 & # 8211؛ في إكسيل: إنشاء وحفظ ملف يسمى myFile. xslm (أي اسم آخر على ما يرام). هذا هو ملف تمكين ماكرو الذي تقوم بحفظه في الدليل الذي تختاره. مرة واحدة يتم حفظ الملف إغلاقه.
4 & # 8211؛ افتح الملف الذي تم إنشاؤه أعلاه في محرر واجهة المستخدم المخصصة: بعد فتح الملف، الصق الشفرة التالية.
يجب أن يكون لديك شيء من هذا القبيل في محرر شمل:
أساسا هذه القطعة من رمز شمل يخلق قائمة إضافية (رترادر)، مجموعة جديدة (مجموعتي) وزر تعريف المستخدم (زر جديد) في الشريط إكسيل. بعد الانتهاء من إجراء ذلك، افتح myFile. xslm في إكسيل وأغلق محرر واجهة المستخدم المخصص. يجب أن نرى شيئا من هذا القبيل.
5 & ​​# 8211؛ فتح محرر فبا: في myFile. xlsm إدراج وحدة نمطية جديدة. قم بلصق التعليمة البرمجية أدناه في الوحدة النمطية التي تم إنشاؤها حديثا.
يؤدي ذلك إلى محو النتائج السابقة في ورقة العمل قبل التعامل مع نتائج جديدة.
6 & # 8211؛ انقر فوق زر جديد: الآن عد إلى جدول البيانات وفي القائمة رترادر ​​انقر فوق & # 8220؛ زر جديد & # 8221؛ زر. يجب أن تشاهد شيئا مثل ما يظهر أدناه.
الدليل أعلاه هو نسخة أساسية جدا من ما يمكن تحقيقه باستخدام بيرت لكنه يظهر لك كيفية الجمع بين قوة عدة أدوات محددة لبناء التطبيق المخصص الخاص بك. من وجهة نظري مصلحة هذا النهج هو القدرة على الغراء معا R و إكسيل الواضح ولكن أيضا لتشمل عن طريق شمل (والدفعة) قطعة من التعليمات البرمجية من بايثون، سكل وأكثر من ذلك. هذا هو بالضبط ما كنت بحاجة إليه. وأخيرا أود أن تكون غريبة لمعرفة ما إذا كان أي شخص لديه أي خبرة مع بيرت؟
استراتيجية التداول: الاستفادة القصوى من البيانات من العينة.
عند اختبار استراتيجيات التداول هناك نهج مشترك هو تقسيم مجموعة البيانات الأولية إلى بيانات العينة: الجزء من البيانات المصممة لمعايرة النموذج والخروج من بيانات العينة: جزء من البيانات المستخدمة للتحقق من صحة المعايرة وضمان أن الأداء التي تم إنشاؤها في عينة ستنعكس في العالم الحقيقي. وكقاعدة عامة يمكن استخدام حوالي 70٪ من البيانات الأولية للمعايرة (أي في العينة) و 30٪ للتحقق (أي من العينة). ثم تساعد مقارنة البيانات داخل وخارج العينة على تحديد ما إذا كان النموذج قويا بما فيه الكفاية. ويهدف هذا المنصب إلى المضي قدما خطوة أخرى ويوفر طريقة إحصائية لتقرير ما إذا كان خارج العينة البيانات يتماشى مع ما تم إنشاؤه في العينة.
في الرسم البياني أدناه تمثل المنطقة الزرقاء خارج أداء العينة لأحد استراتيجياتي.
فحص بصري بسيط يكشف عن تناسب جيد بين داخل وخارج أداء العينة ولكن ما هي درجة الثقة لدي في هذا؟ في هذه المرحلة ليس كثيرا، وهذه هي القضية. والمطلوب حقا هو مقياس للتشابه بين مجموعات البيانات داخل وخارج العينة. ومن الناحية الإحصائية، يمكن ترجمة ذلك على أنه احتمال أن تأتي أرقام أداء العينة وخارجها من نفس التوزيع. هناك اختبار إحصائي غير بارامتري الذي يفعل بالضبط هذا: اختبار كروسكال واليس. ويمكن العثور على تعريف جيد لهذا الاختبار على R-توتور & # 8220؛ مجموعة من عينات البيانات مستقلة إذا كانت تأتي من السكان غير ذات الصلة والعينات لا تؤثر على بعضها البعض. باستخدام اختبار كروسكال واليس، يمكننا أن نقرر ما إذا كانت التوزيعات السكانية متطابقة دون افتراض أنها تتبع التوزيع الطبيعي. & # 8221؛ الفائدة الإضافية لهذا الاختبار لا يفترض توزيع طبيعي.
وتوجد اختبارات أخرى من نفس الطبيعة يمكن أن تتلاءم مع هذا الإطار. اختبار مان-ويتني-ويلكوكسون أو اختبارات كولموغوروف-سميرنوف يناسب تماما الإطار يصف هنا ولكن هذا خارج نطاق هذه المقالة لمناقشة إيجابيات وسلبيات كل من هذه الاختبارات. ويمكن الاطلاع على وصف جيد جنبا إلى جنب مع الأمثلة R هنا.
هنا الرمز المستخدم لإنشاء الرسم البياني أعلاه والتحليل:
في المثال أعلاه في فترة العينة أطول من خارج الفترة عينة ولذلك أنا عشوائيا إنشاء 1000 مجموعات فرعية من البيانات في العينة كل واحد لها نفس طول البيانات خارج العينة. ثم اختبرت كل عينة فرعية في مقابل عينة من البيانات وسجلت قيم p. هذه العملية لا تخلق قيمة P واحدة لاختبار كروسكال واليس ولكن التوزيع يجعل التحليل أكثر قوة. في هذا المثال يكون متوسط ​​قيم p أعلى بكثير من الصفر (0.478) مما يشير إلى أنه يجب قبول الفرضية الصفرية: فهناك أدلة قوية على أن البيانات داخل وخارج العينة تأتي من نفس التوزيع.
كالمعتاد ما هو عرض في هذا المنصب هو مثال لعبة أن خدوش فقط على سطح المشكلة ويجب أن تكون مصممة لتلبية الاحتياجات الفردية. ومع ذلك أعتقد أنه يقترح إطارا إحصائيا للاهتمام والعقلاني لتقييم نتائج العينة.
هذه المقالة مستوحاة من الورقتين التاليتين:
فيجيل ألكسندر، شميل سوان (2007)، "آثار وظائف التحسين المختلفة على الخروج من عينة أداء استراتيجيات التداول المتطورة وراثيا"، التنبؤ مؤتمر الأسواق المالية.
فيجيه أليكساندر، شميل سوان (2018)، "عملية التحسين لتحسين / الخروج من عينة الاتساق، حالة سوق الأسهم»، مؤتمر مورغان كازينوف الأسهم الكمية الكمية، لندن أكتوبر 2018.
تقديم فيدلر: فينانسيال داتا لوادير.
فيدلر هو رستوديو أدين تهدف إلى تبسيط عملية تنزيل البيانات المالية من مختلف مقدمي الخدمات. هذا الإصدار الأولي هو المجمع حول وظيفة جيتسيمبولس في حزمة كوانتمود ويتم دعم فقط ياهو، جوجل، فريد و أواندا. أنا ربما إضافة وظائف مع مرور الوقت. كالمعتاد مع هذه الأشياء مجرد تذكير نوع: & # 8220؛ يتم توفير البرنامج & # 8220؛ كما هو & # 8221؛، دون ضمان من أي نوع & # 8230؛ & # 8221؛
كيفية تثبيت واستخدام فيدلر؟
يمكنك الحصول على أدين / حزمة من مستودع جيثب هنا (وسوف يسجل على كران في وقت لاحق) تثبيت أدين. هناك تعليمي ممتاز لتثبيت رستوديو أدينز هنا. بمجرد تثبيت أدين يجب أن تظهر في القائمة أدين. اخترت فقط فيدلر في القائمة ونافذة كما في الصورة أدناه يجب أن تظهر. اختر موفر بيانات من القائمة المنسدلة المصدر. حدد نطاق تاريخ من قائمة التاريخ أدخل الرمز الذي ترغب في تنزيله في مربع النص الخاص بالأداة. لتحميل عدة رموز فقط أدخل الرموز مفصولة بفواصل. استخدام أزرار الراديو لاختيار ما إذا كنت ترغب في تحميل الصك في ملف كسف أو في البيئة العالمية. سيتم حفظ ملف كسف في دليل العمل وسيكون هناك ملف كسف واحد لكل أداة. اضغط على تشغيل للحصول على البيانات أو إغلاق لإغلاق أدين.
يتم التعامل مع رسائل الخطأ والتحذيرات من قبل الحزم الأساسية (كوانتمود و لامعة) ويمكن قراءتها من وحدة التحكم.
هذه هي النسخة الأولى جدا من المشروع لذلك لا نتوقع الكمال ولكن نأمل أنه سوف تتحسن مع مرور الوقت. يرجى الإبلاغ عن أي تعليق، اقتراح، علة الخ & # 8230؛ تو: ثيرترادر ​​@ غميل.
الحفاظ على قاعدة بيانات لملفات الأسعار في R.
القيام بالبحث الكمي ينطوي على الكثير من البيانات الطحن واحد يحتاج إلى بيانات نظيفة وموثوق بها لتحقيق ذلك. ما هو مطلوب حقا هو البيانات النظيفة التي يمكن الوصول إليها بسهولة (حتى من دون اتصال بالإنترنت). وكانت الطريقة الأكثر فعالية للقيام بذلك بالنسبة لي للحفاظ على مجموعة من ملفات كسف. من الواضح أن هذه العملية يمكن التعامل معها في نواح كثيرة ولكن وجدت العمل الإضافي فعالة جدا وبسيطة للحفاظ على الدليل حيث يمكنني تخزين وتحديث ملفات كسف. لدي ملف كسف واحد لكل أداة ويسمى كل ملف بعد الصك أنه يحتوي على. السبب في ذلك هو شقين: أولا، لا أريد تحميل البيانات (السعر) من ياهو، غوغل وغيرها & # 8230؛ في كل مرة أريد أن اختبار فكرة جديدة ولكن الأهم من ذلك مرة واحدة حددت وتحديد المشكلة، وأنا لا تريد أن تفعل ذلك مرة أخرى في المرة القادمة أنا بحاجة إلى نفس الأداة. بسيطة لكنها فعالة جدا حتى الآن. يتم تلخيص العملية في الرسم البياني أدناه.
في كل ما يلي، أفترض أن البيانات تأتي من ياهو. يجب تعديل الشفرة للبيانات من غوغل، كواندل إتك & # 8230؛ وبالإضافة إلى ذلك أقدم عملية تحديث بيانات الأسعار اليومية. سيكون الإعداد مختلفا عن بيانات التردد الأعلى والنوع الآخر من مجموعات البيانات (أي مختلف عن الأسعار).
1 & # 8211؛ تنزيل البيانات الأولية (listOfInstruments. R & HistoryData. R)
ملف listOfInstruments. R هو ملف يحتوي فقط على قائمة بجميع الصكوك.
إذا لم يكن أحد الأدوات جزءا من قائمتي (أي ملف كسف في مجلد البيانات) أو إذا قمت بذلك للمرة الأولى، عليك تنزيل مجموعة البيانات التاريخية الأولية. المثال أدناه ينزل مجموعة من صناديق المؤشرات المتداولة يوميا من ياهو فينانس إلى يناير 2000 وتخزين البيانات في ملف كسف.
2 & # 8211؛ تحديث البيانات الموجودة (updateData. R)
The below code starts from existing files in the dedicated folder and updates all of them one after the other. I usually run this process everyday except when I’m on holiday. To add a new instrument, simply run step 1 above for this instrument alone.
3 & # 8211؛ Create a batch file (updateDailyPrices. bat)
Another important part of the job is creating a batch file that automates the updating process above (I’m a Windows user). This avoids opening R/RStudio and run the code from there. The code below is placed on a. bat file (the path has to be amended with the reader’s setup). Note that I added an output file (updateLog. txt) to track the execution.
The process above is extremely simple because it only describes how to update daily price data. I’ve been using this for a while and it has been working very smoothly for me so far. For more advanced data and/or higher frequencies, things can get much trickier.
كالمعتاد أي تعليقات موضع ترحيب.
The Rise of the Robots (Advisors…)
The Asset Management industry is on the verge of a major change. Over the last couple of years Robots Advisors (RA) have emerged as new players. The term itself is hard to define as it encompasses a large variety of services. Some are designed to help traditional advisers to better allocate their clients money and some are real “black box”. The user enter a few criteria (age , income, children etc…) and the robot proposes a tailor-made allocation. Between those two extremes a full range of offers is available. I found the Wikipedia definition pretty good. “They are a class of financial adviser that provides portfolio management online with minimal human intervention”. More precisely they use algorithm-based portfolio management to offer the full spectrum of services a traditional adviser would offer: dividend reinvesting, compliance reports, portfolio rebalancing, tax loss harvesting etc… (well this is what the quantitative investment community is doing for decades!). The industry is still in its infancy with most players still managing a small amount of money but I only realised how profound the change was when I was in NYC a few days ago. When RA get their names on TV adds or on the roof of NYC cab you know something big is happening…
it is getting more and more attention from the media and above all it makes a lot of sense from an investor perspective. There are actually two main advantages in using RA:
Significantly lower fees over traditional advisers Investment is made more transparent and simpler which is more appealing to people with limited financial knowledge.
In this post R is just an excuse to present nicely what is a major trend in the asset management industry. The chart below shows the market shares of most popular RA as of the end of 2017. The code used to generate the chart below can be found at the end of this post and the data is here.
Those figures are a bit dated given how fast this industry evolves but are still very informative. Not surprisingly the market is dominated by US providers like Wealthfront and Betterment but RA do emerge all over the world: Asia (8Now!), Switzerland (InvestGlass), France (Marie Quantier)….. It is starting to significantly affect the way traditional asset managers are doing business. A prominent example is the partnership between Fidelity and Betterment. Since December 2017 Betterment past the $2 billion AUM mark.
Despite all the above, I think the real change is ahead of us. Because they use less intermediaries and low commission products (like ETFs) they charge much lower fees than traditional advisers. RA will certainly gain significant market shares but they will also lowers fees charged by the industry as a whole. Ultimately it will affect the way traditional investment firms do business. Active portfolio management which is having a tough time for some years now will suffer even more. The high fees it charges will be even harder to justify unless it reinvents itself. Another potential impact is the rise of ETFs and low commission financial products in general. Obviously this has started a while ago but I do think the effect will be even more pronounced in the coming years. New generations of ETFs track more complex indices and custom made strategies. This trend will get stronger inevitably.
كالمعتاد أي تعليقات موضع ترحيب.
R financial time series tips everyone should know about.
There are many R time series tutorials floating around on the web this post is not designed to be one of them. Instead I want to introduce a list of the most useful tricks I came across when dealing with financial time series in R. Some of the functions presented here are incredibly powerful but unfortunately buried in the documentation hence my desire to create a dedicated post. I only address daily or lower frequency times series. Dealing with higher frequency data requires specific tools: data. table or highfrequency packages are some of them.
xts : The xts package is the must have when it comes to times series in R. The example below loads the package and creates a daily time series of 400 days normaly distributed returns.
merge. xts (package xts): This is incredibly powerful when it comes to binding two or more times series together whether they have the same length or not. The join argument does the magic! it determines how the binding is done.
apply. yearly/apply. monthly (package xts): Apply a specified function to each distinct period in a given time series object. The example below calculates monthly and yearly returns of the second series in the tsInter object. Note that I use the sum of returns (no compounding)
endpoints (package xts): Extract index values of a given xts object corresponding to the last observations given a period specified by on. The example gives the last day of the month returns for each series in the tsInter object using endpoint to select the date.
na. locf (package zoo): Generic function for replacing each NA with the most recent non-NA prior to it. Extremely useful when dealing with a time series with a few “holes” and when this time series is subsequently used as input for an R functions that does not accept arguments with NAs. In the example I create a time series of random prices then artificially includes a few NAs in it and replace them with the most recent value.
charts. PerformanceSummary (package PerformanceAnalytics): For a set of returns, create a wealth index chart, bars for per-period performance, and underwater chart for drawdown. This is incredibly useful as it displays on a single window all the relevant information for a quick visual inspection of a trading strategy. The example below turns the prices series into an xts object then displays a window with the 3 charts described above.
The list above is by no means exhaustive but once you master the functions describe in this post it makes the manipulation of financial time series a lot easier, the code shorter and the readability of the code better.
كالمعتاد أي تعليقات موضع ترحيب.
Factor Evaluation in Quantitative Portfolio Management.
When it comes to managing a portfolio of stocks versus a benchmark the problem is very different from defining an absolute return strategy. In the former one has to hold more stocks than in the later where no stocks at all can be held if there is not good enough opportunity. The reason for that is the tracking error . This is defined as the standard deviation of the portfolio return minus the benchmark return. The less stocks is held vs. a benchmark the higher the tracking error (e. g higher risk).
The analysis that follows is largely inspired by the book “Active Portfolio Management” by Grinold & Kahn. This is the bible for anyone interested in running a portfolio against a benchmark. I strongly encourage anyone with an interest in the topic to read the book from the beginning to the end. It’s very well written and lays the foundations of systematic active portfolio management (I have no affiliation to the editor or the authors).
Here we’re trying to rank as accurately as possible the stocks in the investment universe on a forward return basis. Many people came up with many tools and countless variant of those tools have been developed to achieve this. In this post I focus on two simple and widely used metrics: Information Coefficient (IC) and Quantiles Return (QR).
The IC gives an overview of the factor forecasting ability. More precisely, this is a measure of how well the factor ranks the stocks on a forward return basis. The IC is defined as the rank correlation ( ρ ) between the metric (e. g. factor) and the forward return. In statistical terms the rank correlation is a nonparametric measure of dependance between two variables. For a sample of size n , the n raw scores are converted to ranks , and ρ is computed from:
The horizon for the forward return has to be defined by the analyst and it’s a function of the strategy’s turnover and the alpha decay (this has been the subject of extensive research). Obviously ICs must be as high as possible in absolute terms.
For the keen reader, in the book by Grinold & Kahn a formula linking Information Ratio (IR) and IC is given: with breadth being the number of independent bets (trades). This formula is known as the fundamental law of active management . The problem is that often, defining breadth accurately is not as easy as it sounds.
In order to have a more accurate estimate of the factor predictive power it’s necessary to go a step further and group stocks by quantile of factor values then analyse the average forward return (or any other central tendency metric) of each of those quantiles. The usefulness of this tool is straightforward. A factor can have a good IC but its predictive power might be limited to a small number of stocks. This is not good as a portfolio manager will have to pick stocks within the entire universe in order to meet its tracking error constraint. Good quantiles return are characterised by a monotonous relationship between the individual quantiles and forward returns.
All the stocks in the S&P500 index (at the time of writing). Obviously there is a survival ship bias: the list of stocks in the index has changed significantly between the start and the end of the sample period, however it’s good enough for illustration purposes only.
The code below downloads individual stock prices in the S&P500 between Jan 2005 and today (it takes a while) and turns the raw prices into return over the last 12 months and the last month. The former is our factor, the latter will be used as the forward return measure.
Below is the code to compute Information Coefficient and Quantiles Return. Note that I used quintiles in this example but any other grouping method (terciles, deciles etc…) can be used. it really depends on the sample size, what you want to capture and wether you want to have a broad overview or focus on distribution tails. For estimating returns within each quintile, median has been used as the central tendency estimator. This measure is much less sensitive to outliers than arithmetic mean.
And finally the code to produce the Quantiles Return chart.
3 & # 8211؛ How to exploit the information above?
In the chart above Q1 is lowest past 12 months return and Q5 highest. There is an almost monotonic increase in the quantiles return between Q1 and Q5 which clearly indicates that stocks falling into Q5 outperform those falling into Q1 by about 1% per month. This is very significant and powerful for such a simple factor (not really a surprise though…). Therefore there are greater chances to beat the index by overweighting the stocks falling into Q5 and underweighting those falling into Q1 relative to the benchmark.
An IC of 0.0206 might not mean a great deal in itself but it’s significantly different from 0 and indicates a good predictive power of the past 12 months return overall. Formal significance tests can be evaluated but this is beyond the scope of this article.
The above framework is excellent for evaluating investments factor’s quality however there are a number of practical limitations that have to be addressed for real life implementation:
Rebalancing : In the description above, it’s assumed that at the end of each month the portfolio is fully rebalanced. This means all stocks falling in Q1 are underweight and all stocks falling in Q5 are overweight relative to the benchmark. This is not always possible for practical reasons: some stocks might be excluded from the investment universe, there are constraints on industry or sector weight, there are constraints on turnover etc… Transaction Costs : This has not be taken into account in the analysis above and this is a serious brake to real life implementation. Turnover considerations are usually implemented in real life in a form of penalty on factor quality. Transfer coefficient : This is an extension of the fundamental law of active management and it relaxes the assumption of Grinold’s model that managers face no constraints which preclude them from translating their investments insights directly into portfolio bets.
And finally, I’m amazed by what can be achieved in less than 80 lines of code with R…
كالمعتاد أي تعليقات موضع ترحيب.
Risk as a “Survival Variable”
I come across a lot of strategies on the blogosphere some are interesting some are a complete waste of time but most share a common feature: people developing those strategies do their homework in term of analyzing the return but much less attention is paid to the risk side its random nature. I’ve seen comment like “a 25% drawdown in 2018 but excellent return overall”. Well my bet is that no one on earth will let you experience a 25% loss with their money (unless special agreements are in place). In the hedge fund world people have very low tolerance for drawdown. Generally, as a new trader in a hedge fund, assuming that you come with no reputation, you have very little time to prove yourself. You should make money from day 1 and keep on doing so for a few months before you gain a bit of credibility.
First let’s say you have a bad start and you lose money at the beginning. With a 10% drawdown you’re most certainly out but even with a 5% drawdown the chances of seeing your allocation reduced are very high. This has significant implications on your strategies. Let’s assume that if you lose 5% your allocation is divided by 2 and you come back to your initial allocation only when you passed the high water mark again (e. g. the drawdown comes back to 0). In the chart below I simulated the experiment with one of my strategies.
You start trading in 1st June 2003 and all goes well until 23rd Jul. 2003 where your drawdown curve hits the -5% threshold (**1**). Your allocation is cut by 50% and you don’t cross back the high water mark level until 05th Dec. 2003 (**3**). If you have kept the allocation unchanged, the high water mark level would have been crossed on 28th Oct. 2003 (**2**) and by the end of the year you would have made more money.
But let’s push the reasoning a bit further. Still on the chart above, assume you get really unlucky and you start trading toward mid-June 2003. You hit the 10% drawdown limit by the beginning of August and you’re most likely out of the game. You would have started in early August your allocation would not have been cut at all and you end up doing a good year in only 4 full months of trading. In those two examples nothing has changed but your starting date….
The trading success of any individual has some form of path dependency and there is not much you can do about it. However you can control the size of a strategy’s drawdown and this should be addressed with great care. A portfolio should be diversified in every possible dimension: asset classes, investment strategies, trading frequencies etc…. From that perspective risk is your “survival variable”. If managed properly you have a chance to stay in the game long enough to realise the potential of your strategy. Otherwise you won’t be there next month to see what happens.

و التاجر R.
استخدام R والأدوات ذات الصلة في التمويل الكمي.
Archive for the ‘Trading Strategies’ الفئة.
Linking R to IQFeed with the QuantTools package.
IQFeed provides streaming data services and trading solutions that cover the Agricultural, Energy and Financial marketplace. It is a well known and recognized data feed provider geared toward retail users and small institutions. The subscription price starts at around $80/month.
Stanislav Kovalevsky has developed a package called QuantTools. It is an all in one package designed to enhance quantitative trading modelling. It allows to download and organize historical market data from multiple sources like Yahoo, Google, Finam, MOEX and IQFeed. The feature that interests me the most is the ability to link IQFeed to R. I’ve been using IQFeed for a few years and I’m happy with it (I’m not affiliated to the company in any way). ويمكن الاطلاع على مزيد من المعلومات هنا. I’ve been looking for an integration within R for a while and here it is. As a result, after I ran a few tests, I moved my code that was still in Python into R. Just for completeness, here’s a link that explains how to download historical data from IQFeed using Python.
QuantTools offers four main functionalities: Get market data, Store/Retrieve market data, Plot time series data and Back testing.
First make sure that IQfeed is open. You can either download daily or intraday data. The below code downloads daily prices (Open, High, Low, Close) for SPY from 1st Jan 2017 to 1st June 2017.
The below code downloads intraday data from 1st May 2017 to 3rd May 2017.
Note the period parameter. It can take any of the following values: tick, 1min, 5min, 10min, 15min, 30min, hour, day, week, month, depending on the frequency you need.
QuantTools makes the process of managing and storing tick market data easy. You just setup storage parameters and you are ready to go. The parameters are where, since what date and which symbols you would like to be stored. Any time you can add more symbols and if they are not present in a storage, QuantTools tries to get the data from specified start date. The code below will save the data in the following directory: “C:/Users/Arnaud/Documents/Market Data/iqfeed”. There is one sub folder by instrument and the data is aved in. rds files.
You can also store data between specific dates. Replace the last line of code above with one of the below.
Now should you want to get back some of the data you stored, just run something like:
Note that only ticks are supported in local storage so period must be ‘tick’
QuantTools provides plot_ts function to plot time series data without weekend, holidays and overnight gaps. In the example below, I first retrieve the data stored above, then select the first 100 price observations and finally draw the chart.
Two things to notice: First spy is a data. table object hence the syntax above. To get a quick overview of data. table capabilities have a look at this excellent cheat sheet from DataCamp. Second the local parameter is TRUE as the data is retrieved from internal storage.
QuantTools allows to write your own trading strategy using its C++ API. I’m not going to elaborate on this as this is basically C++ code. You can refer to the Examples section on QuantTools website.
Overall I find the package extremely useful and well documented. The only missing bit is the live feed between R and IQFeed which will make the package a real end to end solution.
كالمعتاد أي تعليقات موضع ترحيب.
BERT: a newcomer in the R Excel connection.
A few months ago a reader point me out this new way of connecting R and Excel. I don’t know for how long this has been around, but I never came across it and I’ve never seen any blog post or article about it. So I decided to write a post as the tool is really worth it and before anyone asks, I’m not related to the company in any way.
BERT stands for Basic Excel R Toolkit. It’s free (licensed under the GPL v2) and it has been developed by Structured Data LLC. At the time of writing the current version of BERT is 1.07. ويمكن الاطلاع على مزيد من المعلومات هنا. From a more technical perspective, BERT is designed to support running R functions from Excel spreadsheet cells. In Excel terms, it’s for writing User-Defined Functions (UDFs) in R.
In this post I’m not going to show you how R and Excel interact via BERT. There are very good tutorials here, here and here. Instead I want to show you how I used BERT to build a “control tower” for my trading.
My trading signals are generated using a long list of R files but I need the flexibility of Excel to display results quickly and efficiently. As shown above BERT can do this for me but I also want to tailor the application to my needs. By combining the power of XML, VBA, R and BERT I can create a good looking yet powerful application in the form of an Excel file with minimum VBA code. Ultimately I have a single Excel file gathering all the necessary tasks to manage my portfolio: database update, signal generation, orders submission etc… My approach could be broken down in the 3 steps below:
Use XML to build user defined menus and buttons in an Excel file. The above menus and buttons are essentially calls to VBA functions. Those VBA functions are wrapup around R functions defined using BERT.
With this approach I can keep a clear distinction between the core of my code kept in R, SQL and Python and everything used to display and format results kept in Excel, VBA & XML. In the next sections I present the prerequisite to developed such an approach and a step by step guide that explains how BERT could be used for simply passing data from R to Excel with minimal VBA code.
1 & # 8211؛ Download and install BERT from this link . Once the installation has completed you should have a new Add-Ins menu in Excel with the buttons as shown below. This is how BERT materialized in Excel.
2 & # 8211؛ Download and install Custom UI editor : The Custom UI Editor allows to create user defined menus and buttons in Excel ribbon. A step by step procedure is available here.
1 & # 8211؛ R Code: The below R function is a very simple piece of code for illustration purposes only. It calculates and return the residuals from a linear regression. This is what we want to retrieve in Excel. Save this in a file called myRCode. R (any other name is fine) in a directory of your choice.
2 & # 8211؛ functions. R in BERT : From Excel select Add-Ins -> Home Directory and open the file called functions. R . In this file paste the following code. Make sure you insert the correct path.
This is just sourcing into BERT the R file you created above. Then save and close the file functions. R. Should you want to make any change to the R file created in step 1 you will have to reload it using the BERT button “Reload Startup File” from the Add-Ins menu in Excel.
3 & # 8211؛ In Excel: Create and save a file called myFile. xslm (any other name is fine). This is a macro-enabled file that you save in the directory of your choice. Once the file is saved close it.
4 & # 8211؛ Open the file created above in Custom UI editor : Once the file is open, paste the below code.
You should have something like this in the XML editor:
Essentially this piece of XML code creates an additional menu (RTrader), a new group (My Group) and a user defined button (New Button) in the Excel ribbon. Once you’re done, open myFile. xslm in Excel and close the Custom UI Editor. You should see something like this.
5 & ​​# 8211؛ Open VBA editor : In myFile. xlsm insert a new module. Paste the code below in the newly created module.
This erases previous results in the worksheet prior to coping new ones.
6 & # 8211؛ Click New Button : Now go back to the spreadsheet and in the RTrader menu click the “New Button” زر. You should see something like the below appearing.
The guide above is a very basic version of what can be achieved using BERT but it shows you how to combine the power of several specific tools to build your own custom application. From my perspective the interest of such an approach is the ability to glue together R and Excel obviously but also to include via XML (and batch) pieces of code from Python, SQL and more. This is exactly what I needed. Finally I would be curious to know if anyone has any experience with BERT?
Trading strategy: Making the most of the out of sample data.
When testing trading strategies a common approach is to divide the initial data set into in sample data: the part of the data designed to calibrate the model and out of sample data: the part of the data used to validate the calibration and ensure that the performance created in sample will be reflected in the real world. As a rule of thumb around 70% of the initial data can be used for calibration (i. e. in sample) and 30% for validation (i. e. out of sample). Then a comparison of the in and out of sample data help to decide whether the model is robust enough. This post aims at going a step further and provides a statistical method to decide whether the out of sample data is in line with what was created in sample.
In the chart below the blue area represents the out of sample performance for one of my strategies.
A simple visual inspection reveals a good fit between the in and out of sample performance but what degree of confidence do I have in this? At this stage not much and this is the issue. What is truly needed is a measure of similarity between the in and out of sample data sets. In statistical terms this could be translated as the likelihood that the in and out of sample performance figures coming from the same distribution. There is a non-parametric statistical test that does exactly this: the Kruskall-Wallis Test . A good definition of this test could be found on R-Tutor “A collection of data samples are independent if they come from unrelated populations and the samples do not affect each other. Using the Kruskal-Wallis Test , we can decide whether the population distributions are identical without assuming them to follow the normal distribution.” The added benefit of this test is not assuming a normal distribution.
It exists other tests of the same nature that could fit into that framework. The Mann-Whitney-Wilcoxon test or the Kolmogorov-Smirnov tests would perfectly suits the framework describes here however this is beyond the scope of this article to discuss the pros and cons of each of these tests. A good description along with R examples can be found here.
Here’s the code used to generate the chart above and the analysis:
In the example above the in sample period is longer than the out of sample period therefore I randomly created 1000 subsets of the in sample data each of them having the same length as the out of sample data. Then I tested each in sample subset against the out of sample data and I recorded the p-values. This process creates not a single p-value for the Kruskall-Wallis test but a distribution making the analysis more robust. In this example the mean of the p-values is well above zero (0.478) indicating that the null hypothesis should be accepted: there are strong evidences that the in and out of sample data is coming from the same distribution.
As usual what is presented in this post is a toy example that only scratches the surface of the problem and should be tailored to individual needs. However I think it proposes an interesting and rational statistical framework to evaluate out of sample results.
This post is inspired by the following two papers:
Vigier Alexandre, Chmil Swann (2007), “Effects of Various Optimization Functions on the Out of Sample Performance of Genetically Evolved Trading Strategies”, Forecasting Financial Markets Conference.
Vigier Alexandre, Chmil Swann (2018), « An optimization process to improve in/out of sample consistency, a Stock Market case», JP Morgan Cazenove Equity Quantitative Conference, London October 2018.
Introducing fidlr: FInancial Data LoadeR.
fidlr is an RStudio addin designed to simplify the financial data downloading process from various providers. This initial version is a wrapper around the getSymbols function in the quantmod package and only Yahoo, Google, FRED and Oanda are supported. I will probably add functionalities over time. As usual with those things just a kind reminder: “THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND…”
How to install and use fidlr?
You can get the addin/package from its Github repository here (I will register it on CRAN later on) Install the addin. There is an excellent tutorial to install RStudio Addins here. Once the addin is installed it should appear in the Addin menu. Just chose fidlr in the menu and a window as pictured below should appear. Choose a data provider from the the Source dropdown menu. Select a date range from the Date menu Enter the symbol you wish to download in the instrument text box. To download several symbols just enter the symbols separated by commas. Use the Radio buttons to choose whether you want to download the instrument in a csv file or in the global environment. The csv file will be saved in the working directory and there will be one csv file per instrument. Press Run to get the data or Close to close down the addin.
Error messages and warnings are handled by the underlying packages (quantmod and Shiny) and can be read from the console.
This is a very first version of the project so do not expect perfection but hopefully it will get better over time. Please report any comment, suggestion, bug etc… to: thertrader@gmail.
Maintaining a database of price files in R.
Doing quantitative research implies a lot of data crunching and one needs clean and reliable data to achieve this. What is really needed is clean data that is easily accessible (even without an internet connection). The most efficient way to do this for me has been to maintain a set of csv files. Obviously this process can be handled in many ways but I found very efficient and simple overtime to maintain a directory where I store and update csv files. I have one csv file per instrument and each file is named after the instrument it contains. The reason I do so is twofold: First, I don’t want to download (price) data from Yahoo, Google etc… every time I want to test a new idea but more importantly once I identified and fixed a problem, I don’t want to have to do it again the next time I need the same instrument. Simple yet very efficient so far. The process is summarized in the chart below.
In everything that follows, I assume that data is coming from Yahoo. The code will have to be amended for data from Google, Quandl etc… In addition I present the process of updating daily price data. The setup will be different for higher frequency data and other type of dataset (i. e. different from prices).
1 & # 8211؛ Initial data downloading (listOfInstruments. R & historicalData. R)
The file listOfInstruments. R is a file containing only the list of all instruments.
If an instrument isn’t part of my list (i. e. no csv file in my data folder) or if you do it for the very first time you have to download the initial historical data set. The example below downloads a set of ETFs daily prices from Yahoo Finance back to January 2000 and store the data in a csv file.
2 & # 8211؛ Update existing data (updateData. R)
The below code starts from existing files in the dedicated folder and updates all of them one after the other. I usually run this process everyday except when I’m on holiday. To add a new instrument, simply run step 1 above for this instrument alone.
3 & # 8211؛ Create a batch file (updateDailyPrices. bat)
Another important part of the job is creating a batch file that automates the updating process above (I’m a Windows user). This avoids opening R/RStudio and run the code from there. The code below is placed on a. bat file (the path has to be amended with the reader’s setup). Note that I added an output file (updateLog. txt) to track the execution.
The process above is extremely simple because it only describes how to update daily price data. I’ve been using this for a while and it has been working very smoothly for me so far. For more advanced data and/or higher frequencies, things can get much trickier.
كالمعتاد أي تعليقات موضع ترحيب.
Factor Evaluation in Quantitative Portfolio Management.
When it comes to managing a portfolio of stocks versus a benchmark the problem is very different from defining an absolute return strategy. In the former one has to hold more stocks than in the later where no stocks at all can be held if there is not good enough opportunity. The reason for that is the tracking error . This is defined as the standard deviation of the portfolio return minus the benchmark return. The less stocks is held vs. a benchmark the higher the tracking error (e. g higher risk).
The analysis that follows is largely inspired by the book “Active Portfolio Management” by Grinold & Kahn. This is the bible for anyone interested in running a portfolio against a benchmark. I strongly encourage anyone with an interest in the topic to read the book from the beginning to the end. It’s very well written and lays the foundations of systematic active portfolio management (I have no affiliation to the editor or the authors).
Here we’re trying to rank as accurately as possible the stocks in the investment universe on a forward return basis. Many people came up with many tools and countless variant of those tools have been developed to achieve this. In this post I focus on two simple and widely used metrics: Information Coefficient (IC) and Quantiles Return (QR).
The IC gives an overview of the factor forecasting ability. More precisely, this is a measure of how well the factor ranks the stocks on a forward return basis. The IC is defined as the rank correlation ( ρ ) between the metric (e. g. factor) and the forward return. In statistical terms the rank correlation is a nonparametric measure of dependance between two variables. For a sample of size n , the n raw scores are converted to ranks , and ρ is computed from:
The horizon for the forward return has to be defined by the analyst and it’s a function of the strategy’s turnover and the alpha decay (this has been the subject of extensive research). Obviously ICs must be as high as possible in absolute terms.
For the keen reader, in the book by Grinold & Kahn a formula linking Information Ratio (IR) and IC is given: with breadth being the number of independent bets (trades). This formula is known as the fundamental law of active management . The problem is that often, defining breadth accurately is not as easy as it sounds.
In order to have a more accurate estimate of the factor predictive power it’s necessary to go a step further and group stocks by quantile of factor values then analyse the average forward return (or any other central tendency metric) of each of those quantiles. The usefulness of this tool is straightforward. A factor can have a good IC but its predictive power might be limited to a small number of stocks. This is not good as a portfolio manager will have to pick stocks within the entire universe in order to meet its tracking error constraint. Good quantiles return are characterised by a monotonous relationship between the individual quantiles and forward returns.
All the stocks in the S&P500 index (at the time of writing). Obviously there is a survival ship bias: the list of stocks in the index has changed significantly between the start and the end of the sample period, however it’s good enough for illustration purposes only.
The code below downloads individual stock prices in the S&P500 between Jan 2005 and today (it takes a while) and turns the raw prices into return over the last 12 months and the last month. The former is our factor, the latter will be used as the forward return measure.
Below is the code to compute Information Coefficient and Quantiles Return. Note that I used quintiles in this example but any other grouping method (terciles, deciles etc…) can be used. it really depends on the sample size, what you want to capture and wether you want to have a broad overview or focus on distribution tails. For estimating returns within each quintile, median has been used as the central tendency estimator. This measure is much less sensitive to outliers than arithmetic mean.
And finally the code to produce the Quantiles Return chart.
3 & # 8211؛ How to exploit the information above?
In the chart above Q1 is lowest past 12 months return and Q5 highest. There is an almost monotonic increase in the quantiles return between Q1 and Q5 which clearly indicates that stocks falling into Q5 outperform those falling into Q1 by about 1% per month. This is very significant and powerful for such a simple factor (not really a surprise though…). Therefore there are greater chances to beat the index by overweighting the stocks falling into Q5 and underweighting those falling into Q1 relative to the benchmark.
An IC of 0.0206 might not mean a great deal in itself but it’s significantly different from 0 and indicates a good predictive power of the past 12 months return overall. Formal significance tests can be evaluated but this is beyond the scope of this article.
The above framework is excellent for evaluating investments factor’s quality however there are a number of practical limitations that have to be addressed for real life implementation:
Rebalancing : In the description above, it’s assumed that at the end of each month the portfolio is fully rebalanced. This means all stocks falling in Q1 are underweight and all stocks falling in Q5 are overweight relative to the benchmark. This is not always possible for practical reasons: some stocks might be excluded from the investment universe, there are constraints on industry or sector weight, there are constraints on turnover etc… Transaction Costs : This has not be taken into account in the analysis above and this is a serious brake to real life implementation. Turnover considerations are usually implemented in real life in a form of penalty on factor quality. Transfer coefficient : This is an extension of the fundamental law of active management and it relaxes the assumption of Grinold’s model that managers face no constraints which preclude them from translating their investments insights directly into portfolio bets.
And finally, I’m amazed by what can be achieved in less than 80 lines of code with R…
كالمعتاد أي تعليقات موضع ترحيب.
Risk as a “Survival Variable”
I come across a lot of strategies on the blogosphere some are interesting some are a complete waste of time but most share a common feature: people developing those strategies do their homework in term of analyzing the return but much less attention is paid to the risk side its random nature. I’ve seen comment like “a 25% drawdown in 2018 but excellent return overall”. Well my bet is that no one on earth will let you experience a 25% loss with their money (unless special agreements are in place). In the hedge fund world people have very low tolerance for drawdown. Generally, as a new trader in a hedge fund, assuming that you come with no reputation, you have very little time to prove yourself. You should make money from day 1 and keep on doing so for a few months before you gain a bit of credibility.
First let’s say you have a bad start and you lose money at the beginning. With a 10% drawdown you’re most certainly out but even with a 5% drawdown the chances of seeing your allocation reduced are very high. This has significant implications on your strategies. Let’s assume that if you lose 5% your allocation is divided by 2 and you come back to your initial allocation only when you passed the high water mark again (e. g. the drawdown comes back to 0). In the chart below I simulated the experiment with one of my strategies.
You start trading in 1st June 2003 and all goes well until 23rd Jul. 2003 where your drawdown curve hits the -5% threshold (**1**). Your allocation is cut by 50% and you don’t cross back the high water mark level until 05th Dec. 2003 (**3**). If you have kept the allocation unchanged, the high water mark level would have been crossed on 28th Oct. 2003 (**2**) and by the end of the year you would have made more money.
But let’s push the reasoning a bit further. Still on the chart above, assume you get really unlucky and you start trading toward mid-June 2003. You hit the 10% drawdown limit by the beginning of August and you’re most likely out of the game. You would have started in early August your allocation would not have been cut at all and you end up doing a good year in only 4 full months of trading. In those two examples nothing has changed but your starting date….
The trading success of any individual has some form of path dependency and there is not much you can do about it. However you can control the size of a strategy’s drawdown and this should be addressed with great care. A portfolio should be diversified in every possible dimension: asset classes, investment strategies, trading frequencies etc…. From that perspective risk is your “survival variable”. If managed properly you have a chance to stay in the game long enough to realise the potential of your strategy. Otherwise you won’t be there next month to see what happens.
كالمعتاد أي تعليقات موضع ترحيب.
A Simple Shiny App for Monitoring Trading Strategies – الجزء الثاني.
This is a follow up on my previous post “A Simple Shiny App for Monitoring Trading Strategies“. I added a few improvements that make the app a bit better (at least for me!). Below is the list of new features :
A sample. csv file (the one that contains the raw data) A “EndDate” drop down box allowing to specify the end of the period. A “Risk” page containing a VaR analysis and a chart of worst performance over various horizons A “How To” page explaining how to use and tailor the app to individual needs.
I also made the app totally self contained. It is now available as a stand alone product and there is no need to have R/RStudio installed on your computer to run it. It can be downloaded from the R Trader Google drive account. This version of the app runs using portable R and portable Chrome. For the keen reader, this link explains in full details how to package a Shiny app into a desktop app (Windows only for now).
1 & # 8211؛ How to install & run the app on your computer.
Create a specific folder Unzip the contain of the. zip file onto that new folder. Change the paths in the runShinyApp file to match your setings To run the app, you just have launch the run. vbs file. I also included an icon (RTraderTradingApp. ico) should you want to create a shortcut on your desktop.
ui. R: controls the layout and appearance of the app server. R: contains the instructions needed to build the app. You can load as much strategies as you want as long as the corresponding csv file has the right format (see below). shinyStrategyGeneral. R: loads the required packages and launches the app.
3 & # 8211؛ How to add a trading strategy?
Create the corresponding. csv file in the right directory Create a new input in the data reactive function (within the server. R file) Add an extra element to the choice parameter in the first selectInput in the sidebarPanel (within the ui. R file). The element’s name should match the name of the new input above.
Remove the input in the data reactive function corresponding to the strategy you want to remove (within the server. R file) Remove the element in the choice parameter in the first selectInput in the sidebarPanel corresponding to the strategy you want to remove (within the ui. R file).
Please feel free to get in touch should you have any suggestion.
A Simple Shiny App for Monitoring Trading Strategies.
In a previous post I showed how to use R, Knitr and LaTeX to build a template strategy report. This post goes a step further by making the analysis interactive. Besides the interactivity, the Shiny App also solves two problems :
I can now access all my trading strategies from a single point regardless of the instrument traded. Coupled with the Shiny interactivity, it allows easier comparison. I can focus on a specific time period.
The code used in this post is available on a Gist/Github repository. There are essentially 3 files.
ui. R : controls the layout and appearance of the app. server. R : contains the instructions needed to build the app. It loads the data and format it. There is one csv file per strategy each containing at least two columns: date and return with the following format: (“2018-12-22″,”0.04%” ). You can load as much strategies as you want as long as they have the right format. shinyStrategyG eneral. R : loads the required packages and launches the app.
This app is probably far from perfect and I will certainly improve it in the future. Feel free to get in touch should you have any suggestion.
A big thank you to the RStudio/Shiny team for such a great tool.
استخدام الخوارزميات الجينية في التداول الكمي.
السؤال الذي يجب أن يطرح نفسه دائما عند استخدام المؤشرات الفنية هو ما يمكن أن يكون معيارا موضوعيا لتحديد مؤشرات المؤشرات (على سبيل المثال، لماذا استخدام مؤشر القوة النسبية 14 يوما بدلا من 15 أو 20 يوما؟). الخوارزميات الجينية (غا) هي أدوات مناسبة تماما للإجابة على هذا السؤال. في هذا المنصب أنا & # 8217؛ سوف تظهر لك كيفية إعداد المشكلة في R. قبل أن أبدأ تذكير المعتاد: ما أقدم في هذا المنصب هو مجرد مثال لعبة وليس دعوة للاستثمار. انها ليست استراتيجية الانتهاء إما ولكن فكرة البحث التي تحتاج إلى مزيد من البحث، وضعت ومصممة خصيصا للاحتياجات الفردية.
ما هي الخوارزميات الجينية؟
أفضل وصف غا أنا جاء عبر يأتي من سايبرناتيك تجارة كتاب من قبل موراي A. روجيرو. & غ؛ خوارزميات جينية تم اختراعها من قبل جون هولاند في منتصف 1970 لحل مشاكل التحسين الصعب. يستخدم هذا الأسلوب الانتقاء الطبيعي، والبقاء للأصلح & # 8221؛. وتتبع العملية العامة الخطوات التالية:
ترميز المشكلة في الكروموسومات باستخدام الترميز، تطوير وظيفة اللياقة البدنية لاستخدامها في تقييم كل قيمة كروموسوم & # 8217؛ s في حل مشكلة معينة تهيئة السكان من الكروموسومات تقييم كل كروموسوم في السكان إنشاء كروموسومات جديدة عن طريق التزاوج اثنين من الكروموسومات. ويتم ذلك عن طريق كتم وإعادة ربط اثنين من الآباء والأمهات لتشكيل طفلين (يتم اختيار الآباء عشوائيا ولكن منحازة من قبل لياقتهم) تقييم الكروموسوم الجديد حذف عضو من السكان الذي هو أقل ملاءمة من الكروموسوم الجديد وإدراج كروموسوم جديد في السكان . إذا تم التوصل إلى معايير التوقف (الحد الأقصى لعدد الأجيال، ومعايير اللياقة البدنية جيدة بما فيه الكفاية & # 8230؛) ثم إرجاع أفضل كروموسوم بدلا من الذهاب إلى الخطوة 4.
من منظور التداول غا مفيدة جدا لأنها جيدة في التعامل مع المشاكل غير الخطية للغاية. ومع ذلك فإنها تظهر بعض الميزات سيئة التي تستحق الذكر:
أكثر من المناسب: هذه هي المشكلة الرئيسية وانها إلى المحلل لإعداد المشكلة بطريقة تقلل من هذا الخطر. وقت الحوسبة: إذا لم يتم تحديد المشكلة بشكل صحيح، يمكن أن تكون طويلة للغاية للوصول إلى حل لائق والتعقيد يزيد أضعافا مضاعفة مع عدد من المتغيرات. وبالتالي ضرورة اختيار بعناية المعلمات.
هناك العديد من حزم R التعامل مع غا، اخترت استخدام واحد الأكثر شيوعا: رجنود.
أسعار الإغلاق اليومية لمعظم صناديق الاستثمار المتداولة السائلة من ياهو المالية تعود إلى يناير 2000. في الفترة عينة من يناير كانون الثاني 2000 إلى ديسمبر 2018. تبدأ فترة خارج العينة في يناير 2018.
المنطق هو كما يلي: يتم تحسين وظيفة اللياقة البدنية على مدى فترة العينة للحصول على مجموعة من المعلمات الأمثل للمؤشرات الفنية المحددة. ثم يتم تقييم أداء تلك المؤشرات في فترة خارج العينة. ولكن قبل القيام بذلك، يجب اختيار المؤشرات الفنية.
سوق الأسهم يظهر اثنين من الخصائص الرئيسية التي هي مألوفة لأي شخص لديه بعض الخبرة التجارية. الزخم على المدى الطويل وعكس المدى القصير. ويمكن ترجمة هذه الميزات على أساس المؤشرات الفنية من خلال: تحريك المتوسطات عبر و رسي. ويمثل هذا مجموعة من 4 معلمات: فترات العودة إلى الوراء للمتوسطات المتحركة طويلة وقصيرة الأجل، فترة نظر إلى الوراء لمؤشر القوة النسبية ومؤشر القوة النسبية. مجموعات من المعلمات هي الكروموسومات. العنصر الرئيسي الآخر هو وظيفة اللياقة البدنية. قد نرغب في استخدام شيء مثل: الحد الأقصى للعائد أو نسبة شارب أو الحد الأدنى لمعدل السحب. في ما يلي، اخترت لتعظيم نسبة شارب.
تنفيذ R هو مجموعة من 3 وظائف:
فيتنيسفونكتيون: يحدد وظيفة اللياقة البدنية (على سبيل المثال، نسبة شارب القصوى) لاستخدامها في تجارة محرك غا. الخصائص: ملخص إحصاءات التداول لفترات العينة وخارجها لأغراض المقارنة جينود: محرك غا من حزمة رجنود.
وظيفة الجينود معقدة إلى حد ما ولكن أنا & # 8217؛ م لن تشرح ما يعني كل معلمة كما أريد للحفاظ على هذا المنصب القصير (والوثائق جيدة حقا).
في الجدول أدناه أعرض لكل صك المعلمات المثلى (مؤشر القوة النسبية فترة العودة إلى الوراء، عتبة مؤشر القوة النسبية، المتوسط ​​المتحرك على المدى القصير، والمتوسط ​​المتحرك على المدى الطويل) جنبا إلى جنب مع داخل وخارج إحصاءات التجارة العينة.
قبل التعليق على النتائج المذكورة أعلاه، أريد أن أشرح بضع نقاط مهمة. لمطابقة المنطق المحدد أعلاه، ربطت المعلمات للتأكد من أن فترة النظر إلى الوراء للمتوسط ​​المتحرك على المدى الطويل هي دائما أطول أن المتوسط ​​المتحرك أقصر. كما قيدت المحسن لاختيار الحلول فقط مع أكثر من 50 حرفا في فترة العينة (على سبيل المثال؛ دلالة إحصائية).
وعموما فإن النتائج خارج العينة لا تزال مثيرة للإعجاب. عوائد منخفضة حتى لو كان عدد من الصفقات الصغيرة لجعل النتيجة كبيرة حقا. ومع ذلك هناك خسارة كبيرة في الكفاءة بين وخارج فترة العينة لليابان (إوج) التي من المرجح جدا يعني أكثر من المناسب.
تهدف هذه الوظيفة إلى إعطاء القارئ الأدوات اللازمة لاستخدام غا بشكل صحيح في إطار التداول الكمي. Once again, It’s just an example that needs to be further refined. وهناك عدد قليل من التحسينات المحتملة لاستكشاف ما يلي:
وظيفة اللياقة البدنية: تعظيم نسبة شارب تبسيط جدا. A & # 8220؛ أكثر ذكاء & # 8221؛ وظيفة من المؤكد أن تحسين الخروج من نموذج إحصاءات التجارة نمط: نحن نحاول التقاط نمط واضح جدا. وهناك حاجة إلى مزيد من البحوث نمط العمق. التحسين: هناك العديد من الطرق لتحسين طريقة إجراء التحسين. وهذا من شأنه أن يحسن من سرعة الحساب وعقلانية النتائج.
تتوفر التعليمات البرمجية المستخدمة في هذا المنصب على مستودع جيست.

Quantitative Trading Strategy Using R: A Step by Step Guide.
In this post we will discuss about building a trading strategy using R. Before dwelling into the trading jargons using R let us spend some time understanding what R is. R هو مصدر مفتوح. There are more than 4000 add on packages,18000 plus members of LinkedIn’s group and close to 80 R Meetup groups currently in existence. بل هو أداة مثالية للتحليل الإحصائي وخاصة لتحليل البيانات. الإعداد موجزة من شبكة الأرشيف R شاملة يعرف كما كران يوفر لك قائمة الحزم جنبا إلى جنب مع تركيب قاعدة المطلوبة. هناك الكثير من الحزم المتاحة اعتمادا على تحليل يجب القيام به. لتنفيذ استراتيجية التداول، وسوف نستخدم حزمة تسمى كوانسترات.
عملية أربع خطوات لأي إستراتيجية تداول أساسية.
تكوين الفرضية اختبار تكرير الإنتاج.
وقد صيغت فرضيتنا على أنها "السوق يعني العودة". متوسط ​​العائد هو نظرية تشير إلى أن الأسعار تتحرك في نهاية المطاف إلى القيمة المتوسطة. أما الخطوة الثانية فتتضمن اختبار الفرضية التي نقوم بصياغة إستراتيجية لها على فرضيتنا وحساب المؤشرات والإشارات ومقاييس الأداء. يمكن تقسيم مرحلة الاختبار إلى ثلاث خطوات، والحصول على البيانات، وكتابة الاستراتيجية وتحليل الإخراج. في هذا المثال نعتبر نيفتي النحل. وهو صندوق تداول تبادل تدار من قبل جولدمان ساكس. نس لديها حجم ضخم للأداة وبالتالي فإننا نعتبر هذا. تظهر الصورة أدناه السعر المفتوح-العالي-المنخفض-إغلاق نفسه.
وضعنا مستوى عتبة لمقارنة التقلبات في السعر. في حالة ارتفاع / نقص السعر نقوم بتحديث عمود العتبة. يتم مقارنة سعر الإغلاق مع الفرقة العليا ومع الفرقة السفلى. عندما يتم عبر الشريط العلوي، هو إشارة للبيع. Similarly when the lower band is crossed, it is a signal for sell.
ويمكن تلخيص قسم الترميز على النحو التالي،
ويرد في الرسم البياني أدناه عرض لطائرات الهليكوبتر باتجاه إنتاج الاستراتيجية.
وبالتالي فإن فرضيتنا أن السوق يعني العودة يتم دعم. وبما أن هذا هو الاختبار الخلفي لدينا مجال لتنقيح المعلمات التجارية التي من شأنها تحسين متوسط ​​عوائدنا والأرباح المحققة. ويمكن القيام بذلك عن طريق تحديد مستويات عتبة مختلفة، وقواعد دخول أكثر صرامة، ووقف الخسارة وما إلى ذلك يمكن للمرء أن يختار المزيد من البيانات للاختبار الخلفي، واستخدام نهج بايسيان لعتبة اقامة، تأخذ التقلب بعين الاعتبار.
مرة كنت واثقا من استراتيجية التداول المدعومة من نتائج الاختبار الخلفي هل يمكن أن خطوة إلى التداول المباشر. بيئة الإنتاج هو موضوع كبير في حد ذاته وانها خارج النطاق في سياق المقال. لشرح باختصار هذا ينطوي على كتابة الاستراتيجية على منصة التداول.
كما ذكرنا سابقا، سنقوم ببناء النموذج باستخدام حزمة كوانسترات. يوفر كوانتسترات بنية تحتية عامة لنموذج واستراتيجيات باكتست إشارة القائمة على إشارة. بل هو طبقة التجريد رفيعة المستوى (التي بنيت على شتس، فينانسيالينسترومنت، النازل، الخ) التي تسمح لك لبناء واختبار الاستراتيجيات في عدد قليل جدا من خطوط التعليمات البرمجية.
الملامح الرئيسية ل كوانسترات هي،
يدعم الاستراتيجيات التي تشمل المؤشرات والإشارات والقواعد يسمح الاستراتيجيات التي سيتم تطبيقها على المحافظ متعددة الأصول يدعم السوق، والحد، و ستوبليميت، وأنواع ستوبترايلينغ النظام يدعم ترتيب التحجيم وتحسين المعلمة.
في هذا المنصب نبني استراتيجية تتضمن المؤشرات والإشارات والقواعد.
وفيما يتعلق بالنموذج العام القائم على الإشارة التالية،
الأدوات - تحتوي على بيانات السوق المؤشرات - القيم الكمية المستمدة من بيانات السوق الإشارات - نتيجة التفاعل بين بيانات السوق والمؤشرات القواعد - إصدار الأوامر باستخدام بيانات السوق والمؤشرات والإشارات.
دون الكثير من اللغط دعونا مناقشة جزء الترميز. نحن نفضل R ستوديو للترميز وتصر على استخدام نفس. يجب أن يكون لديك حزم معينة مثبتة قبل برمجة الاستراتيجية.
تقوم مجموعة الأوامر التالية بتثبيت الحزم الضرورية.
بمجرد تثبيت الحزم التي استيرادها لمزيد من الاستخدام.
قراءة البيانات من ملف كسف وتحويله إلى كائن شتس.
نقوم بتهيئة المحفظة مع المخزون والعملة وحقوق الملكية الأولية ونوع الاستراتيجية.
أضف حد الموضع إذا كنت ترغب في التداول أكثر من مرة على نفس الجانب.
إنشاء كائن الاستراتيجية.
نحن نبني وظيفة تحسب العتبات التي نريد التجارة. إذا تحرك السعر من قبل thresh1 نقوم بتحديث عتبة السعر الجديد. نطاقات جديدة للتداول هي عتبة +/- ثريش 2. الإخراج هو كائن شتس على الرغم من أننا نستخدم وظيفة إعادة التصنيف لضمان.
أضف المؤشر والإشارة وقاعدة التداول.
تشغيل الاستراتيجية وإلقاء نظرة على كتاب النظام.
تحديث محفظة وعرض إحصاءات التجارة.
هنا هو رمز كامل.
Once you are familiar with these basics you could take a look at how to start using quantimod package in R. Or in case you’re good at C++, take a look at an example strategy coded in C++.
إذا كنت تاجر التجزئة أو المهنية المهنية تبحث لبدء مكتب التداول الآلي الخاص بك، والبدء في تعلم ألغو التداول اليوم! تبدأ مع المفاهيم الأساسية مثل هندسة التداول الآلي، المجهرية السوق، باكتستينغ استراتيجية ونظام إدارة النظام.

Comments