// Filter Name :   HF-518 FREE
// User: Mostafa Ejarestaghi
// UPDATE: 1402/09/21
// version : 1.1.1
// ------------ site inormation -------------
// website : www.hooshefaal.ir
// Email : hooshefaal@gmail.com
//Telegram Channel : @hooshefaal
//Telegram Support : @hooshefaal_support
// Instagram Page :  @hooshefaal.ir
//whatsapp number :  09364549266
// --------------END---------------
/*
کف و سقف چند روزه سهام
کراس مووینگ یک و 10
افزایش حجم
آستانه بالا و پایین روز گذشته
*/
true == function () {
  //------------------COMMAND START----------------------- 
  var period = 30;  // تعداد روزهای مورد نظر را وارد نمایید
  Hooshefaal();
  (cfield0) = "";
  (cfield1) = "";
  (cfield2) = "";


  if ([ih][0].PClosing != (pc) && [ih][0].ZTotTran != (tno) && [ih][0].QTotCap != (tval)) {
    var len = [ih].length;
    if (typeof [ih][0].fixed == 'undefined') {
      for (var i = len; i > 0; i--) {
        if (typeof [ih][i] == 'undefined') {
          [ih][i] = {};
        }
        [ih][i].PriceFirst = [ih][i - 1].PriceFirst;
        [ih][i].PClosing = [ih][i - 1].PClosing;
        [ih][i].PDrCotVal = [ih][i - 1].PDrCotVal;
        [ih][i].ZTotTran = [ih][i - 1].PriceFirst;
        [ih][i].QTotTran5J = [ih][i - 1].QTotTran5J;
        [ih][i].QTotCap = [ih][i - 1].QTotCap;
        [ih][i].PriceChange = [ih][i - 1].PriceChange;
        [ih][i].PriceMin = [ih][i - 1].PriceMin;
        [ih][i].PriceMax = [ih][i - 1].PriceMax;
        [ih][i].PriceYesterday = [ih][i - 1].PriceYesterday;
      }
      [ih][0].fixed = 1;
    }
    [ih][0].PriceFirst = (pf);
    [ih][0].PClosing = (pc);
    [ih][0].PDrCotVal = (pl);
    [ih][0].ZTotTran = (tno);
    [ih][0].QTotTran5J = (tvol);
    [ih][0].QTotCap = (tval);
    [ih][0].PriceChange = (pcc);
    [ih][0].PriceMin = (pmin);
    [ih][0].PriceMax = (pmax);
    [ih][0].PriceYesterday = (py);
  }
  var i;
  var buy_ratio = Math.round(((((ct).Buy_I_Volume / (ct).Buy_CountI) * (pc)) / 10000000) * 10) / 10;
  var sell_ratio = Math.round(((((ct).Sell_I_Volume / (ct).Sell_CountI) * (pc)) / 10000000) * 10) / 10;
  var buy_power = Math.round((((ct).Buy_I_Volume / (ct).Buy_CountI) / ((ct).Sell_I_Volume / (ct).Sell_CountI)) * 100) / 100;
  var moneyVal = ((ct).Buy_I_Volume - (ct).Sell_I_Volume) * (pc);
  moneyVal = ((moneyVal / 10) / 1000000).toFixed(2);

  if (isNaN(buy_ratio)) {
    buy_ratio = 0;
  }
  if (isNaN(sell_ratio)) {
    sell_ratio = 0;
  }
  if (isNaN(buy_power)) {
    buy_power = 0;
  }
  if (isNaN(moneyVal)) {
    moneyVal = 0;
  }

  var MoneyColor = "";
  if (moneyVal > 0) {
    MoneyColor = "green";
  }
  if (moneyVal < 0) {
    MoneyColor = "red";
  }
  var up = "";
  var down = "";
  c = ((tmax) / (py)).toFixed(2);
  d = ((tmin) / (py)).toFixed(2);
  a = Math.floor([ih][1].PClosing * c) //tmax آستانه بالا
  b = Math.ceil([ih][1].PClosing * d) //tmin آستانه پایین
  if (a < 5000) {
    up = a;
    down = b;
  } else if (a > 5000 && a < 20000) {
    up = Math.floor(a / 10) * 10;
    down = Math.ceil(b / 10) * 10;
  } else if (a > 20000) {
    up = Math.floor(a / 50) * 50;
    down = Math.ceil(b / 50) * 50;
  }


  (cfield0) = upFunc(period);
  (cfield1) = Downfunc(period);
  var strResult = "<span style='opacity: 0.8;border-top: 1px solid #C0C0C0;border-left: 1px solid #000000;width:100px;display:inline-block;background:;text-align: center;color:;font-weight: bold;' title='فاصله تا سقف و کف'>&nbsp" + PersentageFunc(period) + "</span>";;
  strResult += "<span style='opacity: 0.8;border-top: 1px solid #C0C0C0;border-left: 1px solid #000000;width:100px;display:inline-block;background:#8502f7;text-align: center;color:#fff;font-weight: bold;' title=' آستانه پایین'>&nbsp" + down + "</span>";;
  strResult += "<span style='opacity: 0.8;border-top: 1px solid #C0C0C0;border-left: 1px solid #000000;width:100px;display:inline-block;background:#000;text-align: center;color:#fff;font-weight: bold;' title=' اسنانه بالا '>&nbsp" + up + "</span>";;
  strResult += "<span style='opacity: 0.8;border-top: 1px solid #C0C0C0;border-left: 1px solid #000000;width:100px;display:inline-block;background:#ffffff;text-align: center;color:" + MoneyColor + ";font-weight: bold;' title='قدرت خریدار '>&nbsp" + buy_power + "</span>";;
  strResult += "<span style='opacity: 0.8;border-top: 1px solid #C0C0C0;border-left: 1px solid #000000;width:100px;display:inline-block;background:#ffffff;text-align: center;color:" + MoneyColor + ";font-weight: bold;' title='ورود و خروج پول'>&nbsp" + moneyVal + "</span>";;
  (cfield2) = strResult;

  if (
    (pl) > (0.95 * (pmax)) && (pl) >= (pc) && (pl) >= (pf) && ema(0, 1) > ema(0, 10) && ema(2, 1) < ema(2, 10) && (tvol) > [is5] && (pl) != (tmax)
  ) {
    return true;
  }



}()
//=========================END FUNCTION =========================
function Hooshefaal() {
  var HelpLink = "https://www.hooshefaal.ir/tse-filter-hf-518/";
  var FilterName = "HF-518";

  var column1 = "سقف", column2 = "کف", column3 = "ورود و خروج پول --------قدرت خریدار-----------آستانه بالا ------ آستانه پایین ---------- درصد مانه به سقف و کف", column4 = "", column5 = "";
  var my_data_title0 = '<div style = "direction:rtl;";>';
  my_data_title0 += ' <span style = "border: 1px solid gray;width: 100%;display: inline-block;text-align: center;color: #000;background: #FFAB73;font-weight: normal;"> ' + column1 + '</span>'
  my_data_title0 += '<span style="left: 10px;direction: rtl; display: inline-block; background: #F6F7BF;font-weight: normal; text-align: center;border: 0 solid red;border-radius: 6px; padding: 3px;top: 42%;position: fixed;font-family: Tahoma; font-size: 12px; background-color: #FFD700 !important;  box-shadow: 0 2px 5px rgb(0 0 0 / 50%);color: Blue; width: 150px;animation: NotificationAnim 2s infinite linear;"><a href="https://www.hooshefaal.ir/category/vip-filter/gold/" title="فیلتر های طلایی سایت هوش فعال" target="_blank"><b>💎فیلتر های ویژه💎</b></a> </span>';
  my_data_title0 += '<span style="left: 10px;direction: rtl;display: inline-block;background: #F6F7BF;font-weight: normal;text-align: center;border: 0 solid red;border-radius: 6px;padding: 3px;top: 45%;position: fixed;font-family: Tahoma;font-size: 12px;background-color: #06FF00 !important;box-shadow: 0 2px 5px rgb(0 0 0 / 50%);color: Blue;width: 150px;animation: NotificationAnim 2s infinite linear;"><a href="https://www.hooshefaal.ir/category/%d9%81%db%8c%d9%84%d8%aa%d8%b1-%d9%86%d9%88%db%8c%d8%b3%db%8c-%d8%a8%d9%88%d8%b1%d8%b3/free-filter/"title="دانلود رایگان فیلتر های بورسی" target="_blank">دانلود فیلترهای رایگان</a></span>';
  my_data_title0 += '<span style="left: 10px;direction: rtl; display: inline-block; background: #F6F7BF;font-weight: normal; text-align: center;border: 0 solid red;border-radius: 6px; padding: 3px;top: 48%;position: fixed;font-family: Tahoma; font-size: 12px; background-color: #06FF00 !important;  box-shadow: 0 2px 5px rgb(0 0 0 / 50%);color: Blue; width: 150px;animation: NotificationAnim 2s infinite linear;"><a href="https://www.hooshefaal.ir/category/indicator-download/" title=" دانلود رایگان اندیکاتور" target="_blank">دانلود رایگان اندیکاتور </a> </span>';
  my_data_title0 += '<span style="left: 10px;direction: rtl; display: inline-block; background: #F6F7BF;font-weight: normal; text-align: center;border: 0 solid red;border-radius: 6px; padding: 3px;top: 51%;position: fixed;font-family: Tahoma; font-size: 12px; background-color: #A4EBF3 !important;  box-shadow: 0 2px 5px rgb(0 0 0 / 50%);color: Blue; width: 150px;animation: NotificationAnim 2s infinite linear;">سفارش <a href="https://www.hooshefaal.ir/expert-order/" title="سفارش اکسپرت" target="_blank">اکسپرت</a> و <a href="https://www.hooshefaal.ir/tsetmc-filter-order/" title="سفارش فیلتر" target="_blank">فیلتر </a> </span>';
  my_data_title0 += '<span style="left: 10px;direction: rtl; display: inline-block; background: #F6F7BF;font-weight: normal; text-align: center;border: 0 solid red;border-radius: 6px; padding: 3px;top: 54%;position: fixed;font-family: Tahoma; font-size: 12px; background-color: #A4EBF3 !important;  box-shadow: 0 2px 5px rgb(0 0 0 / 50%);color: Blue; width: 150px;animation: NotificationAnim 2s infinite linear;"><a href="https://t.me/hooshefaal/" title="کانال تلگرام هوش فعال" target="_blank">کانال تلگرام هوش فعال</a> </span>';
  my_data_title0 += '<span style="left: 10px;direction: rtl; display: inline-block; background: #F6F7BF;font-weight: normal; text-align: center;border: 0 solid red;border-radius: 6px; padding: 3px;top: 57%;position: fixed;font-family: Tahoma; font-size: 12px; background-color: #ffad00 !important;  box-shadow: 0 2px 5px rgb(0 0 0 / 50%);color: Blue; width: 150px;animation: NotificationAnim 2s infinite linear;"><a href="https://www.youtube.com/channel/UCMWCAmtUbiZcF4d7vZVtjfg" title="کانال یوتیوب هوش فعال" target="_blank">یوتیوب هوش فعال </a> </span>';
  my_data_title0 += '<span style="left: 10px;direction: rtl; display: inline-block; background: #F6F7BF;font-weight: normal; text-align: center;border: 0 solid red;border-radius: 6px; padding: 3px;top: 60%;position: fixed;font-family: Tahoma; font-size: 12px; background-color: #ffcc00 !important;  box-shadow: 0 2px 5px rgb(0 0 0 / 50%);color: Blue; width: 150px;animation: NotificationAnim 2s infinite linear;"><a href="https://www.aparat.com/vitanet" title="کانال آپارات  هوش فعال" target="_blank">آپارات هوش فعال </a> </span>';
  my_data_title0 += '<span style="left: 10px;direction: rtl; display: inline-block; background: #F6F7BF;font-weight: normal; text-align: center;border: 0 solid red;border-radius: 6px; padding: 3px;top: 63%;position: fixed;font-family: Tahoma; font-size: 12px; background-color: #ffff00 !important;  box-shadow: 0 2px 5px rgb(0 0 0 / 50%);color: Blue; width: 150px;animation: NotificationAnim 2s infinite linear;"><b><a href="' + HelpLink + '" title="لینک آموزش شیوه استفاده از این فیلتر" target="_blank">راهنمای این فیلتر</a> </b></span>';
  my_data_title0 += '<span style="left: 10px;direction: rtl; display: inline-block; background: #F6F7BF;font-weight: normal; text-align: center;border: 0 solid red;border-radius: 6px; padding: 3px;top: 66%;position: fixed;font-family: Tahoma; font-size: 25px; background-color: #ff5;  box-shadow: 0 2px 5px rgb(0 0 0 / 50%);color: Blue !important; width: 150px;animation: NotificationAnim 2s infinite linear;"><a href="' + HelpLink + '" title=" سایت  هوش فعال" target="_blank"><b>هوش فعال</b><br><b>' + FilterName + '</b></a> </span></div>';

  var my_data_title1 = '<span style = "border: 1px solid gray;width: 100%;display: inline-block;text-align: center;color: #000;background: #FFD384;font-weight: normal;"> ' + column2 + '</span>'
  var my_data_title2 = '<span style = "border: 1px solid gray;width: 100%;display: inline-block;text-align: right;color: #000;background: #FFF9B0;font-weight: normal;"> ' + column3 + '</span>'

  //cfield Titel
  var cfield0_title = my_data_title0, cfield1_title = my_data_title1, cfield2_title = my_data_title2;
  //Columns width
  var wc0 = "100px", wc1 = "100px", wc2 = "1200px";

  var title = document.getElementsByClassName("t0head");
  for (var i = 0; i < title.length; i++) {
    if (title[i].outerHTML.includes("cfield0")) {
      title[i].innerHTML = cfield0_title;
      // title[i].style.width = wc0;
      title[i].style.fontWeight = "normal";
    }

    else if (title[i].outerHTML.includes("cfield1")) {
      title[i].innerHTML = cfield1_title;
      // title[i].style.width = wc1;
      title[i].style.fontWeight = "normal";
    }
    else if (title[i].outerHTML.includes("cfield2")) {
      title[i].innerHTML = cfield2_title;
      //title[i].style.width = wc2;
      title[i].style.fontWeight = "normal";
    }
  }

}

