Project_N_1/Include/General_class_.mqh

444 lines
36 KiB
MQL5
Raw Permalink Normal View History

2025-05-30 16:18:18 +02:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| ProjectName |
//| Copyright 2020, CompanyName |
//| http://www.companyname.net |
//+------------------------------------------------------------------+
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\AccountInfo.mqh>
#include <Trade\DealInfo.mqh>
#include <Trade\OrderInfo.mqh>
#include <Expert\Money\MoneyFixedMargin.mqh>
CTrade a_trade;
CPositionInfo a_position;
CSymbolInfo a_symbol;
CAccountInfo a_accauntInfo;
CDealInfo a_deal;
COrderInfo a_order;
//--- 1JO2;O5< AB@C:BC@K, :>B>@K5 1C4CB 8A?>;L7>20BLAO 4;O B>@3>2;8
MqlTick latest_price; // C45B 8A?>;L7>20BLAO 4;O B5:CI8E :>B8@>2>:
MqlTradeRequest mrequest; // C45B 8A?>;L7>20BLAO 4;O >BAK;:8 B>@3>2KE 70?@>A>2
MqlTradeResult mresult; // C45B 8A?>;L7>20BLAO 4;O ?>;CG5=8O @57C;LB0B>2 2K?>;=5=8O B>@3>2KE 70?@>A>2
MqlRates mrate[]; // C45B A>45@60BL F5=K, >1J5<K 8 A?@54 4;O :064>3> 10@0
sinput int Magic = 0;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CGeneral_class
{
private:
public:
CGeneral_class();
~CGeneral_class();
double LotSize(int _SL, double MR);
bool OpenBuyPosition(double volume,double price, double StopLoss_, double TP_);
bool OpenSellPosition(double volume,double price, double StopLoss_, double TP_);
int CheckOpen(int &ob, int &os, datetime & t_ob, datetime & t_os);
void CheckVolume(double &Volume_current_sell,double &Volume_current_buy);
void CloseBuy();
void CloseSell();
void CloseBuyPartial(string Symbol_, int x);
void CloseSellPartial(string Symbol_, int x);
bool isNewBar(ENUM_TIMEFRAMES per);
string st5(double x);
bool CheckTimePeriod(string Start_Time, string End_Time);
bool SpreadFilter(int bars, int pauseTime, int x);
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CGeneral_class::CGeneral_class()
{
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CGeneral_class::~CGeneral_class()
{
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------------+
//+- 0AG5B ;>B0 2 7028A8<>AB8 >B @0AAB>O=8O 4> StopLoss +//
//+------------------------------------------------------------------------+
double CGeneral_class::LotSize(int _SL, double MR)// SL-StopLoss MR-MaxRisk 2 % >B 10;0=A0
{
if(_SL == 0)
return(0); // 8A:;NG5=85 45;5=8O =0 =>;L
double Free = AccountInfoDouble(ACCOUNT_BALANCE);
double LotVal = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE); //AB>8<>ABL 1 ?C=:B0 1 ;>B0
double Min_Lot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);
double Max_Lot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);
double Step = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);
double Lot = MathFloor((Free * MR / 100) / (_SL * LotVal) / Step) * Step;
if(Lot < Min_Lot)
Lot = Min_Lot;
if(Lot > Max_Lot)
Lot = Max_Lot;
return(Lot);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CGeneral_class :: OpenSellPosition(double volume,double price, double StopLoss_, double TP_)
{
ZeroMemory(mrequest);
ZeroMemory(mresult);
mrequest.action = TRADE_ACTION_DEAL; // =5<54;5==>5 8A?>;=5=85
mrequest.price = NormalizeDouble(price, _Digits); // ?>A;54=OO F5=0 Bid
mrequest.sl = StopLoss_; // Stop Loss
mrequest.tp = TP_ ;// NormalizeDouble(latest_price.bid - TP * _Point, _Digits); // Take Profit
mrequest.symbol = _Symbol; // A8<2>;
mrequest.volume = volume; // :>;8G5AB2> ;>B>2 4;O B>@3>2;8
mrequest.magic = Magic; // Magic Number
mrequest.type = ORDER_TYPE_SELL; // >@45@ =0 ?@>406C
mrequest.type_filling = ORDER_FILLING_FOK; // B8? 8A?>;=5=8O >@45@0 - 2A5 8;8 =8G53>
mrequest.deviation = 100; // ?@>A:0;L7K20=85 >B B5:CI59 F5=K
//--- >BAK;05< >@45@
OrderSend(mrequest, mresult);
// 0=0;878@C5< :>4 2>72@0B0 B>@3>2>3> A5@25@0
if(mresult.retcode == 10009 || mresult.retcode == 10008) //Request is completed or order placed
{
Print("@45@ Sell CA?5H=> ?><5I5=, B8:5B >@45@0 #:", mresult.order, "!!");
return true;
}
else
{
SendMail(_Symbol + " 0?@>A =0 CAB0=>2:C >@45@0 Buy =5 2K?>;=5=. :>4 >H81:8: " + GetLastError(),
_Symbol + " result retcode: " + mresult.retcode + "result.deal: " + mresult.deal + "result.order " + mresult.order + "5@8>4: " + GetNameTF());
Print("Sell -> false. Result Retcode: ",+ mresult.retcode + " result.deal: " + mresult.deal + "result.order " + mresult.order + "5@8>4: " + GetNameTF());
return false;
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CGeneral_class :: OpenBuyPosition(double volume, double price, double StopLoss_, double TP_)
{
ZeroMemory(mrequest);
ZeroMemory(mresult);
mrequest.action = TRADE_ACTION_DEAL; // =5<54;5==>5 8A?>;=5=85
mrequest.price = NormalizeDouble(price, _Digits); // ?>A;54=OO F5=0 Bid
mrequest.sl = StopLoss_; // Stop Loss
mrequest.tp = TP_;//NormalizeDouble(latest_price.ask + TP * _Point, _Digits); // Take Profit
mrequest.symbol = _Symbol; // A8<2>;
mrequest.volume = volume; // :>;8G5AB2> ;>B>2 4;O B>@3>2;8
mrequest.magic = Magic; // Magic Number
mrequest.type = ORDER_TYPE_BUY; // >@45@ =0 ?@>406C
mrequest.type_filling = ORDER_FILLING_FOK; // B8? 8A?>;=5=8O >@45@0 - 2A5 8;8 =8G53>
mrequest.deviation = 100; // ?@>A:0;L7K20=85 >B B5:CI59 F5=K
//--- >BAK;05< >@45@
OrderSend(mrequest, mresult);
// 0=0;878@C5< :>4 2>72@0B0 B>@3>2>3> A5@25@0
if(mresult.retcode == 10009 || mresult.retcode == 10008) //Request is completed or order placed
{
return true;
Print("@45@ Sell CA?5H=> ?><5I5=, B8:5B >@45@0 #:", mresult.order, "!!");
}
else
{
SendMail(_Symbol + " 0?@>A =0 CAB0=>2:C >@45@0 Buy =5 2K?>;=5=. :>4 >H81:8: " + GetLastError(),
_Symbol + " result retcode: " + mresult.retcode + "result.deal: " + mresult.deal + "result.order " + mresult.order + "5@8>4: " + GetNameTF());
Print("Buy -> false. Result Retcode: ",+ mresult.retcode + " result.deal: " + mresult.deal + "result.order " + mresult.order + "5@8>4: " + GetNameTF());
return false;
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CGeneral_class :: CheckOpen(int &ob, int &os, datetime & t_ob, datetime & t_os)
{
ob = 0;
os = 0;
int res = 0;
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
a_position.SelectByIndex(i);
if(a_position.Magic() == Magic && a_position.Symbol() == _Symbol)
{
if(a_position.PositionType() == POSITION_TYPE_BUY)
{
t_ob = a_position.Time();
ob++;
}
if(a_position.PositionType() == POSITION_TYPE_SELL)
{
t_os = a_position.Time();
os++;
}
}
}
res = ob + os;
return res;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//double Volume_current_sell;
//double Volume_current_buy;
void CGeneral_class :: CheckVolume(double &Volume_current_sell,double &Volume_current_buy)
{
Volume_current_sell = 0;
Volume_current_buy = 0;
for(int i = PositionsTotal()-1; i >= 0; i--)
{
a_position.SelectByIndex(i);
if(a_position.Magic() == Magic && a_position.Symbol() == _Symbol)
{
if(a_position.PositionType() == POSITION_TYPE_BUY)
{
Volume_current_buy += PositionGetDouble(POSITION_VOLUME);
}
if(a_position.PositionType() == POSITION_TYPE_SELL)
{
Volume_current_sell += PositionGetDouble(POSITION_VOLUME);
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CGeneral_class :: CloseSell()
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
a_position.SelectByIndex(i);
if(a_position.Magic() == Magic && a_position.Symbol() == _Symbol)
{
if(a_position.PositionType() == POSITION_TYPE_SELL)
a_trade.PositionClose(a_position.Ticket());
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CGeneral_class :: CloseBuy()
{
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
a_position.SelectByIndex(i);
if(a_position.Magic() == Magic && a_position.Symbol() == _Symbol)
{
if(a_position.PositionType() == POSITION_TYPE_BUY)
{
int o = a_trade.PositionClose(a_position.Ticket());
if(o <= 0)
Print("<--- CloseBuy ", _LastError, " ", __LINE__);
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CGeneral_class :: CloseBuyPartial(string Symbol_, int x)
{
double Volume_current_buy = 0;
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
a_position.SelectByIndex(i);
if(a_position.Magic() == Magic && a_position.Symbol() == Symbol_)
{
if(a_position.PositionType() == POSITION_TYPE_BUY)
{
Volume_current_buy = PositionGetDouble(POSITION_VOLUME);
double Lot = NormalizeDouble(Volume_current_buy/x, 2);
bool o = a_trade.PositionClosePartial(a_position.Ticket(),Lot,-1);
if(o == false)
Print("<--- CloseBuy ", _LastError, " ", __LINE__);
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CGeneral_class ::CloseSellPartial(string Symbol_, int x)
{
double Volume_current_sell = 0;
for(int i = PositionsTotal() - 1; i >= 0; i--)
{
a_position.SelectByIndex(i);
if(a_position.Magic() == Magic && a_position.Symbol() == Symbol_)
{
if(a_position.PositionType() == POSITION_TYPE_SELL)
{
Volume_current_sell = PositionGetDouble(POSITION_VOLUME);
double Lot = NormalizeDouble(Volume_current_sell/x, 2);
bool o = a_trade.PositionClosePartial(a_position.Ticket(),Lot,-1);
if(o == false)
Print("<--- CloseBuy ", _LastError, " ", __LINE__);
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CGeneral_class :: isNewBar(ENUM_TIMEFRAMES per)
{
//--- 2 AB0B8G5A:>9 ?5@5<5==>9 1C45< ?><=8BL 2@5<O >B:@KB8O ?>A;54=53> 10@0
static datetime last_time = 0;
//--- B5:CI55 2@5<O
datetime lastbar_time = SeriesInfoInteger(Symbol(), per, SERIES_LASTBAR_DATE);
//--- 5A;8 MB> ?5@2K9 2K7>2 DC=:F88
if(last_time == 0)
{
//--- CAB0=>28< 2@5<O 8 2K945<
last_time = lastbar_time;
return(false);
}
//--- 5A;8 2@5<O >B;8G05BAO
if(last_time != lastbar_time)
{
//--- 70?><=8< 2@5<O 8 25@=5< true
last_time = lastbar_time;
return(true);
}
//--- 4>H;8 4> MB>3> <5AB0 - 7=0G8B 10@ =5 =>2K9, 25@=5< false
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string CGeneral_class :: st5(double x)
{
return DoubleToString(x, _Digits);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CGeneral_class :: CheckTimePeriod(string Start_Time, string End_Time)
{
datetime currentTime = TimeCurrent();
datetime start_time = StringToTime(Start_Time);
datetime end_time = End_Time > Start_Time ? StringToTime(End_Time) : StringToTime(StringToTime(End_Time) + 3600 * 24);
//+------------------------------------------------------------------+
// "0:>9 284 :>=AB@C:F88 =07K205BAO B5@=0@=>9 >?5@0F859. -B> 1>;55 :@0A820O D>@<0 70?8A8 :>40 =865:
//
//if (End_Time > Start_Time)
//{
// end_time = StringToTime(End_Time)
//}
//else
//{
// end_time = StringToTime(StringToTime(End_Time) + 3600 * 24)
//}
// !B@C:BC@0 B5@=0@=>9 >?5@0F88 B0:0O: if() ? valueIfTrue : valueIfFalse;
//
// A;8 B@51C5BAO 2K?>;=8BL =5A:>;L:> >?5@0F89 2 @57C;LB0B5 8AE>40, B> 8A?>;L7C5BAO if.
// ".5 B0: =5 ?>;CG8BAO
// end_time = End_Time > Start_Time ? StringToTime(End_Time) *BCB 5I5 :0:0O B> >?5@0F8O* : StringToTime(StringToTime(End_Time) + 3600 * 24) *8;8 BCB*;
//+------------------------------------------------------------------+
//Print("Current time: ", currentTime);
//Print("Start time: ", start_time);
//Print("End time: ", end_time);
return currentTime > start_time && currentTime < end_time;
// =0;>38G=> MB><C:
// if(currentTime > start_time && currentTime < end_time)
// {
// return true;
// }
// return false;
}
//+------------------------------------------------------------------+
//|@>25@O5< @07<0E A?@540 |
//+------------------------------------------------------------------+
bool CGeneral_class :: SpreadFilter(int bars, int pauseTime, int x)
{
double spread = 0;
int values[];
int count = CopySpread(_Symbol, PERIOD_H1, 0, bars, values);
for(int i = 0; i < count; i++)
{
spread += values[i];
}
double middleSpread = spread / bars * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
double currentSpread = SymbolInfoInteger(_Symbol, SYMBOL_SPREAD);
datetime lastAllowedSpreadTime = 0;
// A;8 A?@54 1>;LH>9, B> 70?><8=05< B5:CI55 2@5<O 8 2K68405< =5:>B>@CN ?0C7C, ?>A;5 :>B>@>9 >= <>65B C<5=LH8BAO.
if(currentSpread > (middleSpread+x))// x - MB> ?>3@5H=>ABL, :>B>@CN <K CG8BK205<
{
lastAllowedSpreadTime = TimeCurrent();
return false;
}
else
{
if(TimeCurrent() < lastAllowedSpreadTime + pauseTime)
return false;
lastAllowedSpreadTime = 0;
return true;
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
string GetNameTF(int TimeFrame = 0)
{
if(TimeFrame == 0)
TimeFrame = Period();
switch(TimeFrame)
{
case PERIOD_M1:
return("M1");
case PERIOD_M5:
return("M5");
case PERIOD_M10:
return("M10");
case PERIOD_M15:
return("M15");
case PERIOD_M20:
return("M20");
case PERIOD_M30:
return("M30");
case PERIOD_H1:
return("H1");
case PERIOD_H2:
return("H2");
case PERIOD_H4:
return("H4");
case PERIOD_D1:
return("Daily");
case PERIOD_W1:
return("Weekly");
case PERIOD_MN1:
return("Monthly");
default:
return("Unknown Period");
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+