#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(),14,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<30)
signal="buy";
if(myRSIValue>70)
signal="sell";
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());
}
}
}
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());
}
}
}
trade.Sell(0.01,Symbol());
Comment ("The current signal is: ",signal);
}
}
I hope this is what you need to fix.
This is a start, maybe you want to add other conditions.
Look at the comments, i hope you can understand me.