Need Help With AccountMarginLevel

 

Hi All

I'm Struggling to figure out the ACCOUNT_MARGIN_LEVEL function. I want ACCOUNT_MARGIN_LEVEL below 200% not to open trades AT ALL. If there's a way to do it or different way to do this, please feel free to help out.

Here is my code below

#property copyright "Jack Buda Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern double TakeProfit=300;
extern double StopLoss=1000;
extern int MagicNumber=700;
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int CountTrades()
 {
  int Count=0;
  for(int a=OrdersTotal()-1;a>=0;a--)
   {
    if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES))
    if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
    if(OrderType()==OP_BUY||OrderType()==OP_SELL)
    Count++;
   }
  return(Count);
 } 

bool NewCandle()
 {
  static int BarsOnChart=0;
  if(Bars==BarsOnChart)
  return(false);
  BarsOnChart=Bars;
  return(true);
 } 
 
void Buy()
 {
  double StopLossBuy=Ask-StopLoss*Point;
  double TakeProfitBuy=Ask+TakeProfit*Point;
  
  double CloseBar=iClose(NULL,0,1);
  double OpenBar=iOpen(NULL,0,1);
  double LowBar=iLow(NULL,0,1);
  double HighBar=iHigh(NULL,0,1);
  
  if(CloseBar>OpenBar)
  {
   int BuyTrade=OrderSend(Symbol(),OP_BUY,0.1,Ask,0,0,0,"JackBuda",MagicNumber,0,clrBlue);
   if(BuyTrade>0)
    {
     bool OSBuy=OrderSelect(BuyTrade,SELECT_BY_POS);
     bool BuyMod=OrderModify(BuyTrade,OrderOpenPrice(),StopLossBuy,TakeProfitBuy,0,clrNONE);
     RefreshRates();
    }
  }
 }
 
void Sell()
 {
  double StopLossSell=Bid+StopLoss*Point;
  double TakeProfitSell=Bid-TakeProfit*Point;
  
  double CloseBar=iClose(NULL,0,1);
  double OpenBar=iOpen(NULL,0,1);
  double LowBar=iLow(NULL,0,1);
  double HighBar=iHigh(NULL,0,1);
  
  if(CloseBar<OpenBar)
  {
   int SellTrade=OrderSend(Symbol(),OP_SELL,0.1,Bid,0,0,0,"JackBuda",MagicNumber,0,clrRed);
   if(SellTrade>0)
    {
     bool OSSell=OrderSelect(SellTrade,SELECT_BY_POS);
     bool SellMod=OrderModify(SellTrade,OrderOpenPrice(),StopLossSell,TakeProfitSell,0,clrNONE);
     RefreshRates();
    }
  }
 } 
 
void OnTick()
  {
   double AccountMarginLevel=AccountInfoDouble(ACCOUNT_MARGIN_LEVEL);
   int NoTrades=(int)AccountInfoInteger(ACCOUNT_LIMIT_ORDERS);
   
   if(AccountMarginLevel<200&&NoTrades>0){Buy();}
   if(AccountMarginLevel<200&&NoTrades>0){Sell();}
   
   if(AccountBalance()>10&&CountTrades()<2){Buy();}
   if(AccountBalance()>10&&CountTrades()<2){Sell();}
   
  for(int Loop2=OrdersTotal()-1;Loop2>=0;Loop2--)
  {
   if(OrderSelect(Loop2,SELECT_BY_POS,MODE_TRADES))
   if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
   { 
    bool CloseTrade=false;
    int Type=OrderType(); 
    double Profit=OrderLots()*200;
    switch(Type)
    {
     case OP_BUY:if(OrderProfit()>=Profit)CloseTrade=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrGreen);break;
     case OP_SELL:if(OrderProfit()>=Profit)CloseTrade=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,clrGreen);break;                
    }
   }
  }
 }