EA not working live only in strategy tester

To add comments, please log in or register
samvendor
48
samvendor  

Hello, 


This is an old

// coded by hirdes, Chemnitz, Germany, 07.2019              

extern double LongEntry=0.00;
extern double ShortEntry=0.00;
extern int hour=8;
extern int minute=00;
extern int Account=0;
extern double Lots=1.0;
extern int Slippage=2;
extern int StopLoss=200;
extern int TrailingStop=0;
extern int TakeProfit=100;

bool runnable=true;
bool init=true;

datetime timeprev=0;

int init()
{
 return(0);
}

int deinit()
{
 return(0);
}

//===========================================================================================
//===========================================================================================

int start()
{
//Runnable
 if(runnable!=true)
  return(-1);
  
//Init
 if(init==true)
 {
  init=false;

  if(IsTesting()==false&&Account!=AccountNumber())
  {
   runnable=false;
   Alert("*** WARNING: Please check Account Number! ***");
   return(-1);
  }
 }//Init

//Trailing Stop
 TrailingAlls(TrailingStop); 
  
  if (OrdersTotal()==0) 
  {
// Trade at only certain times (before news releases)
// place the straddle 5 minutes before the hour:
   if(TimeHour(CurTime())==hour&&TimeMinute(CurTime())==minute)
   {
//
//Immediately straddle the trade:
// Enter Long: 
     if(LongEntry>0)  OrderSend(Symbol(),OP_BUYSTOP,Lots,LongEntry,Slippage,StopLong(LongEntry,StopLoss),TakeLong(LongEntry,TakeProfit),NULL,0,0,Blue);
     if(ShortEntry>0) OrderSend(Symbol(),OP_SELLSTOP,Lots,ShortEntry,Slippage,StopShort(ShortEntry,StopLoss),TakeShort(ShortEntry,TakeProfit),NULL,0,0,Red);
     int exitday = DayOfWeek()+1;
      if(exitday == 6)
         exitday = 0; 
    }
   } 
  else 
//
//Exit all trades 23:59 after entry, if any trades are still open
//
  {
   if(DayOfWeek()==exitday&&TimeHour(CurTime())==hour&&TimeMinute(CurTime())==minute-1)
   {
    CloseLongs();
    CloseShorts(); 
   }
  }
  
return(0);
}

//===========================================================================================
//===========================================================================================


double StopLong(double price,int stop)
{
 if(stop==0)
  return(0.0); // if no stop loss
 return(price-(stop*Point)); 
             // minus, since the stop loss is below us for long positions
             // Point is 0.01 or 0.0001 depending on currency, so stop*POINT is a way to convert pips into price with multiplication 
}

double StopShort(double price,int stop)
{
 if(stop==0)
  return(0.0); // if no stop loss
 return(price+(stop*Point)); 
             // plus, since the stop loss is above us for short positions
}

double TakeLong(double price,int take)
{
 if(take==0)
  return(0.0); // if no take profit
 return(price+(take*Point)); 
             // plus, since the take profit is above us for long positions
}

double TakeShort(double price,int take)
{
 if(take==0)
  return(0.0); // if no take profit
 return(price-(take*Point)); 
             // minus, since the take profit is below us for short positions
}

void CloseLongs()
{
 int trade;
 int trades=OrdersTotal();
 for(trade=0;trade<trades;trade++)
 {
  OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
  
  if(OrderSymbol()!=Symbol())
   continue;
   
  if(OrderType()==OP_BUY)
   OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Blue); 
 } //for
}

void CloseShorts()
{
 int trade;
 int trades=OrdersTotal();
 for(trade=0;trade<trades;trade++)
 {
  OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
  
  if(OrderSymbol()!=Symbol())
   continue;
   
  if(OrderType()==OP_SELL)
   OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red); 
 } //for
}

void TrailingAlls(int trail)
{
 if(trail==0)
  return;
  
 double stopcrnt;
 double stopcal;
  
 int trade;
 int trades=OrdersTotal();
 for(trade=0;trade<trades;trade++)
 {
  OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
  
  if(OrderSymbol()!=Symbol())
   continue;

//Long 
  if(OrderType()==OP_BUY)
  {
   stopcrnt=OrderStopLoss();
   stopcal=Bid-(trail*Point); 
   if(stopcrnt==0)
   {
    OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);
   }
   else
   {
    if(stopcal>stopcrnt)
    {
     OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);
    }
   }
  }//Long 
  
//Short 
  if(OrderType()==OP_SELL)
  {
   stopcrnt=OrderStopLoss();
   stopcal=Ask+(trail*Point); 
   if(stopcrnt==0)
   {
    OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);
   }
   else
   {
    if(stopcal<stopcrnt)
    {
     OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);
    }
   }
  }//Short   
  
  
 } //for
}


EA, to create buystop and sell stop order with time and value but only working in strategy tester mode not working live. Please let me know where is the problem in the code. 

William Roeder
19170
William Roeder  
Always use strict. Fixing the warnings will save you hours of debugging. If you do, then this will not even compile.
     int exitday = DayOfWeek()+1;
      if(exitday == 6)
         exitday = 0; 
    }
   } 
  else 
//
//Exit all trades 23:59 after entry, if any trades are still open
//
  {
   if(DayOfWeek()==exitday
To add comments, please log in or register