خطوة أخرى أيضا.
يرجى إكمال الفحص الأمني للوصول إلى رمز التداول.
لماذا يتعين علي إكمال اختبار كابتشا؟
إكمال اختبار كابتشا يثبت أنك إنسان وتمنحك إمكانية الوصول المؤقت إلى موقع الويب.
ماذا يمكنني أن أفعل لمنع ذلك في المستقبل؟
إذا كنت على اتصال شخصي، كما هو الحال في المنزل، يمكنك تشغيل فحص مكافحة الفيروسات على جهازك للتأكد من أنه لم يصاب مع البرامج الضارة.
إذا كنت في مكتب أو شبكة مشتركة، يمكنك أن تطلب من مسؤول الشبكة إجراء فحص عبر الشبكة تبحث عن الأجهزة التي تمت تهيئتها بشكل خاطئ أو المصابة.
كلودفلار راي إد: 3e34cbfa155a5a0e & بول؛ إب الخاص بك: 78.109.24.111 & الثور؛ الأداء & أمب؛ الأمن من قبل كلودفلار.
خطوة أخرى أيضا.
يرجى إكمال الفحص الأمني للوصول إلى رمز التداول.
لماذا يتعين علي إكمال اختبار كابتشا؟
إكمال اختبار كابتشا يثبت أنك إنسان وتمنحك إمكانية الوصول المؤقت إلى موقع الويب.
ماذا يمكنني أن أفعل لمنع ذلك في المستقبل؟
إذا كنت على اتصال شخصي، كما هو الحال في المنزل، يمكنك تشغيل فحص مكافحة الفيروسات على جهازك للتأكد من أنه لم يصاب مع البرامج الضارة.
إذا كنت في مكتب أو شبكة مشتركة، يمكنك أن تطلب من مسؤول الشبكة إجراء فحص عبر الشبكة تبحث عن الأجهزة التي تمت تهيئتها بشكل خاطئ أو المصابة.
طريقة أخرى لمنع الحصول على هذه الصفحة في المستقبل هي استخدام بريفاسي باس. تحقق من إضافة المتصفح في فايرفوكس أد-أونس ستور.
كلودفلار راي إد: 3e34cbfa27b65a14 & بول؛ إب الخاص بك: 78.109.24.111 & الثور؛ الأداء & أمب؛ الأمن من قبل كلودفلار.
الاستراتيجيات.
الاستراتيجية هي الدراسة التي يمكن أن ترسل وتعديل وإلغاء أوامر (لشراء / بيع). استراتيجيات تسمح لك لأداء باكتستينغ (مضاهاة استراتيجية التداول على البيانات التاريخية) والتحويل إلى الأمام (مضاهاة استراتيجية التداول على البيانات في الوقت الحقيقي) وفقا خوارزميات مسبقة الخاص بك.
استراتيجية مكتوبة بلغة الصنوبر الصنوبر لديها نفس القدرات مثل مؤشر الصنوبر. عند كتابة رمز إستراتيجية، يجب أن تبدأ بالكلمة الرئيسية "إستراتيجية"، وليس "دراسة". الاستراتيجيات ليس فقط مؤامرة شيء، ولكن أيضا مكان وتعديل وإلغاء أوامر. لديهم إمكانية الوصول إلى المعلومات الأساسية عن أداء الاستراتيجية من خلال كلمات رئيسية محددة. نفس المعلومات متاحة لك على علامة التبويب "اختبار استراتيجية". مرة واحدة يتم احتساب استراتيجية على البيانات التاريخية، يمكنك أن ترى يملأ أمر افتراضي.
مثال استراتيجي بسيط.
حالما يتم تجميع البرنامج النصي وتطبيقه على الرسم البياني، يمكنك أن ترى علامات النظام شغل على ذلك وكيف كان رصيدك يتغير خلال باكتستينغ (منحنى الأسهم). هو أبسط استراتيجية أن يشتري ويبيع في كل شريط.
استراتيجية الخط الأول ("اختبار") تنص على رمز ينتمي إلى نوع الاستراتيجية واسمها هو "اختبار". ستراتيجيك. إنتري () أمر بإرسال أوامر "شراء" و "بيع". مؤامرة (استراتيجية. المساواة) قطع منحنى الأسهم.
كيفية تطبيق استراتيجية على الرسم البياني.
لاختبار الاستراتيجية الخاصة بك، وتطبيقه على الرسم البياني. استخدم فترات الرمز والوقت التي تريد اختبارها. يمكنك استخدام استراتيجية مضمنة من "المؤشرات & أمب؛ ستراتيديس "، أو الكتابة الخاصة بك في محرر الصنوبر.
ملاحظة: عند استخدام غير القياسية (رينكو، كاجي، خط كسر، نقطة والشكل، هيكين العشي، نشر المخططات) أنواع الرسم البياني كأساس للاستراتيجية، تحتاج إلى إدراك أن النتيجة ستكون مختلفة. سيتم تنفيذ الأوامر على أسعار هذا المخطط (على سبيل المثال ل هيكين العشي أنها سوف تأخذ أسعار هيكين العشي (المتوسط منها) ليس أسعار السوق الحقيقية). لذلك نحن نوصي بشدة لاستخدام نوع المخطط القياسية للاستراتيجيات.
باكتستينغ و فرونتستستينغ.
يتم احتساب استراتيجيات ترادينغفيو على جميع البيانات التاريخية المتاحة على الرسم البياني وتابع تلقائيا حساب عندما تأتي البيانات في الوقت الحقيقي.
خلال حساب التاريخ والحساب في الوقت الحقيقي، يتم احتساب رمز على شريط يغلق افتراضيا.
إذا كان هذا هو فروردتستينغ، رمز يحسب على كل القراد في الوقت الحقيقي. لتمكين هذا، تحقق من خيار "إعادة حساب على كل علامة" في إعدادات أو القيام بذلك في البرنامج النصي نفسه: استراتيجية (.
يمكنك تعيين استراتيجية لإجراء عملية حسابية إضافية بعد تعبئة الطلب. لهذا تحتاج إلى التحقق من "إعادة حساب بعد أمر شغل" في إعدادات أو القيام بذلك في السيناريو نفسه: استراتيجية (...، calc_on_order_fills = صحيح،.).
محاكي الوسيط.
هناك محاكي وسيط على ترادينجفيو لاستراتيجيات الاختبار. على عكس التداول الحقيقي، المضاهاة يملأ أوامر فقط في أسعار الرسم البياني، وهذا هو السبب في أمر يمكن ملؤها فقط على القراد المقبل في فرونتستستينغ وعلى شريط المقبل في باكتستينغ (أو في وقت لاحق) بعد استراتيجية محسوبة.
كما ذكر أعلاه، يتم احتساب استراتيجية باكتستينغ على إغلاق بار. يتم استخدام المنطق التالي لمحاكاة طلبات التعبئة:
إذا فتح سعر شريط أقرب إلى أعلى سعر من نفس الشريط، وسيط محاكي يفترض أن السعر إنترابار كان يتحرك بهذه الطريقة: فتح → عالية → منخفضة → إغلاق. إذا فتح سعر شريط أقرب إلى أدنى سعر لنفس شريط، محاكي وسيط يفترض أن السعر إنترابار كان يتحرك بهذه الطريقة: فتح → منخفضة → عالية → إغلاق. المحاكي وسيط يفترض أنه لم يكن هناك ثغرات داخل شريط، وهذا يعني جميع أسعار إنترابار متاحة لتنفيذ النظام. إذا تم تمكين الخيار "إعادة حساب على كل علامة" في خصائص استراتيجية (أو استراتيجية (.con_on_every_tick = ترو،.) في البرنامج النصي)، لا يزال يحسب رمز فقط على إغلاق شريط، بعد المنطق أعلاه.
هنا هي استراتيجية تبين كيف يتم شغل أوامر من قبل المحاكي وسيط:
يتم حساب هذا الرمز مرة واحدة في شريط افتراضيا، على إغلاقه، ولكن هناك حساب إضافية بمجرد تعبئة أمر. هذا هو السبب في أنك يمكن أن نرى 4 أوامر شغلها على كل شريط: 2 أوامر على فتح، 1 أمر على ارتفاع و 1 أمر على انخفاض. هذا هو باكتستينغ. إذا كان في الوقت الحقيقي، سيتم تنفيذ أوامر على كل علامة جديدة.
ومن الممكن أيضا لمحاكاة قائمة انتظار النظام. ويسمى الإعداد "التحقق من الأسعار لأوامر الحد" ويمكن العثور عليها في خصائص استراتيجية أو تعيين في البرنامج النصي نفسه: استراتيجية (. باكتست_fill_limits_assumption = X،.). القيمة المحددة = عدد النقاط / النقاط (الحد الأدنى لحركات السعر)، القيمة الافتراضية = 0. يتم تعبئة أمر الحد إذا كان السعر الحالي أفضل (أعلى لأوامر البيع، وانخفاض لأوامر الشراء) لعدد محدد من النقاط / نقطة. سعر التنفيذ لا يزال يطابق سعر أمر الحد.
backtest_fill_limits_assumption = 1. حركة السعر الدنيا = 0.25.
يتم وضع أمر حد الشراء بسعر 12.50.
السعر الحالي هو 12.50.
لا يمكن ملء النظام في السعر الحالي فقط لأن backtest_fill_limits_assumption = 1. لملء النظام يجب أن يصبح السعر 0.25 * 1 أقل. يتم وضع النظام في قائمة الانتظار.
افترض أن القراد التالي يأتي بسعر 12.00. هذا السعر هو 2 نقاط أقل، ما يعني أن الشرط "backtest_fill_limits_assumption = 1" هو راض، لذلك يجب أن يتم ملء النظام. يتم تعبئة الطلب في 12.50 (سعر النظام الأصلي)، حتى لو كان السعر غير متوفر بعد الآن.
أوامر ترتيب الطلب.
تبدأ جميع الكلمات الرئيسية المصممة للاستراتيجيات مع البادئة "إستراتيجية". يتم استخدام الأوامر التالية لوضع الأوامر: ستراتيغي. إنتري، ستراتيغي. ورد أند ستراتيجيك. إكسيت:
strategic. entry - هذا الأمر يضع أوامر الدخول فقط. يتأثر إعداد بيراميدينغ (في خصائص الإستراتيجية) ومن خلال الكلمة الرئيسية للاستراتيجية. risk. allow_entry_in. إذا كان هناك وضع السوق المفتوح عندما يتم إنشاء أمر الاتجاه المعاكس، سيتم زيادة عدد العقود / أسهم / الكثير / وحدات من عدد العقود المفتوحة حاليا (ما يعادل السكريبت: استراتيجية. بوسيتيون + سيز + كوانتيتي). ونتيجة لذلك، فإن حجم موقف السوق لفتح يكون مساويا لحجم النظام، المحدد في استراتيجية القيادة. import. order - هذا الأمر يضع أوامر الدخول والخروج. لا يتأثر إعداد بيراميدينغ و ستراتيجيك. risk. allow_entry_in الكلمة الرئيسية. انها تسمح لك لخلق معقدة الدخول والخروج بناء النظام عندما قدرات الاستراتيجية. نتري و ستراتيجيت. التوقيع ليست كافية. invest. exit - هذا الأمر يسمح لك للخروج من موقف السوق من قبل أمر أو نموذج استراتيجية متعددة أجل الخروج (وقف الخسارة، هدف الربح، وقف زائدة). كل هذه الأوامر هي جزء من نفس المجموعة استراتيجيا. لا يمكن وضع أمر خروج إذا لم يكن هناك موقف سوق مفتوح أو لا يوجد أمر دخول نشط (أمر الخروج مرتبط بمعرف أمر دخول). ليس من الممكن الخروج من موقع مع نظام السوق باستخدام الأمر. لتحقيق هذا الهدف، يجب استخدام الأوامر التالية: ستراتيغي. close أور strategic. close_all. إذا كان عدد العقود / األسهم / الوحدات / الوحدات المحددة لالستراتيجية أقل من حجم المركز المفتوح الحالي، سيكون الخروج جزئيا. لا يمكن الخروج من أمر الدخول نفسه أكثر من مرة واحدة باستخدام نفس أمر الخروج (إد)، الذي يسمح لك بإنشاء استراتيجيات الخروج بمستويات متعددة. في حالة، عندما يتم تشكيل وضع السوق من خلال أوامر دخول متعددة (تمكين بيراميدينغ)، كل أوامر الخروج ملزمة لكل أمر دخول بشكل فردي.
الاستراتيجية المذكورة أعلاه عكس موقف السوق باستمرار من +4 إلى -6، ذهابا وإيابا، ما هو مبين من مؤامرة لها.
هذه الاستراتيجية توضح الحالة، عندما يتم إغلاق موقف السوق أبدا، لأنه يستخدم أمر الخروج إلى إغلاق السوق في وضع جزئي فقط، وأنه لا يمكن استخدامها أكثر من مرة. إذا كنت مضاعفة الخط للخروج، فإن استراتيجية إغلاق موقف السوق تماما.
هذا الرمز يولد 2 مستويات من الأقواس (2 أخذ أوامر الربح و 2 أوامر وقف الخسارة). يتم تنشيط كلا المستويين في نفس الوقت: المستوى الأول للخروج من 2 العقود والثانية للخروج من بقية.
أول أوامر الربح ووقف الخسارة (المستوى 1) هي في مجموعة واحدة أوكا. أوامر أخرى (المستوى 2) في مجموعة أخرى أوكا. وهذا يعني أنه بمجرد ملء أمر من المستوى 1، لا يتم إلغاء أوامر من المستوى 2، فإنها تبقى نشطة.
كل أمر وضع أمر له معرف (قيمة سلسلة) - معرف النظام الفريد. إذا تم بالفعل وضع أمر بنفس المعرف (ولكن لم يتم شغله بعد)، فإن الأمر الحالي يعدل الأمر الحالي. إذا كان التعديل غير ممكن (التحويل من شراء للبيع)، يتم إلغاء النظام القديم، يتم وضع النظام الجديد. ستراتيجيك. إنتري و ستراتيغي. يعمل مع نفس المعرفات (يمكنهم تعديل نفس أمر الدخول). ستراتيغي. يعمل مع معرفات الطلب الأخرى (من الممكن أن يكون هناك أمر دخول وأمر الخروج مع نفس المعرف).
لإلغاء أمر معين (بواسطة معرفه) يجب استخدام الأمر الأمر. إلغاء (معرف السلسلة). لإلغاء كافة الأوامر المعلقة، يجب استخدام إستراتيجية الأمر. cancel_all (). يتم وضع أوامر استراتيجية حالما يتم استيفاء شروطهم ويسمى الأمر في التعليمات البرمجية. محاكي السمسار لا تنفذ أوامر قبل القراد المقبل يأتي بعد احتساب رمز، بينما في التداول الحقيقي مع وسيط حقيقي، أمر يمكن ملؤها عاجلا. وهو ما يعني أنه إذا تم إنشاء أمر السوق في نهاية شريط الحالي، يتم ملئه في شريط مفتوح المقبل.
إذا تم تطبيق هذا الرمز على مخطط، يتم تعبئة جميع الطلبات في فتح كل شريط.
يتم تحديد شروط وضع الطلب (عند، الهرمية، ستراتيجيك. ريسك) عند حساب البرنامج النصي. إذا تم استيفاء جميع الشروط، يتم وضع النظام. إذا كان أي شرط غير راض، لم يتم وضع النظام. من المهم إلغاء أوامر السعر (الحد، وقف وإيقاف أوامر الحد).
مثال (ل مسفت 1D):
على الرغم من أن يتم تعطيل الهرمية، يتم ملء هذه الطلبات على حد سواء في باكتستينغ، لأنه عندما يتم إنشاؤها ليس هناك موقف السوق طويلة مفتوحة. يتم وضع كل من الأوامر وعندما يرضي سعر تنفيذ النظام، على حد سواء الحصول على تنفيذها. فمن المستحسن أن تضع أوامر في 1 أوكا المجموعة عن طريق استراتيجيا. في هذه الحالة يتم تعبئة أمر واحد فقط ويتم إلغاء الآخر. في ما يلي الشفرة المعدلة:
إذا، لسبب ما، أمر وضع الشروط لا يتم الوفاء بها عند تنفيذ الأمر، لن يتم وضع أمر الدخول. على سبيل المثال، إذا تم تعيين إعدادات بيراميدينغ إلى 2، يحتوي الموضع الحالي بالفعل على إدخالات اثنين وتحاول الاستراتيجية وضع ثالث، لن يتم وضعه. يتم تقييم شروط الدخول في مرحلة توليد الطلب وليس في مرحلة التنفيذ. لذلك، إذا قمت بإرسال اثنين من إدخالات نوع السعر مع تعطيل بيراميدينغ، مرة واحدة يتم تنفيذ واحد منهم الآخر لن يتم إلغاء تلقائيا. لتجنب القضايا نوصي باستخدام مجموعات أوكا-إلغاء لإدخالات حتى عندما يتم ملء أمر دخول واحد يتم إلغاء الآخرين.
وينطبق الشيء نفسه على مخارج نوع السعر - سيتم وضع الطلبات بمجرد استيفاء شروطها (أي أمر دخول يحمل المعرف المعني).
إذا قمت بتطبيق هذا المثال على مخطط، يمكنك أن ترى أن أمر الخروج قد تم ملئه على الرغم من أنه تم إنشاؤه مرة واحدة فقط قبل أن يتم وضع أمر الدخول إلى إغلاق. ومع ذلك، لم يتم إغلاق الإدخال التالي قبل نهاية الحساب كما تم تشغيل الأمر الخروج بالفعل.
إغلاق موقف السوق.
على الرغم من أنه من الممكن للخروج من إدخال معين في التعليمات البرمجية، عندما تظهر الطلبات في قائمة الصفقات على علامة التبويب ستراتيغيتيستر، فإنها كلها مرتبطة وفقا فيفو (أولا في، أولا) القاعدة. إذا لم يتم تحديد معرف أمر دخول لأمر الخروج في التعليمات البرمجية، فإن أمر الخروج يغلق أمر الدخول الأول الذي فتح موضع السوق. دعونا ندرس المثال التالي:
رمز المذكورة أعلاه يضع 2 أوامر بالتتابع: "Buy1" في سعر السوق و "Buy2" بسعر 10٪ أعلى (وقف النظام). يتم وضع أمر الخروج فقط بعد ملء أوامر الدخول. إذا قمت بتطبيق التعليمات البرمجية على مخطط، سترى أن كل أمر دخول يتم إغلاقه بواسطة أمر الخروج، على الرغم من أننا لم نحدد معرف أمر الدخول لإغلاقه في هذا السطر: ستراتيجيك. إكسيت ("براكيت"، لوس = 10، بروفيت = 10، ون = strategic. position_size == 15)
فإنه يفتح 5 عقود موقف طويل مع النظام "Buy1". وتمتد الصفقة لفترة طويلة من خلال شراء 10 عقود أخرى بسعر 10٪ أعلى مع أمر "Buy2". يتم إكمال أمر الخروج (strategic. close) لبيع 10 عقود (الخروج من "Buy2").
إذا قمت بإلقاء نظرة على المؤامرة، يمكنك أن ترى أن متوسط سعر الدخول = "Buy2" سعر التنفيذ واستراتيجيتنا أغلقت بالضبط هذا أمر الدخول، بينما على علامة التبويب تراديليست يمكننا أن نرى أنه أغلق أول طلب "Buy1" ونصف من الثانية "Buy2". وهذا يعني أنه بغض النظر عن أمر الدخول الذي تحدده لاستراتيجيتك لإغلاق، وسوف محاكي السمسار لا يزال إغلاق أول واحد (وفقا لقاعدة فيفو). وهو يعمل بنفس الطريقة عند التداول من خلال وسيط.
أوكا.
فمن الممكن لوضع أوامر في 2 مجموعات أوكا مختلفة في الصنوبر السيناريو:
ستراتيغي. oca. cancel - بمجرد أن يتم ملء أمر من مجموعة (حتى جزئيا) أو إلغاؤها، يتم إلغاء أوامر أخرى من نفس المجموعة. يجب أن نأخذ في الاعتبار أنه إذا كانت أسعار الطلب هي نفسها أو أنها قريبة، قد يتم ملء أكثر من ترتيب واحد من نفس المجموعة. يتوفر هذا النوع من مجموعة أوكا فقط لأوامر الدخول لأن جميع أوامر الخروج يتم وضعها في invest. oca. reduce.
قد تعتقد أن هذه هي استراتيجية عكسية منذ الهرمية غير مسموح بها، ولكن في الواقع كل من النظام سوف تحصل على شغل لأنها نظام السوق، ما يعني أنها سيتم تنفيذها على الفور بالسعر الحالي. لا يتم إلغاء الأمر الثاني لأن كلاهما يتم ملئها تقريبا في نفس اللحظة ولا يكون لدى النظام الوقت لمعالجة ملء النظام الأول وإلغاء الثانية قبل أن يتم تنفيذها. وينطبق الشيء نفسه إذا كانت هذه أوامر أسعار بنفس الأسعار أو ما شابهها. استراتيجية يضع جميع أوامر (التي يسمح وفقا لموقف السوق، الخ).
تضع الاستراتيجية جميع الأوامر التي لا تتناقض مع القواعد (في موقفنا حالة السوق هو شقة، وبالتالي أي أمر دخول يمكن ملؤها). في كل حساب القراد، أولا يتم تنفيذ جميع أوامر مع شروط راض وفقط ثم أوامر من المجموعة التي تم تنفيذ أمر إلغاء.
ستراتيغي. oca. reduce - يسمح هذا النوع من المجموعة بأوامر متعددة داخل المجموعة ليتم ملؤها. عندما يبدأ شغل أحد الطلبات داخل المجموعة، يتم تخفيض حجم الطلبات الأخرى من خلال قيمة العقود المليئة. ومن المفيد جدا لاستراتيجيات الخروج. وبمجرد أن يلمس السعر نظام الربح والربح الخاص بك ويتم شغله، لا يتم إلغاء وقف الخسارة ولكن يتم تخفيض مبلغها عن طريق كمية العقود شغلها، وبالتالي حماية بقية الموقف المفتوح..I. none - يتم وضع النظام خارج المجموعة (القيمة الافتراضية للأوامر ستراتيجيك. ورد و strategic. entry).
كل مجموعة لديها معرف فريدة من نوعها (بنفس الطريقة كما أوامر لديها). إذا كان مجموعتين لها نفس المعرف، ولكن نوع مختلف، وسوف تعتبر مجموعات مختلفة. مثال:
سيتم وضع "شراء" و "بيع" في مجموعات مختلفة لأن نوعها مختلف. "أمر" سيكون خارج أي مجموعة كما يتم تعيين نوعه إلى strategic. oca. none. وعلاوة على ذلك، سيتم وضع "شراء" في مجموعة الخروج كما يتم وضع المخارج دائما في مجموعة نوع الاستراتيجيه. وكا. ريدوشيزيه.
إدارة المخاطر.
وليس من السهل وضع استراتيجية عالمية مربحة. عادة، يتم إنشاء استراتيجيات لبعض أنماط السوق ويمكن أن تنتج خسائر لا يمكن السيطرة عليها عند تطبيقها على البيانات الأخرى. لذلك وقف تجارة السيارات في الوقت المناسب يجب أن تذهب الأمور سيئة هي قضية خطيرة. وهناك مجموعة خاصة من الأوامر الاستراتيجية لإدارة المخاطر. كلهم يبدأون باستراتيجية. * البادئة *.
يمكنك الجمع بين أي عدد من المخاطر في أي مجموعة ضمن استراتيجية واحدة. يتم احتساب كل أمر فئة المخاطر في كل علامة وكذلك في كل أمر تنفيذ الحدث بغض النظر عن إعداد استراتيجية cal_on_order_fills. لا توجد طريقة لتعطيل أي قاعدة خطر في وقت التشغيل من البرنامج النصي. بغض النظر عن مكان وجود النص في قاعدة الخطر سيتم تطبيقه دائما ما لم يتم حذف السطر مع القاعدة وإعادة ترجمة البرنامج النصي.
إذا كان على حساب المقبل يتم تشغيل أي من القواعد، لن يتم إرسال أوامر. لذلك إذا كانت الاستراتيجية لديها عدة قواعد من نفس النوع مع معلمات مختلفة، فإنه سيتم إيقاف حساب عند تشغيل القاعدة مع المعلمات الأكثر صرامة. عندما يتم إيقاف إستراتيجية يتم إلغاء كافة الطلبات غير المنفذة ثم يتم إرسال أمر السوق لإغلاق الموقف إذا لم يكن مسطح.
وعلاوة على ذلك، فمن الجدير بالذكر أنه عند استخدام قرارات أعلى من 1 يوم، ويعتبر شريط كامل ليكون 1 يوم للقواعد بدءا من البادئة "invest. risk. max_intraday_"
سيتم إغلاق الصفقة وسيتم إيقاف التداول حتى نهاية كل جلسة تداول بعد تنفيذ أمرين خلال هذه الجلسة حيث يتم تشغيل القاعدة الثانية في وقت سابق وتكون صالحة حتى نهاية جلسة التداول.
يجب أن نتذكر أن يتم تطبيق قاعدة survey. risk. allow_entry_in على إدخالات فقط بحيث سيكون من الممكن للدخول في التجارة باستخدام الأمر strategic. order لأن هذا الأمر ليس أمر إدخال في حد ذاته. علاوة على ذلك، عندما تكون القاعدة ستراتيجيك. risk. allow_entry_in نشطة، تصبح الإدخالات في "تجارة محظورة" مخارج بدلا من الصفقات العكسية.
مثال (مسفت 1D):
كما يتم حظر إدخالات قصيرة من قبل قواعد المخاطر، بدلا من الصفقات العكسية سيتم إجراء صفقات الخروج طويلة.
يمكن أن تعمل استراتيجيات ترادينغفيو بالعملة المختلفة عن عملة الأداة. يتم إعادة حساب نيتبروفيت و أوبنبروفيت في عملة الحساب. يتم تعيين عملة الحساب في خصائص الإستراتيجية - القائمة المنسدلة العملة الأساسية أو في البرنامج النصي باستخدام الإستراتيجية (. كيرنسي = كيرنسي. *،.) الكلمة الرئيسية. وفي الوقت نفسه، يتم حساب قيم تقرير الأداء بالعملة المحددة.
يتم احتساب الربح التجاري) مفتوح أو مغلق (على أساس الربح في عملة األدوات مضروبا في المعدل المتقاطع في نهاية يوم التداول السابق للشريط حيث يتم احتساب اإلستراتيجية.
مثال: نحن نتعامل مع اليورو مقابل الدولار الأميركي (د)، وقمنا باختيار اليورو كعملة إستراتيجية. استراتيجيتنا يشتري ويخرج من الموقف باستخدام 1 نقطة بروفيتارجيت أو ستوبلوس.
بعد إضافة هذه الاستراتيجية إلى الرسم البياني يمكننا أن نرى أن خطوط مؤامرة مطابقة. وهذا يدل على أن معدل حساب الربح لكل صفقة كان على أساس نهاية اليوم السابق.
عند التداول على القرارات خلال اليوم سيتم استخدام سعر الفائدة المتبادل عند نهاية يوم التداول السابق للشريط حيث يتم احتساب الإستراتيجية ولن يتم تغييره خلال جلسة التداول بأكملها.
عند التداول على قرارات أعلى من 1 يوم سيتم استخدام سعر الصرف في نهاية يوم التداول السابق إلى إغلاق شريط حيث يتم احتساب الاستراتيجية. لنفترض أننا نتداول على الرسم البياني الأسبوعي، ثم سيتم استخدام سعر الفائدة على جلسة يوم الخميس قريبة دائما لحساب الأرباح.
في الوقت الحقيقي يستخدم معدل إغلاق جلسة أمس.
الاستراتيجيات.
الاستراتيجية هي الدراسة التي يمكن أن ترسل وتعديل وإلغاء أوامر (لشراء / بيع). استراتيجيات تسمح لك لأداء باكتستينغ (مضاهاة استراتيجية التداول على البيانات التاريخية) والتحويل إلى الأمام (مضاهاة استراتيجية التداول على البيانات في الوقت الحقيقي) وفقا خوارزميات مسبقة الخاص بك.
استراتيجية مكتوبة بلغة الصنوبر الصنوبر لديها نفس القدرات مثل مؤشر الصنوبر. عند كتابة رمز إستراتيجية، يجب أن تبدأ بالكلمة الرئيسية "إستراتيجية"، وليس "دراسة". الاستراتيجيات ليس فقط مؤامرة شيء، ولكن أيضا مكان وتعديل وإلغاء أوامر. لديهم إمكانية الوصول إلى المعلومات الأساسية عن أداء الاستراتيجية من خلال كلمات رئيسية محددة. نفس المعلومات متاحة لك على علامة التبويب "اختبار استراتيجية". مرة واحدة يتم احتساب استراتيجية على البيانات التاريخية، يمكنك أن ترى يملأ أمر افتراضي.
مثال استراتيجي بسيط.
حالما يتم تجميع البرنامج النصي وتطبيقه على الرسم البياني، يمكنك أن ترى علامات النظام شغل على ذلك وكيف كان رصيدك يتغير خلال باكتستينغ (منحنى الأسهم). هو أبسط استراتيجية أن يشتري ويبيع في كل شريط.
استراتيجية الخط الأول ("اختبار") تنص على رمز ينتمي إلى نوع الاستراتيجية واسمها هو "اختبار". ستراتيجيك. إنتري () أمر بإرسال أوامر "شراء" و "بيع". مؤامرة (استراتيجية. المساواة) قطع منحنى الأسهم.
كيفية تطبيق استراتيجية على الرسم البياني.
لاختبار الاستراتيجية الخاصة بك، وتطبيقه على الرسم البياني. استخدم فترات الرمز والوقت التي تريد اختبارها. يمكنك استخدام استراتيجية مضمنة من "المؤشرات & أمبير؛ ستراتيديس "، أو الكتابة الخاصة بك في محرر الصنوبر.
ملاحظة: عند استخدام غير القياسية (رينكو، كاجي، خط كسر، نقطة والشكل، هيكين العشي، نشر المخططات) أنواع الرسم البياني كأساس للاستراتيجية، تحتاج إلى إدراك أن النتيجة ستكون مختلفة. سيتم تنفيذ الأوامر على أسعار هذا المخطط (على سبيل المثال ل هيكين العشي أنها سوف تأخذ أسعار هيكين العشي (المتوسط منها) ليس أسعار السوق الحقيقية). لذلك نحن نوصي بشدة لاستخدام نوع المخطط القياسية للاستراتيجيات.
باكتستينغ و فرونتستستينغ.
يتم احتساب استراتيجيات ترادينغفيو على جميع البيانات التاريخية المتاحة على الرسم البياني وتابع تلقائيا حساب عندما تأتي البيانات في الوقت الحقيقي.
خلال حساب التاريخ والحساب في الوقت الحقيقي، يتم احتساب رمز على شريط يغلق افتراضيا.
إذا كان هذا هو فروردتستينغ، رمز يحسب على كل القراد في الوقت الحقيقي. لتمكين هذا، تحقق من خيار "إعادة حساب على كل علامة" في إعدادات أو القيام بذلك في البرنامج النصي نفسه: استراتيجية (.
يمكنك تعيين استراتيجية لإجراء عملية حسابية إضافية بعد تعبئة الطلب. لهذا تحتاج إلى التحقق من "إعادة حساب بعد أمر شغل" في إعدادات أو القيام بذلك في السيناريو نفسه: استراتيجية (...، calc_on_order_fills = صحيح،.).
محاكي الوسيط.
هناك محاكي وسيط على ترادينجفيو لاستراتيجيات الاختبار. على عكس التداول الحقيقي، المضاهاة يملأ أوامر فقط في أسعار الرسم البياني، وهذا هو السبب في أمر يمكن ملؤها فقط على القراد المقبل في فرونتستستينغ وعلى شريط المقبل في باكتستينغ (أو في وقت لاحق) بعد استراتيجية محسوبة.
كما ذكر أعلاه، يتم احتساب استراتيجية باكتستينغ على إغلاق بار. يتم استخدام المنطق التالي لمحاكاة طلبات التعبئة:
إذا فتح سعر شريط أقرب إلى أعلى سعر من نفس الشريط، وسيط محاكي يفترض أن السعر إنترابار كان يتحرك بهذه الطريقة: فتح → عالية → منخفضة → إغلاق. إذا فتح سعر شريط أقرب إلى أدنى سعر لنفس شريط، محاكي وسيط يفترض أن السعر إنترابار كان يتحرك بهذه الطريقة: فتح → منخفضة → عالية → إغلاق. المحاكي وسيط يفترض أنه لم يكن هناك ثغرات داخل شريط، وهذا يعني جميع أسعار إنترابار متاحة لتنفيذ النظام. إذا تم تمكين الخيار "إعادة حساب على كل علامة" في خصائص استراتيجية (أو استراتيجية (.con_on_every_tick = ترو،.) في البرنامج النصي)، لا يزال يحسب رمز فقط على إغلاق شريط، بعد المنطق أعلاه.
هنا هي استراتيجية تبين كيف يتم شغل أوامر من قبل المحاكي وسيط:
يتم حساب هذا الرمز مرة واحدة في شريط افتراضيا، على إغلاقه، ولكن هناك حساب إضافية بمجرد تعبئة أمر. هذا هو السبب في أنك يمكن أن نرى 4 أوامر شغلها على كل شريط: 2 أوامر على فتح، 1 أمر على ارتفاع و 1 أمر على انخفاض. هذا هو باكتستينغ. إذا كان في الوقت الحقيقي، سيتم تنفيذ أوامر على كل علامة جديدة.
ومن الممكن أيضا لمحاكاة قائمة انتظار النظام. ويسمى الإعداد "التحقق من الأسعار لأوامر الحد" ويمكن العثور عليها في خصائص استراتيجية أو تعيين في البرنامج النصي نفسه: استراتيجية (. باكتست_fill_limits_assumption = X،.). القيمة المحددة = عدد النقاط / النقاط (الحد الأدنى لحركات السعر)، القيمة الافتراضية = 0. يتم تعبئة أمر الحد إذا كان السعر الحالي أفضل (أعلى لأوامر البيع، وانخفاض لأوامر الشراء) لعدد محدد من النقاط / نقطة. سعر التنفيذ لا يزال يطابق سعر أمر الحد.
backtest_fill_limits_assumption = 1. حركة السعر الدنيا = 0.25.
يتم وضع أمر حد الشراء بسعر 12.50.
السعر الحالي هو 12.50.
لا يمكن ملء النظام في السعر الحالي فقط لأن backtest_fill_limits_assumption = 1. لملء النظام يجب أن يصبح السعر 0.25 * 1 أقل. يتم وضع النظام في قائمة الانتظار.
افترض أن القراد التالي يأتي بسعر 12.00. هذا السعر هو 2 نقاط أقل، ما يعني أن الشرط "backtest_fill_limits_assumption = 1" هو راض، لذلك يجب أن يتم ملء النظام. يتم تعبئة الطلب في 12.50 (سعر النظام الأصلي)، حتى لو كان السعر غير متوفر بعد الآن.
أوامر ترتيب الطلب.
تبدأ جميع الكلمات الرئيسية المصممة للاستراتيجيات مع البادئة "إستراتيجية". يتم استخدام الأوامر التالية لوضع الأوامر: ستراتيغي. إنتري، ستراتيغي. ورد أند ستراتيجيك. إكسيت:
strategic. entry - هذا الأمر يضع أوامر الدخول فقط. يتأثر إعداد بيراميدينغ (في خصائص الإستراتيجية) ومن خلال الكلمة الرئيسية للاستراتيجية. risk. allow_entry_in. إذا كان هناك وضع السوق المفتوح عندما يتم إنشاء أمر الاتجاه المعاكس، سيتم زيادة عدد العقود / أسهم / الكثير / وحدات من عدد العقود المفتوحة حاليا (ما يعادل السكريبت: استراتيجية. بوسيتيون + سيز + كوانتيتي). ونتيجة لذلك، فإن حجم موقف السوق لفتح يكون مساويا لحجم النظام، المحدد في استراتيجية القيادة. import. order - هذا الأمر يضع أوامر الدخول والخروج. لا يتأثر إعداد بيراميدينغ و ستراتيجيك. risk. allow_entry_in الكلمة الرئيسية. انها تسمح لك لخلق معقدة الدخول والخروج بناء النظام عندما قدرات الاستراتيجية. نتري و ستراتيجيت. التوقيع ليست كافية. invest. exit - هذا الأمر يسمح لك للخروج من موقف السوق من قبل أمر أو نموذج استراتيجية متعددة أجل الخروج (وقف الخسارة، هدف الربح، وقف زائدة). كل هذه الأوامر هي جزء من نفس المجموعة استراتيجيا. لا يمكن وضع أمر خروج إذا لم يكن هناك وضع سوق مفتوح أو لا يوجد أمر دخول نشط (أمر الخروج مرتبط بمعرف أمر دخول). ليس من الممكن الخروج من موقع مع نظام السوق باستخدام الأمر. لتحقيق هذا الهدف، يجب استخدام الأوامر التالية: ستراتيغي. close أور strategic. close_all. إذا كان عدد العقود / األسهم / الوحدات / الوحدات المحددة لالستراتيجية أقل من حجم المركز المفتوح الحالي، سيكون الخروج جزئيا. لا يمكن الخروج من أمر الدخول نفسه أكثر من مرة واحدة باستخدام نفس أمر الخروج (إد)، الذي يسمح لك بإنشاء استراتيجيات الخروج بمستويات متعددة. في حالة، عندما يتم تشكيل وضع السوق من خلال أوامر دخول متعددة (تمكين بيراميدينغ)، كل أوامر الخروج ملزمة لكل أمر دخول بشكل فردي.
الاستراتيجية المذكورة أعلاه عكس موقف السوق باستمرار من +4 إلى -6، ذهابا وإيابا، ما هو مبين من مؤامرة لها.
هذه الاستراتيجية توضح الحالة، عندما يتم إغلاق موقف السوق أبدا، لأنه يستخدم أمر الخروج إلى إغلاق السوق في وضع جزئي فقط، وأنه لا يمكن استخدامها أكثر من مرة. إذا كنت مضاعفة الخط للخروج، فإن استراتيجية إغلاق موقف السوق تماما.
هذا الرمز يولد 2 مستويات من الأقواس (2 أخذ أوامر الربح و 2 أوامر وقف الخسارة). يتم تنشيط كلا المستويين في نفس الوقت: المستوى الأول للخروج من 2 العقود والثانية للخروج من بقية.
أول أوامر الربح ووقف الخسارة (المستوى 1) هي في مجموعة واحدة أوكا. أوامر أخرى (المستوى 2) في مجموعة أخرى أوكا. وهذا يعني أنه بمجرد ملء أمر من المستوى 1، لا يتم إلغاء أوامر من المستوى 2، فإنها تبقى نشطة.
كل أمر وضع أمر له معرف (قيمة سلسلة) - معرف النظام الفريد. إذا تم بالفعل وضع أمر بنفس المعرف (ولكن لم يتم شغله بعد)، فإن الأمر الحالي يعدل الأمر الحالي. إذا كان التعديل غير ممكن (التحويل من شراء للبيع)، يتم إلغاء النظام القديم، يتم وضع النظام الجديد. ستراتيجيك. إنتري و ستراتيغي. يعمل مع نفس المعرفات (يمكنهم تعديل نفس أمر الدخول). ستراتيغي. يعمل مع معرفات الطلب الأخرى (من الممكن أن يكون هناك أمر دخول وأمر الخروج مع نفس المعرف).
لإلغاء أمر معين (بواسطة معرفه) يجب استخدام الأمر الأمر. إلغاء (معرف السلسلة). لإلغاء كافة الأوامر المعلقة، يجب استخدام إستراتيجية الأمر. cancel_all (). يتم وضع أوامر استراتيجية حالما يتم استيفاء شروطهم ويسمى الأمر في التعليمات البرمجية. محاكي السمسار لا تنفذ أوامر قبل القراد المقبل يأتي بعد احتساب رمز، بينما في التداول الحقيقي مع وسيط حقيقي، أمر يمكن ملؤها عاجلا. وهو ما يعني أنه إذا تم إنشاء أمر السوق في نهاية شريط الحالي، يتم ملؤها في فتح شريط المقبل.
إذا تم تطبيق هذا الرمز على مخطط، يتم تعبئة جميع الطلبات في فتح كل شريط.
يتم تحديد شروط وضع الطلب (عند، الهرمية، ستراتيجيك. ريسك) عند حساب البرنامج النصي. إذا تم استيفاء جميع الشروط، يتم وضع النظام. إذا كان أي شرط غير راض، لم يتم وضع النظام. من المهم إلغاء أوامر السعر (الحد، وقف وإيقاف أوامر الحد).
مثال (ل مسفت 1D):
على الرغم من أن يتم تعطيل الهرمية، يتم ملء هذه الطلبات على حد سواء في باكتستينغ، لأنه عندما يتم إنشاؤها ليس هناك موقف السوق طويلة مفتوحة. يتم وضع كل من الأوامر وعندما يرضي سعر تنفيذ النظام، على حد سواء الحصول على تنفيذها. فمن المستحسن أن تضع أوامر في 1 أوكا المجموعة عن طريق استراتيجيا. في هذه الحالة يتم تعبئة أمر واحد فقط ويتم إلغاء الآخر. في ما يلي الشفرة المعدلة:
إذا، لسبب ما، أمر وضع الشروط لا يتم الوفاء بها عند تنفيذ الأمر، لن يتم وضع أمر الدخول. على سبيل المثال، إذا تم تعيين إعدادات بيراميدينغ إلى 2، يحتوي الموضع الحالي بالفعل على إدخالات اثنين وتحاول الاستراتيجية وضع ثالث، لن يتم وضعه. يتم تقييم شروط الدخول في مرحلة توليد الطلب وليس في مرحلة التنفيذ. لذلك، إذا قمت بإرسال اثنين من إدخالات نوع السعر مع تعطيل بيراميدينغ، مرة واحدة يتم تنفيذ واحد منهم الآخر لن يتم إلغاء تلقائيا. لتجنب القضايا نوصي باستخدام مجموعات أوكا-إلغاء لإدخالات حتى عندما يتم ملء أمر دخول واحد يتم إلغاء الآخرين.
وينطبق الشيء نفسه على مخارج نوع السعر - سيتم وضع الطلبات بمجرد استيفاء شروطها (أي أمر دخول يحمل المعرف المعني).
إذا قمت بتطبيق هذا المثال على مخطط، يمكنك أن ترى أن أمر الخروج قد تم ملئه على الرغم من أنه تم إنشاؤه مرة واحدة فقط قبل أن يتم وضع أمر الدخول إلى إغلاق. ومع ذلك، لم يتم إغلاق الإدخال التالي قبل نهاية الحساب كما تم تشغيل الأمر الخروج بالفعل.
إغلاق موقف السوق.
على الرغم من أنه من الممكن للخروج من إدخال معين في التعليمات البرمجية، عندما تظهر الطلبات في قائمة الصفقات على علامة التبويب ستراتيغيتيستر، فإنها كلها مرتبطة وفقا فيفو (أولا في، أولا) القاعدة. إذا لم يتم تحديد معرف أمر دخول لأمر الخروج في التعليمات البرمجية، فإن أمر الخروج يغلق أمر الدخول الأول الذي فتح موضع السوق. دعونا ندرس المثال التالي:
رمز المذكورة أعلاه يضع 2 أوامر بالتتابع: "Buy1" في سعر السوق و "Buy2" بسعر 10٪ أعلى (وقف النظام). يتم وضع أمر الخروج فقط بعد ملء أوامر الدخول. إذا قمت بتطبيق التعليمات البرمجية على مخطط، سترى أن كل أمر دخول يتم إغلاقه بواسطة أمر الخروج، على الرغم من أننا لم نحدد معرف أمر الدخول لإغلاقه في هذا السطر: ستراتيجيك. إكسيت ("براكيت"، لوس = 10، بروفيت = 10, when=strategy. position_size == 15)
It opens 5 contracts long position with the order “Buy1”. It extends the long position by purchasing 10 more contracts at 10% higher price with the order “Buy2”. The exit order (strategy. close) to sell 10 contracts (exit from “Buy2”) is filled.
If you take a look at the plot, you can see that average entry price = “Buy2” execution price and our strategy closed exactly this entry order, while on the TradeList tab we can see that it closed the first "Buy1" order and half of the second “Buy2”. It means that the no matter what entry order you specify for your strategy to close, the broker emulator will still close the the first one (according to FIFO rule). It works the same way when trading with through broker.
OCA groups.
It is possible to put orders in 2 different OCA groups in Pine Script:
strategy. oca. cancel - as soon as an order from group is filled (even partially) or cancelled, the other orders from the same group get cancelled. One should keep in mind that if order prices are the same or they are close, more than 1 order of the same group may be filled. This OCA group type is available only for entry orders because all exit orders are placed in strategy. oca. reduce .
You may think that this is a reverse strategy since pyramiding is not allowed, but in fact both order will get filled because they are market order, what means they are to be executed immediately at current price. The second order doesn’t get cancelled because both are filled almost at the same moment and the system doesn’t have time to process first order fill and cancel the second one before it gets executed. The same would happen if these were price orders with same or similar prices. Strategy places all orders (which are allowed according to market position, etc).
The strategy places all orders that do not contradict the rules (in our case market position is flat, therefore any entry order can be filled). At each tick calculation, firstly all orders with the satisfied conditions are executed and only then the orders from the group where an order was executed are cancelled.
strategy. oca. reduce - this group type allows multiple orders within the group to be filled. As one of the orders within the group starts to be filled, the size of other orders is reduced by the filled contracts amount. It is very useful for the exit strategies. Once the price touches your take-profit order and it is being filled, the stop-loss is not cancelled but its amount is reduced by the filled contracts amount, thus protecting the rest of the open position. strategy. oca. none - the order is placed outside of the group (default value for the strategy. order and strategy. entry commands).
Every group has its own unique id (the same way as the orders have). If two groups have the same id, but different type, they will be considered different groups. مثال:
“Buy” and “Sell” will be placed in different groups as their type is different. “Order” will be outside of any group as its type is set to strategy. oca. none . Moreover, “Buy” will be placed in the exit group as exits are always placed in the strategy. oca. reduce_size type group.
Risk Management.
It is not easy to create a universal profitable strategy. Usually, strategies are created for certain market patterns and can produce uncontrollable losses when applied to other data. Therefore stopping auto trading in time should things go bad is a serious issue. There is a special group of strategy commands to manage risks. They all start with the strategy. risk.* prefix.
You can combine any number of risks in any combination within one strategy. Every risk category command is calculated at every tick as well as at every order execution event regardless of the calc_on_order_fills strategy setting. There is no way to disable any risk rule in runtime from script. Regardless of where in the script the risk rule is located it will always be applied unless the line with the rule is deleted and the script is recompiled.
If on the next calculation any of the rules is triggered, no orders will be sent. Therefore if a strategy has several rules of the same type with different parameters, it will stop calculating when the rule with the most strict parameters is triggered. When a strategy is stopped all unexecuted orders are cancelled and then a market order is sent to close the position if it is not flat.
Furthermore, it is worth remembering that when using resolutions higher than 1 day, the whole bar is considered to be 1 day for the rules starting with prefix “ strategy. risk. max_intraday_ ”
The position will be closed and trading will be stopped until the end of every trading session after two orders are executed within this session as the second rule is triggered earlier and is valid until the end of the trading session.
One should remember that the strategy. risk. allow_entry_in rule is applied to entries only so it will be possible to enter in a trade using the strategy. order command as this command is not an entry command per se. Moreover, when the strategy. risk. allow_entry_in rule is active, entries in a “prohibited trade” become exits instead of reverse trades.
Example (MSFT 1D):
As short entries are prohibited by the risk rules, instead of reverse trades long exit trades will be made.
TradingView strategies can operate in the currency different from the instrument currency. NetProfit and OpenProfit are recalculated in the account currency. Account currency is set in the strategy properties - the Base Currency drop-down list or in the script via the strategy(. currency=currency.*, . ) keyword. At the same time, performance report values are calculated in the selected currency.
Trade profit (open or closed) is calculated based on the profit in the instrument currency multiplied by the cross-rate on the Close of the trading day previous to the bar where the strategy is calculated.
Example: we trade EURUSD, D and have selected EUR as the strategy currency. Our strategy buys and exits the position using 1 point profitTarget or stopLoss.
After adding this strategy to the chart we can see that the plot lines are matching. This demonstrates that the rate to calculate the profit for every trade was based on the close of the previous day.
When trading on intra-day resolutions the cross-rate on the close of the trading day previous to the bar where the strategy is calculated will be used and it will not be changed during whole trading session.
When trading on resolutions higher than 1 day the cross-rate on the close of the trading day previous to the close of the bar where the strategy is calculated will be used. Let’s say we trade on a weekly chart, then the cross rate on Thursday’s session close will always be used to calculate the profits.
In real-time the yesterday’s session close rate is used.
الاستراتيجيات.
A strategy is a study that can send, modify and cancel orders (to buy/sell). Strategies allow you to perform backtesting (emulation of strategy trading on historical data) and forwardtesting (emulation of strategy trading on real-time data) according to your precoded algorithms.
A strategy written in Pine Script language has all the same capabilities as a Pine indicator. When you write a strategy code, it should start with the keyword “ strategy ”, not “ study ”. Strategies not only plot something, but also place, modify and cancel orders. They have access to essential strategy performance information through specific keywords. The same information is available for you on the “Strategy Tester” tab. Once a strategy is calculated on historical data, you can see hypothetical order fills.
A simple strategy example.
As soon as the script is compiled and applied to a chart, you can see filled order marks on it and how your balance was changing during backtesting (equity curve). It is a simplest strategy that buys and sells on every bar.
The first line strategy(“test”) states the code belongs to strategy type and its name is “test”. strategy. entry() is a command to send “buy” and “sell” orders. plot(strategy. equity) plots the equity curve.
How to apply a strategy to the chart.
To test your strategy, apply it to the chart. Use the symbol and time intervals that you want to test. You can use a built-in strategy from the “Indicators & Stratedies” dialog box, or write your own in Pine Editor.
Note: When using non-standard (Renko, Kagi, Line Break, Point and Figure, Heikin Ashi, Spread Charts) types of chart as a basis for strategy, you need to realize that the result will be different. The orders will be executed at the prices of this chart (e. g.for Heikin Ashi it’ll take Heikin Ashi prices (the average ones) not the real market prices). Therefore we highly recommend you to use standard chart type for strategies.
Backtesting and forwardtesting.
On TradingView strategies are calculated on all available historical data on the chart and automatically continue calculation when real-time data comes in.
Both during historical and real-time calculation, code is calculated on bar closes by default.
If this is forwardtesting, code calculates on every tick in real-time. To enable this, check off the option “Recalculate On Every Tick” in settings or do it in script itself: strategy(. calc_on_every_tick=true, . ) .
You can set the strategy to perform additional calculation after an order is filled. For this you need to check off “Recalculate After Order filled” in settings or do it in script itself: strategy(…, calc_on_order_fills=true, . ) .
Broker emulator.
There is a broker emulator on TradingView for testing strategies. Unlike real trading, the emulator fills orders only at chart prices, that is why an order can be filled only on next tick in forwardtesting and on next bar in backtesting (or later) after strategy calculated.
As stated above, in backtesting strategy is calculated on bar’s close. The following logic is used to emulate order fills:
If opening price of bar is closer to highest price of the same bar, the broker emulator assumes that intrabar price was moving this way: Open → High → Low → Close. If opening price of bar is closer to lowest price of the same bar, the broker emulator assumes that intrabar price was moving this way: Open → Low → High → Close. Broker emulator assumes that there were no gaps inside bar, meaning all intrabar prices are available for order execution. If the option “Recalculate On Every Tick” in strategy properties is enabled (or strategy(. calc_on_every_tick=true, . ) is specified in script), code is still calculated only on bar’s close, following the above logic.
Here is the strategy demonstrating how orders are filled by the broker emulator:
This code is calculated once per bar by default, on its close, however there is an additional calculation as soon as an order is filled. That is why you can see 4 filled orders on every bar: 2 orders on open, 1 order on high and 1 order on low. This is backtesting. If it were in real-time, orders would be executed on every new tick.
It is also possible to emulate order queue. The setting is called “Verify Price For Limit Orders” and can be found in strategy properties or set in script itself: strategy(. backtest_fill_limits_assumption=X, . ) . The specified value = number of points/pips (minimum price movements), default value = 0. A limit order is filled if current price is better (higher for sell orders, lower for buy orders) for the specified number of points/pips. The execution price still matches the limit order price.
backtest_fill_limits_assumption = 1. Minimum price movement = 0.25.
A buy limit order is placed at price 12.50.
Current price is 12.50.
The order cannot be filled at current price only because backtest_fill_limits_assumption = 1. To fill the order the price must become 0.25*1 lower. The order is put in queue.
Assume that the next tick comes at price 12.00. This price is 2 points lower, what means the condition “backtest_fill_limits_assumption = 1” is satisfied, so the order should be filled. The order is filled at 12.50 (original order price), even if the price is not available anymore.
Order placement commands.
All keywords that are designed for strategies start with “ strategy .” prefix. The following commands are used for placing orders: strategy. entry , strategy. order and strategy. exit :
strategy. entry — this command places only entry orders. It is affected by pyramiding setting (in strategy properties) and by strategy. risk. allow_entry_in keyword. If there is an open market position when an opposite direction order is generated, the number of contracts/shares/lots/units will be increased by the number of currently open contracts (script equivalent: strategy. position_size + quantity ). As the result, the size of market position to open will be equal to order size, specified in the command strategy. entry . strategy. order — this command places both entry and exit orders. It is not affected by pyramiding setting and by strategy. risk. allow_entry_in keyword. It allows you to create complex enter and exit order constructions when capabilities of the strategy. entry and strategy. exit are not enough. strategy. exit — this command allows you to exit a market position by an order or or form multiple exit order strategy (stop loss, profit target, trailing stop). All such orders are part of the same strategy. oca. reduce group. An exit order cannot be placed if there is no open market position or there is no active entry order (an exit order is bound to ID of an entry order). It is not possible to exit a position with a market order using the command strategy. exit . For this goal the following commands should be used: strategy. close or strategy. close_all . If number of contracts/shares/lots/units specified for the strategy. exit is less than the size of current open position, the exit will be partial. It is not possible to exit from the same entry order more than 1 time using the same exit order (ID), that allows you to create exit strategies with multiple levels. In case, when a market position was formed by multiple entry orders (pyramiding enabled), each exit orders is bound to each entry order individually.
The above strategy constantly reverses market position from +4 to -6, back and forth, what is shown by its plot.
This strategy demonstrates the case, when market position is never closed, because it uses exit order to close market position only partially and it cannot be used more than once. If you double the line for exiting, the strategy will close market position completely.
This code generates 2 levels of brackets (2 take profit orders and 2 stop loss orders). Both levels are activated at the same time: first level to exit 2 contracts and the second one to exit all the rest.
The first take profit and stop loss orders (level 1) are in one OCA group. The other orders (level 2) are in another OCA group. It means that as soon as an order from level 1 is filled, the orders from level 2 are not cancelled, they stay active.
Every command placing an order has ID (string value) — unique order identifier. If an order with same ID is already placed (but not yet filled), current command modifies the existing order. If modification is not possible (conversion from buy to sell), the old order is cancelled, the new order is placed. strategy. entry and strategy. order work with the same IDs (they can modify the same entry order). strategy. exit works with other order IDs (it is possible to have an entry order and an exit order with the same ID).
To cancel a specific order (by its ID) the command strategy. cancel(string id) should be used. To cancel all pending orders the command strategy. cancel_all() should be used. Strategy orders are placed as soon as their conditions are satisfied and command is called in code. Broker emulator doesn’t execute orders before next tick comes after the code was calculated, while in real trading with real broker, an order can be filled sooner. It means that if a market order is generated at close of current bar, it is filled at open oif next bar.
If this code is applied to a chart, all orders are filled at open of every bar.
Conditions for order placement ( when , pyramiding , strategy. risk ) are checked when script is calculated. If all conditions are satisfied, the order is placed. If any condition is not satisfied, the order is not placed. It is important to cancel price orders (limit, stop and stop-limit orders).
Example (for MSFT 1D):
Even though pyramiding is disabled, these both orders are filled in backtesting, because when they are generated there is no open long market position. Both orders are placed and when price satisfies order execution, they both get executed. It is recommended to to put the orders in 1 OCA group by means of strategy. oca. cancel . in this case only one order is filled and the other one is cancelled. Here is the modified code:
If, for some reason, order placing conditions are not met when executing the command, the entry order will not be placed. For example, if pyramiding settings are set to 2, existing position already contains two entries and the strategy tries to place a third one, it will not be placed. Entry conditions are evaluated at the order generation stage and not at the execution stage. Therefore, if you submit two price type entries with pyramiding disabled, once one of them is executed the other will not be cancelled automatically. To avoid issues we recommend using OCA-Cancel groups for entries so when one entry order is filled the others are cancelled.
The same is true for price type exits - orders will be placed once their conditions are met (i. e. an entry order with the respective id is filled).
If you apply this example to a chart, you can see that the exit order has been filled despite the fact that it had been generated only once before the entry order to be closed was placed. However, the next entry was not closed before the end of the calculation as the exit command has already been triggered.
Closing market position.
Despite it is possible to exit from a specific entry in code, when orders are shown in the List of Trades on StrategyTester tab, they all are linked according FIFO (first in, first out) rule. If an entry order ID is not specified for an exit order in code, the exit order closes the first entry order that opened market position. Let’s study the following example:
The code given above places 2 orders sequentially: “Buy1” at market price and “Buy2” at 10% higher price (stop order). Exit order is placed only after entry orders have been filled. If you apply the code to a chart, you will see that each entry order is closed by exit order, though we did not specify entry order ID to close in this line: strategy. exit("bracket", loss=10, profit=10, when=strategy. position_size == 15)
It opens 5 contracts long position with the order “Buy1”. It extends the long position by purchasing 10 more contracts at 10% higher price with the order “Buy2”. The exit order (strategy. close) to sell 10 contracts (exit from “Buy2”) is filled.
If you take a look at the plot, you can see that average entry price = “Buy2” execution price and our strategy closed exactly this entry order, while on the TradeList tab we can see that it closed the first "Buy1" order and half of the second “Buy2”. It means that the no matter what entry order you specify for your strategy to close, the broker emulator will still close the the first one (according to FIFO rule). It works the same way when trading with through broker.
OCA groups.
It is possible to put orders in 2 different OCA groups in Pine Script:
strategy. oca. cancel - as soon as an order from group is filled (even partially) or cancelled, the other orders from the same group get cancelled. One should keep in mind that if order prices are the same or they are close, more than 1 order of the same group may be filled. This OCA group type is available only for entry orders because all exit orders are placed in strategy. oca. reduce .
You may think that this is a reverse strategy since pyramiding is not allowed, but in fact both order will get filled because they are market order, what means they are to be executed immediately at current price. The second order doesn’t get cancelled because both are filled almost at the same moment and the system doesn’t have time to process first order fill and cancel the second one before it gets executed. The same would happen if these were price orders with same or similar prices. Strategy places all orders (which are allowed according to market position, etc).
The strategy places all orders that do not contradict the rules (in our case market position is flat, therefore any entry order can be filled). At each tick calculation, firstly all orders with the satisfied conditions are executed and only then the orders from the group where an order was executed are cancelled.
strategy. oca. reduce - this group type allows multiple orders within the group to be filled. As one of the orders within the group starts to be filled, the size of other orders is reduced by the filled contracts amount. It is very useful for the exit strategies. Once the price touches your take-profit order and it is being filled, the stop-loss is not cancelled but its amount is reduced by the filled contracts amount, thus protecting the rest of the open position. strategy. oca. none - the order is placed outside of the group (default value for the strategy. order and strategy. entry commands).
Every group has its own unique id (the same way as the orders have). If two groups have the same id, but different type, they will be considered different groups. مثال:
“Buy” and “Sell” will be placed in different groups as their type is different. “Order” will be outside of any group as its type is set to strategy. oca. none . Moreover, “Buy” will be placed in the exit group as exits are always placed in the strategy. oca. reduce_size type group.
Risk Management.
It is not easy to create a universal profitable strategy. Usually, strategies are created for certain market patterns and can produce uncontrollable losses when applied to other data. Therefore stopping auto trading in time should things go bad is a serious issue. There is a special group of strategy commands to manage risks. They all start with the strategy. risk.* prefix.
You can combine any number of risks in any combination within one strategy. Every risk category command is calculated at every tick as well as at every order execution event regardless of the calc_on_order_fills strategy setting. There is no way to disable any risk rule in runtime from script. Regardless of where in the script the risk rule is located it will always be applied unless the line with the rule is deleted and the script is recompiled.
If on the next calculation any of the rules is triggered, no orders will be sent. Therefore if a strategy has several rules of the same type with different parameters, it will stop calculating when the rule with the most strict parameters is triggered. When a strategy is stopped all unexecuted orders are cancelled and then a market order is sent to close the position if it is not flat.
Furthermore, it is worth remembering that when using resolutions higher than 1 day, the whole bar is considered to be 1 day for the rules starting with prefix “ strategy. risk. max_intraday_ ”
The position will be closed and trading will be stopped until the end of every trading session after two orders are executed within this session as the second rule is triggered earlier and is valid until the end of the trading session.
One should remember that the strategy. risk. allow_entry_in rule is applied to entries only so it will be possible to enter in a trade using the strategy. order command as this command is not an entry command per se. Moreover, when the strategy. risk. allow_entry_in rule is active, entries in a “prohibited trade” become exits instead of reverse trades.
Example (MSFT 1D):
As short entries are prohibited by the risk rules, instead of reverse trades long exit trades will be made.
TradingView strategies can operate in the currency different from the instrument currency. NetProfit and OpenProfit are recalculated in the account currency. Account currency is set in the strategy properties - the Base Currency drop-down list or in the script via the strategy(. currency=currency.*, . ) keyword. At the same time, performance report values are calculated in the selected currency.
Trade profit (open or closed) is calculated based on the profit in the instrument currency multiplied by the cross-rate on the Close of the trading day previous to the bar where the strategy is calculated.
Example: we trade EURUSD, D and have selected EUR as the strategy currency. Our strategy buys and exits the position using 1 point profitTarget or stopLoss.
After adding this strategy to the chart we can see that the plot lines are matching. This demonstrates that the rate to calculate the profit for every trade was based on the close of the previous day.
When trading on intra-day resolutions the cross-rate on the close of the trading day previous to the bar where the strategy is calculated will be used and it will not be changed during whole trading session.
When trading on resolutions higher than 1 day the cross-rate on the close of the trading day previous to the close of the bar where the strategy is calculated will be used. Let’s say we trade on a weekly chart, then the cross rate on Thursday’s session close will always be used to calculate the profits.
In real-time the yesterday’s session close rate is used.
No comments:
Post a Comment