#include <Trade\Trade.mqh> // Inclusão da biblioteca de negociação CTrade trade; //…
- User
#include <Trade\Trade.mqh> // Inclusão da biblioteca de negociação
CTrade trade; // Criando uma instância da classe CTrade para gerenciar operações
// Função para verificar o spread double GetSpread() { return (SymbolInfoDouble(Symbol(), SYMBOL_ASK) - SymbolInfoDouble(Symbol(), SYMBOL_BID)) / _Point; }
// Função para verificar volatilidade com base no ATR (Average True Range) double GetATR(int period = 14) { return iATR(Symbol(), PERIOD_CURRENT, period, 0); }
// Função para verificar se a vela atual é significativa (filtro para evitar consolidação) bool IsValidCandle(double bodySize, double atr) { return bodySize > atr * 0.2; // Tamanho do corpo da vela deve ser maior que 20% do ATR }
// Função para detectar rompimento de máxima/mínima da vela anterior bool IsBreakout(double currentHigh, double currentLow, double prevHigh, double prevLow) { return (currentHigh > prevHigh || currentLow < prevLow); }
// Função principal executada a cada tick void OnTick() { double lotSize = 0.01; double takeProfitPips = 10; // Lucro curto para scalping double stopLossPips = 20; // Stop Loss baseado em volatilidade double trailingStopPips = 10.0; // Trailing Stop para garantir lucros double spreadThreshold = 10.0; // Evitar operar com spread acima de 10 pips
// Obter preços de compra e venda (ask e bid) double askPrice = SymbolInfoDouble(Symbol(), SYMBOL_ASK); double bidPrice = SymbolInfoDouble(Symbol(), SYMBOL_BID); // Evitar operações com spread elevado if (GetSpread() > spreadThreshold) { return; // Não executar ordens } // Preço da vela atual e anterior double open1 = iOpen(NULL, PERIOD_CURRENT, 0); double close1 = iClose(NULL, PERIOD_CURRENT, 0); double low1 = iLow(NULL, PERIOD_CURRENT, 0); double high1 = iHigh(NULL, PERIOD_CURRENT, 0); double open2 = iOpen(NULL, PERIOD_CURRENT, 1); double close2 = iClose(NULL, PERIOD_CURRENT, 1); double low2 = iLow(NULL, PERIOD_CURRENT, 1); double high2 = iHigh(NULL, PERIOD_CURRENT, 1); double atr = GetATR(); // Calcular o ATR atual // Tamanho do corpo da vela double bodySize = MathAbs(close1 - open1); // Verificar se a vela atual é válida para scalping if (!IsValidCandle(bodySize, atr)) { return; // Não operar se a vela for muito pequena } // Verificar rompimento de máxima/mínima if (IsBreakout(high1, low1, high2, low2)) { MqlTradeRequest request; MqlTradeResult result; ZeroMemory(request); ZeroMemory(result); // Regras de compra (rompimento de alta) if (close1 > high2 && bodySize > atr * 0.5) { // Confirmação de alta com corpo significativo request.action = TRADE_ACTION_DEAL; request.symbol = Symbol(); request.volume = lotSize; request.type = ORDER_TYPE_BUY; request.price = NormalizeDouble(askPrice, _Digits); request.tp = NormalizeDouble(askPrice + takeProfitPips * _Point, _Digits); request.sl = NormalizeDouble(askPrice - stopLossPips * _Point, _Digits); request.deviation = 3; if (OrderSend(request, result)) Print("Ordem de Compra Executada!"); else Print("Erro ao executar a ordem de compra: ", GetLastError()); } // Regras de venda (rompimento de baixa) else if (close1 < low2 && bodySize > atr * 0.5) { // Confirmação de baixa com corpo significativo request.action = TRADE_ACTION_DEAL; request.symbol = Symbol(); request.volume = lotSize; request.type = ORDER_TYPE_SELL; request.price = NormalizeDouble(bidPrice, _Digits); request.tp = NormalizeDouble(bidPrice - takeProfitPips * _Point, _Digits); request.sl = NormalizeDouble(bidPrice + stopLossPips * _Point, _Digits); request.deviation = 3; if (OrderSend(request, result)) Print("Ordem de Venda Executada!"); else Print("Erro ao executar a ordem de venda: ", GetLastError()); } } // Gestão de posições abertas com Trailing Stop for (int i = PositionsTotal() - 1; i >= 0; i--) { ulong ticket = PositionGetTicket(i); if (PositionSelectByTicket(ticket)) { double profit = PositionGetDouble(POSITION_PROFIT); double openPrice = PositionGetDouble(POSITION_PRICE_OPEN); double currentPrice = (PositionGetInteger(POSITION_TYPE) == ORDER_TYPE_BUY) ? askPrice : bidPrice; double newSL; // Ajustar o Trailing Stop if (PositionGetInteger(POSITION_TYPE) == ORDER_TYPE_BUY && currentPrice - trailingStopPips * _Point > PositionGetDouble(POSITION_SL)) { newSL = NormalizeDouble(currentPrice - trailingStopPips * _Point, _Digits); trade.PositionModify(ticket, newSL, PositionGetDouble(POSITION_TP)); Print("Trailing Stop ajustado para posição de compra: ", DoubleToString(newSL, _Digits)); } else if (PositionGetInteger(POSITION_TYPE) == ORDER_TYPE_SELL && currentPrice + trailingStopPips * _Point < PositionGetDouble(POSITION_SL)) { newSL = NormalizeDouble(currentPrice + trailingStopPips * _Point, _Digits); trade.PositionModify(ticket, newSL, PositionGetDouble(POSITION_TP)); Print("Trailing Stop ajustado para posição de venda: ", DoubleToString(newSL, _Digits)); } } } - Assistant
The error you're encountering is due to the incorrect number of parameters passed to the
iATRfunction in MQL5. Let's fix your code to call this function correctly. According to the documentation, theiATRfunction requires three parameters:- Symbol name as a string.
- Timeframe as an enumeration.
- Period as an integer.
However, in your current call to
iATR, you are missing the symbol and timeframe parameters. Let's modify theGetATRfunction to resolve this:double GetATR(int period = 14) { return iATR(Symbol(), PERIOD_CURRENT, period, 0); // Adding missing parameters: Symbol() and PERIOD_CURRENT }Additionally, ensure that you call this function correctly from within your
OnTickfunction. Here's the corrected code for context:// Função para verificar volatilidade com base no ATR (Average True Range) double GetATR(int period = 14) { return iATR(Symbol(), PERIOD_CURRENT, period); } - User
o robo nao esta abrindo ordens
- Assistant
- User
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.