#include <Trade\Trade.mqh> input double lotSize = 0.1; // Variáveis globais para…
- User
#include <Trade\Trade.mqh>
input double lotSize = 0.1;
// Variáveis globais para controle de ordens bool orderOpened = false; ulong orderTicket = 0; bool orderAlreadyOpen = false; // Nova variável para controlar se uma ordem já estava aberta.
// Variáveis para armazenar rompimento anterior bool lastBreakoutAlta = false; bool lastBreakoutBaixa = false; double breakoutHigh = 0.0; double breakoutLow = 0.0;
// Configurações de trailing stop input int TslTriggerPoints = 15; // Pontos de lucro antes do trailing stop ser ativado input int TslPoints = 10; // Trailing stop em pips
//+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { // Configurações do EA return(INIT_SUCCEEDED); }
//+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { // Função de finalização }
//+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { // Variáveis para bandas de Bollinger double bollingerUpper[], bollingerLower[], bollingerMiddle[];
// Configurações das Bandas de Bollinger (período 20, desvio 2) int bbPeriod = 20; double bbDeviation = 2.0; double slippage = 3 * _Point; // Obtenção das Bandas de Bollinger int bbHandle = iBands(_Symbol, PERIOD_M2, bbPeriod, bbDeviation, 0, PRICE_CLOSE); // Verifique se o handle é válido if (bbHandle == INVALID_HANDLE) { Print("Erro ao obter handle das Bandas de Bollinger: ", GetLastError()); return; } // Inicializar os buffers das bandas de Bollinger ArraySetAsSeries(bollingerUpper, true); ArraySetAsSeries(bollingerMiddle, true); ArraySetAsSeries(bollingerLower, true); // Preencher os buffers if (CopyBuffer(bbHandle, 0, 0, 1, bollingerUpper) <= 0 || CopyBuffer(bbHandle, 1, 0, 1, bollingerMiddle) <= 0 || CopyBuffer(bbHandle, 2, 0, 1, bollingerLower) <= 0) { Print("Erro ao copiar buffer das Bandas de Bollinger: ", GetLastError()); return; } // Preço atual e range da vela double priceHigh = iHigh(_Symbol, PERIOD_M2, 0); double priceLow = iLow(_Symbol, PERIOD_M2, 0); double priceClose = iClose(_Symbol, PERIOD_M2, 0); double range = priceHigh - priceLow; // Objeto para enviar ordens CTrade trade; // Fechar todas as posições no lucro se o preço estiver perto da linha do meio da Banda de Bollinger for (int i = PositionsTotal() - 1; i >= 0; i--) { if (PositionGetTicket(i) > 0) { ulong positionTicket = PositionGetTicket(i); double positionProfit = PositionGetDouble(POSITION_PROFIT); double positionPrice = PositionGetDouble(POSITION_PRICE_CURRENT); // Verifica se a posição está no lucro e se o preço está perto da linha do meio if (positionProfit > 0 && positionPrice >= bollingerMiddle[0] - slippage && positionPrice <= bollingerMiddle[0] + slippage) { if (trade.PositionClose(positionTicket)) { Print("Posição lucrativa fechada perto da linha central da Banda de Bollinger. Ticket: ", positionTicket); } else { Print("Erro ao fechar a posição: ", GetLastError()); } } } } // Atualizar informações de rompimento e confirmação if (lastBreakoutAlta && priceClose > breakoutHigh && !orderAlreadyOpen) // Ignora abertura de ordem se uma já estava aberta. { double slBuy = priceClose - 10 * _Point; if (trade.Buy(lotSize, _Symbol, slBuy)) { orderOpened = true; orderTicket = trade.ResultOrder(); Print("Ordem de compra aberta com sucesso após confirmação! Ticket: ", orderTicket); lastBreakoutAlta = false; } else { Print("Erro ao abrir ordem de compra: ", trade.ResultRetcode(), " - ", GetLastError()); } } if (lastBreakoutBaixa && priceClose < breakoutLow && !orderAlreadyOpen) // Ignora abertura de ordem se uma já estava aberta. { double slSell = priceClose + 10 * _Point; if (trade.Sell(lotSize, _Symbol, slSell)) { orderOpened = true; orderTicket = trade.ResultOrder(); Print("Ordem de venda aberta com sucesso após confirmação! Ticket: ", orderTicket); lastBreakoutBaixa = false; } else { Print("Erro ao abrir ordem de venda: ", trade.ResultRetcode(), " - ", GetLastError()); } } // Verificar rompimento de alta if (range >= 30 * _Point && priceClose > bollingerUpper[0] && !orderAlreadyOpen) { lastBreakoutAlta = true; breakoutHigh = priceHigh; Print("Possível rompimento de alta detectado."); } // Verificar rompimento de baixa if (range >= 30 * _Point && priceClose < bollingerLower[0] && !orderAlreadyOpen) { lastBreakoutBaixa = true; breakoutLow = priceLow; Print("Possível rompimento de baixa detectado."); } // Gerenciamento de trailing stop TrailStop(); - Assistant
The error messages and warnings you are encountering indicate a few issues in your code. Let's address each of them:
-
Possible loss of data due to type conversion:
This warning is about converting anintto another type (probablydouble) or vice versa without explicitly casting, which might lead to losing information. The warning occurs at line 51, which is related to settingslippage:
-
- User
- Assistant
- User
- Assistant