Need help to troubleshoot my EA

 

Hi fellow traders,

Recently I have developed an EA, but has a glitch where I can not fix it. So would any of you please help me find and fix the problem?

Quick intro: This EA is to Buy on a break above RSI level of 30, and is to Sell on a break below RSI level of 70 at the candle close. The issue is that, it opens the order on every tick and doesn't wait for the candle to close.

Below is the EA script:


input double TakeProfit    =1000;
input double Lots          =0.1;
input double TrailingStop  =0;
input double StopLoss      =10000;
input int    RSIPeriod     =14;
input int    RSIUpperLevel =70;
input int    RSILowerLevel =30;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick(void)
  {
   double RSICurrent,RSIPrevious;
   int    ticket,total;
//---

   if(Bars<100)
     {
      Print("bars less than 100");
      return;
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return;
     }
//--- to simplify the coding and speed up access data are put into internal variables
    RSICurrent=iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,0);
    RSIPrevious=iRSI(NULL,0,RSIPeriod,PRICE_CLOSE,1);

//--------------------------------------------------------------------------      
   total=OrdersTotal();
   static datetime prevTime;
   if(Time[0]!=prevTime)

     {
      //--- no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ",AccountFreeMargin());
         return;
        }
      //--- check for long position (BUY) possibility
      if(RSICurrent>RSILowerLevel && RSIPrevious<RSILowerLevel)
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"My sample",16384,0,Green);
          
         
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               { Print("BUY order opened : ",OrderOpenPrice());
                 prevTime=Time[0];
                 ticket=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-StopLoss*Point,Ask+TakeProfit*Point,0,Yellow);
               }
           }
         else
            Print("Error opening BUY order : ",GetLastError());
         return;
        }
      //--- check for short position (SELL) possibility
      if(RSICurrent<RSIUpperLevel && RSIPrevious>RSIUpperLevel)
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"My sample",16384,0,Red);
                
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
               { Print("SELL order opened : ",OrderOpenPrice());
                 prevTime=Time[0];
                 ticket=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+StopLoss*Point,Bid-TakeProfit*Point,0,Yellow);
               }
           }
         else
            Print("Error opening SELL order : ",GetLastError());
        }       
      //--- exit from the "no opened orders" block
      return;
     }
//--- it is important to enter the market correctly, but it is more important to exit it correctly...   

  }
 
mjstock78:

Hi fellow traders,

Recently I have developed an EA, but has a glitch where I can not fix it. So would any of you please help me find and fix the problem?

Quick intro: This EA is to Buy on a break above RSI level of 30, and is to Sell on a break below RSI level of 70 at the candle close. The issue is that, it opens the order on every tick and doesn't wait for the candle to close.

Below is the EA script:


You could add this to your opening conditions:

Volume[0]<=1

So effectively a trade can only be opened when a new bar shows up.

 
if(RSICurrent<RSIUpperLevel && RSIPrevious>RSIUpperLevel && Volume[0]<=1), the same for the other operation
 
Catalin Zachiu:

Thanks. But after adding that command to my EA, it doesn't open any order. !!!

 
mjstock78:

Thanks. But after adding that command to my EA, it doesn't open any order. !!!

oh yeah that means that the action happens inside the canlde , your EA looks very similar to the Macd Sample from mt4 , you should try to code it within the Moving Average sample , it has separate entry and exit with Open on candle open but that can be resolved . will try to send you an example ......

 
Catalin Zachiu:

oh yeah that means that the action happens inside the canlde , your EA looks very similar to the Macd Sample from mt4 , you should try to code it within the Moving Average sample , it has separate entry and exit with Open on candle open but that can be resolved . will try to send you an example ......

Yes. I modified MACD sample to create my EA. 

I appreciate your kind help Catalin.

 
mjstock78:

Yes. I modified MACD sample to create my EA. 

I appreciate your kind help Catalin.

Files:
 
Catalin Zachiu:

it works now , fine results , though stoploss is very large , in the long term it will be on break-even 

 
2019.02.05 12:11:37.005 Not enough memory for 251150 bars for indicator Moving Average (AUDCAD,M1)
I have this message at Journal on MT4 EA at varous symbols. Anybody would help me on that?
 
is there anything to do with viruses?
 

simple way

if(Volume[0]<=1) ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"My sample",16384,0,Green);
Reason: