---
# System prepended metadata

title: Untitled

---

//+------------------------------------------------------------------+
//|                                                  candle_sign.mq5 |
//|                                              2022/00/00  RELEASE |
//|                                      by Cross Retailing Co, LTD. |
//+------------------------------------------------------------------+
#property copyright "Copyright (c) , Cross Retailing Co, LTD."
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2

#property indicator_color1 clrHotPink
#property indicator_color2 clrCornflowerBlue

//#property indicator_style1 STYLE_SOLID
//#property indicator_style2 STYLE_SOLID

#property indicator_width1 3
#property indicator_width2 3

#property indicator_type1 DRAW_ARROW
#property indicator_type2 DRAW_ARROW

input bool Alert_ON=true;
input bool Mail_ON=true;
input bool USDJPY_Alert=false;
input bool EURJPY_Alert=false;
input bool GBPJPY_Alert=false;
input bool AUDJPY_Alert=false;
input bool CADJPY_Alert=false;
input bool EURUSD_Alert=false;
input bool GBPUSD_Alert=false;
input bool AUDUSD_Alert=false;
input bool USDCAD_Alert=false;
input bool EURGBP_Alert=false;
input bool EURAUD_Alert=false;
input bool EURCAD_Alert=false;
input bool GBPAUD_Alert=false;
input bool GBPCAD_Alert=false;
input bool AUDCAD_Alert=false;

/*extern int MAMetod=0;
extern int MAPeriod=5;
extern int MAMetod2=1;
extern int MAPeriod2=10;*/

double up[],dn[];

int other[15][5];

int Buyflg = 0;
int Sellflg = 0;

double Pips,Tick_Value;

static double ArrowPos=20.0;
static double adjustment=1.0;

string plusSymbol;

double OpenPrice[],ClosePrice[],High_LowPrice[],High_LowPrice2[];
int AveCandle_Handle;

datetime timeflg;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
	PipsAdjust();
	SetIndexBuffer(0,up);
   SetIndexBuffer(1,dn);
   PlotIndexSetString(0,PLOT_LABEL,NULL);
   PlotIndexSetString(1,PLOT_LABEL,NULL);
	
	PlotIndexSetInteger(0,PLOT_ARROW,233);
   PlotIndexSetInteger(1,PLOT_ARROW,234);
   ArraySetAsSeries(up,true);
   ArraySetAsSeries(dn,true);
   ArraySetAsSeries(OpenPrice,true);
   ArraySetAsSeries(ClosePrice,true);
   ArraySetAsSeries(High_LowPrice,true);
   ArraySetAsSeries(High_LowPrice2,true);

   AveCandle_Handle = iCustom(Symbol(),PERIOD_CURRENT,"MAX_Average_Candle");
   //testAveCandle_Handle = iCustom(Symbol(),PERIOD_CURRENT,"heiken_ashi");
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   //int counted_bars=IndicatorCounted();
   if(prev_calculated<0)return(-1);
   int i;
   if(CopyBuffer(AveCandle_Handle,0,0,Bars(_Symbol,PERIOD_CURRENT),High_LowPrice) == -1)return(0);
   if(CopyBuffer(AveCandle_Handle,1,0,Bars(_Symbol,PERIOD_CURRENT),High_LowPrice2) == -1)return(0);
   if(CopyBuffer(AveCandle_Handle,3,0,Bars(_Symbol,PERIOD_CURRENT),OpenPrice) == -1)return(0);
   if(CopyBuffer(AveCandle_Handle,4,0,Bars(_Symbol,PERIOD_CURRENT),ClosePrice) == -1)return(0);

   if(0==prev_calculated){
		ArrayInitialize(up,EMPTY_VALUE);
		ArrayInitialize(dn,EMPTY_VALUE);
		//ArrayInitialize(OpenPrice,EMPTY_VALUE);
		//ArrayInitialize(ClosePrice,EMPTY_VALUE);
		//ArrayInitialize(High_LowPrice,EMPTY_VALUE);
		//ArrayInitialize(High_LowPrice2,EMPTY_VALUE);
		for(i=iBars(_Symbol,PERIOD_CURRENT)-10;i>=0;i--){
		   check(i,false);
		}
	}else{
		check(0,true);
	}
   return(rates_total);
}
//+------------------------------------------------------------------+
void check(int i,bool message)
{  
   if(i < 100){
   Print(iTime(_Symbol,PERIOD_CURRENT,i)," High_LowPrice[i] ",High_LowPrice[i],"  High_LowPrice2[i] ",High_LowPrice2[i],"  OpenPrice[i] ",OpenPrice[i],"  ClosePrice[i] ",ClosePrice[i]);
   timeflg = iTime(_Symbol,PERIOD_CURRENT,i);
   }
   
   
   //適用していない15通貨の判定
   if(Alert_ON || Mail_ON){
      GetSmoothedPrice(i,USDJPY_Alert,message,"USDJPY"+plusSymbol,0);
      GetSmoothedPrice(i,EURJPY_Alert,message,"EURJPY"+plusSymbol,1);
      GetSmoothedPrice(i,GBPJPY_Alert,message,"GBPJPY"+plusSymbol,2);
      GetSmoothedPrice(i,AUDJPY_Alert,message,"AUDJPY"+plusSymbol,3);
      GetSmoothedPrice(i,CADJPY_Alert,message,"CADJPY"+plusSymbol,4);
      GetSmoothedPrice(i,EURUSD_Alert,message,"EURUSD"+plusSymbol,5);
      GetSmoothedPrice(i,GBPUSD_Alert,message,"GBPUSD"+plusSymbol,6);
      GetSmoothedPrice(i,AUDUSD_Alert,message,"AUDUSD"+plusSymbol,7);
      GetSmoothedPrice(i,USDCAD_Alert,message,"USDCAD"+plusSymbol,8);
      GetSmoothedPrice(i,EURGBP_Alert,message,"EURGBP"+plusSymbol,9);
      GetSmoothedPrice(i,EURAUD_Alert,message,"EURAUD"+plusSymbol,10);
      GetSmoothedPrice(i,EURCAD_Alert,message,"EURCAD"+plusSymbol,11);
      GetSmoothedPrice(i,GBPAUD_Alert,message,"GBPAUD"+plusSymbol,12);
      GetSmoothedPrice(i,GBPCAD_Alert,message,"GBPCAD"+plusSymbol,13);
      GetSmoothedPrice(i,AUDCAD_Alert,message,"AUDCAD"+plusSymbol,14);
   }
   //平均足スムーズドの価格確認
   double SortPrice[4];
   SortPrice[0] = High_LowPrice[i];
   SortPrice[1] = High_LowPrice2[i];
   SortPrice[2] = OpenPrice[i];
   SortPrice[3] = ClosePrice[i];
   
   //ArraySort(SortPrice,4,0,MODE_DESCEND);
   ArraySetAsSeries(SortPrice,false);
   //同じ方向のサインを出さない処理
   if(OpenPrice[i+1] < ClosePrice[i+1] && up[i+1] != EMPTY_VALUE){
      Buyflg = 1;
      Sellflg = 0;

   }else if(OpenPrice[i+1] > ClosePrice[i+1] && dn[i+1] != EMPTY_VALUE){
      Buyflg = 0;
      Sellflg = 1;
   }
   
   //サイン判定
   if(OpenPrice[i] < ClosePrice[i] && Buyflg == 0){
      if(up[i] == EMPTY_VALUE){
         up[i]=SortPrice[3]-SignPosition();
         if(message){
            AlertMail("Buy chance!",Symbol());
         }
         //Print("up  ",iTime(_Symbol,PERIOD_CURRENT,i),"   ",up[i]);
      }
   }else if(OpenPrice[i] > ClosePrice[i]
    && Sellflg == 0){
      if(dn[i] == EMPTY_VALUE){
         dn[i]=SortPrice[0]+SignPosition();
         if(message){
            AlertMail("Sell chance!",Symbol());
         }
      }
   }
   
   //条件が崩れていたらサインを消す
   if(OpenPrice[i+1] > ClosePrice[i+1] && up[i+1] != EMPTY_VALUE){
      up[i+1] = EMPTY_VALUE;
      
   }
   if(OpenPrice[i+1] < ClosePrice[i+1] && dn[i+1] != EMPTY_VALUE){
      dn[i+1] = EMPTY_VALUE;
   }
}
//他の15通貨の条件判定
void GetSmoothedPrice(int i,bool message, bool message2,string symbol,int index)
{
   if(message){
      //OpenPrice = iCustom(symbol,0,"MAX_Average_Candle",2,i);
      //ClosePrice = iCustom(symbol,0,"MAX_Average_Candle",3,i);
      //OpenPrice2 = iCustom(symbol,0,"MAX_Average_Candle",2,i+1);
      //ClosePrice2 = iCustom(symbol,0,"MAX_Average_Candle",3,i+1);
      
      if(OpenPrice[i+1] < ClosePrice[i+1] && other[index][0] != EMPTY_VALUE){
         other[index][2] = 1;
         other[index][3] = 0;
      }else if(OpenPrice[i+1] > ClosePrice[i+1] && other[index][1] != EMPTY_VALUE){
         other[index][2] = 0;
         other[index][3] = 1;
      }
      
      if(OpenPrice[i] < ClosePrice[i] && other[index][2] == 0){
         if(other[index][0] == EMPTY_VALUE){
            other[index][0]=1;
            other[index][1]=EMPTY_VALUE;
            other[index][4] = i;
            if(message2 && symbol != Symbol()){
               AlertMail("Buy chance!",symbol);
            }
         }
      }else if(OpenPrice[i] > ClosePrice[i] && other[index][3] == 0){
         if(other[index][1] == EMPTY_VALUE){
            other[index][1]=1;
            other[index][0]=EMPTY_VALUE;
            other[index][4] = i;
            if(message2 && symbol != Symbol()){
               AlertMail("Sell chance!",symbol);
            }
         }
      }
   
      if(OpenPrice[i+1] > ClosePrice[i+1] && other[index][0] != EMPTY_VALUE && other[index][4]  < i ){
         other[index][0] = EMPTY_VALUE;
         
      }
      if(OpenPrice[i+1] < ClosePrice[i+1] && other[index][1] != EMPTY_VALUE && other[index][4]  < i){
         other[index][1] = EMPTY_VALUE;
      }
   }
}
void AlertMail(string AlertMassage,string symbol)
{
	string strText,strPeriod,strSymbol;
	int nPeriod=Period();
	switch(nPeriod){
		case PERIOD_H1:strPeriod="] [H1] ";break;
		case PERIOD_H4:strPeriod="] [H4] ";break;
		case PERIOD_D1:strPeriod="] [D1] ";break;
		case PERIOD_W1:strPeriod="] [W1] ";break;
		case PERIOD_MN1:strPeriod="] [MN] ";break;
		default:strPeriod="] [M"+DoubleToString(nPeriod,0)+"] ";
	}
	strSymbol=symbol;
	strText="[MAXONE] ["+StringSubstr(strSymbol,0,3)+"/"+StringSubstr(strSymbol,3,3)+strPeriod+AlertMassage;
	if(Alert_ON)Alert(strText);
	if(Mail_ON)SendMail(strText,strText);
}

void PipsAdjust()
{
   double dig1,dig2;
   string s=Symbol();

   if(6<StringLen(s))s=StringSubstr(s,6);else s="";
   dig1=SymbolInfoInteger("USDJPY"+s,SYMBOL_DIGITS);
   dig2=SymbolInfoInteger("EURUSD"+s,SYMBOL_DIGITS);
   Tick_Value=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
   if(2==dig1||4==dig2){
      Pips = _Point;
   }else        if(3==dig1||5==dig2){
     Pips = _Point*10;
      Tick_Value = Tick_Value*10;
   }else        if(4==dig1||6==dig2){
      Pips = _Point*100;
      Tick_Value = Tick_Value*100;
   }else        if(0==dig1&&0==dig2){
      if(3==_Digits||5==_Digits){
         Pips = _Point*10;
         Tick_Value = Tick_Value*10;
      }else{
         Pips = _Point;
      }
   }else{
      Pips = _Point*10;
      Tick_Value = Tick_Value*10;
   }
}

//サインの表示位置
double SignPosition()
{
   return(ArrowPos*adjustment*Point());
}
