Sure! Below is the MQL5 version of your provided MQL4 code:
//+------------------------------------------------------------------+
//| SSBEA.mq5 |
//| Copyright ? 2023, Converted from MQL4 to MQL5 |
//| http://ssb.bigfx.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright ? 2009, Yury V. Reshetov http://ssb.bigfx.ru"
#property link "http://ssb.bigfx.ru"
#property version "1.00"
#property description "Converted from MQL4 to MQL5"
#include <Trade\Trade.mqh>
input double lots = 1.0;
input int prsi = 13;
input int pcci = 21;
input int fastmacd = 6;
input int slowmacd = 60;
input int signalmacd = 46;
input int magic = 888;
input int slippage = 0;
static datetime prevtime = 0;
CTrade trade;
// Indicator handles
int rsi_handle;
int cci_handle;
int ac_handle;
int ao_handle;
int macd_handle;
int osma_handle;
int OnInit()
{
prevtime = iTime(_Symbol, _Period, 0);
// Initialize indicators
rsi_handle = iRSI(_Symbol, _Period, prsi, PRICE_OPEN);
if(rsi_handle == INVALID_HANDLE)
{
Print("Failed to create RSI handle");
return(INIT_FAILED);
}
cci_handle = iCCI(_Symbol, _Period, pcci, PRICE_OPEN);
if(cci_handle == INVALID_HANDLE)
{
Print("Failed to create CCI handle");
return(INIT_FAILED);
}
ac_handle = iAC(_Symbol, _Period);
if(ac_handle == INVALID_HANDLE)
{
Print("Failed to create AC handle");
return(INIT_FAILED);
}
ao_handle = iAO(_Symbol, _Period);
if(ao_handle == INVALID_HANDLE)
{
Print("Failed to create AO handle");
return(INIT_FAILED);
}
macd_handle = iMACD(_Symbol, _Period, fastmacd, slowmacd, signalmacd, PRICE_OPEN);
if(macd_handle == INVALID_HANDLE)
{
Print("Failed to create MACD handle");
return(INIT_FAILED);
}
osma_handle = iOsMA(_Symbol, _Period, fastmacd, slowmacd, signalmacd, PRICE_OPEN);
if(osma_handle == INVALID_HANDLE)
{
Print("Failed to create OsMA handle");
return(INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
if(rsi_handle != INVALID_HANDLE) IndicatorRelease(rsi_handle);
if(cci_handle != INVALID_HANDLE) IndicatorRelease(cci_handle);
if(ac_handle != INVALID_HANDLE) IndicatorRelease(ac_handle);
if(ao_handle != INVALID_HANDLE) IndicatorRelease(ao_handle);
if(macd_handle != INVALID_HANDLE) IndicatorRelease(macd_handle);
if(osma_handle != INVALID_HANDLE) IndicatorRelease(osma_handle);
}
void OnTick()
{
// Check if trading is allowed
if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED) || !MQLInfoInteger(MQL_TRADE_ALLOWED))
return;
// Check if we've processed this bar already
datetime currentTime = iTime(_Symbol, _Period, 0);
if(currentTime == prevtime)
return;
prevtime = currentTime;
// First, see if we have any positions with our magic number and symbol
int total = PositionsTotal();
bool position_found = false;
for(int i = total - 1; i >= 0; i--)
{
if(PositionSelectByIndex(i))
{
string position_symbol = PositionGetString(POSITION_SYMBOL);
long position_magic = PositionGetInteger(POSITION_MAGIC);
if(position_symbol == _Symbol && position_magic == magic)
{
position_found = true;
ulong prevticket = PositionGetInteger(POSITION_TICKET);
ENUM_POSITION_TYPE positionType = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
if(positionType == POSITION_TYPE_BUY)
{
if(shortsignal())
{
// Close the BUY position and open a SELL position
double volume = PositionGetDouble(POSITION_VOLUME);
double price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
bool closed = trade.PositionClose(prevticket, slippage * _Point);
if(!closed)
{
Print("Failed to close BUY position. Error: ", GetLastError());
prevtime = iTime(_Symbol, _Period, 1); // Try again in next bar
return;
}
Sleep(1000); // Wait a bit before opening new position
bool opened = trade.Sell(2.0 * lots, NULL, 0.0, 0.0, 0.0, "SSB Sell");
if(!opened)
{
Print("Failed to open SELL position. Error: ", GetLastError());
prevtime = iTime(_Symbol, _Period, 1); // Try again in next bar
}
return;
}
}
else if(positionType == POSITION_TYPE_SELL)
{
if(longsignal())
{
// Close the SELL position and open a BUY position
double volume = PositionGetDouble(POSITION_VOLUME);
double price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
bool closed = trade.PositionClose(prevticket, slippage * _Point);
if(!closed)
{
Print("Failed to close SELL position. Error: ", GetLastError());
prevtime = iTime(_Symbol, _Period, 1); // Try again in next bar
return;
}
Sleep(1000); // Wait a bit before opening new position
bool opened = trade.Buy(2.0 * lots, NULL, 0.0, 0.0, 0.0, "SSB Buy");
if(!opened)
{
Print("Failed to open BUY position. Error: ", GetLastError());
prevtime = iTime(_Symbol, _Period, 1); // Try again in next bar
}
return;
}
}
return;
}
}
}
// If there are no positions, open new positions based on signals
if(!position_found)
{
if(longsignal())
{
bool opened = trade.Buy(lots, NULL, 0.0, 0.0, 0.0, "SSB Buy");
if(!opened)
{
Print("Failed to open BUY position. Error: ", GetLastError());
prevtime = iTime(_Symbol, _Period, 1); // Try again in next bar
}
return;
}
if(shortsignal())
{
bool opened = trade.Sell(lots, NULL, 0.0, 0.0, 0.0, "SSB Sell");
if(!opened)
{
Print("Failed to open SELL position. Error: ", GetLastError());
prevtime = iTime(_Symbol, _Period, 1); // Try again in next bar
}
return;
}
}
}
bool longsignal()
{
if(fcandle() < 0) return(false);
if(frsi() < 0) return(false);
if(fcci() < 0) return(false);
if(fac1() < 0) return(false);
if(fao() < 0) return(false);
if(fao1() < 0) return(false);
if(fmacd() < 0) return(false);
if(fmacd1() < 0) return(false);
if(fosma1() < 0) return(false);
return(true);
}
bool shortsignal()
{
if(fcandle() > 0) return(false);
if(frsi() > 0) return(false);
if(fcci() > 0) return(false);
if(fac1() > 0) return(false);
if(fao() > 0) return(false);
if(fao1() > 0) return(false);
if(fmacd() > 0) return(false);
if(fmacd1() > 0) return(false);
if(fosma1() > 0) return(false);
return(true);
}
int fcandle()
{
int result = 0;
double open_prices[2];
if(CopyOpen(_Symbol, _Period, 0, 2, open_prices) != 2)
{
Print("Failed to copy Open prices. Error:", GetLastError());
return 0;
}
if(open_prices[0] < open_prices[1])
result = 1;
else if(open_prices[0] > open_prices[1])
result = -1;
return(result);
}
int frsi()
{
int result = 0;
double rsi_values[2];
if(CopyBuffer(rsi_handle, 0, 0, 2, rsi_values) != 2)
{
Print("Failed to copy RSI data. Error:", GetLastError());
return 0;
}
double ind = rsi_values[0] - 50.0;
if(ind < 0)
result = 1;
else if(ind > 0)
result = -1;
return result;
}
int fcci()
{
int result = 0;
double cci_values[1];
if(CopyBuffer(cci_handle, 0, 0, 1, cci_values) != 1)
{
Print("Failed to copy CCI data. Error:", GetLastError());
return 0;
}
double ind = cci_values[0];
if(ind > 0)
result = 1;
else if(ind < 0)
result = -1;
return result;
}
int fac1()
{
int result = 0;
double ac_values[2];
if(CopyBuffer(ac_handle, 0, 0, 2, ac_values) != 2)
{
Print("Failed to copy AC data. Error:", GetLastError());
return 0;
}
double ind = ac_values[0];
double ind1 = ac_values[1];
if((ind - ind1) > 0)
result = 1;
else if((ind - ind1) < 0)
result = -1;
return result;
}
int fao()
{
int result = 0;
double ao_values[1];
if(CopyBuffer(ao_handle, 0, 0, 1, ao_values) != 1)
{
Print("Failed to copy AO data. Error:", GetLastError());
return 0;
}
double ind = ao_values[0];
if(ind < 0)
result = 1;
else if(ind > 0)
result = -1;
return result;
}
int fao1()
{
int result = 0;
double ao_values[2];
if(CopyBuffer(ao_handle, 0, 0, 2, ao_values) != 2)
{
Print("Failed to copy AO data. Error:", GetLastError());
return 0;
}
double ind = ao_values[0];
double ind1 = ao_values[1];
if((ind - ind1) > 0)
result = 1;
else if((ind - ind1) < 0)
result = -1;
return result;
}
int fmacd()
{
int result = 0;
double macd_main[1];
if(CopyBuffer(macd_handle, 0, 0, 1, macd_main) != 1)
{
Print("Failed to copy MACD data. Error:", GetLastError());
return 0;
}
double ind = macd_main[0];
if(ind < 0)
result = 1;
else if(ind > 0)
result = -1;
return result;
}
int fmacd1()
{
int result = 0;
double macd_main[2];
if(CopyBuffer(macd_handle, 0, 0, 2, macd_main) != 2)
{
Print("Failed to copy MACD data. Error:", GetLastError());
return 0;
}
double ind = macd_main[0];
double ind1 = macd_main[1];
if((ind - ind1) > 0)
result = 1;
else if((ind - ind1) < 0)
result = -1;
return result;
}
int fosma1()
{
int result = 0;
double osma_values[2];
if(CopyBuffer(osma_handle, 0, 0, 2, osma_values) != 2)
{
Print("Failed to copy OsMA data. Error:", GetLastError());
return 0;
}
double ind = osma_values[0];
double ind1 = osma_values[1];
if((ind - ind1) > 0)
result = 1;
else if((ind - ind1) < 0)
result = -1;
return result;
}
This code is an MQL5 version of your provided MQL4 code. It uses the CTrade class for trading operations and CopyBuffer to access the indicator data. It also includes error checking for each indicator and handles the trading logic accordingly.