TAASA_Equity_Control

Author: Copyright 2016,Nikolay Gaylis
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself It can change open orders parameters, due to possible stepping strategy
Miscellaneous
It plays sound alerts
0 Views
0 Downloads
0 Favorites
TAASA_Equity_Control
//+------------------------------------------------------------------+
//|                                                        TAASA.mq4 |
//|                                    Copyright 2016,Nikolay Gaylis |
//|                                             https://vk.com/taasa |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016,Nikolay Gaylis"
#property link      "https://www.mql5.com/ru/users/niktron"
#property version   "1.3"
#property strict

extern string ____="===================== Settings risk and reward ======================";
extern int TP=0;         //Automatic take profit in pips(0-disabled) 
extern int SL=0;         //Automatic stop loss in pips(0-disabled) 
extern int TS=0;         //Trailing stop in pips(0-disabled) 
extern double PP=0;      //A percentage of the profits from the Deposit to close(0-disabled) 
extern double PU=0;      //The maximum drawdown in percent,to close(0-disabled) 

extern string __________="================ The algorithm is complete working week ==================";
extern bool End_Week=false;          //Inclusion of algorithm
extern double Min_PP_Freeday=1;      //The minimum percentage of the profits from the Deposit to close 
extern int Plus_Friday_Hour=17;      //The hour at which we try to profit 
extern bool Profit_Plus_Friday=true; //The inclusion of attempts to complete the trading week  >=0
extern int Hour_Friday_End_Zero=19;  //The hour at which we try to close at breakeven >=0
extern bool Friday_end_On=true;      //The inclusion of forced closure of positions on Friday 
extern int Friday_Hour_Close=22;     //The hours of forced closure of positions on Friday 

extern string ____________="========================= Other settings ============================";
extern int kol_open=2;               //The number of attempts of opening and closing positions 
extern int pause_open=5;             //Pause between attempts in the closing seconds 
extern int slip=20;                  //Slippage
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
enum corner
  {
   CORNER_NONE =    -1,  //Don't show info
   CORNER_LEFT_UP =  0,  //Left upper
   CORNER_RIGHT_UP = 1,  //Right upper
   CORNER_LEFT_DN =  2,  //Left lower
   CORNER_RIGHT_DN = 3,  //Right lower
  };
extern corner InfoCorner=CORNER_LEFT_UP; //Positioning reviews

double P_Close,U_Close,P_Freeday_Close;
int OM,C;
string indicatorname="TAASA Equity Control v1.3";
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   ObjectsDeleteAll();
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OC()
  {
   for(int cl=OrdersTotal()-1; cl>=0; cl--)
     {
      if(!OrderSelect(cl,SELECT_BY_POS,MODE_TRADES)){break;}
      for(int h=1; h<=kol_open; h++)
        {
         if(OrderType()==OP_BUY)
           {
            C=OrderClose(OrderTicket(),OrderLots(),Bid,0);
           }
         if(OrderType()==OP_SELL)
           {
            C=OrderClose(OrderTicket(),OrderLots(),Ask,0);
           }
         if(OrderType()==OP_BUYLIMIT||OrderType()==OP_SELLLIMIT||
            OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP){C=OrderDelete(OrderTicket());}
         RefreshRates();
         if(C<1)
           {
            Print("Failed to close a position, a new attempt ",h);
            Sleep(pause_open*1000);
            RefreshRates();}else { PlaySound("ok.wav");Print("The order closed with a profit :",OrderProfit());break;
           }
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void createText(string name,int x,int y,string text,string font,color colr,int _corner,int fontsize)
  {
   if(ObjectFind(name)<0)
     {
      ObjectCreate(name,OBJ_LABEL,0,0,0);
      ObjectSet(name,OBJPROP_CORNER,_corner);
      ObjectSet(name,OBJPROP_XDISTANCE,x);
      ObjectSet(name,OBJPROP_YDISTANCE,y);
      ObjectSetText(name,text,fontsize,font,colr);
     }
   else ObjectSetText(name,text,fontsize,font,colr);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void checkText()
  {
   string str,str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12;
   str="";
   str1="OFF";
   str2="";
   str4= " "+AccountInfoString(ACCOUNT_CURRENCY);
   str5=IntegerToString(Plus_Friday_Hour)+":00";
   str6="OFF";if(SL!=0){str6=IntegerToString(SL)+" pips";}
   str7="OFF";if(TP!=0){str7=IntegerToString(TP)+" pips";}
   str8="OFF";if(TS!=0){str8=IntegerToString(TS)+" pips";}
   str9="OFF";
   str10="";
   if(Profit_Plus_Friday==true){str9="ON";str10=IntegerToString(Hour_Friday_End_Zero)+":00";}
   str11="OFF";
   str12="";
   if(Friday_end_On==true){str11="ON";str12=IntegerToString(Friday_Hour_Close)+":00";}
   if(End_Week==true)
     {
      str1="ON";
      if(Min_PP_Freeday!=0){str=DoubleToString(P_Freeday_Close,2);}
     }
   if(PU!=0){str2=DoubleToString(U_Close,2);}
   if(PP!=0){str3=DoubleToString(P_Close,2);}

   createText(indicatorname,5,12,indicatorname,"Arial",clrAquamarine,InfoCorner,8);

   createText(indicatorname+"1",5,24,"---------------------------------------------------","Arial",clrDeepSkyBlue,InfoCorner,8);
   createText(indicatorname+"2",5,36,"Balance: "+DoubleToString(AccountBalance(),2)+str4,"Arial",clrDeepSkyBlue,InfoCorner,8);
   createText(indicatorname+"3",5,48,"Equity: "+DoubleToString(AccountEquity(),2)+str4,"Arial",clrDeepSkyBlue,InfoCorner,8);
   if(PP!=0&&PU==0){createText(indicatorname+"4",5,60,"Profits to close: "+str3+str4,"Arial",clrDeepSkyBlue,InfoCorner,8);}
   if(PU!=0&&PP==0){createText(indicatorname+"5",5,60,"Maximum drawdown: "+str2+str4,"Arial",clrDeepSkyBlue,InfoCorner,8);}
   if(PP!=0&&PU!=0){createText(indicatorname+"6",5,60,"Profits to close: "+str3+str4,"Arial",clrDeepSkyBlue,InfoCorner,8);}
   if(PP!=0 && PU!=0){createText(indicatorname+"7",5,72,"Maximum drawdown: "+str2+str4,"Arial",clrDeepSkyBlue,InfoCorner,8);}
   createText(indicatorname+"8",5,84,"---------------------------------------------------","Arial",clrDeepSkyBlue,InfoCorner,8);
   createText(indicatorname+"9",5,96,"Algorithm End Week: "+str1,"Arial",clrDeepSkyBlue,InfoCorner,8);
   createText(indicatorname+"10",5,108,"Profit EW: "+str+str4+"   Time close: "+str5+" +","Arial",clrDeepSkyBlue,InfoCorner,8);
   createText(indicatorname+"11",5,120,"Profit >=0 EW: "+str9+"   Time close: "+str10+" +","Arial",clrDeepSkyBlue,InfoCorner,8);
   createText(indicatorname+"12",5,132,"All close EW: "+str11+"   Time close: "+str12,"Arial",clrDeepSkyBlue,InfoCorner,8);
   createText(indicatorname+"13",5,144,"---------------------------------------------------","Arial",clrDeepSkyBlue,InfoCorner,8);
   createText(indicatorname+"14",5,156,"Auto take profit: "+str7,"Arial",clrDeepSkyBlue,InfoCorner,8);
   createText(indicatorname+"15",5,168,"Auto stop loss: "+str6,"Arial",clrDeepSkyBlue,InfoCorner,8);
   createText(indicatorname+"16",5,180,"Trailingstop: "+str8,"Arial",clrDeepSkyBlue,InfoCorner,8);
   createText(indicatorname+"17",5,192,"---------------------------------------------------","Arial",clrDeepSkyBlue,InfoCorner,8);
   createText(indicatorname+"18",5,204,"AccountProfit: "+DoubleToString(AccountProfit(),2),"Arial",clrDeepSkyBlue,InfoCorner,8);

  }
//+---------------------------------------------------------------------------------------------------------+
void OnTick()
  {
   P_Close=AccountBalance()*PP/100;
   U_Close=AccountBalance()*PU/100;
   P_Freeday_Close=AccountBalance()*Min_PP_Freeday/100;
   if(InfoCorner!=-1)
     {
      checkText();
     }
   if(OrdersTotal()!=0 && SL!=0)
     {
      for(int pos=OrdersTotal()-1;pos>=0;pos--)
        {
         if(!OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)){break;}
         if(OrderType()==OP_BUY && OrderStopLoss()==0)
           {
            OM=OrderModify(OrderTicket(),OrderOpenPrice(),Ask-SL*Point,OrderTakeProfit(),0,0);
           }
         if(OrderType()==OP_SELL && OrderStopLoss()==0)
           {
            OM=OrderModify(OrderTicket(),OrderOpenPrice(),Bid+SL*Point,OrderTakeProfit(),0,0);
           }
        }
     }
   if(OrdersTotal()!=0 && TP!=0)
     {
      for(int pos=OrdersTotal()-1;pos>=0;pos--)
        {
         if(!OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)){break;}
         if(OrderType()==OP_BUY && OrderTakeProfit()==0)
           {
            OM=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask+TP*Point,0,0);
           }
         if(OrderType()==OP_SELL && OrderTakeProfit()==0)
           {
            OM=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Bid-TP*Point,0,0);
           }
        }
     }
   if(OrdersTotal()!=0 && TS!=0 && SL!=0)
     {
      for(int pos=OrdersTotal()-1;pos>=0;pos--)
        {
         if(!OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)){break;}
         if(OrderType()==OP_BUY && OrderStopLoss()<Bid-(SL*Point+TS*Point))
           {
            OM=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-SL*Point,OrderTakeProfit(),0,0);
           }
         if(OrderType()==OP_SELL && OrderStopLoss()>Ask+(SL*Point+TS*Point))
           {
            OM=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+SL*Point,OrderTakeProfit(),0,0);
           }
        }
     }
   ZPP();
//+-------------------------------------------------------END---------------------------------------------------------------+
   return;
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void ZPP()
  {
   if(OrdersTotal()!=0)
     {
      if(PP!=0 && AccountProfit()>P_Close)
        {
         Print("***TAASA*** ","The achievement of a predetermined profit!");
         OC();
        }
      if(PU!=0 && AccountProfit()<-U_Close)
        {
         Print("***TAASA*** ","The maximum drawdown!");
         OC();
        }
      if(End_Week==TRUE && DayOfWeek()==5)
        {
         if(AccountProfit()>P_Freeday_Close && Hour()>=Plus_Friday_Hour)
           {
            Print("***TAASA*** ","The achievement of predetermined profit on Friday!");
            OC();
           }
         if(Profit_Plus_Friday==TRUE)
           {
            if(Hour()>=Hour_Friday_End_Zero && AccountProfit()>=0)
              {
               Print("***TAASA*** ","The achievement of breakeven on Friday!");
               OC();
              }
           }
         if(Friday_end_On==TRUE && Hour()>=Friday_Hour_Close)
           {
            Print("***TAASA*** ","Closing all positions on Friday in specified hours!");
            OC();
           }
        }
     }
  }
//+-------------------------------------------------------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---