//+------------------------------------------------------------------+ // JacssonMT4.mq4…
- User
//+------------------------------------------------------------------+ //| JacssonMT4.mq4| //| Generated by ChatGPT | //+------------------------------------------------------------------+
input double Lots = 0.1; // Tamanho do lote input int EMA_Period = 45; // Período da EMA input double Risk_Reward_Ratio = 2.0; // Relação risco/retorno input double StopLossPips = 100; // Stop Loss em Pips input int slippage = 3; // Slippage permitido
//+------------------------------------------------------------------+ //| Função para calcular a EMA | //+------------------------------------------------------------------+ double CalculateEMA(int period, int shift) { return iMA(NULL, 0, period, 0, MODE_EMA, PRICE_CLOSE, shift); }
//+------------------------------------------------------------------+ //| Verifica condições de compra | //+------------------------------------------------------------------+ bool CheckBuyCondition(double ema) { if (Close[1] < ema && Close[0] > ema) return true; return false; }
//+------------------------------------------------------------------+ //| Verifica condições de venda | //+------------------------------------------------------------------+ bool CheckSellCondition(double ema) { if (Close[1] > ema && Close[0] < ema) return true; return false; }
//+------------------------------------------------------------------+ //| Função para o OnTick | //+------------------------------------------------------------------+ void OnTick() { if (Symbol() != "XAUUSD") return; // Executa apenas para GOLD (XAUUSD)
double ema_value = CalculateEMA(EMA_Period, 0);
// Atualiza a EMA no gráfico if (iMA(NULL, 0, EMA_Period, 0, MODE_EMA, PRICE_CLOSE, 0) != 0) { for (int i = 0; i < Bars - EMA_Period; i++) { double ema_value_i = CalculateEMA(EMA_Period, i); ObjectCreate(0, "EMA_45_" + IntegerToString(i), OBJ_TREND, 0, Time[i], ema_value_i, Time[i + 1], CalculateEMA(EMA_Period, i + 1)); ObjectSetInteger(0, "EMA_45_" + IntegerToString(i), OBJPROP_COLOR, clrBlue); } }
if (CheckBuyCondition(ema_value) && OrdersTotal() == 0) { double stop_loss_buy = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, 10, 1)); double take_profit_buy = Close[0] + (Close[0] - stop_loss_buy) * Risk_Reward_Ratio;
codeint buy_ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, slippage, stop_loss_buy, take_profit_buy, "Buy order", 0, 0, clrGreen); if(buy_ticket < 0) { Print("Erro ao abrir a ordem de compra: ", GetLastError()); } - Assistant