মাল্টি-টাইম ফ্রেম জিরো লেটেন্সি ট্রেন্ড সিগন্যাল ট্রেডিং কৌশল: ZLEMA ভিত্তিক গতিশীল প্রবেশ এবং প্রস্থান ব্যবস্থা

ZLEMA ATR EMA MTF RR TP SL
সৃষ্টির তারিখ: 2025-05-20 14:45:41 অবশেষে সংশোধন করুন: 2025-05-20 14:45:41
অনুলিপি: 0 ক্লিকের সংখ্যা: 592
2
ফোকাস
319
অনুসারী

মাল্টি-টাইম ফ্রেম জিরো লেটেন্সি ট্রেন্ড সিগন্যাল ট্রেডিং কৌশল: ZLEMA ভিত্তিক গতিশীল প্রবেশ এবং প্রস্থান ব্যবস্থা মাল্টি-টাইম ফ্রেম জিরো লেটেন্সি ট্রেন্ড সিগন্যাল ট্রেডিং কৌশল: ZLEMA ভিত্তিক গতিশীল প্রবেশ এবং প্রস্থান ব্যবস্থা

ওভারভিউ

মাল্টি টাইম ফ্রেম জিরো লেটেন্সি ট্রেন্ড সিগন্যাল ট্রেডিং স্ট্র্যাটেজি হল একটি কোয়ান্টাম ট্রেডিং সিস্টেম যা জিরো লেটেন্সি ইনডেক্স মুভিং এভারেজ (ZLEMA) এর উপর ভিত্তি করে তৈরি করা হয়েছে যা প্রচলিত মুভিং এভারেজের পিছনে থাকা কমাতে এবং দ্রুত এবং সঠিক ট্রেন্ড সনাক্তকরণ সংকেত সরবরাহ করে। এই কৌশলটি কেবলমাত্র প্রবণতা পরিবর্তন সনাক্ত করার জন্য অস্থিরতার চ্যানেলগুলিকে সংযুক্ত করে না, তবে এটি বিভিন্ন ধরণের নমনীয় প্রস্থান ব্যবস্থাও অন্তর্ভুক্ত করে, যার মধ্যে রয়েছে ঝুঁকি রিটার্ন, প্রস্থান লাভের লক্ষ্য প্রস্থান, এটিআর-ভিত্তিক স্টপ এবং স্টপ লস, গতিশীল ট্র্যাকিং স্টপ লস এবং লাইনগুলি সমস্ত প্রস্থান অতিক্রম করে। বিশেষত মাঝারি এবং দীর্ঘমেয়াদী ব্যবসায়ের জন্য উপযুক্ত। কৌশলটি কেবলমাত্র একাধিক বা দ্বি-মুখী ব্যবসায়ের জন্য কনফিগার করা যেতে পারে এবং জিরো লাইন পুনরায় প্রবেশের বিকল্প সরবরাহ করে, যা ট্রেডিং সিস্টেমের অভিযোদনশীলতা

কৌশল নীতি

এই কৌশলটির মূল নীতিটি হল জিরো লেটেন্সি ইনডেক্স মুভিং এভারেজ (ZLEMA) এর উপর ভিত্তি করে, এটি একটি প্রযুক্তিগত সূচক যা মুভিং এভারেজের প্রতিক্রিয়া গতি বাড়ানোর জন্য মূল্যের ডেটা বিলম্বকে নির্মূল বা হ্রাস করে। বাস্তবায়নের পদক্ষেপগুলি নিম্নরূপঃ

  1. শূন্য বিলম্ব গণনাকৌশলঃ প্রথমে ZLEMA গণনা করুন, সূত্রের মাধ্যমেzlema = ta.ema(src + (src - src[lag]), length)এর মধ্যেlagএই পদ্ধতিটি ঐতিহ্যবাহী EMA-র মধ্যে পিছিয়ে পড়া কার্যকরভাবে হ্রাস করে।

  2. প্রবণতা সনাক্তকরণ

    • ZLEMA-এর উপর ভিত্তি করে ভোল্টেবল চ্যানেল যোগ করা হয়েছে (বুলিন-ব্যান্ডের মত), চ্যানেলের প্রস্থ নির্ধারণ করা হয়েছে ATR-এর সর্বোচ্চ মানের গুণিতক দ্বারা
    • যখন দাম উপরে চলে যায়, তখন ট্রেন্ড উপরে চলে যায় (১)
    • যখন দাম নিচের ট্র্যাকটি অতিক্রম করে, তখন প্রবণতাটি নিচে চলে যায় (-1)
    • সিস্টেমটি 5 টি ক্রমাগত কে-লাইনের ZLEMA দিকনির্দেশের নিশ্চিতকরণও সরবরাহ করে।zlemaUpTrendএবংzlemaDownTrendপরিবর্তনশীল বাস্তবায়ন
  3. প্রবেশের বৈচিত্র্য

    • বেসিক মাল্টি-এন্ট্রিঃ দামগুলি ট্র্যাকের উপরে এবং তারিখের মধ্যে রয়েছে
    • আরো প্রবেশের জন্য অগ্রগতিঃ বেসিক শর্তাবলী এবং ZLEMA এর ক্রমাগত 5 টি K লাইন আপ ট্রেন্ড নিশ্চিত করেছে
    • উড়োজাহাজে প্রবেশঃ দামের নিচে এবং তারিখের মধ্যে (বিকল্প বৈশিষ্ট্য)
    • ZLEMA শূন্য রেখায় পুনরায় প্রবেশঃ দামের সংক্ষিপ্ত পুনর্নির্মাণের পরে আবার ZLEMA-এর উপরে ফিরে আসে এবং এখনও বহু প্রবণতার মধ্যে রয়েছে
  4. ইন্টিগ্রেটেড ডায়নামিক আউট সিস্টেম

    • রিস্ক-রিটার্ন-প্রফিট টার্গেটঃ নির্দিষ্ট রিস্ক-রিটার্ন-প্রফিট অনুপাতের জন্য টার্গেট মূল্য যা প্রবেশ মূল্য এবং স্টপ লস পয়েন্টের উপর ভিত্তি করে গণনা করা হয়
    • ATR বেস স্টপ এবং স্টপঃ এটিআর গুণক ব্যবহার করে গতিশীলভাবে স্টপ এবং স্টপ অবস্থান গণনা করুন
    • ATR ট্র্যাকিং স্টপ লসঃ দামের পরিবর্তনের সাথে সাথে স্বয়ংক্রিয়ভাবে স্টপ লস স্থানান্তর করে
    • লাভ-ক্ষতির ভারসাম্য ক্ষতিঃ যখন লাভ একটি নির্দিষ্ট ঝুঁকি-ফেরতের অনুপাত অর্জন করে, তখন স্টপ লস প্রবেশের মূল্যে স্থানান্তরিত হয়
    • প্রবণতা বিপরীত প্রস্থানঃ প্রবণতা সূচকটি পরিবর্তিত হলে স্বয়ংক্রিয়ভাবে প্রস্থান করুন
    • ইএমএ আউটঃ যখন দাম নির্দিষ্ট ইএমএ অতিক্রম করে তখন বেরিয়ে আসে

কৌশলগত সুবিধা

মাল্টিপল টাইম ফ্রেম জিরো লেটেন্সি ট্রেন্ড সিগন্যাল ট্রেডিং কৌশলগুলির উল্লেখযোগ্য সুবিধা রয়েছেঃ

  1. সংকেত বিলম্ব হ্রাসZLEMA প্রযুক্তির মাধ্যমে, প্রচলিত মুভিং এভারেজের বিলম্ব হ্রাস করা হয়েছে, যা প্রবণতা সনাক্তকরণকে আরও সময়মত করে তোলে এবং প্রবণতার সূচনাকে আরও আগে ধরতে সক্ষম করে।

  2. সম্পূর্ণ ঝুঁকি ব্যবস্থাপনা ব্যবস্থা: একীভূত বহুস্তরীয় ঝুঁকি নিয়ন্ত্রণ ব্যবস্থা, স্থির ক্ষতি, এটিআর গতিশীল ক্ষতি, ট্র্যাকিং ক্ষতি থেকে ক্ষতির সমান্তরাল ক্ষতি পর্যন্ত, বিভিন্ন বাজার পরিবেশের জন্য নিখুঁত সুরক্ষা প্রদান করে।

  3. নমনীয় ট্রেডিং বিকল্প: বিভিন্ন বাজার পছন্দ এবং নিয়ন্ত্রক পরিবেশের সাথে খাপ খাইয়ে নেওয়ার জন্য কেবলমাত্র একাধিক কৌশল বা দ্বি-মুখী ট্রেডিং কৌশল হিসাবে কনফিগার করা যেতে পারে।

  4. পুনরায় ভর্তি প্রক্রিয়া: ZLEMA শূন্য রেখা পুনরায় প্রবেশের মাধ্যমে, শক্তিশালী প্রবণতায় স্বল্পমেয়াদী পুনঃনির্ধারণের পরে পুনরায় প্রবেশের অনুমতি দেওয়া হয়, প্রবণতা লাভকে সর্বাধিক করে তোলে।

  5. বিচ্ছিন্নতা নীতি: বিভিন্ন বাজারের অবস্থার জন্য বিভিন্ন প্রস্থান বিকল্প সরবরাহ করা হয়, যা লাভের লক্ষ্যমাত্রা দ্বারা লাভকে লক করতে পারে এবং স্টপ লস ট্র্যাকিংয়ের মাধ্যমে মুনাফা চালাতে পারে।

  6. ভিজ্যুয়াল সহায়তা: ট্রেডিং সিগন্যাল এবং ঝুঁকি ব্যবস্থাপনার অবস্থানকে ট্রেন্ড শ্যাডো, স্টপ লস লাইন, স্টপ ব্রেক লাইন এবং ট্রেন্ড ইন্ডিকেটরের মতো ভিজ্যুয়াল উপাদানগুলির মাধ্যমে প্রদর্শন করুন।

  7. বিস্তারিত পারফরম্যান্স পরিসংখ্যান

কৌশলগত ঝুঁকি

যদিও এই কৌশলটি যুক্তিসঙ্গতভাবে পরিকল্পিত, তবুও কিছু সম্ভাব্য ঝুঁকি রয়েছে যা সম্পর্কে সতর্ক হওয়া দরকারঃ

  1. পরামিতি সংবেদনশীলতাZLEMA দৈর্ঘ্য এবং ATR গুণিতক এর মতো মূল প্যারামিটারগুলি কৌশলগত পারফরম্যান্সের উপর উল্লেখযোগ্য প্রভাব ফেলে। ভুলভাবে সেট করা হলে এটি খুব বেশি বা খুব কম সংকেত দিতে পারে।

  2. বাজারের ভ্রান্ত সংকেত: কোন সুস্পষ্ট প্রবণতা ছাড়া অস্থির বাজারে, ক্রমাগত ক্ষতির কারণ হিসাবে ঘন ঘন মিথ্যা সংকেত তৈরি হতে পারে।

  3. ট্রেন্ড রিভার্সাল ঝুঁকিযদিও কৌশলগতভাবে অনেকগুলি প্রস্থান করার ব্যবস্থা রয়েছে, তবে তীব্র প্রবণতা বিপরীত হওয়ার পরেও, প্রবণতা বিপরীত হওয়ার পরেও, প্রবণতা বিপরীত হওয়ার পরেও, প্রবণতা বিপরীত হওয়ার পরেও, প্রবণতা বিপরীত হওয়ার পরেও, প্রবণতা বিপরীত হওয়ার পরেও, প্রবণতা বিপরীত হওয়ার পরেও, প্রবণতা বিপরীত হওয়ার পরেও, প্রবণতা বিপরীত হওয়ার পরেও, প্রবণতা বিপরীত হওয়ার পরেও, প্রবণতা বিপরীত হওয়ার পরেও, প্রবণতা বিপরীত হওয়ার পরেও, প্রবণতা বিপরীত হতে পারে।

  4. অতিরিক্ত ফিট হওয়ার ঝুঁকি: একাধিক প্যারামিটারের সমন্বয় ঐতিহাসিক তথ্যের উপর অত্যধিক ফিটনেস সৃষ্টি করতে পারে, যা ভবিষ্যতে বাজারের পরিবেশে দুর্বলভাবে কাজ করবে।

  5. দীর্ঘ-চক্র সংকেত বিরলZLEMA: দীর্ঘ ZLEMA দৈর্ঘ্য ব্যবহার করার সময়, কৌশলটি কম ট্রেডিং সিগন্যাল তৈরি করতে পারে, যা তহবিলের ব্যবহারের দক্ষতাকে প্রভাবিত করে।

  6. প্রস্থ হ্রাসের চ্যালেঞ্জএটিআর-ভিত্তিক স্টপগুলি উচ্চ অস্থিরতার বাজারে খুব প্রশস্ত হতে পারে, যার ফলে একক ক্ষতির পরিমাণ খুব বেশি হতে পারে; এবং নিম্ন অস্থিরতার বাজারে খুব সংকীর্ণ হতে পারে, যার ফলে ঘন ঘন ট্রিগার হতে পারে।

এই ঝুঁকিগুলি হ্রাস করার উপায়গুলির মধ্যে রয়েছেঃ কঠোর প্যারামিটার রিটার্নিং এবং ফরোয়ার্ড যাচাইকরণ, বাজারের অবস্থার সূচকগুলির সাথে মিলিত বাজারের অস্থিরতার সময় ট্রেডিং এড়ানো, কঠোর তহবিল পরিচালনার নিয়ম প্রয়োগ করা এবং বাজারের পরিবর্তনের সাথে সামঞ্জস্য রেখে কৌশলগত প্যারামিটারগুলিকে নিয়মিত পুনরায় অপ্টিমাইজ করা।

কৌশল অপ্টিমাইজেশনের দিকনির্দেশনা

এই কৌশলটি আরও অনেক উন্নতি করতে পারে, যেমনঃ

  1. গতিশীল প্যারামিটার স্বনির্ধারিত: স্বয়ংক্রিয়ভাবে ZLEMA দৈর্ঘ্য এবং ATR গুণককে বাজারের অস্থিরতার উপর ভিত্তি করে সামঞ্জস্য করার জন্য একটি স্ব-অনুকূলিতকরণ ব্যবস্থা তৈরি করা, যা বিভিন্ন বাজারের পরিবেশে কৌশলগুলির অভিযোজনযোগ্যতা উন্নত করে।

  2. বাজার অবস্থা ফিল্টার: বাজারের স্থিতির সূচক (যেমন ADX, অস্থিরতা সূচক) প্রবর্তন করুন, কেবলমাত্র অনুকূল বাজার পরিস্থিতিতে ট্রেড করুন এবং অকার্যকর অস্থির বাজারে ঘন ঘন ট্রেডিং এড়িয়ে চলুন।

  3. মাল্টি টাইম ফ্রেম নিশ্চিতকরণ: উচ্চতর সময়ের ফ্রেমের প্রবণতার দিকের সাথে মিলিত, শুধুমাত্র যখন বড় প্রবণতার দিকের সাথে মিলিত হয় তখনই অংশগ্রহণ করুন, সাফল্যের হার বাড়ান।

  4. লেনদেনের পরিমাণ: ট্রেডিং ভলিউম ইন্টিগ্রেটেড ইন্ডিকেটরকে সহায়ক নিশ্চিতকরণ হিসাবে ব্যবহার করা হয়, যেমন ট্রেডিং ভলিউম বৃদ্ধি পেলে ট্রেন্ড পরিবর্তনের সংকেত নিশ্চিত করা হয়।

  5. মেশিন লার্নিং অপ্টিমাইজেশন: মেশিন লার্নিং অ্যালগরিদম ব্যবহার করে সর্বোত্তম প্যারামিটার সমন্বয় এবং প্রবেশের সময় খুঁজতে, বিশেষত মডেলগুলিকে প্রশিক্ষণ দেওয়া যায় যে কোন সংকেতগুলি সফল হওয়ার সম্ভাবনা বেশি।

  6. মৌসুমী এবং সময় ফিল্টার: কম কার্যকর বা উচ্চ ঝুঁকিপূর্ণ ট্রেডিংয়ের সময়গুলি এড়াতে ট্রেডিং সময় এবং ক্যালেন্ডার ফিল্টার যুক্ত করুন।

  7. সংশ্লিষ্ট সম্পদ সম্পর্কিত বিশ্লেষণ: প্রাসঙ্গিক সম্পদের প্রাসঙ্গিকতা বিশ্লেষণের প্রবর্তন, একাধিক সম্পদের সমান্তরাল নিশ্চিতকরণের সময় সংকেত বিশ্বাসযোগ্যতা বৃদ্ধি।

এই অপ্টিমাইজেশনের দিকগুলি কেবল কৌশলগুলির স্থিতিশীলতা এবং লাভজনকতা বাড়িয়ে তোলে না, বরং ঝুঁকি হ্রাস করে এবং বিভিন্ন বাজার পরিবেশ এবং ব্যক্তিগত ঝুঁকি পছন্দগুলির জন্য আরও উপযুক্ত করে তোলে।

সারসংক্ষেপ

মাল্টি টাইম ফ্রেম জিরো লেটেন্সি ট্রেন্ড সিগন্যাল ট্রেডিং কৌশল একটি বিস্তৃত এবং নমনীয় পরিমাণযুক্ত ট্রেডিং সিস্টেম যা দ্রুত এবং সঠিক প্রবণতা সনাক্তকরণের জন্য জিরো লেটেন্সি ইনডেক্স মুভিং এভারেজ (ZLEMA) প্রযুক্তি এবং ভোল্টেবল চ্যানেলের মাধ্যমে এবং তহবিলের সুরক্ষার জন্য একাধিক স্তরের গতিশীল ঝুঁকি ব্যবস্থাপনা ব্যবস্থার সাথে সংযুক্ত। এই কৌশলটি প্রবণতার শুরুতে প্রবেশের সুযোগগুলিকে ধরা এবং প্রবণতা বিকাশে পুনরায় প্রবেশের ব্যবস্থার মাধ্যমে উপার্জনকে সর্বাধিকতর করতে পারে, একই সাথে বিভিন্ন বাজারের পরিবেশের সাথে খাপ খাইয়ে নিতে একাধিক প্রস্থান কৌশল সরবরাহ করে।

কৌশলটির প্রধান সুবিধা হ’ল সংকেত বিলম্ব হ্রাস করা, একটি বিস্তৃত ঝুঁকি ব্যবস্থাপনার ব্যবস্থা এবং নমনীয় ট্রেডিং কনফিগারেশন বিকল্পগুলি সরবরাহ করা। তবে ব্যবহারকারীদের প্যারামিটার সংবেদনশীলতা, বাজারের ভ্রান্ত সংকেত এবং অত্যধিক সামঞ্জস্যের মতো সম্ভাব্য ঝুঁকির বিষয়ে সতর্ক হওয়া দরকার। কৌশলটির কার্যকারিতা আরও উন্নত করা যেতে পারে যেমন বাজার অবস্থা ফিল্টারিং, একাধিক টাইম ফ্রেম নিশ্চিতকরণ এবং গতিশীল প্যারামিটার স্ব-অনুসরণের মতো অপ্টিমাইজেশন দিক।

একটি প্রযুক্তিগত সূচকের উপর ভিত্তি করে একটি পরিমাণগত ট্রেডিং সিস্টেম হিসাবে, এই কৌশলটি বিশেষভাবে মাঝারি এবং দীর্ঘমেয়াদী প্রবণতা ট্রেডিংয়ের জন্য উপযুক্ত, যা বিভিন্ন আর্থিক বাজারে প্রযোজ্য। যাইহোক, যে কোনও কৌশলকে ব্যক্তিগত ট্রেডিং লক্ষ্য, ঝুঁকি সহনশীলতা এবং বাজারের পছন্দ অনুসারে ব্যক্তিগতকরণ করা প্রয়োজন এবং কঠোর তহবিল পরিচালনার নীতিগুলির সাথে মিলিত হয় যা প্রকৃত লেনদেনে প্রয়োগ করা হয়।

কৌশল সোর্স কোড
//@version=6
// Quant Trading Pro www.quanttradingpro.com
// #1 Strategy Optimizer on the chrome extension store Quant Trading Strategy Optimizer


strategy(title="Quant Trading Zero Lag Trend Signals (MTF) Strategy", shorttitle="QT0️⃣Zero Lag Signals Strategy", overlay=true, 
     commission_type=strategy.commission.percent, 
     commission_value=0.1, 
     slippage=3, 
     default_qty_type=strategy.percent_of_equity, 
     default_qty_value=100, 
     initial_capital=1000, 
     margin_long=0, 
     margin_short=0)

// === INPUT PARAMETERS ===

// 📌 1️⃣ Main Calculations
length = input.int(70, title="Length", minval=70, maxval=7000)
mult   = input.float(1.2, "Band Multiplier", group="Main Calculations")

// === 📊 Trade Data Toggles ===
showFloatingTable = input.bool(true, "Show Floating Stats Table?", group="Trade Data")
showTradeLog      = input.bool(false, "Show Trade Log Labels on Chart?", group="Trade Data")
enableCSVExport = input.bool(false, "Enable CSV Export", group="Trade Data")

// 📌 ZLEMA Trend Confirmation for Entries
useZlemaTrendConfirm = input.bool(false, "Use ZLEMA Trend Confirmation for entry?", group="Entry Conditions")
reEnterOnLongTrend = input.bool(false, "Re-Enter on Long Trend continuation", group="Entry Conditions")

// 📌 2️⃣ Short Trades
allowShortTrades = input.bool(false, "Allow Short Trades?", group="Short Trades")

// 📌 Performance Settings
useProfitTarget  = input.bool(false, "Use Profit Target?", group="Performance Settings")
profitTargetRR   = input.float(2.0, "Profit Target (Risk-Reward Ratio)", group="Performance Settings")


// 📌 4️⃣ Dynamic TP/SL (Each Setting with Its Parameters Directly Below)
useATRStopTP    = input.bool(false, "Use ATR-Based Stop-Loss & Take-Profit?", group="Dynamic TP/SL")
atrLength       = input.int(14, "ATR Length", group="Dynamic TP/SL")
atrMultiplier   = input.float(1.5, "Stop-Loss ATR Multiplier", group="Dynamic TP/SL")
profitATRMultiplier = input.float(2.5, "Profit Target ATR Multiplier", group="Dynamic TP/SL")

useTrailingStop = input.bool(false, "Use ATR Trailing Stop?", group="Dynamic TP/SL")
trailStopMultiplier = input.float(1.5, "Trailing Stop ATR Multiplier", group="Dynamic TP/SL")

useBreakEven    = input.bool(false, "Use Break-Even Stop-Loss?", group="Dynamic TP/SL")
breakEvenRR     = input.float(1.5, "Move SL to Break-Even After RR", group="Dynamic TP/SL")

useTrendExit    = input.bool(false, "Use Trend-Based Take Profit (EMA Exit)?", group="Dynamic TP/SL")
emaExitLength   = input.int(9, "EMA Exit Length", group="Dynamic TP/SL")

// 📌 Debug Options
showZlemaDebug = input.bool(false, "Show ZLEMA Trend Debug?", group="Debug")




// ============================================================
// === 1️⃣ CALCULATIONS (Indicators, Trend, ATR, Stop/TP) ===
// ============================================================
src        = close
lag        = math.floor((length - 1) / 2)
zlema      = ta.ema(src + (src - src[lag]), length)
volatility = ta.highest(ta.atr(length), length * 3) * mult

var trend  = 0
if ta.crossover(close, zlema + volatility)
    trend := 1
if ta.crossunder(close, zlema - volatility)
    trend := -1

atrValue = ta.atr(atrLength)
emaExit  = ta.ema(close, emaExitLength)

var float stopLossLongLevel = na
var float profitTargetLongLevel = na
var float stopLossShortLevel = na
var float profitTargetShortLevel = na

var int lastEntryBarIndex = na
var float lastEntryPrice = na

// ✅ Corrected ZLEMA DownTrend Logic: Ensure ZLEMA has decreased for 5 consecutive bars
// ✅ ZLEMA Increasing for 5 Bars → Bullish Trend (Green Triangle Up at the Bottom)
zlemaUpTrend = zlema > zlema[1] and zlema[1] > zlema[2] and zlema[2] > zlema[3] and zlema[3] > zlema[4]

// ✅ ZLEMA Decreasing for 5 Bars → Bearish Trend (Red Triangle Down at the Top)
zlemaDownTrend = zlema < zlema[1] and zlema[1] < zlema[2] and zlema[2] < zlema[3] and zlema[3] < zlema[4]

if ta.crossover(trend, 0) // Long Entry
    stopLossLongLevel := useATRStopTP ? close - (atrValue * atrMultiplier) : zlema - volatility
    profitTargetLongLevel := useProfitTarget ? (close + ((close - stopLossLongLevel) * profitTargetRR)) : na

if ta.crossunder(trend, 0) // Short Entry
    stopLossShortLevel := useATRStopTP ? close + (atrValue * atrMultiplier) : zlema + volatility
    profitTargetShortLevel := useProfitTarget ? (close - ((stopLossShortLevel - close) * profitTargetRR)) : na

// ATR-Based Stop-Loss Levels for Trade Exits
var float atrStopLossLong = na
var float atrStopLossShort = na

if useATRStopTP
    if strategy.position_size > 0  // Long Position
        atrStopLossLong := na(atrStopLossLong) or strategy.position_size[1] == 0 ? close - (atrValue * atrMultiplier) : math.max(nz(atrStopLossLong[1]), close - (atrValue * atrMultiplier))  // Prevents SL from decreasing

    if strategy.position_size < 0  // Short Position
        atrStopLossShort := na(atrStopLossShort) or strategy.position_size[1] == 0 ? close + (atrValue * atrMultiplier) : math.min(nz(atrStopLossShort[1]), close + (atrValue * atrMultiplier))  // Prevents SL from increasing

// ATR-Based Take-Profit Levels
var float atrTakeProfitLong = na
var float atrTakeProfitShort = na

if useATRStopTP
    if strategy.position_size > 0  // Long Position
        atrTakeProfitLong := strategy.position_avg_price + (atrValue * profitATRMultiplier)  // Dynamic TP Based on ATR

    if strategy.position_size < 0  // Short Position
        atrTakeProfitShort := strategy.position_avg_price - (atrValue * profitATRMultiplier)  // Dynamic TP Based on ATR


// ATR Trailing Stop Levels for Trade Exits and Visualization
var float trailStopLongLine = na
var float trailStopShortLine = na

if useTrailingStop
    if strategy.position_size > 0  // Long Position
        trailStopLongLine := na(trailStopLongLine) or strategy.position_size[1] == 0 ? close - (atrValue * trailStopMultiplier) : 
                               math.max(nz(trailStopLongLine[1]), close - (atrValue * trailStopMultiplier))

    if strategy.position_size < 0  // Short Position
        trailStopShortLine := na(trailStopShortLine) or strategy.position_size[1] == 0 ? close + (atrValue * trailStopMultiplier) : 
                               math.min(nz(trailStopShortLine[1]), close + (atrValue * trailStopMultiplier))

// ============================================================
// === 2️⃣ ENTRY & EXIT LOGIC (Conditions for Trading) ===
// ============================================================


// ✅ Entry Conditions for Long Trades
longCondition = ta.crossover(trend, 0) and close > zlema + volatility 
canEnterLong = strategy.position_size == 0 or strategy.position_size[1] < 0  // Allows long entry right after short exit

if useZlemaTrendConfirm
    longCondition := longCondition and zlemaUpTrend  // Ensure ZLEMA Uptrend is true for long entries

// 🔥 Enforce that `longCondition` CANNOT be true if `zlemaUpTrend` is false
if useZlemaTrendConfirm and not zlemaUpTrend
    longCondition := false

// ✅ Entry Conditions for Short Trades
shortCondition = allowShortTrades and ta.crossunder(trend, 0) and close < zlema - volatility
canEnterShort = strategy.position_size == 0 or strategy.position_size[1] > 0  // Allows short entry right after long exit

if useZlemaTrendConfirm
    shortCondition := shortCondition and zlemaDownTrend  // Ensure ZLEMA Downtrend is true for short entries

// ✅ Long Re-Entry Condition: ZLEMA is green, position is flat, price closed below and now above ZLEMA
reEntryLongCondition = reEnterOnLongTrend and
     trend == 1 and
     trend[1] == 1 and  // ✅ Previous bar must also be in bullish trend (prevents triggering on trend change)
     strategy.position_size == 0 and
     close[1] < zlema[1] and
     close > zlema 


// ✅ Debugging: Display `longCondition` and `zlemaUpTrend` values only if the checkbox is checked
if showZlemaDebug and ta.crossover(trend, 0)
    label.new(x=bar_index, y=low, 
              text="LongCondition: " + str.tostring(longCondition) + 
                   "\nZLEMA UpTrend: " + str.tostring(zlemaUpTrend),
              color=color.white, textcolor=color.black, size=size.small, style=label.style_label_down)



// ============================================================
// === 3️⃣ TRADE EXECUTIONS (Entries, ATR SL/TP, Trailing Stop, Trend Exit) ===
// ============================================================

// 📌 Entry Conditions
if (canEnterLong and longCondition) or reEntryLongCondition
    lastEntryBarIndex := bar_index
    lastEntryPrice := close
    
    // 🛠️ Reset all SL/TP levels on Long Entry or Re-Entry
    if reEntryLongCondition
        // Trend-Based SL/TP
        stopLossLongLevel := useATRStopTP ? na : zlema - volatility
        profitTargetLongLevel := useProfitTarget and not useATRStopTP ? (close + ((close - stopLossLongLevel) * profitTargetRR)) : na

        // ATR-Based SL/TP
        atrStopLossLong := useATRStopTP ? close - (atrValue * atrMultiplier) : na
        atrTakeProfitLong := useATRStopTP ? close + (atrValue * profitATRMultiplier) : na

        // Trailing Stop
        trailStopLongLine := useTrailingStop ? close - (atrValue * trailStopMultiplier) : na

    strategy.entry(id="Long", direction=strategy.long, comment=reEntryLongCondition ? "Re-Entry Long" : "Bullish Trend Change")


if canEnterShort and shortCondition
    lastEntryBarIndex := bar_index
    lastEntryPrice := close
    strategy.entry(id="Short", direction=strategy.short, comment="Bearish Trend Change")


// 📌 ATR-Based Stop-Loss Exits (Ensures SL Always Triggers Correctly)
if useATRStopTP and strategy.position_size > 0
    if low <= atrStopLossLong
        strategy.close("Long", comment="ATR SL Hit - Long")

if useATRStopTP and strategy.position_size < 0
    if high >= atrStopLossShort
        strategy.close("Short", comment="ATR SL Hit - Short")

// 📌 ATR-Based Stop-Loss & Take-Profit Exits (Ensures TP Always Executes)
if useATRStopTP and strategy.position_size > 0 and high >= atrTakeProfitLong
    strategy.close("Long", comment="ATR TP Hit - Long")

if useATRStopTP and strategy.position_size < 0 and low <= atrTakeProfitShort
    strategy.close("Short", comment="ATR TP Hit - Short")


// 📌 Profit Target Exits (Ensures TP Always Executes Independently)
if useProfitTarget and strategy.position_size > 0 and high >= profitTargetLongLevel
    strategy.close("Long", comment="Profit Target Hit - Long")

if useProfitTarget and strategy.position_size < 0 and low <= profitTargetShortLevel
    strategy.close("Short", comment="Profit Target Hit - Short")

// 📌 Stop-Loss for Profit Target Trades (Ensures Stop-Loss Works with TP)
if useProfitTarget and strategy.position_size > 0 and low <= stopLossLongLevel
    strategy.close("Long", comment="Stop-Loss Hit - Long")

if useProfitTarget and strategy.position_size < 0 and high >= stopLossShortLevel
    strategy.close("Short", comment="Stop-Loss Hit - Short")

// 📌 ATR Trailing Stop Loss (Ensures It Works Alongside Other Exits)
if useTrailingStop and strategy.position_size > 0 and low <= trailStopLongLine
    strategy.close("Long", comment="Trailing Stop Hit - Long")

if useTrailingStop and strategy.position_size < 0 and high >= trailStopShortLine
    strategy.close("Short", comment="Trailing Stop Hit - Short")

// 📌 Trend-Based Exit (Closes Position When Trend Reverses)
if strategy.position_size > 0 and ta.crossunder(trend, 0)
    strategy.close("Long", comment="Trend Change to Bearish")

if strategy.position_size < 0 and ta.crossover(trend, 0)
    strategy.close("Short", comment="Trend Change to Bullish")

// 📌 Break-Even Stop-Loss (Adjusts Stop to Entry Price at Set Risk-Reward Ratio)
if useBreakEven and strategy.position_size > 0 and high >= strategy.position_avg_price + (strategy.position_avg_price - atrStopLossLong) * breakEvenRR
    atrStopLossLong := strategy.position_avg_price

if useBreakEven and strategy.position_size < 0 and low <= strategy.position_avg_price - (atrStopLossShort - strategy.position_avg_price) * breakEvenRR
    atrStopLossShort := strategy.position_avg_price

// 📌 EMA Exit (Closes Position if Price Crosses EMA)
if useTrendExit and strategy.position_size > 0 and low < emaExit
    strategy.close("Long", comment="Exit on EMA")

if useTrendExit and strategy.position_size < 0 and high > emaExit
    strategy.close("Short", comment="Exit on EMA")



// ============================================================
// === 4️⃣ VISUALIZATIONS (Trend Shading, Stop-Loss, TP, ATR TS) ===
// ============================================================

// 🟢🔴 Restore Bullish/Bearish Trend Shading
zlemaColor = trend == 1 ? color.new(#00ffbb, 70) : color.new(#ff1100, 70)
m = plot(series=zlema, title="Zero Lag Basis", linewidth=2, color=zlemaColor)

upper = plot(zlema + volatility, style=plot.style_linebr, color=color.new(#ff1100, 90), title="Upper Deviation Band")
lower = plot(zlema - volatility, style=plot.style_linebr, color=color.new(#00ffbb, 90), title="Lower Deviation Band")

fill(plot1=m, plot2=upper, color=(trend == -1 ? color.new(#ff1100, 80) : na), title="Bearish Fill")
fill(plot1=m, plot2=lower, color=(trend == 1 ? color.new(#00ffbb, 80) : na), title="Bullish Fill")

// ✅ Plot a lime green TriangleUp at the bottom when ZLEMA has been going UP for 5 bars
plotshape(series=zlemaUpTrend ? low - ta.atr(5) * 0.5 : na, 
          location=location.bottom, style=shape.triangleup, 
          color=color.lime, title="ZLEMA Uptrend Detected")

// ✅ Plot a red TriangleDown at the top when ZLEMA has been going DOWN for 5 bars
plotshape(series=zlemaDownTrend ? high + ta.atr(5) * 0.5 : na, 
          location=location.top, style=shape.triangledown, 
          color=color.red, title="ZLEMA Downtrend Detected")

// 🔴 Default Stop-Loss Lines (Red) - Only Show If ATR SL/TP Is Not Used
plot(series=(not useATRStopTP and strategy.position_size > 0) ? stopLossLongLevel : na, 
     style=plot.style_linebr, color=color.red, linewidth=2, title="Trend Stop-Loss - Long")

plot(series=(not useATRStopTP and strategy.position_size < 0) ? stopLossShortLevel : na, 
     style=plot.style_linebr, color=color.red, linewidth=2, title="Trend Stop-Loss - Short")

// 🟠 ATR Trailing Stop Loss Line (Thin Orange Line) - Only Draw When Checkbox is Checked
plot(series=(useTrailingStop and strategy.position_size > 0) ? trailStopLongLine : na, 
     style=plot.style_linebr, color=color.new(color.orange, 70), linewidth=1, title="ATR Trailing Stop - Long")

plot(series=(useTrailingStop and strategy.position_size < 0) ? trailStopShortLine : na, 
     style=plot.style_linebr, color=color.new(color.orange, 70), linewidth=1, title="ATR Trailing Stop - Short")

// ✅ ATR-Based Stop-Loss Visualization (Ensures Line Stays Until Trade Closes)
plot(series=(useATRStopTP and strategy.position_size > 0) ? atrStopLossLong : na, 
     style=plot.style_linebr, color=color.new(color.red, 50), linewidth=2, title="ATR-Based Stop-Loss - Long")

plot(series=(useATRStopTP and strategy.position_size < 0) ? atrStopLossShort : na, 
     style=plot.style_linebr, color=color.new(color.red, 50), linewidth=2, title="ATR-Based Stop-Loss - Short")

// ✅ ATR-Based Take-Profit Visualization (Ensures Line Stays Until Trade Closes)
plot(series=(useATRStopTP and strategy.position_size > 0) ? atrTakeProfitLong : na, 
     style=plot.style_linebr, color=color.new(color.green, 50), linewidth=2, title="ATR-Based Take-Profit - Long")

plot(series=(useATRStopTP and strategy.position_size < 0) ? atrTakeProfitShort : na, 
     style=plot.style_linebr, color=color.new(color.green, 50), linewidth=2, title="ATR-Based Take-Profit - Short")

// ✅ Standard Profit Target Visualization (Only Active if ATR SL/TP is NOT Used)
plot(series=(useProfitTarget and not useATRStopTP and strategy.position_size > 0) ? profitTargetLongLevel : na, 
     style=plot.style_linebr, color=color.green, linewidth=2, title="Profit Target - Long")

plot(series=(useProfitTarget and not useATRStopTP and strategy.position_size < 0) ? profitTargetShortLevel : na, 
     style=plot.style_linebr, color=color.green, linewidth=2, title="Profit Target - Short")

// === 📋 FLOATING TABLE: STRATEGY PERFORMANCE & SETTINGS ===

// === Track first and last trade times ===
var int firstTradeTime = na
var int lastTradeTime = na

if strategy.opentrades > 0 and na(firstTradeTime)
    firstTradeTime := time

if strategy.closedtrades > 0
    lastTradeTime := time

// === Format trade date strings safely ===
firstDateStr = "N/A"
lastDateStr  = "N/A"

if not na(firstTradeTime)
    firstDateStr := str.tostring(month(firstTradeTime)) + "/" + str.tostring(dayofmonth(firstTradeTime)) + "/" + str.tostring(year(firstTradeTime) % 100)

if not na(lastTradeTime)
    lastDateStr := str.tostring(month(lastTradeTime)) + "/" + str.tostring(dayofmonth(lastTradeTime)) + "/" + str.tostring(year(lastTradeTime) % 100)