Due the new ESMA rules, Free Margin Check becomes now important.

1st Alert: Level Margin <250%

2nd Alert: Level Margin <200%

Stop Trading: Level Margin <150%

Start Trading again, Margin >300%

Along with the Ordersend execution, 1st you have to check if "MarginStopTrading" is true or false!

Dear Ixbone Customers, if you have any questions, please feel free to contact us!

//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
//| Free Margin Check - Warns and stops if levels reached and restart trading above HighWatermark                                                                                                                                                         |
//+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

#property copyright "Copyright © 2004- IXBONE/Michael Orth"
#property link "ixbone.com"
#property strict

double marginlevel;
bool MarginAlert0=true,MarginAlert1=true;
datetime time1,time2;

extern string MarginStop="=Margin - StopTrading=";
extern bool STMarginAlertON=false;
extern bool STMarginEmailON=true;
extern bool STMarginPrintON=true;
extern bool STMarginSoundON=false;
extern string STMarginExtSound="alert.wav";
extern string MarginOKStop="=Margin - Trading Resumed=";
extern bool STMarginOKAlertON=false;
extern bool STMarginOKEmailON=true;
extern bool STMarginOKPrintON=true;
extern bool STMarginOKSoundON=false;
extern string STMarginOKExtSound="alert.wav";
extern string Accountinfo0="=Which CurrencyChart is used, example EURUSD=";
extern string Accountinfo1="=Named currency prevents sending on all used pairs!!=";
extern string AccountChart="EURUSD";

   if(FreeMarginCheck){     
      marginlevel=AccountInfoDouble(ACCOUNT_MARGIN_LEVEL);
      if(Symbol()==AccountChart+"fx" || Symbol()==AccountChart+"m" || Symbol()==AccountChart+"#" || Symbol()==AccountChart) {
      if ( (marginlevel < 250) && (marginlevel > 200) ) {
         MarginAlerts(1,0);
         }

      if ( (marginlevel < 200) && (marginlevel > 150) ) {
         MarginAlerts(0,1);
         }
       }

      if ( (marginlevel < 150) && (marginlevel > 100) ) {
         MarginStopTrading=true;
         StopTradingAlerts(1,0);
         }

      if(Symbol()==AccountChart+"fx" || Symbol()==AccountChart+"m" || Symbol()==AccountChart+"#" || Symbol()==AccountChart) {
      if ( MarginStopTrading==true && (marginlevel > 300 ) ) {
         MarginStopTrading=false;
         StopTradingAlerts(0,1);
         }
      }

      if ( MarginStopTrading==true && (marginlevel > 300 ) ) {
         MarginStopTrading=false;
         }     
      }

   void MarginAlerts(int AMargin250, int AMargin200){  
        if(AMargin250==1){
            if(time1!=Time[0]){
         if(MAAlertON==true)Alert(DoubleToStr(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),0)+" Low Margin - <250% - Caution! ","Date="+TimeToStr(TimeCurrent()));
         if(MAEmailON==true)SendMail(DoubleToStr(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),0)+" Low Margin - <250% - Caution! ","Date="+TimeToStr(TimeCurrent()));
         if(MAPrintON==true)Print(DoubleToStr(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),0)+" Low Margin - <250% - Caution! ","Date="+TimeToStr(TimeCurrent()));
         if(MASoundON==true)PlaySound(MAExtSoundFileName);
            time1=Time[0];
        }
     }

       if(AMargin200==1){
            if(time2!=Time[0]){
         if(MAAlertON==true)Alert(DoubleToStr(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),0)+" Low Margin - <200% - Caution! ","Date="+TimeToStr(TimeCurrent()));
         if(MAEmailON==true)SendMail(DoubleToStr(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),0)+" Low Margin - <200% - Caution! ","Date="+TimeToStr(TimeCurrent()));
         if(MAPrintON==true)Print(DoubleToStr(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),0)+" Low Margin - <200% - Caution! ","Date="+TimeToStr(TimeCurrent()));
         if(MASoundON==true)PlaySound(MAExtSoundFileName);
            time2=Time[0];
        }
     }     
   return;
   }

   void StopTradingAlerts(int STMargin,int STMarginOK){  
        if(STMargin==1){
            if(MarginAlert0){
         if(STMarginAlertON==true)Alert(DoubleToStr(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),0)+" Low Margin - <150% - Stop Trading! ","Date="+TimeToStr(TimeCurrent()));
         if(STMarginEmailON==true)SendMail(DoubleToStr(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),0)+" Low Margin - <150% - Stop Trading! ","Date="+TimeToStr(TimeCurrent()));
         if(STMarginPrintON==true)Print(DoubleToStr(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),0)+" Low Margin - <150% - Stop Trading! ","Date="+TimeToStr(TimeCurrent()));
         if(STMarginSoundON==true)PlaySound(STMarginExtSound);
            MarginAlert0=false;
            MarginAlert1=true;
        }
     }

        if(STMarginOK==1){
            if(MarginAlert1){
         if(STMarginOKAlertON==true)Alert(DoubleToStr(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),0)+" Normal Margin - >300% - Trading Active  Again!","Date="+TimeToStr(TimeCurrent()));
         if(STMarginOKEmailON==true)SendMail(DoubleToStr(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),0)+" Normal Margin - >300% - Trading Active Again! ","Date="+TimeToStr(TimeCurrent()));
         if(STMarginOKPrintON==true)Print(DoubleToStr(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL),0)+" Normal Margin - >300% - Trading Active Again! ","Date="+TimeToStr(TimeCurrent()));
         if(STMarginOKSoundON==true)PlaySound(STMarginOKExtSound);
            MarginAlert1=false;
            MarginAlert0=true;
        }
     }
   return;
  }