//+------------------------------------------------------------------+
//|                                               MarginCallRate.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "さとしぃ"
#property link      "http://fxdreamer.blog.shinobi.jp/"

#property indicator_chart_window
//#property indicator_buffers 1
//double MRCobj[];

//表示箇所 0 = 左上　1 = 右上 2 = 左下 3 = 右下
extern int SetPoint = 0;

extern int TextSize = 14;
extern bool debug = false;

int LineSize = 3;

double MRC = 0;
double POS_n_BUY;
double POS_n_SELL;
double POS_n_BUY_LOTS;
double POS_n_SELL_LOTS;
double POS_n_BUYRATE;
double POS_n_SELLRATE;

double POS_n_total;
double POS_n_average_rate_buy;
double POS_n_average_rate_sell;
double POS_n_total_lots;
double PIP_sl;
double PIP_Room_BUY;
double PIP_Room_SELL;
double Endure_Rate_BUY;
double Endure_Rate_SELL;
double Margin_Call_Rate_BUY;
double Margin_Call_Rate_SELL;

double CurrencyPoint;
double POINT;

string MOZIRETSU = "";

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//  SetIndexBuffer(0,MRCobj);
//  SetIndexLabel(0,"MarginCallRate");
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
double MarginCallRate()
{
    POS_n_BUY  = 0;
    POS_n_SELL = 0;
    POS_n_BUY_LOTS  = 0;
    POS_n_SELL_LOTS = 0;
    POS_n_BUYRATE = 0;
    POS_n_SELLRATE = 0;
    POS_n_total = 0;
    POS_n_average_rate_buy = 0;
    POS_n_average_rate_sell = 0;
    POS_n_total_lots = 0;
    PIP_Room_BUY = 0;
    PIP_Room_SELL = 0;
    PIP_sl = 1000;
    Endure_Rate_BUY = 0;
    Endure_Rate_SELL = 0;    
    Margin_Call_Rate_BUY = 0;
    Margin_Call_Rate_SELL = 0;    
    
    for( int i = 0 ; i < OrdersTotal() ; i++ ){
        if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ){
            break;
        }
    if(OrderSymbol() != Symbol()){
      continue;
    }
        if( OrderType() == OP_BUY ){
            POS_n_BUY++;
            POS_n_BUYRATE = POS_n_BUYRATE + OrderOpenPrice()*OrderLots();
            POS_n_BUY_LOTS = POS_n_BUY_LOTS + OrderLots();
        }
        else if( OrderType() == OP_SELL ){
            POS_n_SELL++;
            POS_n_SELLRATE = POS_n_SELLRATE + OrderOpenPrice()*OrderLots();
            POS_n_SELL_LOTS = POS_n_SELL_LOTS + OrderLots();
        }
    }

    POS_n_total = POS_n_BUY + POS_n_SELL;


if(AccountCurrency()=="USD"){

   CurrencyPoint=1;

    if(StringFind(Symbol(),"USD",3)>0){
      PIP_sl = MarketInfo(Symbol(),MODE_ASK)*10;
    }
    if(StringFind(Symbol(),"JPY",3)>0){
      PIP_sl = (1+1/MarketInfo(Symbol(),MODE_ASK))*10;
    }
    if(StringFind(Symbol(),"GBP",3)>0){
      PIP_sl = MarketInfo(Symbol(),MODE_ASK)*MarketInfo("GBPUSD",MODE_ASK)*10;
   }   

}
if(AccountCurrency()=="JPY"){

   CurrencyPoint=1;

    if(StringFind(Symbol(),"JPY",3)>0){
      
      PIP_sl = MarketInfo(Symbol(),MODE_LOTSIZE)*Point;
      
    }
    if(StringFind(Symbol(),"USD",3)>0){
      PIP_sl = MarketInfo(Symbol(),MODE_ASK)*MarketInfo("USDJPY",MODE_ASK)*10;
    }
    if(StringFind(Symbol(),"GBP",3)>0){
      PIP_sl = MarketInfo(Symbol(),MODE_ASK)*MarketInfo("GBPJPY",MODE_ASK)*10;
    }   

}

    Print2("PIP_sl = ",PIP_sl);    

    if(POS_n_BUY>0 &&  POS_n_SELL>0){
      if(POS_n_BUY_LOTS > POS_n_SELL_LOTS){
         POS_n_BUY_LOTS = POS_n_BUY_LOTS - POS_n_SELL_LOTS;
         POS_n_average_rate_buy = POS_n_BUYRATE/POS_n_BUY_LOTS;              
         PIP_Room_BUY = ((AccountFreeMargin()*CurrencyPoint)/PIP_sl)/POS_n_BUY_LOTS;    
         Endure_Rate_BUY = PIP_Room_BUY*Point;
         Margin_Call_Rate_BUY = Ask - Endure_Rate_BUY;  

         return(Margin_Call_Rate_BUY);    
      
      }
      
      if(POS_n_SELL_LOTS > POS_n_BUY_LOTS){
         POS_n_SELL_LOTS = POS_n_SELL_LOTS - POS_n_BUY_LOTS;
         POS_n_average_rate_sell = POS_n_SELLRATE/POS_n_SELL_LOTS;       
         PIP_Room_SELL = ((AccountFreeMargin()*CurrencyPoint)/PIP_sl)/POS_n_SELL_LOTS;    
         Endure_Rate_SELL = PIP_Room_SELL*Point;
         Margin_Call_Rate_SELL = Ask + Endure_Rate_SELL;   

         return(Margin_Call_Rate_SELL);
      
      }         
    }      

    if(POS_n_BUY>0){
    
      POS_n_average_rate_buy = POS_n_BUYRATE/POS_n_BUY_LOTS;       
      PIP_Room_BUY = ((AccountFreeMargin()*CurrencyPoint)/PIP_sl)/POS_n_BUY_LOTS;    
      Endure_Rate_BUY = PIP_Room_BUY*Point;
      Margin_Call_Rate_BUY = Ask - Endure_Rate_BUY;      

      return(Margin_Call_Rate_BUY);
    
    }
    
    if(POS_n_SELL>0){
    
      POS_n_average_rate_sell = POS_n_SELLRATE/POS_n_SELL_LOTS;       
      PIP_Room_SELL = ((AccountFreeMargin()*CurrencyPoint)/PIP_sl)/POS_n_SELL_LOTS;    
      Endure_Rate_SELL = PIP_Room_SELL*Point;
      Margin_Call_Rate_SELL = Ask + Endure_Rate_SELL;      

      return(Margin_Call_Rate_SELL);
    
    }
          
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {

 
   int counted_bars=IndicatorCounted();
   int limit = Bars-counted_bars;
   
   ObjectCreate("Margin",OBJ_LABEL,0,0,0);      
   
   for(int i=limit-1; i>=0; i--){

   ObjectSet("Margin",OBJPROP_COLOR,Black);
   ObjectSet("Margin",OBJPROP_CORNER,SetPoint);
   ObjectSet("Margin",OBJPROP_XDISTANCE,10);
   ObjectSet("Margin",OBJPROP_YDISTANCE,10);
   //ObjectSet("Margin",OBJPROP_TIMEFRAMES,PERIOD_M1);
   ObjectSet("Margin",OBJPROP_STYLE,0);
   ObjectSet("Margin",OBJPROP_WIDTH,LineSize);
   MRC = MarginCallRate();
   //MRCobj[i] = MRC;
   Print2("MRC = ",MRC);
   
    Print2("POS_n_average_rate_buy = ",POS_n_average_rate_buy);
    Print2("POS_n_average_rate_sell = ",POS_n_average_rate_sell);
    Print2("PIP_Room_BUY = ",PIP_Room_BUY);
    Print2("PIP_Room_SELL = ",PIP_Room_SELL);
    Print2("Endure_Rate_BUY = ",Endure_Rate_BUY);
    Print2("Endure_Rate_SELL = ",Endure_Rate_SELL);
    Print2("Margin_Call_Rate_BUY = ",Margin_Call_Rate_BUY);
    Print2("Margin_Call_Rate_SELL = ",Margin_Call_Rate_SELL);
   
   
   if(Point==0.01)  POINT=2;
   if(Point==0.001)  POINT=3;
   if(Point==0.0001) POINT=4;
   if(Point==0.00001) POINT=5;
   
   MOZIRETSU = "MarginCallRate = "+ DoubleToStr(MRC,POINT);
   
   
   ObjectSetText("Margin",MOZIRETSU,TextSize,"Times New Roman",Black);
   }
   return(MRC);
  }
void Print2(string data1,string data2="",string data3="",string data4="",string data5="",string data6="",string data7="",string data8="",string data9="",string data10=""){
   if(debug == true){
      Print(data1,data2,data3,data4,data5,data6,data7,data8,data9,data10);
   }
}
//+------------------------------------------------------------------+