Rotator/Rotator.mq5

151 lines
5.2 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 16:21:45 +02:00
//+------------------------------------------------------------------+
//| Rotator.mq5 |
//| Copyright 2022, Allan Serotini |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Estruturas |
//+------------------------------------------------------------------+
struct Deal
{
ENUM_DEAL_TYPE enumTipo;
string strSymbol;
double dbPreco;
};
//+------------------------------------------------------------------+
//| Global Variables |
//+------------------------------------------------------------------+
int iFrequency=1;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create timer
EventSetTimer(iFrequency);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
EA();
}
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void EA()
{
string strComment="";
Deal bestDeal;
ZeroMemory(bestDeal);
string strSymbol="";
double dbYield=0.0;
double dbPreviousYield=-9999.0;
for(int i=0;i<SymbolsTotal(true);i++)
{
strSymbol=SymbolName(i,true);
if(
(SymbolInfoDouble(strSymbol,SYMBOL_ASK)>0.0)
&&(SymbolInfoDouble(strSymbol,SYMBOL_BID)>0.0)
//&&(StringSubstr(SymbolInfoString(strSymbol,SYMBOL_ISIN),6,3)=="CTF")
//&&(StringLen(strSymbol)==6)
//&&(StringSubstr(strSymbol,4,2)=="11")
//&&(StringSubstr(SymbolInfoString(strSymbol,SYMBOL_DESCRIPTION),0,3)=="FII")
)
{
Deal lastDeal = getLastDeal(strSymbol);
if(lastDeal.dbPreco>0.0)
{
if(lastDeal.enumTipo==1)
{
dbYield=(lastDeal.dbPreco-SymbolInfoDouble(strSymbol,SYMBOL_BID))/(2/(1/lastDeal.dbPreco)+1/SymbolInfoDouble(strSymbol,SYMBOL_BID));
}
else
{
dbYield=(SymbolInfoDouble(strSymbol,SYMBOL_BID)-lastDeal.dbPreco)/(2/(1/lastDeal.dbPreco)+1/SymbolInfoDouble(strSymbol,SYMBOL_BID));
}
//dbPreviousYield=(dbPreviousYield<=dbYield?dbYield:dbPreviousYield);
if(dbPreviousYield<=dbYield)
{
dbPreviousYield=dbYield;
bestDeal.strSymbol=strSymbol;
if(lastDeal.enumTipo==1)
{
bestDeal.dbPreco=SymbolInfoDouble(bestDeal.strSymbol,SYMBOL_BID);
}
else
{
bestDeal.dbPreco=SymbolInfoDouble(bestDeal.strSymbol,SYMBOL_ASK);
}
}
else
{
}
// strComment+=StringFormat("%s: %s %.2f%%\n",
// strSymbol,
// StringSubstr(EnumToString((ENUM_DEAL_TYPE)lastDeal.enumTipo),10,1),
// dbYield*100
// );
}
else
{
// strComment+=StringFormat("%s\n",
// strSymbol
// );
}
}
}
// strComment+="\n";
strComment+=StringFormat("%s %s %.2f (%.2f%%)",
StringSubstr(EnumToString((ENUM_DEAL_TYPE)bestDeal.enumTipo),10,4),
bestDeal.strSymbol,
bestDeal.dbPreco,
100*dbPreviousYield
);
Comment(strComment);
}
//+------------------------------------------------------------------+
//| Last Deal |
//+------------------------------------------------------------------+
Deal getLastDeal(const string strSymbol)
{
Deal dealLast;
ZeroMemory( dealLast );
ulong uTicket;
HistorySelect(0,TimeCurrent());
for(int i=0;i<(HistoryDealsTotal());i++)
{
uTicket=HistoryDealGetTicket(i);
if(HistoryDealGetString(uTicket,DEAL_SYMBOL)==strSymbol)
{
dealLast.strSymbol=HistoryDealGetString(uTicket,DEAL_SYMBOL);
dealLast.dbPreco=HistoryDealGetDouble(uTicket,DEAL_PRICE);
dealLast.enumTipo=(ENUM_DEAL_TYPE)HistoryDealGetInteger(uTicket,DEAL_TYPE);
}
}
return( dealLast );
}
//+------------------------------------------------------------------+