Skip to content

Commit

Permalink
Fixed bug in Serializer class (fields by default should not have DEFA…
Browse files Browse the repository at this point in the history
…ULT flags, but UNSPECIFIED). Also WIP on CSV -> Database(SQL) converted. Should be renamed to SerializerDatabase
  • Loading branch information
nseam committed Jul 9, 2021
1 parent 3b6cbfb commit 8ecef43
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 186 deletions.
163 changes: 66 additions & 97 deletions Convert.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
* Class to provide conversion methods.
*/
class Convert {

public:

public:
/*
* Returns order type as buy or sell.
*
Expand Down Expand Up @@ -72,7 +70,7 @@ public:
* Returns OP_BUY when value is positive, OP_SELL when negative, otherwise -1.
*/
static ENUM_ORDER_TYPE ValueToOp(int value) {
return value == 0 ? (ENUM_ORDER_TYPE) -1 : (value > 0 ? ORDER_TYPE_BUY : ORDER_TYPE_SELL);
return value == 0 ? (ENUM_ORDER_TYPE)-1 : (value > 0 ? ORDER_TYPE_BUY : ORDER_TYPE_SELL);
}

/**
Expand All @@ -86,21 +84,19 @@ public:
* Returns OP_BUY when value is positive, OP_SELL when negative, otherwise -1.
*/
static ENUM_ORDER_TYPE ValueToOp(double value) {
return value == 0 ? (ENUM_ORDER_TYPE) -1 : (value > 0 ? ORDER_TYPE_BUY : ORDER_TYPE_SELL);
return value == 0 ? (ENUM_ORDER_TYPE)-1 : (value > 0 ? ORDER_TYPE_BUY : ORDER_TYPE_SELL);
}

/**
* Points per pip given digits after decimal point of a symbol price.
*/
static uint PointsPerPip(uint digits) {
return (uint) pow(10, digits - (digits < 4 ? 2 : 4));
}
static uint PointsPerPip(uint digits) { return (uint)pow(10, digits - (digits < 4 ? 2 : 4)); }

/**
* Returns number of points per pip.
*/
static uint PointsPerPip(string _symbol = NULL) {
return PointsPerPip((uint) SymbolInfo::SymbolInfoInteger(_symbol, SYMBOL_DIGITS));
return PointsPerPip((uint)SymbolInfo::SymbolInfoInteger(_symbol, SYMBOL_DIGITS));
}

/**
Expand All @@ -126,49 +122,43 @@ public:
* Convert pips into price value.
*/
static double PipsToValue(double pips, string _symbol = NULL) {
return PipsToValue(pips, (uint) SymbolInfo::SymbolInfoInteger(_symbol, SYMBOL_DIGITS));
return PipsToValue(pips, (uint)SymbolInfo::SymbolInfoInteger(_symbol, SYMBOL_DIGITS));
}

/**
* Convert value into pips given price value and digits.
*/
static double ValueToPips(double value, uint digits) {
return value * pow(10, digits < 4 ? 2 : 4);
}
static double ValueToPips(double value, uint digits) { return value * pow(10, digits < 4 ? 2 : 4); }

/**
* Convert value into pips.
*/
static double ValueToPips(double value, string _symbol = NULL) {
return ValueToPips(value, (uint) SymbolInfo::SymbolInfoInteger(_symbol, SYMBOL_DIGITS));
return ValueToPips(value, (uint)SymbolInfo::SymbolInfoInteger(_symbol, SYMBOL_DIGITS));
}

/**
* Convert pips into points.
*/
static uint PipsToPoints(double pips, int digits) {
return (uint) pips * PointsPerPip(digits);
}
static uint PipsToPoints(double pips, int digits) { return (uint)pips * PointsPerPip(digits); }

/**
* Convert pips into points.
*/
static uint PipsToPoints(double pips, string _symbol = NULL) {
return PipsToPoints(pips, (uint) SymbolInfo::SymbolInfoInteger(_symbol, SYMBOL_DIGITS));
return PipsToPoints(pips, (uint)SymbolInfo::SymbolInfoInteger(_symbol, SYMBOL_DIGITS));
}

/**
* Convert points into pips.
*/
static double PointsToPips(long pts, int digits) {
return (double) (pts / PointsPerPip(digits));
}
static double PointsToPips(long pts, int digits) { return (double)(pts / PointsPerPip(digits)); }

/**
* Convert points into pips.
*/
static double PointsToPips(long pts, string _symbol = NULL) {
return PointsToPips(pts, (uint) SymbolInfo::SymbolInfoInteger(_symbol, SYMBOL_DIGITS));
return PointsToPips(pts, (uint)SymbolInfo::SymbolInfoInteger(_symbol, SYMBOL_DIGITS));
}

/**
Expand All @@ -177,19 +167,19 @@ public:
*/
static double PointsToValue(long pts, int mode, string _symbol = NULL) {
switch (mode) {
case 0: // Forex.
case 0: // Forex.
// In currencies a tick is a point.
return pts * SymbolInfo::SymbolInfoDouble(_symbol, SYMBOL_TRADE_TICK_SIZE);
case 1: // CFD.
case 1: // CFD.
// In metals a Tick is still the smallest change, but is larger than a point.
// If price can change from 123.25 to 123.50,
// you have a TickSize of 0.25 and a point of 0.01. Pip has no meaning.
// @todo
return pts * SymbolInfo::SymbolInfoDouble(_symbol, SYMBOL_TRADE_TICK_SIZE);
case 2: // Futures.
case 2: // Futures.
// @todo
return pts * SymbolInfo::SymbolInfoDouble(_symbol, SYMBOL_TRADE_TICK_SIZE);
case 3: // CFD for indices.
case 3: // CFD for indices.
// @todo
return pts * SymbolInfo::SymbolInfoDouble(_symbol, SYMBOL_TRADE_TICK_SIZE);
}
Expand All @@ -201,18 +191,18 @@ public:
*/
static double PointsToValue(long pts, int mode, int digits) {
switch (mode) {
case 0: // Forex.
return PipsToValue((double) pts / PointsPerPip(digits), digits);
case 1: // CFD.
case 0: // Forex.
return PipsToValue((double)pts / PointsPerPip(digits), digits);
case 1: // CFD.
// In metals a Tick is still the smallest change, but is larger than a point.
// @todo
return PipsToValue((double) pts / PointsPerPip(digits), digits);
case 2: // Futures.
return PipsToValue((double)pts / PointsPerPip(digits), digits);
case 2: // Futures.
// @todo
return PipsToValue((double) pts / PointsPerPip(digits), digits);
case 3: // CFD for indices.
return PipsToValue((double)pts / PointsPerPip(digits), digits);
case 3: // CFD for indices.
// @todo
return PipsToValue((double) pts / PointsPerPip(digits), digits);
return PipsToValue((double)pts / PointsPerPip(digits), digits);
}
return false;
}
Expand All @@ -221,7 +211,7 @@ public:
* Convert points into price value.
*/
static double PointsToValue(long pts, string _symbol = NULL) {
return PointsToValue(pts, (int) SymbolInfo::SymbolInfoInteger(_symbol, SYMBOL_TRADE_CALC_MODE));
return PointsToValue(pts, (int)SymbolInfo::SymbolInfoInteger(_symbol, SYMBOL_TRADE_CALC_MODE));
}

/**
Expand Down Expand Up @@ -249,61 +239,68 @@ public:
/**
* Get the difference between two price values (in pips).
*/
static double GetValueDiffInPips(double price1, double price2, bool abs = false, int digits = NULL, string _symbol = NULL) {
digits = digits ? digits : (int) SymbolInfo::SymbolInfoInteger(_symbol, SYMBOL_DIGITS);
static double GetValueDiffInPips(double price1, double price2, bool abs = false, int digits = NULL,
string _symbol = NULL) {
digits = digits ? digits : (int)SymbolInfo::SymbolInfoInteger(_symbol, SYMBOL_DIGITS);
return ValueToPips(abs ? fabs(price1 - price2) : (price1 - price2), digits);
}

/**
* Add currency sign to the plain value.
*/
static string ValueWithCurrency(double value, int digits = 2, string currency = "USD") {
unsigned char sign; bool prefix = true;
unsigned char sign;
bool prefix = true;
currency = currency == "" ? AccountInfoString(ACCOUNT_CURRENCY) : currency;
if (currency == "USD") sign = (unsigned char) '$';
else if (currency == "GBP") sign = (unsigned char) 0xA3; // ANSI code.
else if (currency == "EUR") sign = (unsigned char) 0x80; // ANSI code.
else { sign = NULL; prefix = false; }
return prefix
? CharToString(sign) + DoubleToString(value, digits)
: DoubleToString(value, digits) + CharToString(sign);
if (currency == "USD")
sign = (unsigned char)'$';
else if (currency == "GBP")
sign = (unsigned char)0xA3; // ANSI code.
else if (currency == "EUR")
sign = (unsigned char)0x80; // ANSI code.
else {
sign = NULL;
prefix = false;
}
return prefix ? CharToString(sign) + DoubleToString(value, digits)
: DoubleToString(value, digits) + CharToString(sign);
}

/**
* Convert integer to hex.
*/
static string IntToHex(long long_number) {
string result;
int integer_number = (int) long_number;
for (int i = 0; i < 4; i++){
int byte = (integer_number >> (i*8)) & 0xff;
result += StringFormat("%02x", byte);
int integer_number = (int)long_number;
for (int i = 0; i < 4; i++) {
int byte = (integer_number >> (i * 8)) & 0xff;
result += StringFormat("%02x", byte);
}
return result;
}

/**
* Convert character into integer.
*/
static int CharToInt(int &_chars[]) {
static int CharToInt(int& _chars[]) {
return ((_chars[0]) | (_chars[1] << 8) | (_chars[2] << 16) | (_chars[3] << 24));
}

/**
* Assume: len % 4 == 0.
*/
static int String4ToIntArray(int &output[], string in) {
static int String4ToIntArray(int& output[], string in) {
int len;
int i, j;
len = StringLen(in);
if (len % 4 != 0) len = len - len % 4;
int size = ArraySize(output);
if (size < len / 4) {
ArrayResize(output, len/4);
ArrayResize(output, len / 4);
}
for (i = 0, j = 0; j < len; i++, j += 4) {
output[i] = (StringGetCharacter(in, j)) | ((StringGetCharacter(in, j + 1)) << 8)
| ((StringGetCharacter(in, j+2)) << 16) | ((StringGetCharacter(in, j + 3)) << 24);
output[i] = (StringGetCharacter(in, j)) | ((StringGetCharacter(in, j + 1)) << 8) |
((StringGetCharacter(in, j + 2)) << 16) | ((StringGetCharacter(in, j + 3)) << 24);
}
return (len / 4);
}
Expand All @@ -312,57 +309,31 @@ public:
_out = _value != "" && _value != NULL && _value != "0" && _value != "false";
}

static void StringToType(string _value, char& _out) {
_out = (char)StringToInteger(_value);
}
static void StringToType(string _value, char& _out) { _out = (char)StringToInteger(_value); }

static void StringToType(string _value, unsigned char& _out) {
_out = (unsigned char)StringToInteger(_value);
}
static void StringToType(string _value, unsigned char& _out) { _out = (unsigned char)StringToInteger(_value); }

static void StringToType(string _value, int& _out) {
_out = (int)StringToInteger(_value);
}
static void StringToType(string _value, int& _out) { _out = (int)StringToInteger(_value); }

static void StringToType(string _value, unsigned int& _out) {
_out = (unsigned int)StringToInteger(_value);
}
static void StringToType(string _value, unsigned int& _out) { _out = (unsigned int)StringToInteger(_value); }

static void StringToType(string _value, long& _out) {
_out = (long)StringToInteger(_value);
}
static void StringToType(string _value, long& _out) { _out = (long)StringToInteger(_value); }

static void StringToType(string _value, unsigned long& _out) {
_out = (unsigned long)StringToInteger(_value);
}
static void StringToType(string _value, unsigned long& _out) { _out = (unsigned long)StringToInteger(_value); }

static void StringToType(string _value, short& _out) {
_out = (short) StringToInteger(_value);
}
static void StringToType(string _value, short& _out) { _out = (short)StringToInteger(_value); }

static void StringToType(string _value, unsigned short& _out) {
_out = (unsigned short) StringToInteger(_value);
}
static void StringToType(string _value, unsigned short& _out) { _out = (unsigned short)StringToInteger(_value); }

static void StringToType(string _value, float& _out) {
_out = (float)StringToDouble(_value);
}
static void StringToType(string _value, float& _out) { _out = (float)StringToDouble(_value); }

static void StringToType(string _value, double& _out) {
_out = StringToDouble(_value);
}
static void StringToType(string _value, double& _out) { _out = StringToDouble(_value); }

static void StringToType(string _value, color& _out) {
_out = StringToColor(_value);
}
static void StringToType(string _value, color& _out) { _out = StringToColor(_value); }

static void StringToType(string _value, datetime& _out) {
_out = StringToTime(_value);
}
static void StringToType(string _value, datetime& _out) { _out = StringToTime(_value); }

static void StringToType(string _value, string& _out) {
_out = _value;
}
static void StringToType(string _value, string& _out) { _out = _value; }

/**
* Converts MqlParam struct to double.
Expand Down Expand Up @@ -407,7 +378,7 @@ public:
return param.integer_value;
case TYPE_DOUBLE:
case TYPE_FLOAT:
return (int) param.double_value;
return (int)param.double_value;
case TYPE_CHAR:
case TYPE_COLOR:
case TYPE_STRING:
Expand All @@ -416,8 +387,6 @@ public:
}
return INT_MIN;
}

};


#endif // CONVERT_MQH
#endif // CONVERT_MQH
Loading

0 comments on commit 8ecef43

Please sign in to comment.