Add setting in an EA that makes a lot of money

 
Hello!

I have an EA that makes a lot of money, to earn 100% in the month quietly, but he needs two changes, the EA is based on Martingale.

First: Put a setting that when he loses a certain amount of money (that I can change this amount), it quits all open and pending orders and stop negotiating until I put it to work again.

Second: Put a setting for EA to work on a specific time and that I can change that time.

Appreciate the help!

Hug!

Excuse me for my English
 
Fonscky:
Hello!

I have an EA that makes a lot of money, to earn 100% in the month quietly, but he needs two changes, the EA is based on Martingale.

First: Put a setting that when he loses a certain amount of money (that I can change this amount), it quits all open and pending orders and stop negotiating until I put it to work again.

Second: Put a setting for EA to work on a specific time and that I can change that time.

Appreciate the help!

Hug!

Excuse me for my English
Quite easy... Just Put the variables dLossLimit and tFromToTimes in Global Variables.
 
gjrexach Thanks!

I could not add dLossLimit, you can help me?

extern int MinGS=20;
extern int TP=27;
extern double lot=0.10;
extern int MaxTrades=7;
extern int RegularSpread=3;
extern double Multiplier=2;
extern double LotInc=0;
extern double MarginLevelAlert=1000;

int magic;
double lot2;
datetime tob,tos,toe; //Time Out Buy & Sell & email


//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----


   
//----
   return(0);
  }
  
int init()
  {
   if (Symbol() == "AUDCADm" || Symbol() == "AUDCAD") { magic = 211001; }
   if (Symbol() == "AUDJPYm" || Symbol() == "AUDJPY") { magic = 211002; }
   if (Symbol() == "AUDNZDm" || Symbol() == "AUDNZD") { magic = 211003; }
   if (Symbol() == "AUDUSDm" || Symbol() == "AUDUSD") { magic = 211004; }
   if (Symbol() == "CHFJPYm" || Symbol() == "CHFJPY") { magic = 211005; }
   if (Symbol() == "EURAUDm" || Symbol() == "EURAUD") { magic = 211006; }
   if (Symbol() == "EURCADm" || Symbol() == "EURCAD") { magic = 211007; }
   if (Symbol() == "EURCHFm" || Symbol() == "EURCHF") { magic = 211008; }
   if (Symbol() == "EURGBPm" || Symbol() == "EURGBP") { magic = 211009; }
   if (Symbol() == "EURJPYm" || Symbol() == "EURJPY") { magic = 211010; }
   if (Symbol() == "EURUSDm" || Symbol() == "EURUSD") { magic = 211011; }
   if (Symbol() == "GBPCHFm" || Symbol() == "GBPCHF") { magic = 211012; }
   if (Symbol() == "GBPJPYm" || Symbol() == "GBPJPY") { magic = 211013; }
   if (Symbol() == "GBPUSDm" || Symbol() == "GBPUSD") { magic = 211014; }
   if (Symbol() == "NZDJPYm" || Symbol() == "NZDJPY") { magic = 211015; }
   if (Symbol() == "NZDUSDm" || Symbol() == "NZDUSD") { magic = 211016; }
   if (Symbol() == "USDCHFm" || Symbol() == "USDCHF") { magic = 211017; }
   if (Symbol() == "USDJPYm" || Symbol() == "USDJPY") { magic = 211018; }
   if (Symbol() == "USDCADm" || Symbol() == "USDCAD") { magic = 211019; }
   if (magic == 0) { magic = 211999; }
   return (0);
   return(0);
  }

void OpenBuy()
 {
    int ticket,err;
        if (!GlobalVariableCheck("InTrade")) {
               GlobalVariableSet("InTrade", CurTime());  // set lock indicator
               ticket = OrderSend(Symbol(),OP_BUY,lot2,Ask,1,0,Ask+TP*Point,"EA Order",magic,0,Red);
               GlobalVariableDel("InTrade");   // clear lock indicator
        }
 }

void OpenSell()
 {
    int ticket,err;
        if (!GlobalVariableCheck("InTrade")) {
               GlobalVariableSet("InTrade", CurTime());  // set lock indicator
               ticket = OrderSend(Symbol(),OP_SELL,lot2,Bid,1,0,Bid-TP*Point,"EA Order",magic,0,Red);
               GlobalVariableDel("InTrade");   // clear lock indicator
        }
 }


void ManageBuy() 
 {
   int lasttradetime = 0;
   double lastopenprice=0;
   double maxlots = 0;
   double lasttp=0;
   int lastordertype=-1;
   int lastorderticket=0;
   int y=0; 
   
   
   for (y = 0; y < OrdersTotal(); y++)
   {
      OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
      if ((OrderMagicNumber() != magic) || (OrderType()!=OP_BUY)) { continue; }
      if (OrderOpenTime() > lasttradetime) { 
        lasttradetime = OrderOpenTime();
        lastopenprice = OrderOpenPrice();
        lastordertype=OrderType();
        lastorderticket=OrderTicket();
        lasttp=OrderTakeProfit();
      }     
      if (OrderLots() > maxlots) { maxlots = OrderLots(); }
   }
   
   int lvl=MathRound(MathLog((maxlots-LotInc)/lot)/MathLog(Multiplier)+1);
   
   Print(lvl);

   if (lvl<0) lvl=0;
   
   lot2=lot*MathPow(Multiplier,lvl)+LotInc;
   
   if ((lvl==0) && (((Ask-Bid)/Point)<=RegularSpread)) OpenBuy();
   
   if ((lastopenprice-Ask>MinGS*Point) && (lvl<MaxTrades) && (((Ask-Bid)/Point)<=RegularSpread)) {
     OpenBuy();
     return(0);
   }

/////////// TP
/*
   double sumlots=0;
   double sump=0;
   double avgp=0;
   for (y = 0; y < OrdersTotal(); y++)
   {
      OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
      if ((OrderMagicNumber() != magic) || (OrderType()!=OP_BUY)) { continue; }
      sumlots=sumlots+OrderLots();
      sump=OrderOpenPrice()*OrderLots()+sump;
   }
   
   if (sumlots>0) {
     avgp=NormalizeDouble(sump/sumlots,Digits);
     double tpp=NormalizeDouble(TP/10/sumlots*Point+avgp,Digits);
     
     for (y = 0; y < OrdersTotal(); y++)
     {
       OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
       if ((OrderMagicNumber() != magic) || (OrderType()!=OP_BUY) || (OrderTakeProfit()==tpp)) { continue; }
       OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tpp,0,Red);
     }
     
     
   }
 */  

     for (y = 0; y < OrdersTotal(); y++)
     {
       OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
       if ((OrderMagicNumber() != magic) || (OrderType()!=OP_BUY) || (OrderTakeProfit()==lasttp) || (lasttp==0)) { continue; }
       OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),lasttp,0,Red);
     }

   
}
    
void ManageSell() 
 {
   int lasttradetime = 0;
   double lastopenprice=0;
   double maxlots = 0;
   double lasttp=0;
   int lastordertype=-1;
   int lastorderticket=0;
   int y=0; 

   for (y = 0; y < OrdersTotal(); y++)
   {
      OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
      if ((OrderMagicNumber() != magic) || (OrderType()!=OP_SELL)) { continue; }
      if (OrderOpenTime() > lasttradetime) { 
        lasttradetime = OrderOpenTime();
        lastopenprice = OrderOpenPrice();
        lastordertype=OrderType();
        lastorderticket=OrderTicket();
        lasttp=OrderTakeProfit();
      }     
      if (OrderLots() > maxlots) { maxlots = OrderLots(); }
   }
   
   int lvl=MathRound(MathLog((maxlots-LotInc)/lot)/MathLog(Multiplier)+1);

   if (lvl<0) lvl=0;
   
   lot2=lot*MathPow(Multiplier,lvl)+LotInc;
   
   if ((lvl==0) && (((Ask-Bid)/Point)<=RegularSpread)) OpenSell();
   
   if ((Bid-lastopenprice>MinGS*Point) && (lastopenprice>0) && (lvl<MaxTrades) && (((Ask-Bid)/Point)<=RegularSpread)) {
     OpenSell();
     return(0);
   }

/////////// TP
/*   double sumlots=0;
   double sump=0;
   double avgp=0;
   for (y = 0; y < OrdersTotal(); y++)
   {
      OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
      if ((OrderMagicNumber() != magic) || (OrderType()!=OP_SELL)) { continue; }
      sumlots=sumlots+OrderLots();
      sump=OrderOpenPrice()*OrderLots()+sump;
   }
   
   if (sumlots>0) {
     avgp=NormalizeDouble(sump/sumlots,Digits);
     double tpp=NormalizeDouble(-TP/10/sumlots*Point+avgp,Digits);
     
     for (y = 0; y < OrdersTotal(); y++)
     {
       OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
       if ((OrderMagicNumber() != magic) || (OrderType()!=OP_SELL) || (OrderTakeProfit()==tpp)) { continue; }
       OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tpp,0,Red);
     }
     
     
   }
 */ 
 
      for (y = 0; y < OrdersTotal(); y++)
     {
       OrderSelect (y, SELECT_BY_POS, MODE_TRADES);
       if ((OrderMagicNumber() != magic) || (OrderType()!=OP_SELL) || (OrderTakeProfit()==lasttp) || (lasttp==0)) { continue; }
       OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),lasttp,0,Red);
     }
 
   
 }
 
void CheckMargin() 
{
if (AccountMargin()!=0) {
  if (((AccountEquity()/AccountMargin()*100)<MarginLevelAlert) && (TimeCurrent()>toe+3600)) {
    SendMail("Account Margin Warning","Account Free Margin is "+AccountFreeMargin());
    toe=TimeCurrent();
  }
}
}
  


  
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

  ManageBuy();
  
  ManageSell();
  
  CheckMargin();
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

 

Second: Put a setting for EA to work on a specific time and that I can change that time.

As long as the trading time does not go through the Server Midnight Hour then

extern int MinGS=20;
extern int TP=27;
extern double lot=0.10;
extern int MaxTrades=7;
extern int RegularSpread=3;
extern double Multiplier=2;
extern double LotInc=0;
extern double MarginLevelAlert=1000; 
//Add the next two variables
extern int StartTradeHr=0 ;
extern int EndTradeHr=23 ;

With

  if ( (Hour()>=StartTradeHr) && (Hour()<=EndTradeHr) ) 
    {
     ManageBuy();
      
     ManageSell();
    }

The '=' in Hour()<=EndTradeHr will trade to the 59:59 minutes:seconds of the EndTradeHr, removing the '=' and it wont trade after EndTradeHr. Hour() reads your brokers server time.

 
Fonscky:
First: Put a setting that when he loses a certain amount of money (that I can change this amount), it quits all open and pending orders and stop negotiating until I put it to work again.

This statement is not well defined. The EA is designed to work on many Curency Pairs at the same time. So are all EA's on all the charts to close up and stop trading when a certain Loss is reached or

does this happen for just one currency pair?

Is the certain amount to be on closed trades only or is it to include both open trades and closed trades?

When it comes to restarting the EA how is the EA going to recognise that the previous 'certain amount' is no longer active and can start again? Here I suggest that in any one day only a certain amount may be lost.

 

Ickyrus:

As long as the trading time does not go through the Server Midnight Hour then

    int         DOW = TimeDayOfWeek(now),   /* forum.mql4.com/33851
    // reports DayOfWeek() always returns 5 in the tester. No refresh?*/
                DayMask = 1 << DOW;
    //                      #define DAYS_MAX    0x3F// 1<<6-1=63. (S-F)
    //extern int      Days.Mask               =  55;      // Not Wed
    if (Days.Mask & DayMask == 0){  EA.status="Day="+DOW;           return; }
    //extern double   TradeHr.UTC.Start   =   7.3;    // London-1
    //extern double   TradeHr.UTC.End     =  12.9;    // NY open
    int secStart    = 3600*TradeHr.UTC.Start,
        secEnd      = 3600*TradeHr.UTC.End,
        hrBeg       = (now-secStart+86400)%86400,
        hrEnd       = (now-secEnd  +86400)%86400;
    if (hrBeg > hrEnd){ double Tminus=hrBeg/3600.-24;
                            EA.status="HR"+DoubleToStr(Tminus,2);   return; }
 
WHRoeder:


I thought the answer was trivial and then I got to thinking about it!

Thanks WHRoeder

 
Fonscky:

gjrexach Thanks!

I could not add dLossLimit, you can help me?


 
Fonscky:
Hello!

I have an EA that makes a lot of money, to earn 100% in the month quietly, but he needs two changes, the EA is based on Martingale.

First: Put a setting that when he loses a certain amount of money (that I can change this amount), it quits all open and pending orders and stop negotiating until I put it to work again.

Second: Put a setting for EA to work on a specific time and that I can change that time.

Appreciate the help!

Hug!

Excuse me for my English
 
Fonscky:
Hello!

I have an EA that makes a lot of money, to earn 100% in the month quietly, but he needs two changes, the EA is based on Martingale.

First: Put a setting that when he loses a certain amount of money (that I can change this amount), it quits all open and pending orders and stop negotiating until I put it to work again.

Second: Put a setting for EA to work on a specific time and that I can change that time.

Appreciate the help!

Hug!

Excuse me for my English

Fonscky:

This EA is very poorly designed!!

I have made the changes but it has a bug in it and That is for you to resolve. For starters it checks margin after the trade cycle?? This it horribly wrong! I switched it to the beginning but it got a 0 divide exception. Switched it back to how the original code was but it will not generate any trades. Just gives a Print lvl 0, which is exactly what your version does. The code uses GlobalVariableGet insteat of GlobalVariableCheckOnCondition for directing traffic regarding Busy/NotBusy trading Status, which is also incorrect.

Obviously, this is a crude attempt at simulating a Multipair EA? You going to put the EA on every chart that you want to Trade? Good Luck,Man!!

Here is a better version, of the EA, FOR EDUCATIONAAL PURPOSES ONLY!!. Review it with your MQL manual to understand the code that I changed. Either way, with that kind of coding, I would not trust the EA, even on a demo account!!

Instructions on dLimitLoss and Time:

The first EA to be launched will populate the Global variables TimeFrom,TimeTo, and dLossLimit. From thereon in, All EAs will pick these values from the Global Variables.

The TimeFrom and To formats are as follows: DHHMM ...D=dayOfWeek(0-6:Sunday-Saturday, and 7 when you want to trade daily for a given block of Hours:

Default is 00001(Sunday 1201AM) to 51159(Friday 1159PM) if you want to trade daily for a block of Hours:70900 and 71700 will trade from 0900AM to 0500PM every trading day.

If the Loss Limit is reached, you will have to reset the amount as Well as the Busy Global. Go To Tools-GlobalVariables and change them there(0 in Busy Variable to enable trading) & NewAmount for dLossLimit .

P.S. Have changed the CheckMargin to the Top. Problem of 0 divide has to do with a METATRADER Compiler Problem which does not evaluate certain expressions correctly. Fixed..

DateFunctions are working, Limit is Working.. Test it And let me know the results ... My testing was disastrous..

 

I am learning to create EA.

My idea was to create an EA based martigale, the idea was that when EA began to run it would open two orders, a buy and sell, without analyzing trend, then every a certain number of pips of loss and open a new order with a larger volume, to have a return to cover the losses.
It works well, but to have more security would have to put a level of loss, that was what I asked here, he is also designed to work outside of large movements of money that would be placed, I use EUR / USD M15 or M30.

Thanks for everyone's help!

I'll try and post the results here!

Excuse me for my English!