爆仓卫士源代码-Brust guard source code

 


//+------------------------------------------------------------------+

//|                                                   BrustGuard.mq4 |
//|                              BillAbiao,QQ:327151901,WX:billabiao |
//|                               https://one.exness.link/a/sduus6ff |
//+------------------------------------------------------------------+
#property copyright "BillAbiao,QQ:327151901,WX:billabiao"
#property link      "https://one.exness.link/a/sduus6ff"
#property version   "1.00"
#property strict
input string _show1_="";//when Equity < setMinEquity, then close All orders
input string _show2_="";//当净值小于设定值时,平掉所有订单
input double setMinEquity=1000;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(6);
   //LabelCreate();
   ObjectDelete( 0,"Label");
   LabelCreate(0,"Label",0,10,30,0,"","Arial",35,clrYellow,0.0);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
      RUN(1);
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
      RUN(2);
  }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

//---
   return(ret);
  }
//+------------------------------------------------------------------+

bool runFlag=false;
string debugMsg="";

void RUN(int who)
{
   debugMsg=StringFormat("date=%s,balance=%.2f,equity=%.2f,setMinEquity=%.2f\n", TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS),AccountBalance(),AccountEquity(),setMinEquity);
   if( runFlag==false) 
   {
      runFlag=true;
      mainRUN();
      runFlag=false;
   }
   
   debugMsg+=StringFormat("who=%d", who);
   Comment(debugMsg);
   return ;
}


void mainRUN()
{
  // showMsg();
  string str1=StringFormat("Balance=%.2f, Equity=%.2f, setMinEquity=%.2f",AccountBalance(),AccountEquity(),setMinEquity);
  LabelTextChange(0,"Label",str1);
   if( AccountEquity()< setMinEquity  )
   {
      closeAllChart();
      closeAllOrders();
   }
   
   return ;
}


void closeAllOrders()
{
   int i,n=OrdersTotal();
   int k;
   for(k=0;k<10;k++)
   {
      n=OrdersTotal();
      if( n<=0) break;
      for(i=0;i<n;i++)
      {
         if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
            continue;
         if(OrderType()==OP_BUY)
         {
            double bid=SymbolInfoDouble(OrderSymbol(), SYMBOL_BID);
            if(!OrderClose(OrderTicket(),OrderLots(),bid,3,Violet))
            {
               break;
            }
         }
         else if(OrderType()==OP_SELL)
         {
            double ask=SymbolInfoDouble(OrderSymbol(), SYMBOL_ASK);
            if(!OrderClose(OrderTicket(),OrderLots(),ask,3,Violet))
            {
               break;
            }
         }
         else{
            bool ret=OrderDelete(OrderTicket(),Violet);
         }
         
      }
      
   }
 
   return ;

}

void closeAllChart()
{
   
//   long  id1=ChartFirst();
   long  id0=ChartID();
  // debugMsg+=StringFormat("id0=%I64d,id1=%I64d\n",id0, id1);
   
   for(;;)
   {
      long id1=ChartFirst();
      if( id1==-1) break;
      if( id1!=id0)
      {
         ChartClose(id1);
      }
      else{
         long id2=ChartNext(id1);
         if( id2==-1) break;
         ChartClose(id2);
         
      }
   }
}

//+------------------------------------------------------------------+ 
//| Create a text label                                              | 
//+------------------------------------------------------------------+ 
bool LabelCreate(const long              chart_ID=0,               // chart's ID 
                 const string            name="Label",             // label name 
                 const int               sub_window=0,             // subwindow index 
                 const int               x=0,                      // X coordinate 
                 const int               y=0,                      // Y coordinate 
                 const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring 
                 const string            text="Label",             // text 
                 const string            font="Arial",             // font 
                 const int               font_size=10,             // font size 
                 const color             clr=clrRed,               // color 
                 const double            angle=0.0,                // text slope 
                 const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type 
                 const bool              back=false,               // in the background 
                 const bool              selection=false,          // highlight to move 
                 const bool              hidden=true,              // hidden in the object list 
                 const long              z_order=0)                // priority for mouse click 
  { 
//--- reset the error value 
   ResetLastError(); 
//--- create a text label 
   if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0)) 
     { 
      Print(__FUNCTION__, 
            ": failed to create text label! Error code = ",GetLastError()); 
      return(false); 
     } 
//--- set label coordinates 
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x); 
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y); 
//--- set the chart's corner, relative to which point coordinates are defined 
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner); 
//--- set the text 
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text); 
//--- set text font 
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font); 
//--- set font size 
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size); 
//--- set the slope angle of the text 
   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle); 
//--- set anchor type 
   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor); 
//--- set color 
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr); 
//--- display in the foreground (false) or background (true) 
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back); 
//--- enable (true) or disable (false) the mode of moving the label by mouse 
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection); 
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection); 
//--- hide (true) or display (false) graphical object name in the object list 
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden); 
//--- set the priority for receiving the event of a mouse click in the chart 
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order); 
//--- successful execution 
   return(true); 
  } 

//+------------------------------------------------------------------+ 
//| Change the object text                                           | 
//+------------------------------------------------------------------+ 
bool LabelTextChange(const long   chart_ID=0,   // chart's ID 
                     const string name="Label", // object name 
                     const string text="Text")  // text 
  { 
//--- reset the error value 
   ResetLastError(); 
//--- change object text 
   if(!ObjectSetString(chart_ID,name,OBJPROP_TEXT,text)) 
     { 
      Print(__FUNCTION__, 
            ": failed to change the text! Error code = ",GetLastError()); 
      return(false); 
     } 
//--- successful execution 
   return(true); 
  } 
选择 Exness——交易尽在掌控中
  • www.exness.com
通过 Exness 交易,享受金融市场最佳交易条件!订单执行最快仅需 0.1 秒。差价合约交易有风险。
 

When you post code please use the CODE button (Alt-S)!

Use the CODE button

Thank you.

 
Sergey Golubev:

When you post code please use the CODE button (Alt-S)!

Thank you.

OK,I know. thank you.