Code line for loss closing

 

hi guys,

I have an EA but want to add a loss parameter for all open trades to close when it hits a certain amount. Can anyone provide me with that line of code please? I have attached the mq4.

many thanks on advance.

Files:
Bimodal.mq4  4 kb
 
//+------------------------------------------------------------------+
//|                                                      Bimodal.mq4 |
//|                         Copyright 2018, Marius Ovidiu Sunzuiana. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, Marius Ovidiu Sunzuiana."
#property link      "https://www.mql5.com"
#property version   "1.777"

extern string StartHour="11:00";  //Not Used
extern string EndHour="20:00";    //Not Used

extern int  MagicNumber=777;
extern bool StandardMODE=true;
extern bool RandomMODE=false;
extern double takeprofit=0.0080;
extern double stoploss=0.0030;
extern double lot=0.01;
extern double Max_Floating_Loss_Currency=200;

int total=OrdersTotal();

#include <stdlib.mqh>
#include <stderror.mqh>
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   MessageBox("Default is set to StandardMODE");
   if(Bars<100)
     {
      Print("Not enough bars for EA to trade");
      return(0);
     }
   Calculatedigits(Symbol());
   Comment("BIMODAL BALANCE : "+AccountBalance());
   if(IsDemo()==false)
     {
      Alert("Bimodal EA is set on live account");
     }
   else
     {
      Alert("Bimodal EA is set on demo account");
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int x;
   double floatingProfit=0;
   for(x=OrdersTotal()-1;x>=0;x--)
      if(OrderSelect(x,SELECT_BY_POS) && OrderMagicNumber()==MagicNumber)
         floatingProfit+=OrderProfit()+OrderCommission()+OrderSwap();
   if(floatingProfit<=-Max_Floating_Loss_Currency)
      for(x=OrdersTotal()-1;x>=0;x--)
         if(OrderSelect(x,SELECT_BY_POS) && OrderMagicNumber()==MagicNumber)
           {
            if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),10,clrRed))
               Print("Error Closing Order #",OrderTicket(),". ",ErrorDescription(GetLastError()));
           }

   if(RandomMODE==true)
     {
      if(CalculateRandom()==0)
         OrderSend(Symbol(),OP_BUY,lot,Ask,4,(Ask-stoploss),(Ask+takeprofit),NULL,MagicNumber,0,clrDeepPink);
     }
   else if(StandardMODE==true)
     {
      double sar1;
      sar1=iSAR(Symbol(),0,0.02,0.2,0);
      if(Close[0]<sar1)
        {
         OrderSend(Symbol(),OP_BUY,lot,Ask,4,(Ask-stoploss),(Ask+takeprofit),NULL,MagicNumber,0,clrDeepPink);
        }
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int CalculateRandom()
  {
   MathSrand(GetTickCount());
   double random=MathRand()%2;
   return random;

  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Calculatedigits(string currency)
  {
   int digits=MarketInfo(currency,MODE_DIGITS);
   if(digits==2 || digits==3)
      return 0.01;
   else if(digits==3 || digits==5)
      return 0.0001;
   return(Digits);
  }
//+------------------------------------------------------------------+

Not checked or tested

 

Hi Keith,

Thanks so much for your response and code. I tried to compile but I got 4 errors and 3 warnings (please see attached). Don't really have experience with mql4 code but seems like some variables are not being recognised.

Files:
 
darkpoet:

Hi Keith,

Thanks so much for your response and code. I tried to compile but I got 4 errors and 3 warnings (please see attached). Don't really have experience with mql4 code but seems like some variables are not being recognised.

I tried it and it compiles just fine.

Must be some error on your side.

 
darkpoet:

Hi Keith,

Thanks so much for your response and code. I tried to compile but I got 4 errors and 3 warnings (please see attached). Don't really have experience with mql4 code but seems like some variables are not being recognised.

I have highlighted additional code in the previously posted code. You have obviously only added the code in OnTick(), not the rest.

3 warnings already existed, I only dealt with 1 so there are now 2 left.

 
I saw two returnvalue check warnings not errors like in his picture.
 
Marco vd Heijden:
I saw two returnvalue check warnings not errors like in his picture.

The errors are because he didn't copy the whole code with the new inputs.

 
Hi guys, my apologies yes you are right, I missed a line of code at the bottom. It has now compiled right but don't think the floating loss close is working. Here is a quick strategy test I did plus the input parameters (attached). I entered 1000 so all open trades were supposed to close when they all reached a floating loss of -1000 gbp but as you can see that did not happen. Or may be i entered the wrong input?
Files:
 
darkpoet:
Hi guys, my apologies yes you are right, I missed a line of code at the bottom. It has now compiled right but don't think the floating loss close is working. Here is a quick strategy test I did plus the input parameters (attached). I entered 1000 so all open trades were supposed to close when they all reached a floating loss of -1000 gbp but as you can see that did not happen. Or may be i entered the wrong input?

Hello,

I can't believe that I missed off the OrderLots() parameter in the OrderClose()

I will edit my earlier post to correct it and attach file here

Files:
 
Keith Watford:

Hello,

I can't believe that I missed off the OrderLots() parameter in the OrderClose()

I will edit my earlier post to correct it and attach file here

Thanks so much Keith, worked like a charm!

Reason: