//+------------------------------------------------------------------+
bool PositionExists(int type)
{
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
if (PositionSelect(i))
{
if (PositionGetInteger(POSITION_TYPE) == type)
return true;
}
}
return false;
}
{
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
if (PositionSelect(i))
{
ulong ticket = PositionGetInteger(POSITION_TICKET);
if (trade.PositionClose(ticket))
{
Print("Posição fechada com sucesso: Ticket ", IntegerToString(ticket));
}
else
{
Print("Falha ao fechar posição: Ticket ", IntegerToString(ticket), " Erro: ", GetLastError());
}
}
}
}
implicit conversion from 'number' to 'string' HFT_Medias_04.mq5 127 28
implicit conversion from 'number' to 'string' HFT_Medias_04.mq5 143 28
erros acima:
meu codigo abaixo:
#include <Trade\Trade.mqh>
input int FastEMAPeriod = 13; // Período da EMA rápida
input int SlowEMAPeriod = 45; // Período da EMA lenta
input double LotSize = 0.1; // Tamanho do lote
input double Slippage = 3; // Slippage tolerado
double FastEMA[], SlowEMA[];
int FastEMA_Handle, SlowEMA_Handle;
CTrade trade;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
FastEMA_Handle = iMA(NULL, 0, FastEMAPeriod, 0, MODE_EMA, PRICE_CLOSE);
SlowEMA_Handle = iMA(NULL, 0, SlowEMAPeriod, 0, MODE_EMA, PRICE_CLOSE);
if (FastEMA_Handle < 0 || SlowEMA_Handle < 0)
{
Print("Erro na criação das EMAs!");
return INIT_FAILED;
}
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if (FastEMA_Handle >= 0) IndicatorRelease(FastEMA_Handle);
if (SlowEMA_Handle >= 0) IndicatorRelease(SlowEMA_Handle);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if (CopyBuffer(FastEMA_Handle, 0, 0, 3, FastEMA) < 0 || CopyBuffer(SlowEMA_Handle, 0, 0, 3, SlowEMA) < 0)
{
Print("Erro ao copiar os buffers das EMAs!");
return;
}
if (FastEMA[1] > SlowEMA[1] && FastEMA[2] <= SlowEMA[2])
{
if (IsBullishEngulfing() && !PositionExists(POSITION_TYPE_BUY))
{
SendTradeOrder(POSITION_TYPE_BUY);
}
}
else if (FastEMA[1] < SlowEMA[1] && FastEMA[2] >= SlowEMA[2])
{
if (IsBearishEngulfing() && !PositionExists(POSITION_TYPE_SELL))
{
CloseAllPositions();
SendTradeOrder(POSITION_TYPE_SELL);
}
}
}
//+------------------------------------------------------------------+
//| Funções para identificação de padrões de velas |
//+------------------------------------------------------------------+
bool IsBullishEngulfing()
{
double Close[], Open[];
if (CopyClose(NULL, PERIOD_CURRENT, 0, 3, Close) < 3 || CopyOpen(NULL, PERIOD_CURRENT, 0, 3, Open) < 3)
return false;
return (Close[1] > Open[1] && Open[2] > Close[2] && Close[1] > Open[2] && Open[1] < Close[2]);
}
bool IsBearishEngulfing()
{
double Close[], Open[];
if (CopyClose(NULL, PERIOD_CURRENT, 0, 3, Close) < 3 || CopyOpen(NULL, PERIOD_CURRENT, 0, 3, Open) < 3)
return false;
return (Close[1] < Open[1] && Open[2] < Close[2] && Close[1] < Open[2] && Open[1] > Close[2]);
}
//+------------------------------------------------------------------+
//| Função para enviar ordens de compra e venda |
//+------------------------------------------------------------------+
bool SendTradeOrder(int type)
{
double price = (type == POSITION_TYPE_BUY) ? SymbolInfoDouble(_Symbol, SYMBOL_ASK) : SymbolInfoDouble(_Symbol, SYMBOL_BID);
if (MathAbs(SymbolInfoDouble(_Symbol, SYMBOL_LAST) - FastEMA[1]) < SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE) * 10)
{
bool result = false;
if (type == POSITION_TYPE_BUY)
{
result = trade.Buy(LotSize, NULL, price, 0, 0, "Buy Order");
}
else if (type == POSITION_TYPE_SELL)
{
result = trade.Sell(LotSize, NULL, price, 0, 0, "Sell Order");
}
if (result)
{
Print("Trade executado com sucesso: ", (type == POSITION_TYPE_BUY ? "COMPRA" : "VENDA"));
}
else
{
Print("Falha ao executar trade: ", GetLastError());
}
return result;
}
return false;
}
//+------------------------------------------------------------------+
//| Função para verificar se há posições abertas |
//+------------------------------------------------------------------+
bool PositionExists(int type)
{
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
if (PositionSelect(i))
{
if (PositionGetInteger(POSITION_TYPE) == type)
return true;
}
}
return false;
}
//+------------------------------------------------------------------+
//| Função para fechar todas as posições abertas |
//+------------------------------------------------------------------+
void CloseAllPositions()
{
for (int i = PositionsTotal() - 1; i >= 0; i--)
{
if (PositionSelect(i))
{
ulong ticket = PositionGetInteger(POSITION_TICKET);
if (trade.PositionClose(ticket))
{
Print("Posição fechada com sucesso: Ticket ", IntegerToString(ticket));
}
else
{
Print("Falha ao fechar posição: Ticket ", IntegerToString(ticket), " Erro: ", GetLastError());
}
}
}
}