Turn an Ea from working on every tick to work on open prices only, or every 15 seconds or any such interval.

 

I have been exposed to writing the trading logic onside the ontick() function and see them trade on the logic programmed.


void OnTick()
  {
   ArraySetAsSeries(ma1,true);
   ArraySetAsSeries(ma2,true);
   ArraySetAsSeries(close,true);
   CopyBuffer(maHandle1,0,0,1,ma1);
   CopyBuffer(maHandle2,0,0,1,ma2);
   CopyClose(_Symbol,0,0,1,close);
// Trade structures
   MqlTradeRequest request;
   MqlTradeResult result;
   ZeroMemory(request);
// Current position information
   bool openPosition = PositionSelect(_Symbol);
   long positionType = PositionGetInteger(POSITION_TYPE);
   ulong position_ticket=PositionGetTicket(0);
    if(ma2[0]>ma1[0])
     {
      // Open buy market order
      if(positionType==POSITION_TYPE_SELL)
         m_trade.PositionClose(position_ticket);
      if(openPosition==false || positionType==POSITION_TYPE_SELL)
        {
         ZeroMemory(request);
         request.action = TRADE_ACTION_DEAL;
         request.type = ORDER_TYPE_BUY;
         request.symbol = _Symbol;
         request.volume = TradeVolume;
         request.type_filling = ORDER_FILLING_IOC;
         request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
         request.sl = 0;
         request.tp = 0;
         request.deviation = 50;
         OrderSend(request,result);
         request.position = result.order;
         // Modify SL/TP
         if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
           {
            request.action = TRADE_ACTION_SLTP;

            double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
            if(StopLoss > 0)
               request.sl = positionOpenPrice - (StopLoss * _Point);
            if(TakeProfit > 0)
               request.tp = positionOpenPrice + (TakeProfit * _Point);
            if(request.sl > 0 && request.tp > 0)
               OrderSend(request,result);
           }
        }
     }
   if(ma2[0]<ma1[0])
     {
       if(positionType==POSITION_TYPE_BUY)
         m_trade.PositionClose(position_ticket);
      // Open sell market order
      if(openPosition == false|| positionType==POSITION_TYPE_BUY)
        {
         ZeroMemory(request);
         request.action = TRADE_ACTION_DEAL;
         request.type = ORDER_TYPE_SELL;
         request.symbol = _Symbol;
         request.volume = TradeVolume;
         request.type_filling = ORDER_FILLING_IOC;
         request.price = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
         request.sl = 0;
         request.tp = 0;
         request.deviation = 50;
         OrderSend(request,result);
         request.position = result.order;
         // Modify SL/TP
         if(result.retcode == TRADE_RETCODE_PLACED || result.retcode == TRADE_RETCODE_DONE)
           {
            request.action = TRADE_ACTION_SLTP;
            double positionOpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
            if(StopLoss > 0)
               request.sl = positionOpenPrice - (StopLoss * _Point);
            if(TakeProfit > 0)
               request.tp = positionOpenPrice + (TakeProfit * _Point);
            if(request.sl > 0 && request.tp > 0)
               OrderSend(request,result);
           }
        }
     }
  }

This is a simple code( I know this is non-working but take it for a practical code that we will work on). I want it to trade or check trading conditions every 15 seconds. So what changes do I need to make ???


Thanks.

 
use OnTimer() for it
 
Pak Hong Poon #:
use OnTimer() for it

Thanks a Lot.


I browsed through the forum and was stuck in using datetime variable along with itime function and a fixed time gap variable. But since tick delay in not constant, so the diy timer was inaccurate. Worked almost fine for timegap greater than 2-3 mins.


But this OnTimer() is perfect . solved the issue in minutes.

 
saurabhsuman003 #:

Thanks a Lot.


I browsed through the forum and was stuck in using datetime variable along with itime function and a fixed time gap variable. But since tick delay in not constant, so the diy timer was inaccurate. Worked almost fine for timegap greater than 2-3 mins.


But this OnTimer() is perfect . solved the issue in minutes.

you are welcome:)