মাল্টি-ইনডেক্স ওয়েটেড ইন্টেলিজেন্ট ট্রেডিং কৌশল

MACD RSI EMA SRSI ATR SMA TP SL
সৃষ্টির তারিখ: 2025-02-25 11:29:23 অবশেষে সংশোধন করুন: 2025-02-25 11:29:23
অনুলিপি: 0 ক্লিকের সংখ্যা: 582
2
ফোকাস
319
অনুসারী

মাল্টি-ইনডেক্স ওয়েটেড ইন্টেলিজেন্ট ট্রেডিং কৌশল মাল্টি-ইনডেক্স ওয়েটেড ইন্টেলিজেন্ট ট্রেডিং কৌশল

ওভারভিউ

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

কৌশল নীতি

এই কৌশলটির কেন্দ্রবিন্দু হল তার ওজনযুক্ত সংকেত ব্যবস্থা, যা পাঁচটি ভিন্ন উপ-কৌশল দ্বারা ট্রেডিং সংকেত উৎপন্ন করেঃ

  1. ম্যাকড কৌশল: MACD লাইন এবং সিগন্যাল লাইনের ক্রস ব্যবহার করে বাজার প্রবণতা দিক নির্ধারণ করুন। যখন MACD লাইনে সিগন্যাল লাইনটি অতিক্রম করা হয় তখন একটি ক্রয় সংকেত উত্পন্ন হয় এবং যখন এটি অতিক্রম করা হয় তখন একটি বিক্রয় সংকেত উত্পন্ন হয়।

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

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

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

  5. চলমান গড় ক্রসওভার কৌশল: বাজারের প্রবণতা নির্ধারণের জন্য দুটি ভিন্ন সময়ের চলমান গড়ের ক্রস ব্যবহার করুন। দীর্ঘমেয়াদী গড় লাইন অতিক্রম করার সময় একটি ক্রয় সংকেত উত্পন্ন হয় এবং এটি অতিক্রম করার সময় একটি বিক্রয় সংকেত উত্পন্ন হয়।

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

এই বহুস্তরীয় সংকেত নিশ্চিতকরণ প্রক্রিয়াটি কার্যকরভাবে মিথ্যা সংকেত হ্রাস করে, ট্রেডিং সিস্টেমের নির্ভরযোগ্যতা বাড়ায়, এবং নমনীয় প্যারামিটার সেটিং কৌশলগুলিকে বিভিন্ন ট্রেডিং জাত এবং সময়কালের সাথে খাপ খাইয়ে নিতে দেয়।

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

  1. একাধিক বার্তা নিশ্চিতকরণ: পাঁচটি স্বতন্ত্র প্রযুক্তিগত সূচক দ্বারা উত্পন্ন সংকেতগুলির ওজনযুক্ত গণনা, একক সূচক দ্বারা সম্ভাব্য বিভ্রান্তি হ্রাস করে, ট্রেডিং সংকেতের গুণমান এবং নির্ভরযোগ্যতা বাড়ায়।

  2. স্বনির্ধারিত ওজন ব্যবস্থা: প্রতিটি উপ-কৌশলকে বিভিন্ন ওজনের সাথে ভাগ করা যায়, যা ব্যবসায়ীদের বিভিন্ন সূচকের উপর তাদের আস্থা এবং ঐতিহাসিক পারফরম্যান্সের উপর ভিত্তি করে কৌশলটি সামঞ্জস্য করার জন্য কৌশলটির নমনীয়তা বাড়ায়।

  3. ভাল ঝুঁকি ব্যবস্থাপনা: এই কৌশলটি একাধিক স্তরের ঝুঁকি নিয়ন্ত্রণ ব্যবস্থা নিয়ে গঠিত, যার মধ্যে রয়েছে স্টপ লস, মাল্টি-লেভেল স্টপ লস এবং গতিশীলভাবে স্টপ লস পয়েন্টের সমন্বয়, যাতে বাজারের প্রতিকূল পরিবর্তনের সময় ঝুঁকি দ্রুত নিয়ন্ত্রণ করা যায়।

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

  5. উচ্চতর কাস্টমাইজেশনপ্রায় সব প্যারামিটারই সামঞ্জস্যপূর্ণ, যেমন সূচকগুলির গণনা চক্র, ওজন, স্টপ-অফ-লস শতাংশ ইত্যাদি, যা ব্যবসায়ীদের ব্যক্তিগত স্টাইল এবং বিভিন্ন বাজার অবস্থার সাথে সামঞ্জস্য রেখে কৌশলগুলিকে অনুকূলিত করতে দেয়।

  6. বিল্ড-ইন বিল্ডিং: অকাল প্রবেশ বা গোলমালের উপর ভিত্তি করে লেনদেন এড়াতে, কৌশলটি বিলম্বিত নিশ্চিতকরণ ব্যবস্থা গ্রহণ করে, যা নিশ্চিত করে যে কেবলমাত্র ধারাবাহিক সংকেতই লেনদেনকে ট্রিগার করে, যা স্বল্পমেয়াদী ওঠানামা প্রভাবকে হ্রাস করে।

  7. সময় ফিল্টারকৌশলঃ ট্রেডিং শুরু করার তারিখ সেট করার অনুমতি দেয়, যাতে ব্যবসায়ীরা ঐতিহাসিক তথ্যের উপর ভিত্তি করে নির্দিষ্ট সময়কালের পারফরম্যান্সের দিকে ফিরে যেতে পারে, অথবা পরিচিত বাজার অস্বাভাবিক ওঠানামা সময় এড়াতে পারে।

