262 lines
No EOL
8.6 KiB
MQL4
262 lines
No EOL
8.6 KiB
MQL4
//+------------------------------------------------------------------+
|
|
//| EURUSD_Scalper.mq4 |
|
|
//| Copyright 2024, Forex Robot |
|
|
//| https://www.forex.com |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2024, Forex Robot"
|
|
#property link "https://www.forex.com"
|
|
#property version "1.00"
|
|
#property strict
|
|
|
|
//--- Parâmetros de entrada
|
|
extern double LotSize = 0.1; // Tamanho do lote
|
|
extern int StopLoss = 20; // Stop Loss em pips
|
|
extern int TakeProfit = 40; // Take Profit em pips (2x o stop)
|
|
extern int MA_Fast = 10; // Média móvel rápida
|
|
extern int MA_Slow = 20; // Média móvel lenta
|
|
extern int RSI_Period = 14; // Período do RSI
|
|
extern int RSI_Overbought = 70; // Nível de sobrecompra
|
|
extern int RSI_Oversold = 30; // Nível de sobrevenda
|
|
extern int MinDistance = 10; // Distância mínima do spread em pips
|
|
extern bool UseTrailingStop = false; // Usar trailing stop
|
|
extern int TrailingStop = 10; // Trailing stop em pips
|
|
extern int MagicNumber = 12345; // Número mágico para identificação
|
|
|
|
//--- Variáveis globais
|
|
double g_point;
|
|
int g_digits;
|
|
datetime g_lastBarTime = 0;
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Expert initialization function |
|
|
//+------------------------------------------------------------------+
|
|
int OnInit()
|
|
{
|
|
// Configurações básicas
|
|
g_digits = (int)MarketInfo(Symbol(), MODE_DIGITS);
|
|
g_point = MarketInfo(Symbol(), MODE_POINT);
|
|
|
|
// Ajustar para 5 dígitos (alguns brokers)
|
|
if(g_digits == 3 || g_digits == 5)
|
|
g_point *= 10;
|
|
|
|
// Verificar se o símbolo é EUR/USD
|
|
if(StringFind(Symbol(), "EURUSD") < 0)
|
|
{
|
|
Print("ERRO: Este robô foi projetado para EUR/USD apenas!");
|
|
return(INIT_FAILED);
|
|
}
|
|
|
|
// Verificar se o timeframe é M5
|
|
if(Period() != PERIOD_M5)
|
|
{
|
|
Print("ERRO: Este robô foi projetado para timeframe M5 apenas!");
|
|
return(INIT_FAILED);
|
|
}
|
|
|
|
Print("EURUSD Scalper inicializado com sucesso!");
|
|
Print("Stop Loss: ", StopLoss, " pips");
|
|
Print("Take Profit: ", TakeProfit, " pips");
|
|
Print("Média Móvel Rápida: ", MA_Fast);
|
|
Print("Média Móvel Lenta: ", MA_Slow);
|
|
|
|
return(INIT_SUCCEEDED);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Expert deinitialization function |
|
|
//+------------------------------------------------------------------+
|
|
void OnDeinit(const int reason)
|
|
{
|
|
Print("EURUSD Scalper finalizado. Razão: ", reason);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Expert tick function |
|
|
//+------------------------------------------------------------------+
|
|
void OnTick()
|
|
{
|
|
// Verificar se é uma nova barra
|
|
if(!IsNewBar())
|
|
return;
|
|
|
|
// Verificar se há ordens abertas
|
|
if(CountOpenOrders() > 0)
|
|
{
|
|
if(UseTrailingStop)
|
|
ManageTrailingStop();
|
|
return;
|
|
}
|
|
|
|
// Verificar condições de entrada
|
|
int signal = GetSignal();
|
|
|
|
if(signal == 1) // Sinal de compra
|
|
{
|
|
OpenBuyOrder();
|
|
}
|
|
else if(signal == -1) // Sinal de venda
|
|
{
|
|
OpenSellOrder();
|
|
}
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Verificar se é uma nova barra |
|
|
//+------------------------------------------------------------------+
|
|
bool IsNewBar()
|
|
{
|
|
datetime currentBarTime = iTime(Symbol(), Period(), 0);
|
|
if(currentBarTime != g_lastBarTime)
|
|
{
|
|
g_lastBarTime = currentBarTime;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Contar ordens abertas |
|
|
//+------------------------------------------------------------------+
|
|
int CountOpenOrders()
|
|
{
|
|
int count = 0;
|
|
for(int i = 0; i < OrdersTotal(); i++)
|
|
{
|
|
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
|
|
{
|
|
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
|
|
count++;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Obter sinal de entrada |
|
|
//+------------------------------------------------------------------+
|
|
int GetSignal()
|
|
{
|
|
// Calcular médias móveis
|
|
double ma_fast = iMA(Symbol(), Period(), MA_Fast, 0, MODE_EMA, PRICE_CLOSE, 1);
|
|
double ma_slow = iMA(Symbol(), Period(), MA_Slow, 0, MODE_EMA, PRICE_CLOSE, 1);
|
|
double ma_fast_prev = iMA(Symbol(), Period(), MA_Fast, 0, MODE_EMA, PRICE_CLOSE, 2);
|
|
double ma_slow_prev = iMA(Symbol(), Period(), MA_Slow, 0, MODE_EMA, PRICE_CLOSE, 2);
|
|
|
|
// Calcular RSI
|
|
double rsi = iRSI(Symbol(), Period(), RSI_Period, PRICE_CLOSE, 1);
|
|
|
|
// Verificar tendência de alta
|
|
bool uptrend = (ma_fast > ma_slow) && (ma_fast_prev > ma_slow_prev);
|
|
|
|
// Verificar tendência de baixa
|
|
bool downtrend = (ma_fast < ma_slow) && (ma_fast_prev < ma_slow_prev);
|
|
|
|
// Verificar condições de entrada
|
|
if(uptrend && rsi < RSI_Overbought && rsi > 40)
|
|
{
|
|
return 1; // Sinal de compra
|
|
}
|
|
else if(downtrend && rsi > RSI_Oversold && rsi < 60)
|
|
{
|
|
return -1; // Sinal de venda
|
|
}
|
|
|
|
return 0; // Sem sinal
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Abrir ordem de compra |
|
|
//+------------------------------------------------------------------+
|
|
void OpenBuyOrder()
|
|
{
|
|
double price = Ask;
|
|
double sl = price - StopLoss * g_point;
|
|
double tp = price + TakeProfit * g_point;
|
|
|
|
// Verificar distância mínima
|
|
if(MathAbs(price - Bid) > MinDistance * g_point)
|
|
{
|
|
Print("Spread muito alto para entrada de compra");
|
|
return;
|
|
}
|
|
|
|
int ticket = OrderSend(Symbol(), OP_BUY, LotSize, price, 3, sl, tp,
|
|
"EURUSD Scalper Buy", MagicNumber, 0, clrGreen);
|
|
|
|
if(ticket > 0)
|
|
{
|
|
Print("Ordem de compra aberta: Ticket #", ticket, " Preço: ", price,
|
|
" SL: ", sl, " TP: ", tp);
|
|
}
|
|
else
|
|
{
|
|
Print("Erro ao abrir ordem de compra: ", GetLastError());
|
|
}
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Abrir ordem de venda |
|
|
//+------------------------------------------------------------------+
|
|
void OpenSellOrder()
|
|
{
|
|
double price = Bid;
|
|
double sl = price + StopLoss * g_point;
|
|
double tp = price - TakeProfit * g_point;
|
|
|
|
// Verificar distância mínima
|
|
if(MathAbs(price - Ask) > MinDistance * g_point)
|
|
{
|
|
Print("Spread muito alto para entrada de venda");
|
|
return;
|
|
}
|
|
|
|
int ticket = OrderSend(Symbol(), OP_SELL, LotSize, price, 3, sl, tp,
|
|
"EURUSD Scalper Sell", MagicNumber, 0, clrRed);
|
|
|
|
if(ticket > 0)
|
|
{
|
|
Print("Ordem de venda aberta: Ticket #", ticket, " Preço: ", price,
|
|
" SL: ", sl, " TP: ", tp);
|
|
}
|
|
else
|
|
{
|
|
Print("Erro ao abrir ordem de venda: ", GetLastError());
|
|
}
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Gerenciar trailing stop |
|
|
//+------------------------------------------------------------------+
|
|
void ManageTrailingStop()
|
|
{
|
|
for(int i = 0; i < OrdersTotal(); i++)
|
|
{
|
|
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
|
|
{
|
|
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
|
|
{
|
|
if(OrderType() == OP_BUY)
|
|
{
|
|
double newSL = Bid - TrailingStop * g_point;
|
|
if(newSL > OrderStopLoss() && newSL < Bid)
|
|
{
|
|
bool result = OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrBlue);
|
|
if(result)
|
|
Print("Trailing Stop atualizado para compra: ", newSL);
|
|
}
|
|
}
|
|
else if(OrderType() == OP_SELL)
|
|
{
|
|
double newSL = Ask + TrailingStop * g_point;
|
|
if((newSL < OrderStopLoss() || OrderStopLoss() == 0) && newSL > Ask)
|
|
{
|
|
bool result = OrderModify(OrderTicket(), OrderOpenPrice(), newSL, OrderTakeProfit(), 0, clrBlue);
|
|
if(result)
|
|
Print("Trailing Stop atualizado para venda: ", newSL);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |