EA buys a ton of trades super quick?

 

To preface, this is coded in mql4. Don't know where else to put this so I apologize if this is the wrong spot.


Anyways, I'm coding a pretty simple EA that looks at the EMA and Stochastic Oscillator to determine when to make a trade. The only way I want it to close is via a take profit or stop loss. I'm using Oanda for this, and it seems like it is one of those programs that you need to multiply the pips (for stop loss/take profit) by 10 for it to work properly, so that's what is going on there. Secondly, the main issue here is that if you look at the report-


report.PNG


There's like 9 orders here being completed within one second. Here is what the visual graph looks like right now-

graph.PNG


What I'm wondering is how in the world can I get it to make these trades more intelligently. I'm fairly new to mql and forex so is the chart just moving a lot more than I think it is? For anyone wondering here is my code. I took the main bit from a company that created a bot that uses the oscillator and just added to it so it's still kind of messy and I apologize.

extern double LotSize=0.1;             //Position size

extern double StopLoss=30;             //Stop loss in pips
extern double TakeProfit=130;           //Take profit in pips

extern int Slippage=2;                 //Slippage in pips

extern bool TradeEnabled=true;         //Enable trade

extern int StochK=5;                   //Stochastic K Period, default 5
extern int StochD=3;                   //Stochastic D Period, default 3
extern int StochSlowing=3;              //Stochastic Slowing, default 3

extern int UpperThreshold=80;          //Upper Threshold, default 80
extern int LowerThreShold=20;          //Lower Threshold, default 20

//Functional variables
double ePoint;                         //Point normalized

bool CanOrder;                         //Check for risk management
bool CanOpenBuy;                       //Flag if there are buy orders open
bool CanOpenSell;                      //Flag if there are sell orders open

int OrderOpRetry=10;                   //Number of attempts to perform a trade operation
int SleepSecs=3;                       //Seconds to sleep if can't order
int MinBars=60;                        //Minimum bars in the graph to enable trading

//Functional variables to determine prices
double MinSL;
double MaxSL;
double TP;
double SL;
double Spread;
int Slip; 


//Variable initialization function
void Initialize(){          
   RefreshRates();
   ePoint=Point;
   Slip=Slippage;
   if (MathMod(Digits,2)==1){
      ePoint*=10;
      Slip*=10;
   }
   TP=TakeProfit*ePoint;
   SL=StopLoss*ePoint;
   CanOrder=TradeEnabled;
   CanOpenBuy=true;
   CanOpenSell=true;
}


//Check if orders can be submitted
void CheckCanOrder(){            
   if( Bars<MinBars ){
      Print("INFO - Not enough Bars to trade");
      CanOrder=false;
   }
   OrdersOpen();
   return;
}


//Check if there are open orders and what type
void OrdersOpen(){
   for( int i = 0 ; i < OrdersTotal() ; i++ ) {
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ) {
         Print("ERROR - Unable to select the order - ",GetLastError());
         break;
      } 
      if( OrderSymbol()==Symbol() && OrderType() == OP_BUY) CanOpenBuy=false;
      if( OrderSymbol()==Symbol() && OrderType() == OP_SELL) CanOpenSell=false;
   }
   return;
}


//Close all the orders of a specific type and current symbol
void CloseAll(int Command){
   double ClosePrice=0;
   for( int i = 0 ; i < OrdersTotal() ; i++ ) {
      if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false ) {
         Print("ERROR - Unable to select the order - ",GetLastError());
         break;
      }
      if( OrderSymbol()==Symbol() && OrderType()==Command) {
         if(Command==OP_BUY) ClosePrice=Bid;
         if(Command==OP_SELL) ClosePrice=Ask;
         double Lots=OrderLots();
         int Ticket=OrderTicket();
         for(int j=1; j<OrderOpRetry; j++){
            bool res=OrderClose(Ticket,Lots,ClosePrice,Slip,Red);
            if(res){
               Print("TRADE - CLOSE - Order ",Ticket," closed at price ",ClosePrice);
               break;
            }
            else Print("ERROR - CLOSE - error closing order ",Ticket," return error: ",GetLastError());
         }
      }
   }
   return;
}