কৌশলগত ঝুঁকি

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

  2. বাজারের পরিবর্তনের ঝুঁকি: ট্রেন্ডিং বাজার এবং ঝড়ের বাজারে কৌশলটির পারফরম্যান্সের মধ্যে পার্থক্য থাকতে পারে, বাজার অবস্থার হঠাৎ পরিবর্তন কৌশলটির কার্যকারিতা হ্রাস করতে পারে। সমাধানটি হ’ল বাজারের পরিবেশের সনাক্তকরণ ব্যবস্থা প্রবর্তন করা, বিভিন্ন বাজার অবস্থার অধীনে প্যারামিটারগুলি সামঞ্জস্য করা বা বাণিজ্য স্থগিত করা।

  3. সিগন্যাল সংঘর্ষের ঝুঁকিএকসাথে একাধিক সূচক ব্যবহার করা যা পরস্পরবিরোধী সংকেত তৈরি করতে পারে, সিদ্ধান্তের বিভ্রান্তি সৃষ্টি করে। সমাধানটি হল যুক্তিসঙ্গতভাবে প্রতিটি সূচকের ওজন নির্ধারণ করা, আরও নির্ভরযোগ্য সূচককে জোর দেওয়া এবং সংঘাতের সম্ভাবনা কমাতে যুক্তিসঙ্গতভাবে সংকেত থ্রেশহোল্ড সেট করা নিশ্চিত করা।

  4. অর্থের অপব্যবহার: যদিও কৌশলটিতে স্টপ লস ম্যানেজমেন্ট রয়েছে, অর্থের অপ্রয়োজনীয় ব্যবস্থাপনা দ্রুত অর্থের অপচয় করতে পারে। সমাধানটি হ’ল প্রতিটি লেনদেনের জন্য তহবিলের অনুপাতকে কঠোরভাবে নিয়ন্ত্রণ করা এবং একক লেনদেনের সর্বাধিক ঝুঁকি গ্রহণযোগ্যতার মধ্যে রাখা।

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

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

  1. মার্কেটপ্লেস ফিল্টার যোগ করুন: বর্তমান বাজারটি ট্রেন্ডিং বা অস্থিরতা কিনা তা সনাক্ত করতে সক্ষম একটি সূচক বিকাশ করুন, বাজার পরিস্থিতির গতিশীলতার উপর ভিত্তি করে প্রতিটি উপ কৌশলকে ওজন দিন, ট্রেন্ডিং বাজারগুলিতে ট্রেন্ড ট্র্যাকিং কৌশল জোরদার করুন, অস্থির বাজারগুলিতে দোলন কৌশল জোরদার করুন।

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

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

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

  5. আবেগ পরিমাপ যোগ করুন: বাজারের আবেগ সূচক যেমন ভীতি সূচক (VIX), মুদ্রাস্ফীতি ও মুদ্রাস্ফীতির হার ইত্যাদি সংহত করুন, বাজারের চরম আবেগের সময় ট্রেডিং কৌশলটি সামঞ্জস্য করুন বা ট্রেডিং স্থগিত করুন, উচ্চ ওঠানামা চলাকালীন অত্যধিক ট্রেডিং এড়াতে।

  6. ডায়নামিক স্টপ লস ম্যানেজমেন্ট: বাজারের ওঠানামা অনুযায়ী স্বয়ংক্রিয়ভাবে স্টপ-অফ লেভেল সামঞ্জস্য করুন, উচ্চ ওঠানামার বাজারে স্টপ-অফের পরিধি প্রসারিত করুন এবং নিম্ন ওঠানামার বাজারে স্টপ-অফকে আরও কঠোর করুন, যা ঝুঁকি ব্যবস্থাপনাকে আরও নমনীয় এবং কার্যকর করে তোলে।

  7. সময় চক্র অপ্টিমাইজেশান: মাল্টি টাইম সাইকেল অ্যানালিসিস ফাংশন যুক্ত করা হয়েছে, যাতে উচ্চ স্তরের এবং নিম্ন স্তরের টাইম সাইকেলগুলি একই সাথে সংকেত নিশ্চিত করতে পারে, যা মিথ্যা বিরতি এবং মিথ্যা সংকেত হ্রাস করে।

সারসংক্ষেপ

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

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

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

কৌশল সোর্স কোড
/*backtest
start: 2024-09-08 00:00:00
end: 2025-02-23 08:00:00
period: 2d
basePeriod: 2d
exchanges: [{"eid":"Binance","currency":"ETH_USDT"}]
*/

// **********************************************************************************************************************************************************************************************************************************************************************
// Last update: 08/03/2022
// *************************************************************************************************************************************************************************************************************************************************************************
//@version=5
strategy(title='Smart trading', overlay=true, precision=2, commission_value=0.075, commission_type=strategy.commission.percent, initial_capital=1000, currency=currency.USD, default_qty_type=strategy.percent_of_equity, default_qty_value=100, slippage=1,calc_on_every_tick = false,calc_on_order_fills = true)
// *************************************************************************************************************************************************************************************************************************************************************************
// COMMENTS
// *************************************************************************************************************************************************************************************************************************************************************************
chat_id = input("-1001587924564",'Chat ID Telegram')
procent_stop = input.float(1.0,'Процент стоп')
procent_teik = input.float(4.0,'Процент TP4 ')

// *************************************************************************************************************************************************************************************************************************************************************************
// INPUTS
// *************************************************************************************************************************************************************************************************************************************************************************
// * Type trading
allow_longs = input.bool(true, 'Только лонги', group='Trading type')
allow_shorts = input.bool(true, 'Только шорты', group='Trading type')
// * Datastamp
from_day = input.int(1, 'From Day', minval=1, maxval=31, group='DataStamp')
from_month = input.int(1, 'From Month', minval=1, maxval=12, group='DataStamp')
from_year = input.int(2021, 'From Year', minval=1980, maxval=9999, group='DataStamp')
to_day = input.int(1, 'To Day', minval=1, maxval=31, group='DataStamp')
to_month = input.int(1, 'To Month', minval=1, maxval=12, group='DataStamp')
to_year = input.int(9999, 'To Year', minval=2017, maxval=9999, group='DataStamp')
// * Stop loss
stoploss = input.bool(true, 'Стоп лосс в стратегии', group='Stop loss')
movestoploss = input.string('TP-2', 'Перенос стопа', options=['None', 'Percentage', 'TP-1', 'TP-2', 'TP-3'], group='Stop loss')
movestoploss_entry = input.bool(false, 'Перенос стопа на твх', group='Stop loss')
stoploss_perc = input.float(6.0, 'Стоп лосс в %', minval=0, maxval=100, group='Stop loss') * 0.01
move_stoploss_factor = input.float(20.0, 'Фактор переноса стопа в %', group='Stop loss') * 0.01 + 1
stop_source = input.source(hl2, 'Stop Source', group='Stop loss')
// * Take profits
take_profits = input.bool(true, 'Тейк профит в стратегии', group='Take Profits')
// retrade= input.bool(false, 'Retrade', group='Take Profits')
MAX_TP = input.int(6, 'Кол-во TP', minval=1, maxval=10, group='Take Profits')
long_profit_perc = input.float(6.8, 'Long - TP в % каждый', minval=0.0, maxval=999, step=1, group='Take Profits') * 0.01
long_profit_qty = input.float(15, 'Long - TP в % от твх', minval=0.0, maxval=100, step=1, group='Take Profits')
short_profit_perc = input.float(13, 'Short - TP в % каждый', minval=0.0, maxval=999, step=1, group='Take Profits') * 0.01
short_profit_qty = input.float(10, 'Short - TP в % от твх', minval=0.0, maxval=100, step=1, group='Take Profits')
// * Delays
delay_macd = input.int(1, 'Candles delay MACD', minval=1, group='Delays')
delay_srsi = input.int(2, 'Candles delay RSI', minval=1, group='Delays')
delay_rsi = input.int(2, 'Candles delay EMA', minval=1, group='Delays')
delay_super = input.int(1, 'Candles delay Supertrend', minval=1, group='Delays')
delay_cross = input.int(1, 'Candles delay MA', minval=1, group='Delays')
delay_exit = input.int(7, 'Candles delay exit', minval=1, group='Delays')
// * Inputs Smart strategies
str_0 = input.bool(true, 'Strategy 0: Weighted Strategy', group='Weights')
weight_trigger = input.int(2, 'Smart Signal entry [0, 5]', minval=0, maxval=5, step=1, group='Weights')
weight_str1 = input.int(1, 'Smart Strategy 1 [0, 5]', minval=0, maxval=5, step=1, group='Weights')
weight_str2 = input.int(1, 'Smart Strategy 2 [0, 5]', minval=0, maxval=5, step=1, group='Weights')
weight_str3 = input.int(1, 'Smart Strategy 3 [0, 5]', minval=0, maxval=5, step=1, group='Weights')
weight_str4 = input.int(1, 'Smart Strategy 4 [0, 5]', minval=0, maxval=5, step=1, group='Weights')
weight_str5 = input.int(1, 'Smart Strategy 5 [0, 5]', minval=0, maxval=5, step=1, group='Weights')
// * Inputs strategy 1: MACD 
str_1 = input.bool(true, 'Strategy 1: MACD', group='Strategy 1: MACD')
MA1_period_1 = input.int(16, 'MA 1', minval=1, maxval=9999, step=1, group='Strategy 1: MACD')
MA1_type_1 = input.string('EMA', 'MA1 Type', options=['RMA', 'SMA', 'EMA', 'WMA', 'HMA', 'DEMA', 'TEMA', 'VWMA'], group='Strategy 1: MACD')
MA1_source_1 = input.source(hl2, 'MA1 Source', group='Strategy 1: MACD')
MA2_period_1 = input.int(36, 'MA 2', minval=1, maxval=9999, step=1, group='Strategy 1: MACD')
MA2_type_1 = input.string('EMA', 'MA2 Type', options=['RMA', 'SMA', 'EMA', 'WMA', 'HMA', 'DEMA', 'TEMA', 'VWMA'], group='Strategy 1: MACD')
MA2_source_1 = input.source(high, 'MA2 Source', group='Strategy 1: MACD')
// * Inputs strategy 2: RSI oversold/overbought
str_2 = input.bool(true, 'Strategy 2: RSI', group='Strategy 2: RSI')
long_RSI = input.float(70, 'Exit RSI Long (%)', minval=0.0, step=1, group='Strategy 2: RSI')
short_RSI = input.float(27, 'Exit RSI Short (%)', minval=0.0, step=1, group='Strategy 2: RSI')
length_RSI = input.int(14, 'RSI Length', group='Strategy 2: RSI')
length_stoch = input.int(14, 'RSI Stochastic', group='Strategy 2: RSI')
smoothK = input.int(3, 'Smooth', group='Strategy 2: RSI')
// * Inputs strategy 3: EMA oversold/overbought
str_3 = input.bool(true, 'Strategy 3: RSI', group='Strategy 3: RSI')
long_RSI2 = input.float(77, 'Exit EMA Long', minval=0.0, step=1, group='Strategy 3: RSI')
short_RSI2 = input.float(30, 'Exit EMA Short', minval=0.0, step=1, group='Strategy 3: RSI')
// * Inputs strategy 4: Supertrend
str_4 = input.bool(true, 'Strategy 4: Supertrend', group='Strategy 4: Supertrend')
periods_4 = input.int(2, 'ATR Period', group='Strategy 4: Supertrend')
source_4 = input.source(hl2, 'Source', group='Strategy 4: Supertrend')
multiplier = input.float(2.4, 'ATR Multiplier', step=0.1, group='Strategy 4: Supertrend')
change_ATR = input.bool(true, 'Change ATR Calculation Method ?', group='Strategy 4: Supertrend')
// * Inputs strategy 5: MA
str_5 = input.bool(true, 'Strategy 5: MA', group='Strategy 5: MA')
MA1_period_5 = input.int(46, 'MA 1', minval=1, maxval=9999, step=1, group='Strategy 5: MA')
MA1_type_5 = input.string('EMA', 'MA1 Type', options=['RMA', 'SMA', 'EMA', 'WMA', 'HMA', 'DEMA', 'TEMA', 'VWMA'], group='Strategy 5: MA')
MA1_source_5 = input.source(close, 'MA1 Source', group='Strategy 5: MA')
MA2_period_5 = input.int(82, 'MA 2', minval=1, maxval=9999, step=1, group='Strategy 5: MA')
MA2_type_5 = input.string('EMA', 'MA2 Type', options=['RMA', 'SMA', 'EMA', 'WMA', 'HMA', 'DEMA', 'TEMA', 'VWMA'], group='Strategy 5: MA')
MA2_source_5 = input.source(close, 'MA2 Source', group='Strategy 5: MA')
// * Inputs Potential TOP/BOTTOM
str_6 = input.bool(false, 'Потенциальные ордера long/short', group='Potential TOP/BOTTOM')
top_qty = input.float(30, 'Лонг закрыть (%) из оставшейся позиции', minval=0.0, maxval=100, step=1, group='Potential TOP/BOTTOM')
bottom_qty = input.float(30, 'Шорт закрыть (%) из оставшейся позиции', minval=0.0, maxval=100, step=1, group='Potential TOP/BOTTOM') 
source_6_top = input.source(close, 'TP-TOP на предыдущий', group='Potential TOP/BOTTOM')
source_6_bottom = input.source(close, 'TP-BOTTOM на предыдущий', group='Potential TOP/BOTTOM')
long_trail_perc = input.float(150, 'Объем Long (%)', minval=0.0, step=1, group='Potential TOP/BOTTOM') * 0.01
short_trail_perc = input.float(150, 'Объем Short (%)', minval=0.0, step=1, group='Potential TOP/BOTTOM') * 0.01
// * Flags
FLAG_SIGNALS = input.bool(true, 'Show Buy/Sell Signals ?', group='Miscellaneous')
FLAG_SHADOWS = input.bool(true, 'Show shadows satisfied strategies ?', group='Miscellaneous')
// * Alarms
alarm_label_long = input.string('Buy', 'Label open long', group='Basic alarm system')
alarm_label_short = input.string('Sell', 'Label open short', group='Basic alarm system')
alarm_label_close_long = input.string('Close long', 'Label close long', group='Basic alarm system')
alarm_label_close_short = input.string('Close short', 'Label close short', group='Basic alarm system')
alarm_label_TP_long = input.string('TP long', 'Label Take Profit long', group='Basic alarm system')
alarm_label_TP_short = input.string('TP short', 'Label Take Profit short', group='Basic alarm system')
alarm_label_SL = input.string('SL', 'Label Stop-Loss', group='Basic alarm system')
// *************************************************************************************************************************************************************************************************************************************************************************
// ABBREVIATIONS
// *************************************************************************************************************************************************************************************************************************************************************************
// TP: Take profits
// SL: Stop-Loss
// *************************************************************************************************************************************************************************************************************************************************************************
// GLOBAL VARIABLES
// *************************************************************************************************************************************************************************************************************************************************************************
start = timestamp(from_year, from_month, from_day, 00, 00) // backtest start window
end = timestamp(to_year, to_month, to_day, 23, 59)// backtest finish window
var FLAG_FIRST = false
var price_stop_long = 0.
var price_stop_short = 0.
var profit_qty = 0. // Quantity to close per TP from open position
var profit_perc = 0. // Percentage to take profits since open position or last TP
var nextTP = 0. // Next target to take profits
var since_entry = 0 // Number of bars since open last postion
var since_close = 0 // Number of bars since close or TP/STOP last position

// * Compute profit quantity and profit percentage
if strategy.position_size > 0
    profit_qty := long_profit_qty
    profit_perc := long_profit_perc
else if strategy.position_size < 0
    profit_qty := short_profit_qty
    profit_perc := short_profit_perc
else
    nextTP := 0. // Next Take Profit target (out of market)
// *************************************************************************************************************************************************************************************************************************************************************************
// FUNCTIONS
// *************************************************************************************************************************************************************************************************************************************************************************
// * MA type
// *************************************************************************************************************************************************************************************************************************************************************************
ma(MAType, MASource, MAPeriod) =>
    if MAType == 'SMA'
        ta.sma(MASource, MAPeriod)
    else if MAType == 'EMA'
        ta.ema(MASource, MAPeriod)
    else if MAType == 'WMA'
        ta.wma(MASource, MAPeriod)
    else if MAType == 'RMA'
        ta.rma(MASource, MAPeriod)
    else if MAType == 'HMA'
        ta.wma(2 * ta.wma(MASource, MAPeriod / 2) - ta.wma(MASource, MAPeriod), math.round(math.sqrt(MAPeriod)))
    else if MAType == 'DEMA'
        e = ta.ema(MASource, MAPeriod)
        2 * e - ta.ema(e, MAPeriod)
    else if MAType == 'TEMA'
        e = ta.ema(MASource, MAPeriod)
        3 * (e - ta.ema(e, MAPeriod)) + ta.ema(ta.ema(e, MAPeriod), MAPeriod)
    else if MAType == 'VWMA'
        ta.vwma(MASource, MAPeriod)
// *************************************************************************************************************************************************************************************************************************************************************************
// * Number strategies
// *************************************************************************************************************************************************************************************************************************************************************************
n_strategies() =>
    var result = 0.
    if str_1
        result := 1.
    if str_2
        result += 1.
    if str_3
        result += 1.
    if str_4
        result += 1.
    if str_5
        result += 1.
// *************************************************************************************************************************************************************************************************************************************************************************
// * Price take profit
// *************************************************************************************************************************************************************************************************************************************************************************
price_takeProfit(percentage, N) =>
    if strategy.position_size > 0
        strategy.position_avg_price * (1 + N * percentage)
    else
        strategy.position_avg_price * (1 - N * percentage)
// *************************************************************************************************************************************************************************************************************************************************************************
// * Weigthed values
// *************************************************************************************************************************************************************************************************************************************************************************
weight_values(signal) =>
    if signal
        weight = 1.0
    else
        weight = 0.
// *************************************************************************************************************************************************************************************************************************************************************************
// * Weigthed total
// *************************************************************************************************************************************************************************************************************************************************************************
weight_total(signal1, signal2, signal3, signal4, signal5) =>
    weight_str1 * weight_values(signal1) + weight_str2 * weight_values(signal2) + weight_str3 * weight_values(signal3) + weight_str4 * weight_values(signal4) + weight_str5 * weight_values(signal5)
// *************************************************************************************************************************************************************************************************************************************************************************
// * Set alert TP message
// *************************************************************************************************************************************************************************************************************************************************************************
set_alarm_label_TP() =>
    if strategy.position_size > 0
        alarm_label_TP_long
    else if strategy.position_size < 0
        alarm_label_TP_short 
// *************************************************************************************************************************************************************************************************************************************************************************
// * Color
// *************************************************************************************************************************************************************************************************************************************************************************
colors(type, value=0) =>
    switch str.lower(type)
        'buy'=> color.new(color.aqua, value)
        'sell' => color.new(color.gray, value)
        'TP' => color.new(color.aqua, value)
        'SL' => color.new(color.gray, value)
        'signal' => color.new(color.orange, value)
        'profit' => color.new(color.teal, value)
        'loss' => color.new(color.red, value)
        'info' => color.new(color.white, value)
        'highlights' => color.new(color.orange, value)
// *************************************************************************************************************************************************************************************************************************************************************************
// * Bar since last entry
// *************************************************************************************************************************************************************************************************************************************************************************
bars_since_entry() =>
    bar_index - strategy.opentrades.entry_bar_index(0)
// *************************************************************************************************************************************************************************************************************************************************************************
// * Bar since close or TP/STOP
// *************************************************************************************************************************************************************************************************************************************************************************
bars_since_close() =>
    ta.barssince(ta.change(strategy.closedtrades))
// *************************************************************************************************************************************************************************************************************************************************************************
// ADDITIONAL GLOBAL VARIABLES
// *************************************************************************************************************************************************************************************************************************************************************************
// * Compute time since last entry and last close/TP position
since_entry := bars_since_entry()
since_close := bars_since_close()
if strategy.opentrades == 0
    since_entry := delay_exit
if strategy.closedtrades == 0
    since_close := delay_exit
// *************************************************************************************************************************************************************************************************************************************************************************
// STRATEGIES
// *************************************************************************************************************************************************************************************************************************************************************************
// * STRATEGY 1: MACD
// *************************************************************************************************************************************************************************************************************************************************************************
MA1 = ma(MA1_type_1, MA1_source_1, MA1_period_1)
MA2 = ma(MA2_type_1, MA2_source_1, MA2_period_1)

MACD = MA1 - MA2
signal = ma('SMA', MACD, 9)
trend= MACD - signal

long = MACD > signal
short = MACD < signal
proportion = math.abs(MACD / signal)

// * Conditions
long_signal1 = long and long[delay_macd - 1] and not long[delay_macd]
short_signal1 = short and short[delay_macd - 1] and not short[delay_macd]
close_long1 = short and not long[delay_macd]
close_short1 = long and not short[delay_macd]
// *************************************************************************************************************************************************************************************************************************************************************************
// * STRATEGY 2: STOCH RSI
// *************************************************************************************************************************************************************************************************************************************************************************
rsi = ta.rsi(close, length_RSI)
srsi = ta.stoch(rsi, rsi, rsi, length_stoch)
k = ma('SMA', srsi, smoothK)
isRsiOB = k >= long_RSI
isRsiOS = k <= short_RSI

// * Conditions
long_signal2 = isRsiOS[delay_srsi] and not isRsiOB and since_entry >= delay_exit and since_close >= delay_exit
short_signal2 = isRsiOB[delay_srsi] and not isRsiOS and since_entry >= delay_exit and since_close >= delay_exit
close_long2 = short_signal2
close_short2 = long_signal2
// *************************************************************************************************************************************************************************************************************************************************************************
// * STRATEGY 3: RSI
// *************************************************************************************************************************************************************************************************************************************************************************
isRsiOB2 = rsi >= long_RSI2
isRsiOS2 = rsi <= short_RSI2

// * Conditions
long_signal3 = isRsiOS2[delay_rsi] and not isRsiOB2 and since_entry >= delay_exit and since_close >= delay_exit
short_signal3 = isRsiOB2[delay_rsi] and not isRsiOS2 and since_entry >= delay_exit and since_close >= delay_exit
close_long3 = short_signal3
close_short3 = long_signal3
// *************************************************************************************************************************************************************************************************************************************************************************
// * STRATEGY 4: SUPERTREND
// *************************************************************************************************************************************************************************************************************************************************************************
atr2 = ma('SMA', ta.tr, periods_4)
atr = change_ATR ? ta.atr(periods_4) : atr2
up = source_4 - multiplier * atr
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up

dn = source_4 + multiplier * atr
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn

trend := 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend

// * Conditions
long4 = trend == 1
short4 = trend == -1
long_signal4 = trend == 1 and trend[delay_super - 1] == 1 and trend[delay_super] == -1
short_signal4 = trend == -1 and trend[delay_super - 1] == -1 and trend[delay_super] == 1
changeCond = trend != trend[1]
close_long4 = short_signal4
close_short4 = short_signal4
// *************************************************************************************************************************************************************************************************************************************************************************
// * STRATEGY 5: MA CROSS
// *************************************************************************************************************************************************************************************************************************************************************************
MA12 = ma(MA1_type_5, MA1_source_5, MA1_period_5)
MA22 = ma(MA2_type_5, MA2_source_5, MA2_period_5)

long5 = MA12 > MA22
short5 = MA12 < MA22

// * Conditions
long_signal5 = long5 and long5[delay_cross - 1] and not long5[delay_cross]
short_signal5 = short5 and short5[delay_cross - 1] and not short5[delay_cross]
close_long5 = short5 and not long5[delay_cross]
close_short5 = long5 and not short5[delay_cross]
// *************************************************************************************************************************************************************************************************************************************************************************
// * STRATEGY 6: POTENTIAL TOP/BOTTOM
// *************************************************************************************************************************************************************************************************************************************************************************
// * Combination RSI, Stoch RSI, MACD, volume, and weighted-strategy to detect potential TOP/BOTTOMS areas
volumeRSI_condition = volume[2] > volume[3] and volume[2] > volume[4] and volume[2] > volume[5]
condition_OB1 = isRsiOB2 and (isRsiOB or volume < ma('SMA', volume, 20) / 2) and volumeRSI_condition
condition_OS1 = isRsiOS2 and (isRsiOS or volume < ma('SMA', volume, 20) / 2) and volumeRSI_condition

condition_OB2 = volume[2] / volume[1] > (1.0 + long_trail_perc) and isRsiOB and volumeRSI_condition
condition_OS2 = volume[2] / volume[1] > (1.0 + short_trail_perc) and isRsiOS and volumeRSI_condition

condition_OB3 = weight_total(MACD < signal, isRsiOB, isRsiOB2, short4, short5) >= weight_trigger
condition_OS3 = weight_total(MACD > signal, isRsiOS, isRsiOS2, long4, long5) >= weight_trigger

condition_OB = (condition_OB1 or condition_OB2)
condition_OS = (condition_OS1 or condition_OS2)
condition_OB_several = condition_OB[1] and condition_OB[2] or condition_OB[1] and condition_OB[3] or condition_OB[1] and condition_OB[4] or condition_OB[1] and condition_OB[5] or condition_OB[1] and condition_OB[6] or condition_OB[1] and condition_OB[7] 
condition_OS_several = condition_OS[1] and condition_OS[2] or condition_OS[1] and condition_OS[3] or condition_OS[1] and condition_OS[4] or condition_OS[1] and condition_OS[5] or condition_OS[1] and condition_OS[6] or condition_OS[1] and condition_OS[7] 
// *************************************************************************************************************************************************************************************************************************************************************************
// STRATEGY ENTRIES AND EXITS
// *************************************************************************************************************************************************************************************************************************************************************************


long_SL = close - ((close / 100) * procent_stop)
long_OP = close
long_TP_1 = close + ((close / 100) * (procent_teik * 1.1))
long_TP_2 = close + ((close / 100) * (procent_teik * 1.8))
long_TP_3 = close + ((close / 100) * (procent_teik * 2.8))
long_TP_4 = close + ((close / 100) * (procent_teik * 4.5))

short_SL = close + ((close / 100) * procent_stop)
short_OP = close
short_TP_1 = close - ((close / 100) * (procent_teik * 1.1))
short_TP_2 = close - ((close / 100) * (procent_teik * 1.8))
short_TP_3 = close - ((close / 100) * (procent_teik * 2.8))
short_TP_4 = close - ((close / 100) * (procent_teik * 4.5))


if time >= start and time <= end
    // ***************************************************************************************************************************************************************************
    // * Set Entries
    // ***************************************************************************************************************************************************************************
    if str_0
        if not str_1
            weight_str1 := 0
        if not str_2
            weight_str2 := 0
        if not str_3
            weight_str3 := 0
        if not str_4
            weight_str4 := 0
        if not str_5
            weight_str5 := 0
        if allow_shorts == true
            w_total = weight_total(short_signal1, short_signal2, short_signal3, short_signal4, short_signal5)
            if w_total >= weight_trigger
                alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('SHORT') + ' ' + str.tostring(math.round_to_mintick(short_TP_1)) + ' ' + str.tostring(math.round_to_mintick(short_TP_2)) + ' ' + str.tostring(math.round_to_mintick(short_TP_3)) + ' ' + str.tostring(math.round_to_mintick(short_TP_4)) + ' ' + str.tostring(math.round_to_mintick(short_SL)) + ' ' + str.tostring(math.round_to_mintick(close)) + '"}')
                


                strategy.entry('Short', strategy.short)
        if allow_longs == true
            w_total = weight_total(long_signal1, long_signal2, long_signal3, long_signal4, long_signal5)
            if w_total >= weight_trigger
                strategy.entry('Long', strategy.long)
                alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('LONG') + ' ' + str.tostring(math.round_to_mintick(long_TP_1)) + ' ' + str.tostring(math.round_to_mintick(long_TP_2)) + ' ' + str.tostring(math.round_to_mintick(long_TP_3)) + ' ' + str.tostring(math.round_to_mintick(long_TP_4)) + ' ' + str.tostring(math.round_to_mintick(long_SL)) + ' '  + str.tostring(math.round_to_mintick(close)) + '"}')

    else
        if allow_shorts == true
            if str_1
                strategy.entry('Short', strategy.short, when=short_signal1)
                // alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('SHORT')  + ' ' + str.tostring(math.round_to_mintick(long_TP_1)) + ' ' + str.tostring(math.round_to_mintick(long_TP_2)) + ' ' + str.tostring(math.round_to_mintick(long_TP_3)) + ' ' + str.tostring(math.round_to_mintick(long_TP_4)) + ' ' + str.tostring(math.round_to_mintick(long_SL))  + str.tostring(math.round_to_mintick(close)) + '"}')
                alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('SHORT') + ' ' + str.tostring(math.round_to_mintick(short_TP_1)) + ' ' + str.tostring(math.round_to_mintick(short_TP_2)) + ' ' + str.tostring(math.round_to_mintick(short_TP_3)) + ' ' + str.tostring(math.round_to_mintick(short_TP_4)) + ' ' + str.tostring(math.round_to_mintick(short_SL)) + ' ' + str.tostring(math.round_to_mintick(close)) + '"}')

            if str_2
                strategy.entry('Short', strategy.short, when=short_signal2)
                // alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('SHORT')  + ' ' + str.tostring(math.round_to_mintick(long_TP_1)) + ' ' + str.tostring(math.round_to_mintick(long_TP_2)) + ' ' + str.tostring(math.round_to_mintick(long_TP_3)) + ' ' + str.tostring(math.round_to_mintick(long_TP_4)) + ' ' + str.tostring(math.round_to_mintick(long_SL))  + str.tostring(math.round_to_mintick(close)) + '"}')
                alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('SHORT') + ' ' + str.tostring(math.round_to_mintick(short_TP_1)) + ' ' + str.tostring(math.round_to_mintick(short_TP_2)) + ' ' + str.tostring(math.round_to_mintick(short_TP_3)) + ' ' + str.tostring(math.round_to_mintick(short_TP_4)) + ' ' + str.tostring(math.round_to_mintick(short_SL)) + ' ' + str.tostring(math.round_to_mintick(close)) + '"}')

            if str_3
                strategy.entry('Short', strategy.short, when=short_signal3)
                // alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('SHORT')  + ' ' + str.tostring(math.round_to_mintick(long_TP_1)) + ' ' + str.tostring(math.round_to_mintick(long_TP_2)) + ' ' + str.tostring(math.round_to_mintick(long_TP_3)) + ' ' + str.tostring(math.round_to_mintick(long_TP_4)) + ' ' + str.tostring(math.round_to_mintick(long_SL))  + str.tostring(math.round_to_mintick(close)) + '"}')
                alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('SHORT') + ' ' + str.tostring(math.round_to_mintick(short_TP_1)) + ' ' + str.tostring(math.round_to_mintick(short_TP_2)) + ' ' + str.tostring(math.round_to_mintick(short_TP_3)) + ' ' + str.tostring(math.round_to_mintick(short_TP_4)) + ' ' + str.tostring(math.round_to_mintick(short_SL)) + ' ' + str.tostring(math.round_to_mintick(close)) + '"}')

            if str_4
                strategy.entry('Short', strategy.short, when=short_signal4)
                // alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('SHORT')  + ' ' + str.tostring(math.round_to_mintick(long_TP_1)) + ' ' + str.tostring(math.round_to_mintick(long_TP_2)) + ' ' + str.tostring(math.round_to_mintick(long_TP_3)) + ' ' + str.tostring(math.round_to_mintick(long_TP_4)) + ' ' + str.tostring(math.round_to_mintick(long_SL))  + str.tostring(math.round_to_mintick(close)) + '"}')
                alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('SHORT') + ' ' + str.tostring(math.round_to_mintick(short_TP_1)) + ' ' + str.tostring(math.round_to_mintick(short_TP_2)) + ' ' + str.tostring(math.round_to_mintick(short_TP_3)) + ' ' + str.tostring(math.round_to_mintick(short_TP_4)) + ' ' + str.tostring(math.round_to_mintick(short_SL)) + ' ' + str.tostring(math.round_to_mintick(close)) + '"}')

            if str_5
                strategy.entry('Short', strategy.short, when=short_signal5)
                // alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('SHORT') + ' ' + str.tostring(math.round_to_mintick(long_TP_1)) + ' ' + str.tostring(math.round_to_mintick(long_TP_2)) + ' ' + str.tostring(math.round_to_mintick(long_TP_3)) + ' ' + str.tostring(math.round_to_mintick(long_TP_4)) + ' ' + str.tostring(math.round_to_mintick(long_SL))  + str.tostring(math.round_to_mintick(close)) + '"}')
                alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('SHORT') + ' ' + str.tostring(math.round_to_mintick(short_TP_1)) + ' ' + str.tostring(math.round_to_mintick(short_TP_2)) + ' ' + str.tostring(math.round_to_mintick(short_TP_3)) + ' ' + str.tostring(math.round_to_mintick(short_TP_4)) + ' ' + str.tostring(math.round_to_mintick(short_SL)) + ' ' + str.tostring(math.round_to_mintick(close)) + '"}')

        if allow_longs == true
            if str_1
                strategy.entry('Long', strategy.long, when=long_signal1)
                // alert('{"chat_id":"'+ chat_id +'","text":"#' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' min'+ '\n' + str.tostring('LONG') + '\n' +'Вход на 1-3% от депозита \n' + 'TP 1: ' + str.tostring(math.round_to_mintick(long_TP_1)) + '\n' + 'TP 2: ' + str.tostring(math.round_to_mintick(long_TP_2)) + '\n' + 'TP 3: ' + str.tostring(math.round_to_mintick(long_TP_3)) + '\n' + 'TP 4: ' + str.tostring(math.round_to_mintick(long_TP_4)) + '\n' + '?? STOP: ' + str.tostring(math.round_to_mintick(long_SL))  +'"}')
                alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('LONG') + ' ' + str.tostring(math.round_to_mintick(long_TP_1)) + ' ' + str.tostring(math.round_to_mintick(long_TP_2)) + ' ' + str.tostring(math.round_to_mintick(long_TP_3)) + ' ' + str.tostring(math.round_to_mintick(long_TP_4)) + ' ' + str.tostring(math.round_to_mintick(long_SL)) + ' '  + str.tostring(math.round_to_mintick(close)) + '"}')

            if str_2
                strategy.entry('Long', strategy.long, when=long_signal2)
                // alert('{"chat_id":"'+ chat_id +'","text":"#' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' min'+ '\n' + str.tostring('LONG') + '\n' +'Вход на 1-3% от депозита \n' + 'TP 1: ' + str.tostring(math.round_to_mintick(long_TP_1)) + '\n' + 'TP 2: ' + str.tostring(math.round_to_mintick(long_TP_2)) + '\n' + 'TP 3: ' + str.tostring(math.round_to_mintick(long_TP_3)) + '\n' + 'TP 4: ' + str.tostring(math.round_to_mintick(long_TP_4)) + '\n' + '?? STOP: ' + str.tostring(math.round_to_mintick(long_SL))  +'"}')
                alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('LONG') + ' ' + str.tostring(math.round_to_mintick(long_TP_1)) + ' ' + str.tostring(math.round_to_mintick(long_TP_2)) + ' ' + str.tostring(math.round_to_mintick(long_TP_3)) + ' ' + str.tostring(math.round_to_mintick(long_TP_4)) + ' ' + str.tostring(math.round_to_mintick(long_SL)) + ' '  + str.tostring(math.round_to_mintick(close)) + '"}')

            if str_3
                strategy.entry('Long', strategy.long, when=long_signal3)
                // alert('{"chat_id":"'+ chat_id +'","text":"#' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' min'+ '\n' + str.tostring('LONG') + '\n' +'Вход на 1-3% от депозита \n' + 'TP 1: ' + str.tostring(math.round_to_mintick(long_TP_1)) + '\n' + 'TP 2: ' + str.tostring(math.round_to_mintick(long_TP_2)) + '\n' + 'TP 3: ' + str.tostring(math.round_to_mintick(long_TP_3)) + '\n' + 'TP 4: ' + str.tostring(math.round_to_mintick(long_TP_4)) + '\n' + '?? STOP: ' + str.tostring(math.round_to_mintick(long_SL))  +'"}')
                alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('LONG') + ' ' + str.tostring(math.round_to_mintick(long_TP_1)) + ' ' + str.tostring(math.round_to_mintick(long_TP_2)) + ' ' + str.tostring(math.round_to_mintick(long_TP_3)) + ' ' + str.tostring(math.round_to_mintick(long_TP_4)) + ' ' + str.tostring(math.round_to_mintick(long_SL)) + ' '  + str.tostring(math.round_to_mintick(close)) + '"}')

            if str_4
                strategy.entry('Long', strategy.long, when=long_signal4)
                // alert('{"chat_id":"'+ chat_id +'","text":"#' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' min'+ '\n' + str.tostring('LONG') + '\n' +'Вход на 1-3% от депозита \n' + 'TP 1: ' + str.tostring(math.round_to_mintick(long_TP_1)) + '\n' + 'TP 2: ' + str.tostring(math.round_to_mintick(long_TP_2)) + '\n' + 'TP 3: ' + str.tostring(math.round_to_mintick(long_TP_3)) + '\n' + 'TP 4: ' + str.tostring(math.round_to_mintick(long_TP_4)) + '\n' + '?? STOP: ' + str.tostring(math.round_to_mintick(long_SL))  +'"}')
                alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('LONG') + ' ' + str.tostring(math.round_to_mintick(long_TP_1)) + ' ' + str.tostring(math.round_to_mintick(long_TP_2)) + ' ' + str.tostring(math.round_to_mintick(long_TP_3)) + ' ' + str.tostring(math.round_to_mintick(long_TP_4)) + ' ' + str.tostring(math.round_to_mintick(long_SL)) + ' '  + str.tostring(math.round_to_mintick(close)) + '"}')

            if str_5
                strategy.entry('Long', strategy.long, when=long_signal5)
                // alert('{"chat_id":"'+ chat_id +'","text":"#' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' min'+ '\n' + str.tostring('LONG') + '\n' +'Вход на 1-3% от депозита \n' + 'TP 1: ' + str.tostring(math.round_to_mintick(long_TP_1)) + '\n' + 'TP 2: ' + str.tostring(math.round_to_mintick(long_TP_2)) + '\n' + 'TP 3: ' + str.tostring(math.round_to_mintick(long_TP_3)) + '\n' + 'TP 4: ' + str.tostring(math.round_to_mintick(long_TP_4)) + '\n' + '?? STOP: ' + str.tostring(math.round_to_mintick(long_SL))  +'"}')
                alert('{"chat_id":"'+ chat_id +'","text":"' + syminfo.ticker + ' ' + str.tostring(timeframe.period) + ' ' + str.tostring('LONG') + ' ' + str.tostring(math.round_to_mintick(long_TP_1)) + ' ' + str.tostring(math.round_to_mintick(long_TP_2)) + ' ' + str.tostring(math.round_to_mintick(long_TP_3)) + ' ' + str.tostring(math.round_to_mintick(long_TP_4)) + ' ' + str.tostring(math.round_to_mintick(long_SL)) + ' '  + str.tostring(math.round_to_mintick(close)) + '"}')

    // ***************************************************************************************************************************************************************************
    // * Set Take Profits
    // ***************************************************************************************************************************************************************************
    if strategy.position_size != 0 and take_profits and since_entry == 0
        for i = 1 to MAX_TP
            id = 'TP ' + str.tostring(i)
            strategy.exit(id=id, limit=price_takeProfit(profit_perc, i), qty_percent=profit_qty, comment=id)
    // ***************************************************************************************************************************************************************************
    // * Set Stop loss
    // ***************************************************************************************************************************************************************************
    if strategy.position_size > 0
        if since_close == 0
            if high > price_takeProfit(profit_perc, 6) and MAX_TP >= 6
                n = 6
                nextTP := na
                if movestoploss == 'Percentage'
                    price_stop_long := strategy.position_avg_price * (1 + n*profit_perc - stoploss_perc * move_stoploss_factor)
                else if movestoploss == 'TP-1'
                    price_stop_long := price_takeProfit(profit_perc, n-1)
                else if movestoploss == 'TP-2'
                    price_stop_long := price_takeProfit(profit_perc, n-2)
                else if movestoploss == 'TP-3'
                    price_stop_long := price_takeProfit(profit_perc, n-3)
            else if high > price_takeProfit(profit_perc, 5) and MAX_TP >= 5
                n = 5
                nextTP := price_takeProfit(profit_perc, n + 1)
                if movestoploss == 'Percentage'
                    price_stop_long := strategy.position_avg_price * (1 + n*profit_perc - stoploss_perc * move_stoploss_factor)
                else if movestoploss == 'TP-1'
                    price_stop_long := price_takeProfit(profit_perc, n-1)
                else if movestoploss == 'TP-2'
                    price_stop_long := price_takeProfit(profit_perc, n-2)
                else if movestoploss == 'TP-3'
                    price_stop_long := price_takeProfit(profit_perc, n-3)
            else if high > price_takeProfit(profit_perc, 4) and MAX_TP >= 4
                n = 4
                nextTP := price_takeProfit(profit_perc, n + 1)
                if movestoploss == 'Percentage'
                    price_stop_long := strategy.position_avg_price * (1 + n*profit_perc - stoploss_perc * move_stoploss_factor)
                else if movestoploss == 'TP-1'
                    price_stop_long := price_takeProfit(profit_perc, n-1)
                else if movestoploss == 'TP-2'
                    price_stop_long := price_takeProfit(profit_perc, n-2)
                else if movestoploss == 'TP-3'
                    price_stop_long := price_takeProfit(profit_perc, n-3)
            else if high > price_takeProfit(profit_perc, 3) and MAX_TP >= 3
                n = 3
                nextTP := price_takeProfit(profit_perc, n + 1)
                if movestoploss == 'Percentage'
                    price_stop_long := strategy.position_avg_price * (1 + n*profit_perc - stoploss_perc * move_stoploss_factor)
                else if movestoploss == 'TP-1'
                    price_stop_long := price_takeProfit(profit_perc, n-1)
                else if movestoploss == 'TP-2'
                    price_stop_long := price_takeProfit(profit_perc, n-2)
                else if movestoploss == 'TP-3' and movestoploss_entry
                    price_stop_long := strategy.position_avg_price
            else if high > price_takeProfit(profit_perc, 2) and MAX_TP >= 2
                n = 2
                nextTP := price_takeProfit(profit_perc, n + 1)
                if movestoploss == 'Percentage'
                    price_stop_long := strategy.position_avg_price * (1 + n*profit_perc - stoploss_perc * move_stoploss_factor)
                else if movestoploss == 'TP-1'
                    price_stop_long := price_takeProfit(profit_perc, n-1)
                else if movestoploss == 'TP-2' and movestoploss_entry
                    price_stop_long := strategy.position_avg_price
                else if movestoploss == 'TP-3' and movestoploss_entry
                    price_stop_long := strategy.position_avg_price
            else if high > price_takeProfit(profit_perc, 1) and MAX_TP >= 1
                n = 1
                nextTP := price_takeProfit(profit_perc, n + 1)
                if movestoploss == 'Percentage'
                    price_stop_long := strategy.position_avg_price * (1 + n*profit_perc - stoploss_perc * move_stoploss_factor)
                else if movestoploss == 'TP-1' and movestoploss_entry
                    price_stop_long := strategy.position_avg_price
                else if movestoploss == 'TP-2' and movestoploss_entry
                    price_stop_long := strategy.position_avg_price
                else if movestoploss == 'TP-3' and movestoploss_entry
                    price_stop_long := strategy.position_avg_price
        if since_entry == 0
            n = 0
            nextTP := price_takeProfit(profit_perc, n + 1)
            price_stop_long := strategy.position_avg_price * (1 - stoploss_perc) 
    if strategy.position_size < 0
        if since_close == 0
            if low < price_takeProfit(profit_perc, 6) and MAX_TP >= 6
                n = 6
                nextTP := na
                if movestoploss == 'Percentage'
                    price_stop_short := strategy.position_avg_price * (1 - n*profit_perc + stoploss_perc * move_stoploss_factor)
                else if movestoploss == 'TP-1'
                    price_stop_short := price_takeProfit(profit_perc, n-1)
                else if movestoploss == 'TP-2'
                    price_stop_short := price_takeProfit(profit_perc, n-2)
                else if movestoploss == 'TP-3'
                    price_stop_short := price_takeProfit(profit_perc, n-3)
            else if low < price_takeProfit(profit_perc, 5) and MAX_TP >= 5
                n = 5
                nextTP := price_takeProfit(profit_perc, n + 1)
                if movestoploss == 'Percentage'
                    price_stop_short := strategy.position_avg_price * (1 - n*profit_perc + stoploss_perc * move_stoploss_factor)
                else if movestoploss == 'TP-1'
                    price_stop_short := price_takeProfit(profit_perc, n-1)
                else if movestoploss == 'TP-2'
                    price_stop_short := price_takeProfit(profit_perc, n-2)
                else if movestoploss == 'TP-3'
                    price_stop_short := price_takeProfit(profit_perc, n-3)
            else if low < price_takeProfit(profit_perc, 4) and MAX_TP >= 4
                n = 4
                nextTP := price_takeProfit(profit_perc, n + 1)
                if movestoploss == 'Percentage'
                    price_stop_short := strategy.position_avg_price * (1 - n*profit_perc + stoploss_perc * move_stoploss_factor)
                else if movestoploss == 'TP-1'
                    price_stop_short := price_takeProfit(profit_perc, n-1)
                else if movestoploss == 'TP-2'
                    price_stop_short := price_takeProfit(profit_perc, n-2)
                else if movestoploss == 'TP-3'
                    price_stop_short := price_takeProfit(profit_perc, n-3)
            else if low < price_takeProfit(profit_perc, 3) and MAX_TP >= 3
                n = 3
                nextTP := price_takeProfit(profit_perc, n + 1)
                if movestoploss == 'Percentage'
                    price_stop_short := strategy.position_avg_price * (1 - n*profit_perc + stoploss_perc * move_stoploss_factor)
                else if movestoploss == 'TP-1'
                    price_stop_short := price_takeProfit(profit_perc, n-1)
                else if movestoploss == 'TP-2'
                    price_stop_short := price_takeProfit(profit_perc, n-2)
                else if movestoploss == 'TP-3' and movestoploss_entry
                    price_stop_short := strategy.position_avg_price
            else if low < price_takeProfit(profit_perc, 2) and MAX_TP >= 2
                n = 2
                nextTP := price_takeProfit(profit_perc, n + 1)
                if movestoploss == 'Percentage'
                    price_stop_short := strategy.position_avg_price * (1 - n*profit_perc + stoploss_perc * move_stoploss_factor)
                else if movestoploss == 'TP-1'
                    price_stop_short := price_takeProfit(profit_perc, n-1)
                else if movestoploss == 'TP-2' and movestoploss_entry
                    price_stop_short := strategy.position_avg_price
                else if movestoploss == 'TP-3' and movestoploss_entry
                    price_stop_short := strategy.position_avg_price
            else if low < price_takeProfit(profit_perc, 1) and MAX_TP >= 1
                n = 1 
                nextTP := price_takeProfit(profit_perc, n + 1)
                if movestoploss == 'Percentage'
                    price_stop_short := strategy.position_avg_price * (1 - n*profit_perc + stoploss_perc * move_stoploss_factor)
                else if movestoploss == 'TP-1' and movestoploss_entry
                    price_stop_short := strategy.position_avg_price
                else if movestoploss == 'TP-2' and movestoploss_entry
                    price_stop_short := strategy.position_avg_price
                else if movestoploss == 'TP-3' and movestoploss_entry
                    price_stop_short := strategy.position_avg_price
        if since_entry == 0
            n = 0
            nextTP := price_takeProfit(profit_perc, n + 1)
            price_stop_short := strategy.position_avg_price * (1 + stoploss_perc)
    // ***************************************************************************************************************************************************************************
    // * Set Exits
    // ***************************************************************************************************************************************************************************
    if allow_longs == true and allow_shorts == false
        if str_0
            w_total = weight_total(short_signal1, short_signal2, short_signal3, short_signal4, short_signal5)
            strategy.close('Long', when=w_total>=weight_trigger, qty_percent=100, comment='SHORT')
        else
            if str_1
                strategy.close('Long', when=close_long1, qty_percent=100, comment='SHORT')
            if str_2
                strategy.close('Long', when=close_long2, qty_percent=100, comment='SHORT')
            if str_3
                strategy.close('Long', when=close_long3, qty_percent=100, comment='SHORT')
            if str_4
                strategy.close('Long', when=close_long4, qty_percent=100, comment='SHORT')
            if str_5
                strategy.close('Long', when=close_long5, qty_percent=100, comment='SHORT')
    if allow_longs == false and allow_shorts == true
        if str_0
            w_total = weight_total(long_signal1, long_signal2, long_signal3, long_signal4, long_signal5)
            strategy.close('Short', when=w_total>=weight_trigger, qty_percent=100, comment='LONG')
        else
            if str_1
                strategy.close('Short', when=close_long1, qty_percent=100, comment='LONG')
            if str_2
                strategy.close('Short', when=close_long2, qty_percent=100, comment='LONG')
            if str_3
                strategy.close('Short', when=close_long3, qty_percent=100, comment='LONG')
            if str_4
                strategy.close('Short', when=close_long4, qty_percent=100, comment='LONG')
            if str_5
                strategy.close('Short', when=close_long5, qty_percent=100, comment='LONG')
    if allow_shorts == true and strategy.position_size < 0 and stoploss and since_entry > 0
        strategy.close('Short', when=stop_source >= price_stop_short, qty_percent=100, comment='STOP')
        if str_6
            if top_qty == 100
                strategy.close('Short', when=condition_OS_several, qty_percent=bottom_qty, comment='STOP')
            else    
                strategy.exit('Short', when=condition_OS_several, limit=source_6_bottom[1], qty_percent=bottom_qty, comment='TP-B')
    if allow_longs == true and strategy.position_size > 0 and stoploss and since_entry > 0
        strategy.close('Long', when=stop_source <= price_stop_long, qty_percent=100, comment='STOP')
        if str_6
            if top_qty == 100
                strategy.close('Long', when=condition_OB_several, qty_percent=top_qty, comment='STOP')
            else
                strategy.exit('Long', when=condition_OB_several, limit=source_6_top[1], qty_percent=top_qty, comment='TP-T')
// ***********************************************************************************************************************************************************************************************************************************************************************************
// * Data window - debugging
// *************************************************************************************************************************************************************************************************************************************************************************
price_stop = strategy.position_size > 0 ? price_stop_long : price_stop_short

// *************************************************************************************************************************************************************************************************************************************************************************
// * Buy/Sell signals
// *************************************************************************************************************************************************************************************************************************************************************************
w_total_long = weight_total(long_signal1, long_signal2, long_signal3, long_signal4, long_signal5)
w_total_short = weight_total(short_signal1, short_signal2, short_signal3, short_signal4, short_signal5)
// *************************************************************************************************************************************************************************************************************************************************************************
// * Stop loss targets
// *************************************************************************************************************************************************************************************************************************************************************************
plot(series=(strategy.position_size > 0) ? price_stop_long : na, color=color.gray, style=plot.style_cross, linewidth=2, transp=30, title="Long Stop Loss")
plot(series=(strategy.position_size < 0) ? price_stop_short : na, color=color.gray, style=plot.style_cross, linewidth=2, transp=30, title="Short Stop Loss")
// *************************************************************************************************************************************************************************************************************************************************************************
// * TP targets
// *************************************************************************************************************************************************************************************************************************************************************************
plot(strategy.position_size > 0 or strategy.position_size < 0 ? nextTP : na, color=color.aqua, style=plot.style_cross, linewidth=2, transp=30, title="Next TP")
// *************************************************************************************************************************************************************************************************************************************************************************