কার্নেলের উপর ভিত্তি করে অভিযোজিত ট্রেন্ড ট্র্যাকিং সিস্টেম মসৃণ একাধিক চলমান গড়ের উপর ভিত্তি করে

MA RSI ATR MAs NW 趋势追踪 核平滑 滑动止损
সৃষ্টির তারিখ: 2025-03-28 15:13:28 অবশেষে সংশোধন করুন: 2025-03-28 15:13:28
অনুলিপি: 2 ক্লিকের সংখ্যা: 336
2
ফোকাস
319
অনুসারী

কার্নেলের উপর ভিত্তি করে অভিযোজিত ট্রেন্ড ট্র্যাকিং সিস্টেম মসৃণ একাধিক চলমান গড়ের উপর ভিত্তি করে কার্নেলের উপর ভিত্তি করে অভিযোজিত ট্রেন্ড ট্র্যাকিং সিস্টেম মসৃণ একাধিক চলমান গড়ের উপর ভিত্তি করে

ওভারভিউ

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

এর মূল বৈশিষ্ট্যগুলির মধ্যে রয়েছেঃ পাঁচটি চলমান গড়ের সমান্তরাল ব্যান্ডের ব্যবহার করে বর্তমান বাজারের প্রবণতাকে চিত্রিত করা; আরএসআই ফিল্টার, প্রবণতা শক্তি ফিল্টার এবং প্রবণতা নিশ্চিতকরণের সময়কালের মাধ্যমে গোলমাল এবং মিথ্যা সংকেত হ্রাস করা; নির্দিষ্ট শর্ত পূরণ হলেই কেবল প্রবেশের সংকেত ট্রিগার করা; এবং ঝুঁকি পরিচালনা এবং মুনাফা সুরক্ষার জন্য একাধিক প্রস্থান বিকল্প ব্যবহার করা (যেমন শতাংশ ট্র্যাকিং স্টপ লস, এটিআর ট্র্যাকিং স্টপ লস, এটিআর লাভের লক্ষ্য এবং কঠোরতা স্টপ লস) ।

কৌশল নীতি