//Open new order of a given type
void OpenNew(int Command){
   RefreshRates();
   double OpenPrice=0;
   double SLPrice = 0;
   double TPPrice = 0;
   if(Command==OP_BUY){
      OpenPrice=Ask;
      SLPrice=OpenPrice-SL;
      TPPrice=OpenPrice+TP;
   }
   if(Command==OP_SELL){
      OpenPrice=Bid;
      SLPrice=OpenPrice+SL;
      TPPrice=OpenPrice-TP;
   }
   for(int i=1; i<OrderOpRetry; i++){
      int res=OrderSend(Symbol(),Command,LotSize,OpenPrice,Slip,NormalizeDouble(SLPrice,Digits),NormalizeDouble(TPPrice,Digits),"",0,0,Green);
      if(res){
         Print("TRADE - NEW - Order ",res," submitted: Command ",Command," Volume ",LotSize," Open ",OpenPrice," Slippage ",Slip," Stop ",SLPrice," Take ",TPPrice);
         break;
      }
      else Print("ERROR - NEW - error sending order, return error: ",GetLastError());
   }
   return;
}


//Technical analysis of the indicators
bool CrossToOpenBuy=false;
bool CrossToOpenSell=false;
bool CrossToCloseBuy=false;
bool CrossToCloseSell=false;

bool EMAReadyToOpenBuy=false;
bool EMAReadyToOpenSell=false;

bool StochToBuy=false;
bool StochToSell=false;


void CheckEMA() {
   EMAReadyToOpenBuy=false;
   EMAReadyToOpenSell=false;
   double ExponentialMovingAverage50 = iMA(_Symbol, _Period, 50, 0, MODE_EMA, PRICE_CLOSE, 0);
   double ExponentialMovingAverage100 = iMA(_Symbol, _Period, 100, 0, MODE_EMA, PRICE_CLOSE, 0);
   
   if(ExponentialMovingAverage50>ExponentialMovingAverage100) {
      EMAReadyToOpenBuy=true;
   }
   
   if(ExponentialMovingAverage50<ExponentialMovingAverage100) {
      EMAReadyToOpenSell=true;
   }
   
}

void CheckStoch() {
   StochToBuy=false;
   StochToSell=false;
   double StochCurr=iStochastic(Symbol(),0,StochK,StochD,StochSlowing,MODE_SMA,STO_LOWHIGH,MODE_BASE,0);
   if(StochCurr>LowerThreShold) {
      StochToBuy=true;
   }
   if(StochCurr<UpperThreshold) {
      StochToSell=true;
   }

}


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+


void OnTick()
  {
//---
   //Calling initialization, checks and technical analysis
   Initialize();
   CheckCanOrder();
   CheckStoch();
   CheckEMA();
   //Check of Entry/Exit signal with operations to perform
   if(EMAReadyToOpenBuy && StochToBuy) {
      if(CanOpenBuy && CanOrder) OpenNew(OP_BUY);
   }
   if(EMAReadyToOpenSell && StochToSell) {
      if(CanOpenSell && CanOrder) OpenNew(OP_SELL);
   }
   
   
   // if(CrossToCloseBuy) CloseAll(OP_BUY);
   // if(CrossToCloseSell) CloseAll(OP_SELL);
   
  }
//+------------------------------------------------------------------+


Thanks in advance.

Files:
report.PNG  99 kb
graph.PNG  35 kb
 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
 
   if(EMAReadyToOpenBuy && StochToBuy) {
      if(CanOpenBuy && CanOrder)

You are looking at a signal. Act on a change of signal.
          MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 2017.12.12

 
William Roeder:

You are looking at a signal. Act on a change of signal.
          MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 2017.12.12

Thanks! I will try that and see where I get.
Reason: