Execute Trade only at the open on next New Candle- Need help

rkv007
36
rkv007  

Hi friend,


I am new to Mt5 coding. I need help to add a functionality to this EA. 


when signal is generated it opens new position at the next tick price. // I do not want this 

It should open position only at the opening on next candle.


Here is the code is have



#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>

int pos=0;
int myRSIDefinition;

CTrade trade; // To send orders and close positions
CPositionInfo positionInfo; // to get info about current positions

int OnInit()
  {
// Define the handler once at EA Init, not every tick
   myRSIDefinition=iRSI(Symbol(),Period(),200,PRICE_CLOSE);
// If is not posible to get indicator handler print some error information and return with error code
   if(myRSIDefinition==INVALID_HANDLE)
     {
      Print("There was an error creating RSI handler!");
      return(INIT_FAILED);
     }
   return INIT_SUCCEEDED;
  }

void OnTick()
  {
  
  
   string signal="";
   double  Ask=NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_ASK),Digits());
   double  Bid=NormalizeDouble(SymbolInfoDouble(Symbol(),SYMBOL_BID),Digits());
   double myRSIArray[];
   ArraySetAsSeries(myRSIArray,true);
   CopyBuffer(myRSIDefinition,0,0,3,myRSIArray);
   double myRSIValue=NormalizeDouble(myRSIArray[0],2);
   if(myRSIValue<50)
      signal="sell";
   if(myRSIValue>50)
      signal="buy";

   if(signal=="buy")
     {
      //First close all Sell positions
      for(pos=0; pos<PositionsTotal(); pos++)
        {
         //Select the position to load info
         if(positionInfo.SelectByIndex(pos))
           {
            // Get the position type, if sell then close it
            if(positionInfo.PositionType()==POSITION_TYPE_SELL)
              {
               trade.PositionClose(positionInfo.Ticket());
              }
           }
        }
        if (signal =="buy" && PositionsTotal()<1)
      trade.Buy(0.01,Symbol());
     Comment ("The current signal is: ",signal);
     }

   if(signal=="sell")
     {
      //First close all Buy positions
      for(pos=0; pos<PositionsTotal(); pos++)
        {
         //Select the position to load info
         if(positionInfo.SelectByIndex(pos))
           {
            // Get the position type, if buy then close it
            if(positionInfo.PositionType()==POSITION_TYPE_BUY)
              {
               trade.PositionClose(positionInfo.Ticket());
              }
           }
        }
        
        if (signal =="sell" && PositionsTotal()<1)
      trade.Sell(0.01,Symbol());
     Comment ("The current signal is: ",signal);
     }
     


  }
William Roeder
24261
William Roeder  
rkv007: It should open position only at the opening on next candle.
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          New candle - MQL4 programming forum
aodunusi
102
aodunusi  
William Roeder:
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          New candle - MQL4 programming forum
So what can be used pls for MQL5?
Vladimir Karputov
Moderator
234247
Vladimir Karputov  
aodunusi :
So what can be used pls for MQL5?

You cannot rely on PositionsTotal: you sent a trade order, this trade order is being processed for some time (checked, registered with a broker ...)

Scheme of trade operations: from order creation to execution by broker

While a trade order is being converted to a position, you can get several ticks - and all these ticks PositionsTotal will show '0'.


How I overcome this problem: I send a trade order and wait for the result in OnTradeTransaction (I called such a system barabashkakvn Trading engine 3.XXX )

aodunusi
102
aodunusi  
Vladimir Karputov:

You cannot rely on PositionsTotal: you sent a trade order, this trade order is being processed for some time (checked, registered with a broker ...)

While a trade order is being converted to a position, you can get several ticks - and all these ticks PositionsTotal will show '0'.


How I overcome this problem: I send a trade order and wait for the result in OnTradeTransaction (I called such a system barabashkakvn Trading engine 3.XXX )

Thanks, my problem is a bit smaller. I only want to initiate trade at the open of the next bar candle after my conditions have been met. Been googling alot and the different solutions are driving me mad.

I need the extra code that I can add to my If condition, meaning once I've gotten my confirmation I want the buy trade to be delayed until after the next bar candle (open price).

Would really really appreciate any help I can get. Thanks
Vladimir Karputov
Moderator
234247
Vladimir Karputov  
aodunusi:
Thanks, my problem is a bit smaller. I only want to initiate trade at the open of the next bar candle after my conditions have been met. Been googling alot and the different solutions are driving me mad.

I need the extra code that I can add to my If condition, meaning once I've gotten my confirmation I want the buy trade to be delayed until after the next bar candle (open price).

Would really really appreciate any help I can get. Thanks

Example: iRSI simple advisor

How to start with MQL5
How to start with MQL5
  • 2020.03.05
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
Ali Kareshki
10
Ali Kareshki  
Vladimir Karputov:

Example: iRSI simple advisor

Works like a charm!

Thanks a lot :)