এই কৌশলটির মূল যুক্তি নিম্নলিখিত মূল উপাদানগুলির উপর ভিত্তি করে তৈরি করা হয়েছেঃ

  1. পারমাণবিক স্লাইডিং গড়কৌশলঃ স্ট্যান্ডার্ড মুভিং এভারেজের পরিবর্তে কার্নেল মসৃণকরণ প্রযুক্তি ব্যবহার করে, যা প্রচলিত এমএ’র চেয়ে আরও নমনীয় এবং স্বনির্ধারিত মসৃণকরণ সরবরাহ করে। তিনটি ধরণের কার্নেল সমর্থন করেঃ

    • বিটা নিউক্লিয়ারঃ সবচেয়ে শক্তিশালী বিকল্প, যার মাধ্যমেalphaএবংbetaপ্যারামিটারগুলি স্বাধীনভাবে নিয়ন্ত্রণ করা হয়, যা MA-কে মূল্য বৃদ্ধি এবং পতনের জন্য ভিন্নভাবে প্রতিক্রিয়াশীল করে।
    • গ্যাসকুয়েরঃ ঘন্টাধ্বনির ভারসাম্য তৈরি করা।bandwidthপ্যারামিটারগুলি ঘোড়ার আকৃতির বক্ররেখার প্রস্থ নিয়ন্ত্রণ করে।
    • Epanechnikov kernel: একই রকম গসকর্ন এর মত কিন্তু আকৃতি একটু ভিন্ন, একই রকম ব্যবহারbandwidthপ্যারামিটার
  2. সমান্তরাল রেখাপাঁচটি এমএ চার্টের একটি “সমতুল্য রেখা” গঠন করে, যার বিন্যাস এবং আপেক্ষিক অবস্থান প্রবণতা শক্তি এবং দিকনির্দেশের একটি চাক্ষুষ নির্দেশনা প্রদান করে।

  3. ক্রস টেস্টিংকৌশলঃ সমান্তরাল ব্যান্ডে ক্রমাগত এমএ এর মধ্যে ক্রস মনিটরিং, ব্যবহারকারী সম্ভাব্য সংকেত উত্পন্ন করার জন্য প্রয়োজনীয় ক্রস সংখ্যা নির্দিষ্ট করতে পারেন।

  4. RSI ফিল্টার: বাজারের অত্যধিক প্রসারণের সময় প্রবেশ এড়াতে সাহায্য করুন। মাল্টি-হেড প্রবেশের সময়, আরএসআই অবশ্যই ওভারসোলের স্তরের নীচে থাকতে হবে; খালি-হেড প্রবেশের সময়, ওভারসোলের স্তরের উপরে থাকতে হবে।

  5. প্রবণতা শক্তি ফিল্টারট্রেডিং এর ক্ষেত্রে, একটি শক্তিশালী, প্রতিষ্ঠিত ট্রেন্ডের দিকে ট্রেডিং নিশ্চিত করার জন্য একটি চলমান গড়ের আরএসআই ব্যবহার করুন।

  6. প্রবণতা নিশ্চিত: ভুয়া সংকেত আরও কমাতে, প্রবেশের শর্তগুলি (এমএ ক্রস, আরএসআই এবং প্রবণতা শক্তি) কার্যত ট্রেডিংয়ের আগে নির্দিষ্ট সংখ্যক কে লাইনকে ধারাবাহিকভাবে পূরণ করতে হবে।

  7. যুক্তি থেকে বেরিয়ে আসুনকৌশলটি নিম্নলিখিত ক্রম অনুসারে প্রস্থানকে অগ্রাধিকার দেয়ঃ হার্ড স্টপ, ট্র্যাকিং স্টপ (প্রতিশত বা এটিআর ভিত্তিক) এবং লাভ (এটিআর ভিত্তিক) । এটি নিশ্চিত করে যে ক্ষতি কমিয়ে আনা হবে এবং লাভ রক্ষা করা হবে।

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

  1. উচ্চতা কাস্টমাইজড কোর মসৃণতা: কোষের মসৃণতা ব্যবহার (বিশেষত বিটা কোষ) এমএ-র প্রতিক্রিয়াশীলতার জন্য নিয়ন্ত্রণের স্তর সরবরাহ করে যা স্ট্যান্ডার্ড এমএ-তে ব্যবহারযোগ্য নয়। এটি ট্রেন্ড ট্র্যাকিংয়ের জন্য আরও স্বনির্ধারিত এবং সূক্ষ্ম পদ্ধতির অনুমতি দেয়।

  2. প্রবণতার শক্তি এবং নিশ্চিতকরণপ্রবণতা শক্তি ফিল্টার (এমএ এর আরএসআই ব্যবহার করে) এবং প্রবণতা নিশ্চিতকরণ সময়ের সংমিশ্রণ একটি শক্তিশালী ফিল্টারিং প্রক্রিয়া প্রদান করে, যা সহজ এমএ ক্রস বা আরএসআই পাঠের বাইরে চলে যায়। এটি দুর্বল প্রবণতা এবং অস্থিরতা ফিল্টার করতে সহায়তা করে।

  3. একাধিক অগ্রাধিকার অপসারণ বিকল্প: কৌশলটির প্রস্থান লজিকটি অত্যন্ত জটিল, যা স্থির এবং গতিশীল স্টপ লস এবং লাভের স্তরের সমন্বয় সরবরাহ করে। অগ্রাধিকার নিশ্চিত করে যে সর্বাধিক সংরক্ষণশীল প্রস্থান ((কঠোরতা স্টপ লস) প্রথমে ট্রিগার করা হয়, তারপরে স্টপ লস অনুসরণ করা হয় এবং অবশেষে লাভের লক্ষ্য।

  4. সম্পূর্ণ ইনপুট গ্রুপিং: সমস্ত ইনপুট কন্ট্রোল পলিসির নির্দিষ্ট দিকের গোষ্ঠীতে শ্রেণীবদ্ধ করা হয়েছে, যাতে ব্যবহারকারীরা সহজেই ইনপুটগুলিকে দ্রুত সনাক্ত করতে এবং সামঞ্জস্য করতে পারে।

  5. লেনদেনের দিকনির্দেশনাএই কৌশলটি অনেকগুলি কৌশল থেকে ভিন্ন, যা স্বতন্ত্রভাবে মাল্টি-হেড এবং শূন্য-হেড ট্রেডিং সক্ষম বা অক্ষম করতে দেয়।

  6. সর্বস্তরের প্রবণতা ব্যবস্থাএই সূচকটি ব্যবসায়ের জন্য প্রয়োজনীয় বিভিন্ন দিকের সমন্বয় করেঃ প্রবেশের সংকেত, স্টপ লস গণনা, মুনাফা গণনা।

কৌশলগত ঝুঁকি

  1. প্যারামিটার অপ্টিমাইজেশান চ্যালেঞ্জ: কৌশলটিতে প্রচুর পরিমাণে প্যারামিটার রয়েছে, তাই ওভারফিট হওয়ার ঝুঁকি থাকতে পারে। প্যারামিটারগুলিকে খুব সূক্ষ্মভাবে সামঞ্জস্য করার ফলে কৌশলটি ফিটনেসে ভাল পারফর্ম করতে পারে, তবে প্রকৃত লেনদেনের ক্ষেত্রে এটি ব্যর্থ হয়। প্যারামিটার সেটিংটি সর্বজনীন কিনা তা নিশ্চিত করার জন্য একটি শক্তিশালী ক্রস যাচাইকরণ এবং নমুনা পরীক্ষা করার পরামর্শ দেওয়া হয়।

  2. প্রবণতা পরিবর্তনের প্রতিক্রিয়া বিলম্বিত: যদিও কৌশলটি ধারাবাহিক প্রবণতা সনাক্ত করার জন্য তৈরি করা হয়েছে, তবে বাজারের তীব্র বিপরীতমুখী হওয়ার সময় এটি দ্রুত প্রতিক্রিয়া জানাতে পারে না, যার ফলে আংশিক প্রত্যাহারের কারণ হতে পারে। প্রবণতা পরিবর্তনের সংবেদনশীলতা এবং গোলমাল ফিল্টার করার ক্ষমতা সামঞ্জস্য করতে পারে এমএ দৈর্ঘ্য এবং মূল প্যারামিটারগুলি সামঞ্জস্য করে।

  3. MA ক্রস ফালস সিগন্যাল: একাধিক স্তর ফিল্টার থাকা সত্ত্বেও, অস্থির বাজারে ভুয়া সংকেত তৈরি হতে পারে। এই কৌশলটি নির্দিষ্ট ট্রেন্ডিং বাজারে ব্যবহার করার পরামর্শ দেওয়া হয়, বা ভুয়া সংকেত হ্রাস করার জন্য ট্রেন্ডিং নিশ্চিতকরণ সময় বাড়ানো হয়।

  4. অকাল ব্রেকিং: বড় অস্থির বাজারে, স্টপ ক্ষতির কারণ হতে পারে, যার ফলে পরবর্তী মূল্যের পুনরুদ্ধার এবং প্রবণতা পুনরুদ্ধার করা যায়। এটিআর-ভিত্তিক স্টপ বিবেচনা করা যেতে পারে এবং বাজারের অস্থিরতার সাথে সামঞ্জস্য রেখে যথাযথভাবে সামঞ্জস্য করা যেতে পারে।

  5. জটিলতার ঝুঁকিনীতির জটিলতা ত্রুটি নিরসন এবং রিয়েল-টাইম মনিটরিংকে কঠিন করে তুলতে পারে। এটি একটি সহজ কনফিগারেশন দিয়ে শুরু করার পরামর্শ দেওয়া হয়, ধীরে ধীরে জটিল বৈশিষ্ট্য যুক্ত করা হয় যাতে প্রতিটি উপাদানটির ভূমিকা পুরোপুরি বোঝা যায়।

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

  1. টাইম ফ্রেম অভিযোজনযোগ্যতা: বর্তমান কৌশলটি আরও উন্নত করা যেতে পারে যাতে এটি বিভিন্ন সময় ফ্রেমের উপর ভিত্তি করে প্যারামিটারগুলিকে স্বয়ংক্রিয়ভাবে সামঞ্জস্য করতে পারে। উদাহরণস্বরূপ, সময় ফ্রেমের উপর ভিত্তি করে স্বয়ংক্রিয় প্যারামিটার সামঞ্জস্যের বৈশিষ্ট্য যুক্ত করা যেতে পারে, যাতে নীতিটি কার্যকরভাবে দিনরেখা, ঘন্টারেখা বা মিনিটরেখা চার্টগুলিতে কাজ করতে পারে।

  2. বাজার পরিবেশে পরীক্ষা: বাজার পরিবেশে স্বয়ংক্রিয়ভাবে সনাক্তকরণ ব্যবস্থা যোগ করুন (যেমন ট্রেন্ডিং, ব্যাপ্তি বা উচ্চ অস্থিরতা) এবং সনাক্তকরণের ফলাফল অনুযায়ী লেনদেনের পরামিতিগুলিকে সামঞ্জস্য করুন। যেমন, ব্যাপ্তি বাজারগুলিতে ফিল্টারিংয়ের তীব্রতা বাড়ানো বা মুনাফা অর্জনের লক্ষ্যমাত্রা সংশোধন করা, ট্রেন্ডিং বাজারগুলিতে ফিল্টারিংয়ের শর্তগুলি শিথিল করা।

  3. ডায়নামিক আরএসআইRSI-এর ওভার-বই ওভার-সেল থ্রেশহোল্ডটি গতিশীল, স্থির নয়, এবং সাম্প্রতিক বাজারের অস্থিরতার সাথে স্বয়ংক্রিয়ভাবে সামঞ্জস্যপূর্ণ। এটি বিভিন্ন বাজারের অবস্থার সাথে কৌশলটির অভিযোজনযোগ্যতা উন্নত করতে পারে।

  4. ইন্টিগ্রেটেড কোয়ান্টামাইজড ওভারল্যাপিং ইন্ডিকেটর: কৌশলকে ওলট-পালট নির্দেশক (যেমন বোলিংগার ব্যান্ডউইথ) এর সাথে একত্রিত করা যাতে উচ্চ ওলট-পালট পরিবেশে স্টপ-ড্রপ এবং লাভের লক্ষ্যগুলিকে সমন্বয় করা যায় এবং কার্যকর প্রবণতাগুলিকে ছুঁড়ে ফেলার ঝুঁকি হ্রাস করা যায়।

  5. মাল্টি টাইম ফ্রেম নিশ্চিতকরণ: ট্রেডিংয়ের দিকটি বৃহত্তর প্রবণতার সাথে সামঞ্জস্যপূর্ণ তা নিশ্চিত করার জন্য একটি উচ্চতর সময়সীমার ট্রেডিং নিশ্চিতকরণ যুক্ত করুন। উদাহরণস্বরূপ, কেবলমাত্র যখন দিনের লাইনের প্রবণতা ঘন্টা লাইনের প্রবণতার সাথে সামঞ্জস্যপূর্ণ হয় তখনই ট্রেড করুন।

  6. পারফরম্যান্স মনিটরিং এবং অভিযোজন: কৌশলগত পারফরম্যান্সের জন্য একটি রিয়েল-টাইম মনিটরিং সিস্টেম, বিজয় হার, লাভ-ক্ষতি অনুপাত এবং সর্বাধিক প্রত্যাহারের মতো সূচকগুলি ট্র্যাক করে, যখন পারফরম্যান্স সূচকগুলি ডিফল্ট থ্রেশহোল্ডের নীচে নেমে আসে তখন স্বয়ংক্রিয়ভাবে প্যারামিটারগুলি সামঞ্জস্য করে বা লেনদেন স্থগিত করে।

  7. মেশিন লার্নিং: মেশিন লার্নিং অ্যালগরিদমকে প্যারামিটার অপ্টিমাইজেশনের মধ্যে অন্তর্ভুক্ত করার অন্বেষণ, যা কৌশলগুলিকে ঐতিহাসিক তথ্য থেকে সর্বোত্তম প্যারামিটার সমন্বয় শিখতে এবং নতুন তথ্যের সাথে ক্রমাগত উন্নতি করতে সক্ষম করে।

সারসংক্ষেপ

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

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

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

কৌশল সোর্স কোড
/*backtest
start: 2024-03-28 00:00:00
end: 2025-03-27 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
*/

//@version=5
strategy("B4100 - NW Trend Ribbon Strategy", overlay=true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100, commission_type = strategy.commission.percent, commission_value = 0.02)

// === Optimized Functions ===
f_calculate_beta_kernel(length, alpha, beta) =>
    kernel = array.new_float(length, 0)
    sum = 0.0
    for i = 0 to length - 1
        x = i / (length - 1)
        w = math.pow(x, alpha - 1) * math.pow(1 - x, beta - 1)
        array.set(kernel, i, w)
        sum += w
    for i = 0 to length - 1
        array.set(kernel, i, array.get(kernel, i) / sum)
    kernel

f_calculate_gaussian_kernel(length, bandwidth) =>
    kernel = array.new_float(length, 0)
    sum = 0.0
    for i = 0 to length - 1
        x = i / (length - 1)
        w = math.exp(-0.5 * math.pow((x - 0.5) / bandwidth, 2))
        array.set(kernel, i, w)
        sum += w
    for i = 0 to length - 1
        array.set(kernel, i, array.get(kernel, i) / sum)
    kernel

f_calculate_epanechnikov_kernel(length, bandwidth) =>
    kernel = array.new_float(length, 0)
    sum = 0.0
    for i = 0 to length - 1
        x = i / (length - 1)
        w = math.max(0.0, 1 - math.pow((x - 0.5) / bandwidth, 2))
        array.set(kernel, i, w)
        sum += w
    for i = 0 to length - 1
        array.set(kernel, i, array.get(kernel, i) / sum)
    kernel

f_apply_kernel_ma(src, kernel, length) =>
    sum = 0.0
    for i = 0 to length - 1
        sum += src[i] * array.get(kernel, i)
    sum

f_trend_strength(ma, length) =>
    ts = ta.rsi(ma, length) / 100
    ts

// === Inputs ===
src = input.source(close, title="Price Source", tooltip="Select the price data used for calculations.  'Close' is the most common, but you can also use 'Open', 'High', 'Low', 'HL2' (typical price), etc.")

// MA Parameters
maGroup = "Moving Average Settings"
maCrossoverGroup = "Moving Average Crossover Settings"
rsiFilterGroup = "RSI Filter Settings"
trendStrengthGroup = "Trend Strength Filter Settings"
trendConfirmGroup = "Trend Confirmation Settings"
trailingStopGroup = "Trailing Stop Settings"
atrTrailingStopGroup = "ATR Trailing Stop Settings"
atrTakeProfitGroup = "ATR Take Profit Settings"
hardStopGroup = "Hard Stop Loss Settings"
tradeDirectionGroup = "Trade Direction Control"

length1 = input.int(20, title="MA1 Length", minval=1, tooltip="Number of bars used to calculate the first Moving Average.", group=maGroup)
kernelType1 = input.string(title="MA1 Kernel Type", defval="Beta", options=["Beta", "Gaussian", "Epanechnikov"], tooltip="Select the type of smoothing kernel for MA1.  'Beta' allows for lag adjustment. 'Gaussian' and 'Epanechnikov' use a bandwidth.", group=maGroup)
alpha1  = input.float(3.0, title="MA1 Beta Kernel +Lag", minval=1, maxval=10, tooltip="For Beta kernel only: Higher values increase *positive* lag (MA reacts *slower* to price increases).", group=maGroup)
beta1   = input.float(3.0, title="MA1 Beta Kernel -Lag", minval=1, maxval=10, tooltip="For Beta kernel only: Higher values increase *negative* lag (MA reacts *slower* to price decreases).", group=maGroup)
bandwidth1 = input.float(0.3, title="MA1 Bandwidth", minval=0.1, maxval=10.0, tooltip="For Gaussian/Epanechnikov kernels:  Smaller values create a *tighter* fit to the price (more sensitive). Larger values create a *smoother*, less sensitive MA.", group=maGroup)

length2 = input.int(100, title="MA2 Length", minval=1, tooltip="Number of bars for the second Moving Average.", group=maGroup)
kernelType2 = input.string(title="MA2 Kernel Type", defval="Gaussian", options=["Beta", "Gaussian", "Epanechnikov"], tooltip="Kernel type for MA2 (see MA1 Kernel Type for details).", group=maGroup)
alpha2  = input.float(3.0, title="MA2 Beta Kernel +Lag", minval=1, maxval=10, tooltip="Beta kernel positive lag for MA2 (see MA1 Beta Kernel +Lag for details).", group=maGroup)
beta2   = input.float(3.0, title="MA2 Beta Kernel -Lag", minval=1, maxval=10, tooltip="Beta kernel negative lag for MA2 (see MA1 Beta Kernel -Lag for details).", group=maGroup)
bandwidth2 = input.float(0.3, title="MA2 Bandwidth", minval=0.1, maxval=10.0, tooltip="Bandwidth for MA2 (see MA1 Bandwidth for details).", group=maGroup)

length3 = input.int(150, title="MA3 Length", minval=1, tooltip="Number of bars for the third Moving Average.", group=maGroup)
kernelType3 = input.string(title="MA3 Kernel Type", defval="Epanechnikov", options=["Beta", "Gaussian", "Epanechnikov"], tooltip="Kernel type for MA3.", group=maGroup)
alpha3  = input.float(3.0, title="MA3 Beta Kernel +Lag", minval=1, maxval=10, tooltip="Beta kernel positive lag for MA3.", group=maGroup)
beta3   = input.float(3.0, title="MA3 Beta Kernel -Lag", minval=1, maxval=10, tooltip="Beta kernel negative lag for MA3.", group=maGroup)
bandwidth3 = input.float(0.3, title="MA3 Bandwidth", minval=0.1, maxval=10.0, tooltip="Bandwidth for MA3.", group=maGroup)

length4 = input.int(200, title="MA4 Length", minval=1, tooltip="Number of bars for the fourth Moving Average.", group=maGroup)
kernelType4 = input.string(title="MA4 Kernel Type", defval="Beta", options=["Beta", "Gaussian", "Epanechnikov"], tooltip="Kernel type for MA4.", group=maGroup)
alpha4  = input.float(3.0, title="MA4 Beta Kernel +Lag", minval=1, maxval=10, tooltip="Beta kernel positive lag for MA4.", group=maGroup)
beta4   = input.float(3.0, title="MA4 Beta Kernel -Lag", minval=1, maxval=10, tooltip="Beta kernel negative lag for MA4.", group=maGroup)
bandwidth4 = input.float(0.3, title="MA4 Bandwidth", minval=0.1, maxval=10.0, tooltip="Bandwidth for MA4.", group=maGroup)

length5 = input.int(250, title="MA5 Length", minval=1, tooltip="Number of bars for the fifth Moving Average.", group=maGroup)
kernelType5 = input.string(title="MA5 Kernel Type", defval="Gaussian", options=["Beta", "Gaussian", "Epanechnikov"], tooltip="Kernel type for MA5.", group=maGroup)
alpha5  = input.float(3.0, title="MA5 Beta Kernel +Lag", minval=1, maxval=10, tooltip="Beta kernel positive lag for MA5.", group=maGroup)
beta5   = input.float(3.0, title="MA5 Beta Kernel -Lag", minval=1, maxval=10, tooltip="Beta kernel negative lag for MA5.", group=maGroup)
bandwidth5 = input.float(0.3, title="MA5 Bandwidth", minval=0.1, maxval=10.0, tooltip="Bandwidth for MA5.", group=maGroup)

// Entry Logic
maCrossoversRequired = input.int(3, title="MA Crossovers Required", minval=1, maxval=5, tooltip="How many moving averages must cross each other to generate a potential trade signal.  A higher number means a stronger (but potentially later) signal.", group=maCrossoverGroup)
useRsiFilter         = input.bool(true, title="Use RSI Filter", tooltip="If enabled, the RSI must also be in overbought/oversold territory for a signal to be valid.", group=rsiFilterGroup)
rsiLength           = input.int(7, title="RSI Length", minval=2, tooltip="Number of bars used to calculate the RSI.", group=rsiFilterGroup)
rsiOverbought       = input.int(60, title="RSI Overbought", minval=50, maxval=100, tooltip="RSI level considered overbought (for short entries).", group=rsiFilterGroup)
rsiOversold         = input.int(40, title="RSI Oversold", minval=0, maxval=50, tooltip="RSI level considered oversold (for long entries).", group=rsiFilterGroup)

// Trend Strength Filter
useTrendStrengthFilter = input.bool(true, title="Use Trend Strength Filter", tooltip="If enabled, the trend strength (measured by the RSI of a selected MA) must be above/below a threshold.", group=trendStrengthGroup)
trendStrengthLength   = input.int(7, title="Trend Strength Length", minval=1, tooltip="Number of bars for the trend strength calculation (RSI of the selected MA).", group=trendStrengthGroup)
trendStrengthMa       = input.int(1, title="Trend Strength MA", minval=1, maxval=5, tooltip="Which moving average (1-5) to use for calculating trend strength. 1 = MA1, 2 = MA2, etc.", group=trendStrengthGroup)
minTrendStrength     = input.float(0.5, title="Min Trend Strength (Longs)", minval=0.0, maxval=1.0, step=0.01, tooltip="Minimum trend strength (0.0 - 1.0) required for long entries. 0.5 means the selected MA's RSI must be above 50.", group=trendStrengthGroup)
maxTrendStrength     = input.float(0.5, title="Max Trend Strength (Shorts)", minval=0.0, maxval=1.0, step=0.01, tooltip="Maximum trend strength (0.0 - 1.0) required for short entries. 0.5 means the selected MA's RSI must be below 50.", group=trendStrengthGroup)

// Trend Confirmation
trendConfirmationPeriod = input.int(4, title="Trend Confirmation Period", minval=1, tooltip="Number of consecutive bars the entry conditions must be met before a trade is taken. This helps filter out false signals.", group=trendConfirmGroup)


// Exit Logic
useTrailingStop = input.bool(true, title="Use Percentage Trailing Stop", tooltip="Enable a percentage-based trailing stop loss.", group=trailingStopGroup)
trailingStopActivationPercent = input.float(2.0, title="Activation (%)", minval=0.1, step=0.1, tooltip="Percentage above/below the entry price at which the trailing stop activates.", group=trailingStopGroup) / 100
trailingStopOffsetPercent     = input.float(1.0, title="Offset (%)", minval=0.1, step=0.1, tooltip="Percentage offset from the highest/lowest price reached since entry. This determines how tightly the stop trails the price.", group=trailingStopGroup) / 100

useAtrTrailingStop    = input.bool(true, title="Use ATR Trailing Stop", tooltip="Enable a trailing stop based on the Average True Range (ATR).", group=atrTrailingStopGroup)
atrTrailingStopLength = input.int(1, title="ATR Length", minval=1, tooltip="Number of bars used to calculate the ATR.", group=atrTrailingStopGroup)
atrTrailingStopMult   = input.float(200.0, title="ATR Multiplier", minval=0.1, tooltip="Multiplier for the ATR value.  A larger multiplier creates a wider stop.", group=atrTrailingStopGroup)

useAtrTakeProfit              = input.bool(false, title="Use ATR Take Profit", tooltip="Enable a take profit level based on the Average True Range (ATR).", group=atrTakeProfitGroup)
atrTakeProfitLength           = input.int(14, title="ATR Length", minval=1, tooltip="Number of bars used to calculate the ATR for take profit.", group=atrTakeProfitGroup)
atrTakeProfitMultiplier       = input.float(3.0, title="ATR Multiplier", minval=0.1, tooltip="Multiplier for the ATR value. A larger multiplier sets a further take profit target.", group=atrTakeProfitGroup)

useHardStopLoss     = input.bool(false, title="Use Hard Stop Loss", tooltip="Enable a fixed stop loss.", group=hardStopGroup)
hardStopLossPercent = input.float(0.0, title="Hard Stop Loss (%)", minval=0.0, step=0.1, tooltip="Percentage below (long) or above (short) the entry price for the hard stop loss.", group=hardStopGroup) / 100
useAtrHardStopLoss  = input.bool(false, title="Use ATR Hard Stop Loss", tooltip="Use ATR to calculate hard stop loss", group=hardStopGroup)
atrHardStopLossLength = input.int(14, title="ATR Hard Stop Loss Length", minval=1, tooltip="Length of the ATR for the hard stop loss", group=hardStopGroup)
atrHardStopLossMult   = input.float(1.5, title="ATR Hard Stop Loss Multiplier", minval=0.1, tooltip="Multiplier of ATR for the hard stop loss", group=hardStopGroup)

// *** Trade Direction Control ***
enableLongs  = input.bool(true, title="Enable Long Trades", group=tradeDirectionGroup)
enableShorts = input.bool(true, title="Enable Short Trades", group=tradeDirectionGroup)

// === Pre-calculate kernels (do this only once) ===
var kernel1 = array.new_float(length1, 0.0)
var kernel2 = array.new_float(length2, 0.0)
var kernel3 = array.new_float(length3, 0.0)
var kernel4 = array.new_float(length4, 0.0)
var kernel5 = array.new_float(length5, 0.0)

if barstate.isfirst
    if kernelType1 == "Beta"
        kernel1 := f_calculate_beta_kernel(length1, alpha1, beta1)
    else if kernelType1 == "Gaussian"
        kernel1 := f_calculate_gaussian_kernel(length1, bandwidth1)
    else // Epanechnikov
        kernel1 := f_calculate_epanechnikov_kernel(length1, bandwidth1)

    if kernelType2 == "Beta"
        kernel2 := f_calculate_beta_kernel(length2, alpha2, beta2)
    else if kernelType2 == "Gaussian"
        kernel2 := f_calculate_gaussian_kernel(length2, bandwidth2)
    else // Epanechnikov
        kernel2 := f_calculate_epanechnikov_kernel(length2, bandwidth2)

    if kernelType3 == "Beta"
        kernel3 := f_calculate_beta_kernel(length3, alpha3, beta3)
    else if kernelType3 == "Gaussian"
        kernel3 := f_calculate_gaussian_kernel(length3, bandwidth3)
    else // Epanechnikov
        kernel3 := f_calculate_epanechnikov_kernel(length3, bandwidth3)

    if kernelType4 == "Beta"
        kernel4 := f_calculate_beta_kernel(length4, alpha4, beta4)
    else if kernelType4 == "Gaussian"
        kernel4 := f_calculate_gaussian_kernel(length4, bandwidth4)
    else // Epanechnikov
        kernel4 := f_calculate_epanechnikov_kernel(length4, bandwidth4)

    if kernelType5 == "Beta"
        kernel5 := f_calculate_beta_kernel(length5, alpha5, beta5)
    else if kernelType5 == "Gaussian"
        kernel5 := f_calculate_gaussian_kernel(length5, bandwidth5)
    else // Epanechnikov
        kernel5 := f_calculate_epanechnikov_kernel(length5, bandwidth5)

// === Apply pre-calculated kernels to data ===
nw_ma1 = f_apply_kernel_ma(src, kernel1, length1)
nw_ma2 = f_apply_kernel_ma(src, kernel2, length2)
nw_ma3 = f_apply_kernel_ma(src, kernel3, length3)
nw_ma4 = f_apply_kernel_ma(src, kernel4, length4)
nw_ma5 = f_apply_kernel_ma(src, kernel5, length5)

// MA Array for easier iteration
ma_array = array.new_float(5)
array.set(ma_array, 0, nw_ma1)
array.set(ma_array, 1, nw_ma2)
array.set(ma_array, 2, nw_ma3)
array.set(ma_array, 3, nw_ma4)
array.set(ma_array, 4, nw_ma5)

// Calculate ATR values *unconditionally*
atrTrailingValue = ta.atr(atrTrailingStopLength)
atrTakeProfitValue = ta.atr(atrTakeProfitLength)
atrHardStopLossValue = ta.atr(atrHardStopLossLength)

// Calculate Trend Strength *unconditionally* (and only once)
trendStrengthValue = useTrendStrengthFilter ? f_trend_strength(array.get(ma_array, trendStrengthMa - 1), trendStrengthLength) : 0.0

// === Entry Logic ===

// MA Crossovers
longMaCrossovers  = 0
shortMaCrossovers = 0

for i = 0 to 3
    if array.get(ma_array, i) > array.get(ma_array, i + 1)
        longMaCrossovers  := longMaCrossovers  + 1
    if array.get(ma_array, i) < array.get(ma_array, i + 1)
        shortMaCrossovers := shortMaCrossovers + 1

longCrossoverCondition  = longMaCrossovers  >= maCrossoversRequired
shortCrossoverCondition = shortMaCrossovers >= maCrossoversRequired

// RSI Filter
rsiValue = ta.rsi(src, rsiLength)
longRsiCondition  = not useRsiFilter or (rsiValue < rsiOversold)
shortRsiCondition = not useRsiFilter or (rsiValue > rsiOverbought)

// Trend Strength Filter - Simplified Logic
longTrendStrengthCondition  = not useTrendStrengthFilter or trendStrengthValue >= minTrendStrength
shortTrendStrengthCondition = not useTrendStrengthFilter or trendStrengthValue <= maxTrendStrength


// --- Trend Confirmation Logic ---
var int long_confirm_count = 0
var int short_confirm_count = 0
var bool confirmedLong = false
var bool confirmedShort = false

// Update confirmation counters
if longCrossoverCondition and longRsiCondition and longTrendStrengthCondition
    long_confirm_count := long_confirm_count + 1
    short_confirm_count := 0  // Reset opposite counter
else
    long_confirm_count := 0

if shortCrossoverCondition and shortRsiCondition and shortTrendStrengthCondition
    short_confirm_count := short_confirm_count + 1
    long_confirm_count := 0 // Reset opposite counter
else
    short_confirm_count := 0

// Check for confirmed trend
confirmedLong := long_confirm_count >= trendConfirmationPeriod
confirmedShort := short_confirm_count >= trendConfirmationPeriod

// Combined Entry Conditions (using confirmed trend)
longCondition = confirmedLong  and enableLongs // Added trade direction check
shortCondition = confirmedShort and enableShorts // Added trade direction check

// === Exit Logic ===
var float longTrail = na
var float shortTrail = na
var float longTakeProfitPrice = na
var float shortTakeProfitPrice = na
var float longHardStopLossPrice = na
var float shortHardStopLossPrice = na

// Hard Stop Loss and Take Profit calculation on entry
if longCondition or shortCondition
    // Calculate Hard Stop Loss
    if useHardStopLoss
        if useAtrHardStopLoss
            longHardStopLossPrice  := close - (atrHardStopLossValue * atrHardStopLossMult)
            shortHardStopLossPrice := close + (atrHardStopLossValue * atrHardStopLossMult)
        else
            longHardStopLossPrice  := close * (1 - hardStopLossPercent)
            shortHardStopLossPrice := close * (1 + hardStopLossPercent)
    else
        longHardStopLossPrice := na
        shortHardStopLossPrice := na

    // Calculate Take Profit
    if useAtrTakeProfit
        longTakeProfitPrice  := close + (atrTakeProfitValue * atrTakeProfitMultiplier)
        shortTakeProfitPrice := close - (atrTakeProfitValue * atrTakeProfitMultiplier)
    else
        longTakeProfitPrice := na
        shortTakeProfitPrice := na

// Trailing Stop Logic - updated for each bar
if strategy.position_size > 0
    // Calculate trailing stop
    float tempTrail = na

    if useTrailingStop
        if close > strategy.position_avg_price * (1 + trailingStopActivationPercent)
            tempTrail := close * (1 - trailingStopOffsetPercent)
            if na(longTrail) or tempTrail > longTrail
                longTrail := tempTrail

    if useAtrTrailingStop
        float atrTrail = close - (atrTrailingValue * atrTrailingStopMult)
        if na(longTrail) or atrTrail > longTrail
            longTrail := atrTrail

if strategy.position_size < 0
    // Calculate trailing stop
    float tempTrail = na

    if useTrailingStop
        if close < strategy.position_avg_price * (1 - trailingStopActivationPercent)
            tempTrail := close * (1 + trailingStopOffsetPercent)
            if na(shortTrail) or tempTrail < shortTrail
                shortTrail := tempTrail

    if useAtrTrailingStop
        float atrTrail = close + (atrTrailingValue * atrTrailingStopMult)
        if na(shortTrail) or atrTrail < shortTrail
            shortTrail := atrTrail

// === Strategy Execution ===
if longCondition
    strategy.entry("Long", strategy.long)
    longTrail := na  // Reset on new entry
    shortTrail := na // Reset on new entry

if shortCondition
    strategy.entry("Short", strategy.short)
    shortTrail := na // Reset on new entry
    longTrail := na  // Reset on new entry

// Unified exit logic with proper ordering
if strategy.position_size > 0
    // Define effective stop level (combining hard stop and trailing stop)
    float effectiveStopLevel = na

    if not na(longHardStopLossPrice) and useHardStopLoss
        effectiveStopLevel := longHardStopLossPrice

    if not na(longTrail) and (useTrailingStop or useAtrTrailingStop)
        if na(effectiveStopLevel) or longTrail > effectiveStopLevel
            effectiveStopLevel := longTrail

    // Combined exit strategy with proper parameters
    strategy.exit("Long Exit", "Long",
                 limit = useAtrTakeProfit ? longTakeProfitPrice : na,
                 stop = effectiveStopLevel)

if strategy.position_size < 0
    // Define effective stop level (combining hard stop and trailing stop)
    float effectiveStopLevel = na

    if not na(shortHardStopLossPrice) and useHardStopLoss
        effectiveStopLevel := shortHardStopLossPrice

    if not na(shortTrail) and (useTrailingStop or useAtrTrailingStop)
        if na(effectiveStopLevel) or shortTrail < effectiveStopLevel
            effectiveStopLevel := shortTrail

    // Combined exit strategy with proper parameters
    strategy.exit("Short Exit", "Short",
                 limit = useAtrTakeProfit ? shortTakeProfitPrice : na,
                 stop = effectiveStopLevel)

// === Plotting ===
plotColorMa1 = nw_ma1 > nw_ma1[1] ? color.rgb(100, 250, 120) : color.rgb(255, 100, 120)
plotColorMa2 = nw_ma2 > nw_ma2[1] ? color.rgb(100, 250, 120) : color.rgb(255, 100, 120)
plotColorMa3 = nw_ma3 > nw_ma3[1] ? color.rgb(100, 250, 120) : color.rgb(255, 100, 120)
plotColorMa4 = nw_ma4 > nw_ma4[1] ? color.rgb(100, 250, 120) : color.rgb(255, 100, 120)
plotColorMa5 = nw_ma5 > nw_ma5[1] ? color.rgb(100, 250, 120) : color.rgb(255, 100, 120)

plot(nw_ma1, title="NW MA 1", color=plotColorMa1, linewidth=2)
plot(nw_ma2, title="NW MA 2", color=plotColorMa2, linewidth=2)
plot(nw_ma3, title="NW MA 3", color=plotColorMa3, linewidth=2)
plot(nw_ma4, title="NW MA 4", color=plotColorMa4, linewidth=2)
plot(nw_ma5, title="NW MA 5", color=plotColorMa5, linewidth=2)