Gervis DCA

This commit is contained in:
Nkondog Anselme
2022-01-01 15:32:05 +01:00
parent 0717013a21
commit 2bb0816712
27 changed files with 1777 additions and 2 deletions
@@ -0,0 +1,16 @@
/*
Trade.mqh
Copyright 2013-2020, Orchard Forex
https://www.orchardforex.com
*/
#ifdef __MQL4__
#include "Trade_mql4.mqh"
#endif
#ifdef __MQL5__
#include "Trade_mql5.mqh"
#endif
@@ -0,0 +1,123 @@
/*
Trade.mqh
(For MQL4)
Copyright 2013-2020, Orchard Forex
https://www.orchardforex.com
*/
#include "..\CommonBase.mqh"
struct MqlTradeRequest {
int action; // Trade operation type (as int here)
ulong magic; // Expert Advisor ID (magic number)
ulong order; // Order ticket
string symbol; // Trade symbol
double volume; // Requested volume for a deal in lots
double price; // Price
double stoplimit; // StopLimit level of the order
double sl; // Stop Loss level of the order
double tp; // Take Profit level of the order
ulong deviation; // Maximal possible deviation from the requested price
ENUM_ORDER_TYPE type; // Order type
int type_filling; // Order execution type (int here)
int type_time; // Order expiration type (int here)
datetime expiration; // Order expiration time (for the orders of ORDER_TIME_SPECIFIED type)
string comment; // Order comment
ulong position; // Position ticket
ulong position_by; // The ticket of an opposite position
};
enum ENUM_POSITION_TYPE {
POSITION_TYPE_BUY = ORDER_TYPE_BUY,
POSITION_TYPE_SELL = ORDER_TYPE_SELL
};
class CTradeCustom : public CCommonBase {
private:
protected: // member variables
int mMagic; // expert magic number
public: // constructors
CTradeCustom();
~CTradeCustom();
public:
ulong RequestMagic() { return(mMagic); }
void SetExpertMagicNumber(const int magic) { mMagic=magic; }
double BuyPrice(string symbol) { return(SymbolInfoDouble(symbol, SYMBOL_ASK)); }
double SellPrice(string symbol) { return(SymbolInfoDouble(symbol, SYMBOL_BID)); }
bool Buy(const double volume,const string symbol=NULL,double price=0.0,const double sl=0.0,const double tp=0.0,const string comment="");
bool Sell(const double volume,const string symbol=NULL,double price=0.0,const double sl=0.0,const double tp=0.0,const string comment="");
bool PositionCloseByType(const string symbol, ENUM_POSITION_TYPE positionType,const int deviation=ULONG_MAX);
////New
void PositionCountByType(const string symbol, int &count[]);
};
CTradeCustom::CTradeCustom() {
mMagic = 0;
}
CTradeCustom::~CTradeCustom() {
}
bool CTradeCustom::Buy(const double volume,const string symbol=NULL,double price=0.0,const double sl=0.0,const double tp=0.0,const string comment="") {
if (price==0.0) price = BuyPrice(symbol);
int ticket = OrderSend(symbol, ORDER_TYPE_BUY, volume, price, 0, sl, tp, comment, mMagic);
return(ticket>0);
}
bool CTradeCustom::Sell(const double volume,const string symbol=NULL,double price=0.0,const double sl=0.0,const double tp=0.0,const string comment="") {
if (price==0.0) price = SellPrice(symbol);
int ticket = OrderSend(symbol, ORDER_TYPE_SELL, volume, price, 0, sl, tp, comment, mMagic);
return(ticket>0);
}
bool CTradeCustom::PositionCloseByType(const string symbol, ENUM_POSITION_TYPE positionType, const int deviation=ULONG_MAX) {
int slippage = (deviation==ULONG_MAX) ? 0 : deviation;
bool result = true;
int cnt = OrdersTotal();
for (int i = cnt-1; i>=0; i--) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==symbol && OrderMagicNumber()==mMagic && OrderType()==positionType) {
result &= OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), slippage);
}
}
}
return(result);
}
////New
void CTradeCustom::PositionCountByType(const string symbol, int &count[]) {
ArrayResize(count, 6);
ArrayInitialize(count, 0);
int cnt = OrdersTotal();
for (int i = cnt-1; i>=0; i--) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol()==symbol && OrderMagicNumber()==mMagic) {
count[(int)OrderType()]++;
}
}
}
return;
}
@@ -0,0 +1,66 @@
/*
Trade.mqh
(For MQL5)
Copyright 2013-2020, Orchard Forex
https://www.orchardforex.com
*/
#include <Trade/Trade.mqh>
class CTradeCustom : public CTrade {
private:
protected: // member variables
public: // constructors
public:
bool PositionCloseByType(const string symbol, ENUM_POSITION_TYPE positionType,const ulong deviation=ULONG_MAX);
////New
void PositionCountByType(const string symbol, int &count[]);
};
bool CTradeCustom::PositionCloseByType(const string symbol, ENUM_POSITION_TYPE positionType, const ulong deviation=ULONG_MAX) {
bool result = true;
int cnt = PositionsTotal();
for (int i = cnt-1; i>=0; i--) {
ulong ticket = PositionGetTicket(i);
if (PositionSelectByTicket(ticket)) {
if (PositionGetString(POSITION_SYMBOL)==symbol && PositionGetInteger(POSITION_TYPE)==positionType && PositionGetInteger(POSITION_MAGIC)==m_magic) {
result &= PositionClose(ticket, deviation);
}
} else {
m_result.retcode=TRADE_RETCODE_REJECT;
result = false;
}
}
return(result);
}
////New
void CTradeCustom::PositionCountByType(const string symbol, int &count[]) {
ArrayResize(count, 6);
ArrayInitialize(count, 0);
int cnt = PositionsTotal();
for (int i = cnt-1; i>=0; i--) {
ulong ticket = PositionGetTicket(i);
if (PositionSelectByTicket(ticket)) {
if (PositionGetString(POSITION_SYMBOL)==symbol && PositionGetInteger(POSITION_MAGIC)==m_magic) {
count[(int)PositionGetInteger(POSITION_TYPE)]++;
}
}
}
return;
}