
ओपनिंग रेंज ब्रेकआउट एटीआर ट्रैकिंग स्टॉप लॉस रणनीति एक मात्रात्मक ट्रेडिंग प्रणाली है जो ओपनिंग रेंज ब्रेकआउट और स्मार्ट मार्केट एनालिसिस स्मार्ट मनी कॉन्सेप्ट्स को जोड़ती है। यह रणनीति अमेरिकी शेयर बाजार के खुलने के 5 मिनट बाद 09:30-09:35 ईएसटी) में बनने वाले मूल्य क्षेत्र के ब्रेकआउट अवसरों को पकड़ने पर केंद्रित है, और ट्रेडिंग सिग्नल की गुणवत्ता सुनिश्चित करने के लिए कई फ़िल्टरिंग शर्तों के साथ संयुक्त है। सिस्टम को तुरंत या वापस बुलाया जाता है, जोखिम-वापसी गतिशील समायोजन तंत्र का उपयोग करता है, और लाभप्रदता प्रबंधन को अनुकूलित करने के लिए एटीआर ट्रैक करने के लिए एटीआर ट्रैक करने का विकल्प है। रणनीति में “दूसरा मौका” ट्रेडिंग सुविधा भी है, जो शुरुआती ट्रेडिंग विफल होने के बाद रिवर्स ब्रेकआउट अवसरों को पकड़ने की अनुमति देती है, जबकि एक व्यापक दृश्यता सुविधा प्रदान करती है ताकि व्यापारियों को बाजार की गतिशीलता को बेहतर ढंग से समझने में मदद मिल सके।
ओपनिंग रेंज को तोड़ने के लिए एटीआर ट्रैकिंग स्टॉप-लॉस रणनीति का मुख्य तर्क बाजार के खुलने के बाद प्रारंभिक मूल्य सीमा के महत्व पर आधारित है। यह रणनीति पहले एक विशिष्ट समय विंडो (09:30-09:35 ईएसटी) में कीमतों के उच्चतम और निम्नतम बिंदुओं को पकड़ती है और रिकॉर्ड करती है, जिससे “ओपनिंग रेंज” (ओपनिंग रेंज) का गठन होता है। इसके बाद, सिस्टम इस क्षेत्र में मूल्य के उल्लंघन की निगरानी करता है, निम्नलिखित महत्वपूर्ण तंत्रों के साथ व्यापार की गुणवत्ता सुनिश्चित करने के लिएः
खुले क्षेत्र की पहचान और सफलता सत्यापन: सिस्टम एक निर्दिष्ट समय विंडो के भीतर कीमतों के उच्चतम और निम्नतम बिंदुओं को रिकॉर्ड करता है, और बाद में ब्रीच की निगरानी करता है। ब्रीच को दोहरे फ़िल्टरिंग तंत्र द्वारा सत्यापित किया जाना चाहिएः
प्रवेश प्रणालीइस योजना के तहत दो तरह के प्रवेशों का समर्थन किया जाता है:
रोक नुकसान सेटिंगयह दो प्रकार के स्टॉप लॉस प्रदान करता हैः
जोखिम प्रबंधनजोखिमः रिवार्ड मल्टीप्लायर सिस्टम स्वचालित रूप से स्टॉप पोजीशन की गणना करता है, गतिशील जोखिम प्रबंधन को लागू करता है। उदाहरण के लिए, 2:1 का जोखिम-लाभ अनुपात सेट करने का मतलब है कि संभावित लाभ संभावित नुकसान से दोगुना है।
एटीआर ट्रैक रोकएक बार जब मुनाफा पूर्वनिर्धारित जोखिम-प्रतिफल अनुपात तक पहुंच जाता है, तो सिस्टम एटीआर-आधारित ट्रैकिंग स्टॉप-लॉस को सक्रिय कर सकता है, जबकि कुछ मुनाफे को लॉक कर सकता है और रुझान को जारी रखने की अनुमति दे सकता है।
दूसरा मौका व्यापारजब ट्रेडों की शुरुआत में स्टॉपलॉस या विफलता होती है, तो सिस्टम स्वचालित रूप से ट्रेडों के उद्घाटन के दौरान रिवर्स ब्रेकिंग अवसरों की तलाश करता है, जिससे उसी दिन द्विपक्षीय ट्रेडों की संभावना होती है।
उच्च गुणवत्ता वाले सौदों पर ध्यान केंद्रित करें: बहु-सत्यापन तंत्र के माध्यम से ((शैडो लाइन फ़िल्टर, दूरी फ़िल्टर), रणनीति ने नकली तोड़-फोड़ लेनदेन को काफी कम कर दिया है, जीत की दर में वृद्धि की है।
लचीला प्रवेश तंत्र: तत्काल या रिवर्स प्रविष्टि का समर्थन करें, विभिन्न ट्रेडिंग शैलियों और बाजार स्थितियों के लिए अनुकूल। तत्काल प्रविष्टि मजबूत रुझानों के लिए उपयुक्त है, जबकि रिवर्स प्रविष्टि बेहतर प्रविष्टि मूल्य प्राप्त कर सकती है।
जोखिम प्रबंधन के लिए अनुकूलनगतिशील स्टॉप सेटिंग्स जो रिटर्न पर आधारित हैं, यह सुनिश्चित करते हैं कि प्रत्येक ट्रेड में एक समान जोखिम विशेषता है, जिससे धन का मानकीकृत प्रबंधन होता है।
अधिकतम लाभएटीआर का ट्रैक स्टॉप लॉस फ़ंक्शन मजबूत ट्रेडों को बनाए रखने की अनुमति देता है, जबकि पहले से हासिल किए गए मुनाफे की रक्षा करता है, जिससे समय से पहले बाहर निकलने से बचा जाता है।
उच्च दृश्यतासिस्टम एक व्यापक दृश्य सहायता सुविधा प्रदान करता है, जिसमें अंतर चिह्न, ब्रेक सत्यापन टैग, व्यापार स्थिति संकेत, प्रवेश / हानि / स्टॉप मार्कर आदि शामिल हैं, जो व्यापार निर्णयों की सहजता को बढ़ाता है।
बिना पूर्वाग्रह के डिजाइनरणनीतियों का व्यापक रूप से उपयोगbarstate.isconfirmedयह सुनिश्चित करना कि सभी निर्णय सत्यापित मूल्य आंकड़ों पर आधारित हों, पूर्वानुमान विचलन से बचें और वास्तविक लेनदेन की स्थिति के अनुरूप हों।
दूसरा मौका: द्वितीयक अवसर ट्रेडिंग सुविधा को सक्षम करके, रणनीति बाजार में बदलाव को जल्दी से अनुकूलित कर सकती है जब प्रारंभिक दिशा में गलत निर्णय लिया जाता है, उलट अवसरों को पकड़ती है, और धन के उपयोग की दक्षता में सुधार करती है।
सत्र प्रबंधन अनुकूलित करें: एक अंतर्निहित सत्र समाप्त करने वाली ऑटो-प्लिसिंग सुविधा जो रात भर के जोखिम को कम करती है।
अवधि के बीच के जोखिम9: 09:30-09:35), बाजार में असामान्य उतार-चढ़ाव हो सकता है, जिसके कारण यह क्षेत्र बहुत चौड़ा या बहुत संकीर्ण हो सकता है। बहुत चौड़ा क्षेत्र बहुत बड़ी रोक को जन्म दे सकता है, और बहुत संकीर्ण क्षेत्र अक्सर झूठे ब्रेक को ट्रिगर कर सकता है। समाधान: एक फ़िल्टर को बढ़ाने पर विचार किया जा सकता है जो खुले क्षेत्रों के आकार को बढ़ाता है, असामान्य क्षेत्रों को बाहर करता है; या ट्रेडिंग डेट फ़िल्टर को समायोजित करने के लिए, उच्च अस्थिरता वाले विशिष्ट दिनों से बचने के लिए (जैसे कि महत्वपूर्ण आर्थिक आंकड़ों के प्रकाशन के दिन) ।
इस तरह की घटनाओं के बाद से, यह खतरा बढ़ गया है।: प्रभावी ब्रेकआउट के बाद बाजार में तेज वापसी हो सकती है, जिससे स्टॉप लॉस ट्रिगर होने के बाद बाजार फिर से अपनी मूल दिशा में आगे बढ़ सकता है। समाधान: अधिक आराम से स्टॉप-लॉस सेटिंग्स का उपयोग करने पर विचार करें, जैसे कि समानांतर अंतराल स्टॉप; या बेहतर प्रवेश मूल्य और कम जोखिम के लिए प्रवेश को वापस लेने के लिए प्रवेश तंत्र को समायोजित करें।
सिग्नल की गुणवत्ता फ़िल्टर सेटिंग पर निर्भर करती है: पारदर्शी सत्यापित छायांकन फ़िल्टर और दूरी फ़िल्टर पैरामीटर सेटिंग्स सिग्नल गुणवत्ता पर महत्वपूर्ण प्रभाव डालते हैं, गलत पैरामीटर अच्छे व्यापारिक अवसरों को फ़िल्टर कर सकते हैं या बहुत कम गुणवत्ता वाले सिग्नल प्राप्त कर सकते हैं। समाधान: फिल्टर मापदंडों को अनुकूलित करने के लिए, इतिहास के माध्यम से, एक विशिष्ट बाजार और किस्म के लिए इष्टतम सेटिंग का पता लगाएं; बाजार की अस्थिरता की गतिशीलता के अनुसार फिल्टर मानकों को समायोजित करने के लिए अनुकूलन मापदंडों का उपयोग करने पर विचार करें।
स्टॉप लॉस पैरामीटर संवेदनशीलता को ट्रैक करेंएटीआर ट्रैक स्टॉप लॉस के लिए पैरामीटर को बहुत तंग सेट करने से छोटे रिड्यूस में जल्दी से बाहर निकलने का खतरा हो सकता है, जबकि इसे बहुत अधिक सेट करने से बहुत अधिक रिड्यूस हो सकता है। समाधानएटीआर चक्र और गुणांक को लक्षित किस्मों के आधार पर ऐतिहासिक उतार-चढ़ाव की विशेषताओं के आधार पर समायोजित करें; बैच-बाहर स्टॉक रणनीति को लागू करने पर विचार करें, कुछ पदों के लिए फिक्स्ड स्टॉप और कुछ पदों के लिए ट्रैकिंग स्टॉप का उपयोग करें।
लेनदेन की आवृत्तिरणनीतिः एक दिन में अधिकतम दो ट्रेडों को निष्पादित करें (प्रारंभिक ट्रेडों और दूसरे अवसर ट्रेडों के साथ) और आप दिन के सभी अवसरों का पूरा उपयोग नहीं कर सकते हैं। समाधान: विस्तार रणनीति पर विचार करें, दिन के अन्य समय के महत्वपूर्ण मूल्य क्षेत्रों की निगरानी करें; या अन्य तकनीकी संकेतकों के साथ मिलकर एक समग्र रणनीति बनाएं, व्यापार संकेत स्रोतों को बढ़ाएं।
खुले अंतराल चक्र के लिए अनुकूलितवर्तमान में रणनीति में 5 मिनट की एक निश्चित खुली अवधि का उपयोग किया जाता है, लेकिन बाजार की अस्थिरता के आधार पर इस अवधि को समायोजित करने पर विचार किया जा सकता है। कम अस्थिरता वाले बाजारों में अंतराल समय को 3 मिनट तक छोटा किया जा सकता है, जबकि उच्च अस्थिरता वाले बाजारों में इसे 10 मिनट तक बढ़ाया जा सकता है, जो विभिन्न बाजार स्थितियों के अनुकूल है।
संयुग्मित लेन-देन की पुष्टि: ब्रीच प्रमाणीकरण तंत्र में लेनदेन फ़िल्टरिंग शर्तों को जोड़ें, ब्रीचिंग के समय लेनदेन की मात्रा को पिछले कुछ चक्रों की औसत लेनदेन से काफी अधिक करने की आवश्यकता है, जिससे ब्रीचिंग की प्रभावशीलता बढ़ जाती है। यह ब्रीचिंग लेनदेन की मात्रा और पिछले एन चक्रों के लेनदेन की औसत मूल्य के अनुपात की गणना करके किया जा सकता है।
बहु-समय-सीमा विश्लेषण: उच्चतर समय फ़्रेम के लिए प्रवृत्ति दिशा फ़िल्टर का परिचय दें, केवल डेली या हॉर्स लाइन प्रवृत्ति दिशा के साथ एक साथ प्रवेश करें, ट्रेड जीतने की दर में सुधार करें। उच्चतर समय फ़्रेम प्रवृत्ति को सरल चलती औसत ढलान या अधिक उन्नत प्रवृत्ति संकेतक द्वारा निर्धारित किया जा सकता है।
धन प्रबंधन का अनुकूलन: एक गतिशील पोजीशन स्केल समायोजन तंत्र को लागू करना, जो ऐतिहासिक अस्थिरता, वर्तमान खाता आकार और हाल के प्रदर्शन के आधार पर अनुबंधों की संख्या को स्वचालित रूप से समायोजित करता है, और अधिक परिष्कृत जोखिम नियंत्रण प्राप्त करता है। उदाहरण के लिए, लगातार मुनाफे के बाद स्थिति में वृद्धि और लगातार नुकसान के बाद स्थिति में कमी।
एकीकृत मशीन लर्निंग मॉडल: मशीन लर्निंग मॉडल को प्रवेश करने के लिए ब्रेकडाउन की गुणवत्ता का आकलन करें, ऐतिहासिक डेटा प्रशिक्षण मॉडल के माध्यम से सबसे सफल सफल ब्रेकडाउन पैटर्न की पहचान करें। लक्षणों में शामिल हो सकते हैं ओपन स्पेस का आकार, बाजार की अस्थिरता, पिछले ट्रेडिंग दिन की कीमतों की गति, विशिष्ट समय पैटर्न, आदि।
दूसरे अवसर के व्यापारिक तर्क को मजबूत करना: दूसरे अवसर के व्यापार के लिए ट्रिगर की शर्तों का अनुकूलन, न केवल प्रारंभिक व्यापार की विफलता के आधार पर, बल्कि बाजार संरचना में बदलाव और नए गतिशीलता संकेतकों को ध्यान में रखते हुए, दूसरे अवसर के व्यापार की सफलता की दर में सुधार करना।
व्यक्तिगत नस्ल पैरामीटर: विभिन्न व्यापारिक किस्मों के लिए अनुकूलित पैरामीटर सेट विकसित करें, प्रत्येक किस्म की अद्वितीय अस्थिरता और मूल्य व्यवहार को ध्यान में रखते हुए। उदाहरण के लिए, अधिक अस्थिरता वाली किस्मों को अधिक आराम से फ़िल्टर सेटिंग और अधिक रूढ़िवादी जोखिम-लाभ अनुपात की आवश्यकता हो सकती है।
बाजार की भावना के सूचकांक को एकीकृत करना: VIX सूचकांक या अन्य बाजार भावना सूचकांकों को पेश करना, चरम बाजार भावना के दौरान रणनीति पैरामीटर को समायोजित करना या उच्च अनिश्चितता वाले वातावरण से बचने के लिए व्यापार को अस्थायी रूप से बंद करना।
ओपनिंग ब्रीज ATR ट्रैकिंग स्टॉप लॉस रणनीति एक अच्छी तरह से संरचित मात्रात्मक ट्रेडिंग प्रणाली है, जो ओपनिंग ब्रीज, स्मार्ट फ़िल्टरिंग तंत्र, लचीले प्रवेश विकल्प और उन्नत जोखिम प्रबंधन सुविधाओं को जोड़ती है। यह रणनीति विशेष रूप से अमेरिकी शेयर और वायदा बाजारों में दिन के कारोबार के लिए उपयुक्त है, जो ओपनिंग के बाद दिशात्मक ब्रेक को पकड़कर लाभप्रदता प्राप्त करती है।
रणनीति का मुख्य मूल्य इसकी बहु-स्तरीय सत्यापन तंत्र और जोखिम प्रबंधन प्रणाली में है, जो छाया रेखा और दूरी फिल्टर के माध्यम से झूठे ब्रेकआउट ट्रेडों को काफी कम करता है, जबकि जोखिम रिटर्न अनुपात गुणांक और एटीआर ट्रैकिंग स्टॉपलॉस का उपयोग करके एक समान जोखिम जोखिम और मुनाफे की सुरक्षा सुनिश्चित करता है। दूसरा मौका ट्रेडिंग सुविधा रणनीति को अनुकूलन और अतिरिक्त मुनाफे के अवसर प्रदान करती है।
इस रणनीति के कई फायदे होने के बावजूद, उपयोगकर्ताओं को पैरामीटर के अनुकूलन के महत्व पर ध्यान देना चाहिए, विभिन्न बाजारों और किस्मों को अनुकूलित करने की आवश्यकता हो सकती है ताकि सर्वोत्तम प्रभाव प्राप्त किया जा सके। साथ ही, यह अनुशंसा की जाती है कि व्यापारी इस रणनीति को एक पूर्ण व्यापार प्रणाली के हिस्से के रूप में उपयोग करें, जो कि व्यापक बाजार विश्लेषण और जोखिम प्रबंधन सिद्धांतों के साथ संयुक्त है।
अनुशंसित दिशाओं के अनुकूलन के कार्यान्वयन के माध्यम से, विशेष रूप से अनुकूली पैरामीटर, बहु-समय सीमा विश्लेषण और उन्नत धन प्रबंधन प्रणाली, रणनीति में पेशेवर व्यापारियों के टूलकिट में एक शक्तिशाली उपकरण बनने के लिए अपनी स्थिरता और लाभप्रदता को और बढ़ाने की क्षमता है।
/*backtest
start: 2025-07-18 00:00:00
end: 2025-07-30 00:00:00
period: 30m
basePeriod: 30m
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
*/
//@version=5
strategy("Casper SMC 5min ORB - Roboquant AI", overlay=true, default_qty_type=strategy.fixed, default_qty_value=1, max_bars_back=500, calc_on_order_fills=true, calc_on_every_tick=false, initial_capital=50000, currency=currency.USD)
// === STRATEGY SETTINGS ===
// Risk Management
contracts = input.int(1, "Contracts", minval=1, group="Risk Management")
risk_multiplier = input.float(2.0, "Risk:Reward Multiplier", minval=0.5, maxval=10.0, group="Risk Management")
sl_points = input.int(2, "Stop Loss Points Below/Above Breakout Candle", minval=1, group="Risk Management")
// Entry Settings
entry_type = input.string("Instant", "Entry Type", options=["Retracement", "Instant"], group="Entry Settings")
retracement_percent = input.float(50.0, "Retracement % of Breakout Candle Body", minval=10.0, maxval=90.0, group="Entry Settings")
// Stop Loss Settings
sl_type = input.string("Opposite Range", "Stop Loss Type", options=["Breakout Candle", "Opposite Range"], group="Stop Loss Settings")
// Second Chance Trade Settings
enable_second_chance = input.bool(false, "Enable Second Chance Trade", group="Second Chance Trade")
second_chance_info = input.string("If initial SL is hit, allow opposite breakout trade", "Info: Second Chance Logic", group="Second Chance Trade")
// Breakout Filter Settings
use_wick_filter = input.bool(false, "Use Wick Filter", group="Breakout Filter")
max_wick_percent = input.float(50.0, "Max Wick % of Candle Body", minval=10.0, maxval=200.0, group="Breakout Filter")
// Breakout Distance Filters
use_breakout_distance_filter = input.bool(true, "Use Breakout Distance Filter", group="Breakout Distance Filter")
min_breakout_multiplier = input.float(0.1, "Min Breakout Distance (OR Size * X)", minval=0.0, maxval=3.0, group="Breakout Distance Filter")
max_breakout_multiplier = input.float(1.6, "Max Breakout Distance (OR Size * X)", minval=0.5, maxval=5.0, group="Breakout Distance Filter")
// Trailing Stop Loss Settings
use_trailing_sl = input.bool(false, "Use Trailing Stop Loss", group="Trailing Stop Loss")
profit_r_multiplier = input.float(1.0, "Start Trailing After X R Profit", minval=0.5, maxval=5.0, group="Trailing Stop Loss")
atr_length = input.int(14, "ATR Length", minval=1, maxval=50, group="Trailing Stop Loss")
atr_multiplier = input.float(1.0, "ATR Multiplier for Trailing", minval=0.5, maxval=5.0, group="Trailing Stop Loss")
// Session Management
or_start_hour = input.int(9, "Opening Range Start Hour", minval=0, maxval=23, group="Session Management")
or_start_minute = input.int(30, "Opening Range Start Minute", minval=0, maxval=59, group="Session Management")
or_end_minute = input.int(35, "Opening Range End Minute", minval=0, maxval=59, group="Session Management")
session_timezone = input.string("America/New_York", "Session Timezone", group="Session Management")
force_session_close = input.bool(true, "Force Close at Session End", group="Session Management")
session_end_hour = input.int(16, "Session End Hour", minval=0, maxval=23, group="Session Management")
session_end_minute = input.int(0, "Session End Minute", minval=0, maxval=59, group="Session Management")
// Day of Week Trading Filters
trade_monday = input.bool(true, "Trade on Monday", group="Day of Week Filters")
trade_tuesday = input.bool(true, "Trade on Tuesday", group="Day of Week Filters")
trade_wednesday = input.bool(true, "Trade on Wednesday", group="Day of Week Filters")
trade_thursday = input.bool(true, "Trade on Thursday", group="Day of Week Filters")
trade_friday = input.bool(true, "Trade on Friday", group="Day of Week Filters")
// Visual Settings
high_line_color = input.color(color.green, title="Opening Range High Line Color", group="Visual Settings")
low_line_color = input.color(color.red, title="Opening Range Low Line Color", group="Visual Settings")
// Label Control Settings
show_trading_disabled_labels = input.bool(false, "Show Trading Disabled Labels", group="Label Controls")
show_breakout_validation_labels = input.bool(true, "Show Breakout Validation Labels", group="Label Controls")
show_second_chance_labels = input.bool(false, "Show Second Chance Labels", group="Label Controls")
show_trade_status_labels = input.bool(false, "Show Trade Status Labels", group="Label Controls")
show_entry_labels = input.bool(false, "Show Entry Labels", group="Label Controls")
show_sl_tp_labels = input.bool(false, "Show Stop Loss / Take Profit Labels", group="Label Controls")
// === VARIABLES ===
// ATR for trailing stop loss
atr = ta.atr(atr_length)
// === NYSE OPENING RANGE LOGIC ===
// FIXED: Using configurable hour/minute inputs with timezone
current_time = time(timeframe.period, "0000-2400:23456", session_timezone)
current_hour = hour(current_time, session_timezone)
current_minute = minute(current_time, session_timezone)
is_opening_range = current_hour == or_start_hour and current_minute >= or_start_minute and current_minute <= or_end_minute
// Check if we're at the start of a new trading day - FIXED: More reliable detection
is_new_day = ta.change(time("1D"))
// ADDED: Check if trading is allowed on current day of week (using session timezone)
current_day = dayofweek(current_time, session_timezone)
is_trading_day_allowed = (current_day == dayofweek.monday and trade_monday) or (current_day == dayofweek.tuesday and trade_tuesday) or (current_day == dayofweek.wednesday and trade_wednesday) or (current_day == dayofweek.thursday and trade_thursday) or (current_day == dayofweek.friday and trade_friday)
// Variables to store opening range high and low for current day
var float or_high = na
var float or_low = na
var bool lines_drawn = false
var bool breakout_occurred = false
var float breakout_candle_high = na
var float breakout_candle_low = na
var float breakout_price = na
var string breakout_direction = na
var int or_start_bar = na // ADDED: Store the bar index when opening range starts
// ADDED: Second chance trade variables
var bool first_trade_sl_hit = false
var string first_trade_direction = na
var bool second_chance_available = false
var bool second_trade_taken = false
var bool daily_trades_complete = false // ADDED: Prevent more than 2 trades per day
// Reset variables at the start of each trading day
if is_new_day
or_high := na
or_low := na
lines_drawn := false
breakout_occurred := false
breakout_candle_high := na
breakout_candle_low := na
breakout_price := na
breakout_direction := na
or_start_bar := na // ADDED: Reset opening range start bar
// ADDED: Reset second chance variables
first_trade_sl_hit := false
first_trade_direction := na
second_chance_available := false
second_trade_taken := false
daily_trades_complete := false // ADDED: Reset trade limit
// Capture opening range data during 09:30-09:35 EST
if is_opening_range
if na(or_high) or na(or_low)
or_high := high
or_low := low
or_start_bar := bar_index // ADDED: Store the bar index when opening range starts
else
or_high := math.max(or_high, high)
or_low := math.min(or_low, low)
// Draw lines when we're past the opening range and haven't drawn yet
if not is_opening_range and not na(or_high) and not na(or_low) and not na(or_start_bar) and not lines_drawn
// FIXED: Lines start from the actual opening range start time and extend forward
start_x = or_start_bar
end_x = bar_index + 50 // Extend lines forward for visibility
lines_drawn := true
// ADDED: Show visual indicator if trading is disabled for current day
if not is_trading_day_allowed and show_trading_disabled_labels
day_name = current_day == dayofweek.monday ? "Monday" :
current_day == dayofweek.tuesday ? "Tuesday" :
current_day == dayofweek.wednesday ? "Wednesday" :
current_day == dayofweek.thursday ? "Thursday" :
current_day == dayofweek.friday ? "Friday" : "Weekend"
label.new(x=bar_index, y=(or_high + or_low) / 2, text="Trading Disabled\n" + day_name, color=color.gray, textcolor=color.white, style=label.style_label_center, size=size.normal)
// Check for breakouts after opening range is complete (only first breakout of the day)
// FIXED: Added barstate.isconfirmed to avoid lookahead bias
if barstate.isconfirmed and not is_opening_range and not na(or_high) and not na(or_low) and lines_drawn and not breakout_occurred and not daily_trades_complete and is_trading_day_allowed
// Calculate candle body and wick percentages
candle_body = math.abs(close - open)
top_wick = high - math.max(open, close)
bottom_wick = math.min(open, close) - low
top_wick_percent = candle_body > 0 ? (top_wick / candle_body) * 100 : 0
bottom_wick_percent = candle_body > 0 ? (bottom_wick / candle_body) * 100 : 0
// ADDED: Calculate opening range size for distance filters
or_size = or_high - or_low
// Check for first breakout above opening range high
if close > or_high
// FIXED: Mark breakout as occurred FIRST (this is THE breakout candle)
breakout_occurred := true
breakout_candle_high := high
breakout_candle_low := low
breakout_price := close
breakout_direction := "long"
// ADDED: Validate this specific breakout candle against distance filter
breakout_distance_valid = true
if use_breakout_distance_filter
min_breakout_level = or_high + (or_size * min_breakout_multiplier)
max_breakout_level = or_high + (or_size * max_breakout_multiplier)
breakout_distance_valid := close >= min_breakout_level and close <= max_breakout_level
// Apply wick filter for long breakouts
wick_filter_valid = not use_wick_filter or top_wick_percent <= max_wick_percent
// Show appropriate label based on validation results
if show_breakout_validation_labels
if wick_filter_valid and breakout_distance_valid
label.new(x=bar_index, y=high, text="VALID", color=high_line_color, textcolor=color.white, style=label.style_label_down, size=size.tiny)
else
label.new(x=bar_index, y=high, text="INVALID", color=color.gray, textcolor=color.white, style=label.style_label_down, size=size.tiny)
// Mark breakout as invalid so no trade will be placed (regardless of label setting)
if not (wick_filter_valid and breakout_distance_valid)
breakout_direction := "invalid"
// Check for first breakout below opening range low
else if close < or_low
// FIXED: Mark breakout as occurred FIRST (this is THE breakout candle)
breakout_occurred := true
breakout_candle_high := high
breakout_candle_low := low
breakout_price := close
breakout_direction := "short"
// ADDED: Validate this specific breakout candle against distance filter
breakout_distance_valid = true
if use_breakout_distance_filter
min_breakout_level = or_low - (or_size * min_breakout_multiplier)
max_breakout_level = or_low - (or_size * max_breakout_multiplier)
breakout_distance_valid := close <= min_breakout_level and close >= max_breakout_level
// Apply wick filter for short breakouts
wick_filter_valid = not use_wick_filter or bottom_wick_percent <= max_wick_percent
// Show appropriate label based on validation results
if show_breakout_validation_labels
if wick_filter_valid and breakout_distance_valid
label.new(x=bar_index, y=low, text="VALID", color=low_line_color, textcolor=color.white, style=label.style_label_up, size=size.tiny)
else
label.new(x=bar_index, y=low, text="INVALID", color=color.gray, textcolor=color.white, style=label.style_label_up, size=size.tiny)
// Mark breakout as invalid so no trade will be placed (regardless of label setting)
if not (wick_filter_valid and breakout_distance_valid)
breakout_direction := "invalid"
// ADDED: Check for second chance breakout (opposite direction after initial SL hit)
// FIXED: Added barstate.isconfirmed to avoid lookahead bias
if barstate.isconfirmed and not is_opening_range and not na(or_high) and not na(or_low) and lines_drawn and second_chance_available and not second_trade_taken and not daily_trades_complete and is_trading_day_allowed
// Calculate candle body and wick percentages
candle_body = math.abs(close - open)
top_wick = high - math.max(open, close)
bottom_wick = math.min(open, close) - low
top_wick_percent = candle_body > 0 ? (top_wick / candle_body) * 100 : 0
bottom_wick_percent = candle_body > 0 ? (bottom_wick / candle_body) * 100 : 0
// ADDED: Calculate opening range size for distance filters
or_size = or_high - or_low
// If first trade was LONG and failed, look for SHORT breakout
if first_trade_direction == "long" and close < or_low
// FIXED: Mark second chance breakout as taken FIRST
second_trade_taken := true
second_chance_available := false
breakout_candle_high := high
breakout_candle_low := low
breakout_price := close
breakout_direction := "short"
// ADDED: Validate this specific breakout candle against distance filter
breakout_distance_valid = true
if use_breakout_distance_filter
min_breakout_level = or_low - (or_size * min_breakout_multiplier)
max_breakout_level = or_low - (or_size * max_breakout_multiplier)
breakout_distance_valid := close <= min_breakout_level and close >= max_breakout_level
// Apply wick filter for short breakouts
wick_filter_valid = not use_wick_filter or bottom_wick_percent <= max_wick_percent
// Show appropriate label based on validation results
if show_second_chance_labels
if wick_filter_valid and breakout_distance_valid
label.new(x=bar_index, y=low, text="2nd Chance\nOR Low Break\nVALID", color=color.orange, textcolor=color.white, style=label.style_label_up, size=size.tiny)
else
label.new(x=bar_index, y=low, text="2nd Chance\nOR Low Break\nINVALID", color=color.gray, textcolor=color.white, style=label.style_label_up, size=size.tiny)
// Mark breakout as invalid so no trade will be placed (regardless of label setting)
if not (wick_filter_valid and breakout_distance_valid)
breakout_direction := "invalid"
// If first trade was SHORT and failed, look for LONG breakout
else if first_trade_direction == "short" and close > or_high
// FIXED: Mark second chance breakout as taken FIRST
second_trade_taken := true
second_chance_available := false
breakout_candle_high := high
breakout_candle_low := low
breakout_price := close
breakout_direction := "long"
// ADDED: Validate this specific breakout candle against distance filter
breakout_distance_valid = true
if use_breakout_distance_filter
min_breakout_level = or_high + (or_size * min_breakout_multiplier)
max_breakout_level = or_high + (or_size * max_breakout_multiplier)
breakout_distance_valid := close >= min_breakout_level and close <= max_breakout_level
// Apply wick filter for long breakouts
wick_filter_valid = not use_wick_filter or top_wick_percent <= max_wick_percent
// Show appropriate label based on validation results
if show_second_chance_labels
if wick_filter_valid and breakout_distance_valid
label.new(x=bar_index, y=high, text="2nd Chance\nOR High Break\nVALID", color=color.orange, textcolor=color.white, style=label.style_label_down, size=size.tiny)
else
label.new(x=bar_index, y=high, text="2nd Chance\nOR High Break\nINVALID", color=color.gray, textcolor=color.white, style=label.style_label_down, size=size.tiny)
// Mark breakout as invalid so no trade will be placed (regardless of label setting)
if not (wick_filter_valid and breakout_distance_valid)
breakout_direction := "invalid"
// === STRATEGY LOGIC ===
// Check if we have a breakout and place retracement entry orders
var bool entry_placed = false
var bool second_entry_placed = false // ADDED: Track second trade entry separately
var float entry_price = na
var float stop_loss = na
var float take_profit = na
var float trailing_stop = na
var bool trailing_active = false
var float initial_risk = na
var bool trailing_started = false
var string current_entry_id = na // FIXED: Track which entry ID we're using
// Arrays to store historical trade boxes
var array<box> historical_trade_boxes = array.new<box>()
var array<box> historical_sl_boxes = array.new<box>()
var array<box> historical_tp_boxes = array.new<box>()
// Variables to track current active trade boxes for extending to exit
var box current_profit_box = na
var box current_sl_box = na
// ADDED: General position close detection for extending boxes - Handle timing issues
if barstate.isconfirmed and strategy.position_size == 0 and strategy.position_size[1] != 0
// Extend trade visualization boxes to exact exit point when any position closes
if not na(current_profit_box)
// Ensure minimum 8 bars width or extend to current bar, whichever is longer
box_left = box.get_left(current_profit_box)
min_right = box_left + 8
final_right = math.max(min_right, bar_index)
box.set_right(current_profit_box, final_right)
current_profit_box := na // Clear reference after extending
if not na(current_sl_box)
// Ensure minimum 8 bars width or extend to current bar, whichever is longer
box_left = box.get_left(current_sl_box)
min_right = box_left + 8
final_right = math.max(min_right, bar_index)
box.set_right(current_sl_box, final_right)
current_sl_box := na // Clear reference after extending
// ADDED: Backup safety check - extend boxes if position is closed but boxes still active
if not na(current_profit_box) and strategy.position_size == 0
box_left = box.get_left(current_profit_box)
min_right = box_left + 8
final_right = math.max(min_right, bar_index)
box.set_right(current_profit_box, final_right)
current_profit_box := na
if not na(current_sl_box) and strategy.position_size == 0
box_left = box.get_left(current_sl_box)
min_right = box_left + 8
final_right = math.max(min_right, bar_index)
box.set_right(current_sl_box, final_right)
current_sl_box := na
// Reset entry flag on new day
if is_new_day
entry_placed := false
second_entry_placed := false // ADDED: Reset second entry flag
entry_price := na
stop_loss := na
take_profit := na
trailing_stop := na
trailing_active := false
initial_risk := na
trailing_started := false
current_entry_id := na // FIXED: Reset entry ID
current_profit_box := na // ADDED: Reset current trade boxes
current_sl_box := na
// SIMPLIFIED: Detect when position closes to enable second chance (FIXED for lookahead bias)
if barstate.isconfirmed and strategy.position_size == 0 and strategy.position_size[1] != 0 and entry_placed and not first_trade_sl_hit
// A position just closed and we had an active trade
if enable_second_chance and not second_trade_taken
// Simplified logic - if position closed, enable second chance
first_trade_sl_hit := true
first_trade_direction := breakout_direction
second_chance_available := true
// Reset variables for potential second trade
entry_price := na
trailing_stop := na
trailing_active := false
initial_risk := na
trailing_started := false
current_entry_id := na
// Add visual marker
if show_trade_status_labels
label.new(x=bar_index, y=close, text="Trade Closed\nSecond Chance Available", color=color.yellow, textcolor=color.black, style=label.style_label_down, size=size.tiny)
else
// Second chance not enabled or already taken - mark day complete
daily_trades_complete := true
// ADDED: Handle case where first breakout was invalid (no trade placed)
if breakout_occurred and breakout_direction == "invalid" and enable_second_chance and not first_trade_sl_hit
// First breakout was invalid, enable second chance immediately
first_trade_sl_hit := true
// Determine what direction the invalid breakout was
first_trade_direction := breakout_price > or_high ? "long" : "short"
second_chance_available := true
if show_trade_status_labels
label.new(x=bar_index + 1, y=(or_high + or_low) / 2, text="First Breakout Invalid\nSecond Chance Available", color=color.yellow, textcolor=color.black, style=label.style_label_center, size=size.tiny)
// REMOVED: Complex historical box cleanup to avoid lookahead bias
// Historical boxes will be cleaned up automatically by Pine Script's runtime
// Place entry orders after breakout - FIXED: Add barstate.isconfirmed for consistency
if barstate.isconfirmed and not daily_trades_complete and is_trading_day_allowed and ((breakout_occurred and not entry_placed and not na(breakout_candle_high) and breakout_direction != "invalid") or (second_trade_taken and not second_entry_placed and not na(breakout_candle_high) and breakout_direction != "invalid"))
// For long breakout
if breakout_direction == "long"
// Calculate stop loss based on selected method
if sl_type == "Breakout Candle"
stop_loss := breakout_candle_low - (sl_points * syminfo.mintick)
else
// Use opposite side of opening range (below opening range low)
stop_loss := or_low - (sl_points * syminfo.mintick)
if entry_type == "Retracement"
// Calculate retracement entry price (x% of breakout candle body)
breakout_candle_body = breakout_candle_high - breakout_candle_low
retracement_amount = breakout_candle_body * (retracement_percent / 100)
entry_price := breakout_candle_high - retracement_amount
// FIXED: Store the entry ID we're using (differentiate first vs second chance)
current_entry_id := second_trade_taken ? "Long Retracement 2nd" : "Long Retracement"
// Place buy limit order at retracement level
strategy.entry(current_entry_id, strategy.long, limit=entry_price, qty=contracts)
// Add visual markers
if show_entry_labels
entry_label_text = second_trade_taken ? "BUY LIMIT (2nd)\n" + str.tostring(entry_price, "#.##") : "BUY LIMIT\n" + str.tostring(entry_price, "#.##")
label.new(x=bar_index, y=entry_price, text=entry_label_text, color=color.green, textcolor=color.white, style=label.style_label_up, size=size.tiny)
else
// Immediate entry at breakout candle close
entry_price := breakout_price
// FIXED: Store the entry ID we're using (differentiate first vs second chance)
current_entry_id := second_trade_taken ? "Instant Long 2nd" : "Instant Long"
// Place buy market order
strategy.entry(current_entry_id, strategy.long, qty=contracts)
// Add visual markers
if show_entry_labels
entry_label_text = second_trade_taken ? "BUY MARKET (2nd)\n" + str.tostring(entry_price, "#.##") : "BUY MARKET\n" + str.tostring(entry_price, "#.##")
label.new(x=bar_index, y=entry_price, text=entry_label_text, color=color.green, textcolor=color.white, style=label.style_label_up, size=size.tiny)
// Calculate take profit based on risk:reward
risk_size = entry_price - stop_loss
take_profit := entry_price + (risk_size * risk_multiplier)
// FIXED: Set exit orders with proper entry ID and always include initial stop loss
if use_trailing_sl
// Initialize trailing stop and calculate initial risk
trailing_stop := stop_loss
trailing_active := true
initial_risk := math.abs(entry_price - stop_loss)
trailing_started := false
// FIXED: Always set initial stop loss, even with trailing enabled
exit_id = second_trade_taken ? "Long Exit 2nd" : "Long Exit"
strategy.exit(exit_id, current_entry_id, stop=stop_loss, limit=take_profit)
else
// FIXED: Use stored entry ID
exit_id = second_trade_taken ? "Long Exit 2nd" : "Long Exit"
strategy.exit(exit_id, current_entry_id, stop=stop_loss, limit=take_profit)
// Create trade visualization boxes (TradingView style) - FIXED: Minimum 8 bars width
// Blue profit zone box (from entry to take profit)
// Store trade boxes for historical display - FIXED: Remove time usage
array.push(historical_trade_boxes, current_profit_box)
array.push(historical_sl_boxes, current_sl_box)
array.push(historical_tp_boxes, na) // No TP box for long trades
// Add stop loss and take profit markers
if show_sl_tp_labels
label.new(x=bar_index, y=stop_loss, text="SL\n" + str.tostring(stop_loss, "#.##"), color=color.red, textcolor=color.white, style=label.style_label_down, size=size.tiny)
label.new(x=bar_index, y=take_profit, text="TP\n" + str.tostring(take_profit, "#.##"), color=color.blue, textcolor=color.white, style=label.style_label_down, size=size.tiny)
// ADDED: Set the appropriate entry flag based on which trade this is
if second_trade_taken
second_entry_placed := true
daily_trades_complete := true
else
entry_placed := true
// For short breakout
else if breakout_direction == "short"
// Calculate stop loss based on selected method
if sl_type == "Breakout Candle"
stop_loss := breakout_candle_high + (sl_points * syminfo.mintick)
else
// Use opposite side of opening range (above opening range high)
stop_loss := or_high + (sl_points * syminfo.mintick)
if entry_type == "Retracement"
// Calculate retracement entry price (x% of breakout candle body)
breakout_candle_body = breakout_candle_high - breakout_candle_low
retracement_amount = breakout_candle_body * (retracement_percent / 100)
entry_price := breakout_candle_low + retracement_amount
// FIXED: Store the entry ID we're using (differentiate first vs second chance)
current_entry_id := second_trade_taken ? "Short Retracement 2nd" : "Short Retracement"
// Place sell limit order at retracement level
strategy.entry(current_entry_id, strategy.short, limit=entry_price, qty=contracts)
// Add visual markers
if show_entry_labels
entry_label_text = second_trade_taken ? "SELL LIMIT (2nd)\n" + str.tostring(entry_price, "#.##") : "SELL LIMIT\n" + str.tostring(entry_price, "#.##")
label.new(x=bar_index, y=entry_price, text=entry_label_text, color=color.red, textcolor=color.white, style=label.style_label_down, size=size.tiny)
else
// Immediate entry at breakout candle close
entry_price := breakout_price
// FIXED: Store the entry ID we're using (differentiate first vs second chance)
current_entry_id := second_trade_taken ? "Instant 2nd" : "Instant Short"
// Place sell market order
strategy.entry(current_entry_id, strategy.short, qty=contracts)
// Add visual markers
if show_entry_labels
entry_label_text = second_trade_taken ? "SELL MARKET (2nd)\n" + str.tostring(entry_price, "#.##") : "SELL MARKET\n" + str.tostring(entry_price, "#.##")
label.new(x=bar_index, y=entry_price, text=entry_label_text, color=color.red, textcolor=color.white, style=label.style_label_down, size=size.tiny)
// Calculate take profit based on risk:reward
risk_size = stop_loss - entry_price
take_profit := entry_price - (risk_size * risk_multiplier)
// FIXED: Set exit orders with proper entry ID and always include initial stop loss
if use_trailing_sl
// Initialize trailing stop and calculate initial risk
trailing_stop := stop_loss
trailing_active := true
initial_risk := math.abs(entry_price - stop_loss)
trailing_started := false
// FIXED: Always set initial stop loss, even with trailing enabled
exit_id = second_trade_taken ? "Short Exit 2nd" : "Short Exit"
strategy.exit(exit_id, current_entry_id, stop=stop_loss, limit=take_profit)
else
// FIXED: Use stored entry ID
exit_id = second_trade_taken ? "Short Exit 2nd" : "Short Exit"
strategy.exit(exit_id, current_entry_id, stop=stop_loss, limit=take_profit)
// Create trade visualization boxes (TradingView style) - FIXED: Minimum 8 bars width
// Store trade boxes for historical display - FIXED: Remove time usage
array.push(historical_trade_boxes, current_profit_box)
array.push(historical_sl_boxes, current_sl_box)
array.push(historical_tp_boxes, na) // No TP box for short trades
// Add stop loss and take profit markers
if show_sl_tp_labels
label.new(x=bar_index, y=stop_loss, text="SL\n" + str.tostring(stop_loss, "#.##"), color=color.red, textcolor=color.white, style=label.style_label_up, size=size.tiny)
label.new(x=bar_index, y=take_profit, text="TP\n" + str.tostring(take_profit, "#.##"), color=color.blue, textcolor=color.white, style=label.style_label_up, size=size.tiny)
// ADDED: Set the appropriate entry flag based on which trade this is
if second_trade_taken
second_entry_placed := true
daily_trades_complete := true
else
entry_placed := true
// === TRAILING STOP LOGIC ===
// FIXED: Proper trailing stop loss management
if use_trailing_sl and trailing_active and strategy.position_size != 0 and not na(current_entry_id)
if strategy.position_size > 0 // Long position
// Calculate current unrealized profit in points
current_profit = close - entry_price
profit_r = current_profit / initial_risk
// Check if we should start trailing (after X R profit)
if not trailing_started and profit_r >= profit_r_multiplier
trailing_started := true
// Start trailing from a level that's better than the initial stop
trailing_stop := math.max(trailing_stop, close - (atr * atr_multiplier))
// Update trailing stop if trailing has started
if trailing_started
// Calculate new trailing stop using ATR
potential_new_stop = close - (atr * atr_multiplier)
// Only move stop loss up (never down) and ensure it's better than initial SL
if potential_new_stop > trailing_stop and potential_new_stop > stop_loss
trailing_stop := potential_new_stop
// Update the exit order with new trailing stop
exit_id = second_trade_taken ? "Long Exit 2nd" : "Long Exit"
strategy.exit(exit_id, current_entry_id, stop=trailing_stop, limit=take_profit)
else if strategy.position_size < 0 // Short position
// Calculate current unrealized profit in points
current_profit = entry_price - close
profit_r = current_profit / initial_risk
// Check if we should start trailing (after X R profit)
if not trailing_started and profit_r >= profit_r_multiplier
trailing_started := true
// Start trailing from a level that's better than the initial stop
trailing_stop := math.min(trailing_stop, close + (atr * atr_multiplier))
// Update trailing stop if trailing has started
if trailing_started
// Calculate new trailing stop using ATR
potential_new_stop = close + (atr * atr_multiplier)
// Only move stop loss down (never up) and ensure it's better than initial SL
if potential_new_stop < trailing_stop and potential_new_stop < stop_loss
trailing_stop := potential_new_stop
// Update the exit order with new trailing stop
exit_id = second_trade_taken ? "Short Exit 2nd" : "Short Exit"
strategy.exit(exit_id, current_entry_id, stop=trailing_stop, limit=take_profit)
// === SESSION END CLOSE ===
// Force close all positions at configured session end time (optional)
// FIXED: Using configurable hour/minute with timezone
if force_session_close and current_hour == session_end_hour and current_minute == session_end_minute
// ADDED: Extend boxes immediately before session close to prevent timing issues
if not na(current_profit_box)
// Ensure minimum 8 bars width or extend to current bar, whichever is longer
box_left = box.get_left(current_profit_box)
min_right = box_left + 8
final_right = math.max(min_right, bar_index)
box.set_right(current_profit_box, final_right)
current_profit_box := na // Clear reference after extending
if not na(current_sl_box)
// Ensure minimum 8 bars width or extend to current bar, whichever is longer
box_left = box.get_left(current_sl_box)
min_right = box_left + 8
final_right = math.max(min_right, bar_index)
box.set_right(current_sl_box, final_right)
current_sl_box := na // Clear reference after extending
strategy.close_all(comment="Session End Close")
// === ALERTS ===
alert_once_long = (strategy.position_size > 0) and (strategy.position_size[1] == 0)
alert_once_short = (strategy.position_size < 0) and (strategy.position_size[1] == 0)
alertcondition(alert_once_long, title="Long Entry (Once)", message="Long Entry Signal")
alertcondition(alert_once_short, title="Short Entry (Once)", message="Short Entry Signal")