#include <Trade\Trade.mqh> input double lotSize = 0.01; input int fastSmaPeriod = 20;…
- User
#include <Trade\Trade.mqh>
input double lotSize = 0.01; input int fastSmaPeriod = 20; input int slowSmaPeriod = 200; input double stopLossPoints = 65000.0; input double dailyProfitTarget = 100.0; input double targetPartialProfit = 2.00; // Alvo de lucro para realizar parcial
bool orderOpened = false; ulong orderTicket = 0; CTrade trade;
//+------------------------------------------------------------------+ //| Função para obter o lucro diário atual | //+------------------------------------------------------------------+ double GetDailyProfit() { double profit = 0.0; MqlDateTime today; TimeToStruct(TimeCurrent(), today);
for (int i = 0; i < HistoryDealsTotal(); i++) { ulong dealTicket = HistoryDealGetTicket(i); datetime closeTime = (datetime)HistoryDealGetInteger(dealTicket, DEAL_TIME); MqlDateTime closeTimeStruct; TimeToStruct(closeTime, closeTimeStruct); if (closeTimeStruct.day == today.day && closeTimeStruct.mon == today.mon && closeTimeStruct.year == today.year) { profit += HistoryDealGetDouble(dealTicket, DEAL_PROFIT); } } return profit;}
//+------------------------------------------------------------------+ //| Função para determinar a tendência com base no gráfico H1 | //+------------------------------------------------------------------+ int DetermineTrendOnH1() { double fastSmaH1[2], slowSmaH1[2]; int fastSmaHandleH1 = iMA(_Symbol, PERIOD_H1, fastSmaPeriod, 0, MODE_SMA, PRICE_CLOSE); int slowSmaHandleH1 = iMA(_Symbol, PERIOD_H1, slowSmaPeriod, 0, MODE_SMA, PRICE_CLOSE);
if (CopyBuffer(fastSmaHandleH1, 0, 0, 2, fastSmaH1) <= 0 || CopyBuffer(slowSmaHandleH1, 0, 0, 2, slowSmaH1) <= 0) { Print("Erro ao copiar buffers de SMA para H1: ", GetLastError()); return 0; } if (fastSmaH1[1] > slowSmaH1[1]) { return 1; // Tendência de alta } else if (fastSmaH1[1] < slowSmaH1[1]) { return -1; // Tendência de baixa } return 0; // Sem tendência clara}
//+------------------------------------------------------------------+ //| Função para verificar se é necessário fazer parcial | //+------------------------------------------------------------------+ void CheckPartialProfit() { if (orderOpened && PositionSelect(_Symbol)) { double currentProfit = PositionGetDouble(POSITION_PROFIT); double lotSizeCurrent = PositionGetDouble(POSITION_VOLUME);
if (currentProfit >= targetPartialProfit) { double partialLotSize = lotSizeCurrent * 0.7; // Realiza 70% da posição if (trade.PositionClosePartial(_Symbol, partialLotSize)) { Print("Parcial realizada. Lote: ", partialLotSize); // A ordem permanece aberta com o restante do lote } else { Print("Erro ao realizar parcial: ", trade.ResultRetcode(), " - ", GetLastError()); } } }}
//+------------------------------------------------------------------+ //| Execução de operações no gráfico M5 com base na tendência H1 | //+------------------------------------------------------------------+ void ExecuteTradingOnM5(int trend) { double ask, bid; if (!SymbolInfoDouble(_Symbol, SYMBOL_ASK, ask) || !SymbolInfoDouble(_Symbol, SYMBOL_BID, bid)) { Print("Erro ao obter preços Ask ou Bid: ", GetLastError()); return; }
- Assistant
Para corrigir o problema de não encerrar as operações quando a meta de lucro diário é alcançada, você pode verificar se a operação atual precisa ser fechada no momento certo. A função deve ser adaptada para checar se o lucro diário já atingiu ou excedeu a meta e, caso positivo, fechar todas as posições abertas.
- User
- Assistant