I am trying to enable my ea if spread is ok and if ea loses 3 times in a row the ea is disable.

 

Hi, as the title says I am trying to do a spread check and if ea loses 3 times in a row the ea stop working. I dont know why it is not workinnng. Any help will be much appreciated. I will be using my ea just for forex.


extern string _tmp1_ = " --- Trade params ---";
extern bool QuotesExtraDigit = TRUE;
extern bool SupportECN = TRUE;
extern int Slippage = 3;
extern int Magic = 20121021;
extern double Lots = 0.05;
extern int StopLoss = 10;
extern int TakeProfit = 0;
extern bool CloseByBollingerMiddle = TRUE;
extern bool CloseOnOppositeSignal = TRUE;
extern string _tmp2_ = " --- Bollinger Bands ---";
extern int BBandsperiod = 20;
extern int BBandsdeviation = 2;
extern int BBandsshift = 0;
extern int BBandsapplied_price = 0;
extern bool BBandsIgnoreOpenBar = FALSE;
bool lastLoss=false;
bool timetest=false;
int LosCount=0;
bool SpreadCount=false;
extern int Bistory=3;
extern int MaxSpread=3;
double spread=0;

void init() {

}

void deinit() {
}

void start() {

int OrdHistory=OrdersHistoryTotal();
 
for(int cnt=Bistory;cnt>=1;cnt--)
{
        //check history and count number of consecuutive loss
         if(OrderSelect(OrdHistory-cnt,SELECT_BY_POS,MODE_HISTORY) && OrderProfit()>=0)LosCount=0;//ProfCout++;
         if(OrderSelect(OrdHistory-cnt,SELECT_BY_POS,MODE_HISTORY) && OrderProfit()<0)LosCount++;
         
 if(OrderSymbol()==Symbol())
 {
   if(OrderMagicNumber()==Magic)
   {
   
//check spread logic for 5 digit pairs like EUR USD etc
   spread = (Ask-Bid)*100000;

//check spread 3 digit pairs like JPY NZD CHF
   if (StringFind(Symbol(), "JPY") >= 0) {
      spread = (Ask-Bid)*100;
   } 
   
   if (StringFind("JPY",Symbol()) >= 0){
      spread = (Ask-Bid)*100;
   }
 
    if (StringFind(Symbol(), "NZD") >= 0) {
      spread = (Ask-Bid)*100;
   } 
   
   if (StringFind("NZD",Symbol()) >= 0){
      spread = (Ask-Bid)*100;
   }
   if (StringFind(Symbol(), "CHF") >= 0) {
      spread = (Ask-Bid)*100;
   } 
   
   if (StringFind("CHF",Symbol()) >= 0){
      spread = (Ask-Bid)*100;
   }
    
   
   if(spread<=MaxSpread)
   {
      SpreadCount=false;
   }
   else{
      SpreadCount=true;
   }
   
  if(LosCount==3) 
  {
  Comment("derrotas consecutivas");
  lastLoss=true;
  }
  
  
   } 
  
 }
}

if(lastLoss==false && SpreadCount==false )  // only start ea if there is no 3 consecutive loss and the spread is ok
{ 

...
//rest of the code
 
Lucas Santana:

Hi, as the title says I am trying to do a spread check and if ea loses 3 times in a row the ea stop working. I dont know why it is not workinnng. Any help will be much appreciated. I will be using my ea just for forex.

  1. declare on global for; static int LosCount = 0;
  2. use void OnTick()
  3. you check the whole history & whole symbol
  4. ...
 
Mohamad Zulhairi Baba:
  1. declare on global for; static int LosCount = 0;
  2. use void OnTick()
  3. you check the whole history & whole symbol
  4. ...

Thanks for the answer sir. Did some changes I think its working now

Here is the new code.

static bool lossesS=false;
static int losses=0;
static int spread=0;
static bool SpreadCount=false;


void init() {

}

void deinit() {
}

void OnTick() {

    for(int i=0; i<OrdersHistoryTotal(); i++) 
    {                                         
        if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue; 
        if (OrderSymbol()!=Symbol() || OrderMagicNumber()!=Magic) continue;                      
        if (OrderCloseTime() < 1) continue; // redundant check :D
        if(OrderProfit()<=0) losses++;        
        else                losses=0;                           
    }

   
  if(losses==3) //ALTERAR AQUI CONSECUTIVE LOSS
  {
  Comment("derrotas consecutivas");
  //PlaySound("Alert.wav");
  lossesS=true;
  }
  
   if (losses<3) //ALTERAR AQUI CONSECUTIVE LOSS
   {
   Comment("ea funcionando");
   lossesS=false;
   }
  
     spread = (Ask-Bid)*100000;
    
   if (StringFind(Symbol(), "JPY") >= 0) {
      spread = (Ask-Bid)*1000;
   } 
   
   if (StringFind("JPY",Symbol()) >= 0){
      spread = (Ask-Bid)*1000;
   }
   
    
   
   if(spread<30) //ALTERAR AQUI A SPREAD EM PIPPETES
   {
      SpreadCount=false;
   }
   else{
      Comment("spread alta");
      SpreadCount=true;
   }
 
  


if(lossesS==false && SpreadCount==false)  // only submit orders if last order was not a loss
{