help with - Void Exit ( Exit/close trade on current symbol)

 

Hi Guys,


For my EA I am using Bool function criteria's with return True/False to meet Buy requirement for example:

if(MATRUE (){

   Buy();  
void Buy(){ 
  trade.Buy(sizeOfShare,NULL,Ask,Ask-stopLossPips*_Point,Ask+profitPips*_Point, NULL); }



Now I want to make the same function but then to close trade on symbol.\

I know this following Void Exit part is wrong, someone can give a help?

if (MATRUE () {
   Exit();
  }


  void Exit(){   if (PositionsTotal()>0)      {      if       int k=PositionsTotal()-1;
      while (k>=0)         {     if (trade.PositionClose(PositionGetSymbol(k))) k--; 
 
anyone?
 

An example of an adviser that closes all positions: Close all positions

The command that starts the uninstall:

m_close_all=true;

then we check: if the flag , and check whether the positions remained in the market or not and then delete the positions 

//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(m_close_all)
     {
      if(IsPositionExists())
         CloseAllPositions();
      else
         m_close_all=false;
     }
//--- we work only at the time of the birth of new bar
   static datetime PrevBars=0;
   datetime time_0=iTime(Symbol(),Period(),0);
   if(time_0==PrevBars)
      return;
//---
   if(ProfitAllPositions()>=InpProfit)
      m_close_all=true;
  }
 

Thanks for the input I now almost made it work like this:

But with this closing of trade it now makes: 

"i" - undeclared identifier

Anyone know how to fix this position close on symbol?

  void Exit(){
  trade.PositionClose(PositionGetSymbol(i),0.1);}  
 
  void Exit(){
  for(int i=1;i<PositionsTotal();i++)
  trade.PositionClose(PositionGetSymbol(i),0.1);}  
  

right now I use this and it does not make any errors.


but trades are not closing.. anyone can fix this?


bool EXITRSIAllowsTrade(){
     double exitrsi = iRSI(_Symbol,_Period,RSIPERIODexit,applied_price2exit);
     double         iRSIBuffer[];

     CopyBuffer(exitrsi,0,0,1,iRSIBuffer);
    
     if(iRSIBuffer[0] > RSIMinexit)
     {
      return true;
     }
     return false;}   

this is the bool that I am using as a criteria to exit

 
tradesbyjames:

right now I use this and it does not make any errors.


but trades are not closing.. anyone can fix this?


this is the bool that I am using as a criteria to exit

You define the iRSIBuffer array. But you do not set it as a time series. This means that iRSIBuffer[0] contains the oldest data element. If you set iRSIBuffer as a time series then iRSIBuffer[0] will contain the most recent RSI value.
 
WindmillMQL:
You define the iRSIBuffer array. But you do not set it as a time series. This means that iRSIBuffer[0] contains the oldest data element. If you set iRSIBuffer as a time series then iRSIBuffer[0] will contain the most recent RSI value.

Oh thanks, I was doing it like this for all my indicators.. how do I add a time series?
Thank you for this big find!

 
tradesbyjames:

Oh thanks, I was doing it like this for all my indicators.. how do I add a time series?
Thank you for this big find!

You can find the answer to your question in the documentation:  https://www.mql5.com/en/docs/array

ArraySetAsSeries.

Documentation on MQL5: Array Functions
Documentation on MQL5: Array Functions
  • www.mql5.com
. In a particular case of a one-dimensional array of 50 elements, calling of the first element will appear as array[0], of the last one - as array[49].
Reason: