robo-InsideOutsideBarFibo/robo-parcialInvertida/robo-parcialInvertida.mq5
2026-01-19 20:09:07 -03:00

126 lines
5 KiB
MQL5

//+------------------------------------------------------------------+
//| robo-parcialInvertida.mq5 |
//| Copyright 2024, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#include <Trade\Trade.mqh>
CTrade trade;
MqlRates rates[];
bool fezParcialInvertida = false;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit() {
//--- create timer
EventSetTimer(60);
ArraySetAsSeries(rates, true);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
//--- destroy timer
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick() {
CopyRates(Symbol(), Period(), 0, 3, rates);
if(PositionsTotal()==0 && OrdersTotal()==0) {
trade.Sell(1000.00, Symbol(), 0, 50, 0, "parcial invertida");
}
if(PositionsTotal()==0 && !fezParcialInvertida) {
fezParcialInvertida = realizaParcialInvertida(rates[0].close);
}
}
bool realizaParcialInvertida(double preco) {
for(int i=(PositionsTotal()-1);i>=0;i--) {
string symbol = PositionGetSymbol(i);
if(Symbol() == symbol) {
ulong ticket = PositionGetTicket(i);
double precoEntrada = PositionGetDouble(POSITION_PRICE_OPEN),
p = MathAbs(precoEntrada - preco),
p2= NormalizeDouble(((precoEntrada - preco) / _Point), Digits),
pontos = NormalizeDouble((MathAbs(precoEntrada - preco) / _Point), Digits);
Comment(p + "\n" + pontos + "\n" + p2);
if(pontos >=40) {
return trade.PositionClosePartial(ticket, 500, 0);
}
}
}
return false;
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer() {}
//+------------------------------------------------------------------+
//| Trade function |
//+------------------------------------------------------------------+
void OnTrade() {}
//+------------------------------------------------------------------+
//| TradeTransaction function |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction& trans, const MqlTradeRequest& request, const MqlTradeResult& result) {}
//+------------------------------------------------------------------+
//| Tester function |
//+------------------------------------------------------------------+
double OnTester() {double ret=0.0; return(ret); }
//+------------------------------------------------------------------+
//| TesterInit function |
//+------------------------------------------------------------------+
void OnTesterInit() {}
//+------------------------------------------------------------------+
//| TesterPass function |
//+------------------------------------------------------------------+
void OnTesterPass() {}
//+------------------------------------------------------------------+
//| TesterDeinit function |
//+------------------------------------------------------------------+
void OnTesterDeinit() {}
//+------------------------------------------------------------------+
//| ChartEvent function |
//+------------------------------------------------------------------+
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) {}
//+------------------------------------------------------------------+
//| BookEvent function |
//+------------------------------------------------------------------+
void OnBookEvent(const string &symbol) {}
//+------------------------------------------------------------------+