function upFunc(pr) {
  var str = "";
  var period = pr;   //تعدار روزهایتان را وارد کنید
  var i;
  var j;
  var up = 0;
  var down = 300000;
  for (i = 1; i < period; i++) {
    j = i - 1
    if (([ih][i].PClosing > [ih][j].PClosing) && [ih][i].PClosing > up)
      up = [ih][i].PClosing;
  }
  for (i = 1; i < period; i++) {
    j = i - 1
    if ([ih][i].PClosing > [ih][j].PClosing && [ih][i].PClosing < down)
      down = [ih][j].PClosing;
  }
  var darsad0 = String((((up) / (pl)) - 1) * 100).fontcolor("#16B916");
  var darsad1 = String((((down) / (pl)) - 1) * 100).fontcolor("Magenta");
  str += " 🔺" + String(up).fontcolor("#16B916");

  return str;
}

function Downfunc(pr) {
  var str = "";
  var period = pr;   //تعدار روزهایتان را وارد کنید
  var i;
  var j;
  var up = 0;
  var down = 300000;
  for (i = 1; i < period; i++) {
    j = i - 1
    if (([ih][i].PClosing > [ih][j].PClosing) && [ih][i].PClosing > up)
      up = [ih][i].PClosing;
  }
  for (i = 1; i < period; i++) {
    j = i - 1
    if ([ih][i].PClosing > [ih][j].PClosing && [ih][i].PClosing < down)
      down = [ih][j].PClosing;
  }
  var darsad0 = String((((up) / (pl)) - 1) * 100).fontcolor("#16B916");
  var darsad1 = String((((down) / (pl)) - 1) * 100).fontcolor("Magenta");

  str += "🔻" + String(down).fontcolor("Magenta");

  return str;
}

function PersentageFunc(pr) {
  var str = "";
  var period = pr;   //تعدار روزهایتان را وارد کنید
  var i;
  var j;
  var up = 0;
  var down = 300000;
  for (i = 1; i < period; i++) {
    j = i - 1
    if (([ih][i].PClosing > [ih][j].PClosing) && [ih][i].PClosing > up)
      up = [ih][i].PClosing;
  }
  for (i = 1; i < period; i++) {
    j = i - 1
    if ([ih][i].PClosing > [ih][j].PClosing && [ih][i].PClosing < down)
      down = [ih][j].PClosing;
  }
  var darsad0 = String((((up) / (pl)) - 1) * 100).fontcolor("Green");
  var darsad1 = String((((down) / (pl)) - 1) * 100).fontcolor("red");
  str += Round(darsad1, 1) + " | ".fontcolor("blue") + Round(darsad0, 1)
  return str;
}


function sma(start, day) {
  var sum = 0;
  for (var i = start; i < start + day; i++) {
    sum += [ih][i].PDrCotVal
  }
  var avg = sum / day;
  return avg;
}
function ema(start, day) {
  var len = [ih].length;
  var first = sma(len - day, day);
  var x = 2 / (day + 1);
  for (var i = len - day; i >= 0; i--) {
    if (i == len - day) {
      [ih][i].ema = first;
    }
    else {
      [ih][i].ema = ([ih][i].PDrCotVal - [ih][i + 1].ema) * x + [ih][i + 1].ema;
    }
  }
  return [ih][start].ema;
}