Skip to content

Commit

Permalink
Refs EA31337/EA31337-indicators-stats#2. Closed to finish Indi_Accoun…
Browse files Browse the repository at this point in the history
…tStats indicator.
  • Loading branch information
nseam committed Mar 22, 2024
1 parent bbf8284 commit 20a08a7
Show file tree
Hide file tree
Showing 17 changed files with 444 additions and 47 deletions.
1 change: 1 addition & 0 deletions Account/Account.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ class Account : public AccountBase {
*/
~Account() {}
};

#endif // ACCOUNT_H
40 changes: 40 additions & 0 deletions Account/AccountBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,46 @@ class AccountBase : public Dynamic {
* Class deconstructor.
*/
~AccountBase() {}

/**
* Returns balance value of the current account.
*/
virtual datetime GetDateTime() { return TimeCurrent(); };

/**
* Returns balance value of the current account.
*/
virtual float GetBalance() = 0;

/**
* Returns credit value of the current account.
*/
virtual float GetCredit() = 0;

/**
* Returns profit value of the current account.
*/
virtual float GetProfit() = 0;

/**
* Returns equity value of the current account.
*/
virtual float GetEquity() = 0;

/**
* Returns margin value of the current account.
*/
virtual float GetMarginUsed() = 0;

/**
* Returns free margin value of the current account.
*/
virtual float GetMarginFree() = 0;

/**
* Get account available margin.
*/
virtual float GetMarginAvail() = 0;
};

#endif // ACCOUNTBASE_H
15 changes: 8 additions & 7 deletions Account/AccountMt.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ class AccountMt;
#include "Account.define.h"
#include "Account.enum.h"
#include "Account.extern.h"
#include "Account.h"
#include "Account.struct.h"

/**
* Class to provide functions that return parameters of the current account.
*/
class AccountMt {
class AccountMt : public AccountBase {
protected:
// Struct variables.
BufferStruct<AccountEntry> entries;
Expand Down Expand Up @@ -136,7 +137,7 @@ class AccountMt {
* Returns balance value of the current account.
*/
static double AccountBalance() { return AccountInfoDouble(ACCOUNT_BALANCE); }
float GetBalance() {
float GetBalance() override {
// @todo: Adds caching.
// return UpdateStats(ACC_BALANCE, AccountBalance());
return (float)AccountMt::AccountBalance();
Expand All @@ -146,7 +147,7 @@ class AccountMt {
* Returns credit value of the current account.
*/
static double AccountCredit() { return AccountInfoDouble(ACCOUNT_CREDIT); }
float GetCredit() {
float GetCredit() override {
// @todo: Adds caching.
// return UpdateStats(ACC_CREDIT, AccountCredit());
return (float)AccountMt::AccountCredit();
Expand All @@ -156,7 +157,7 @@ class AccountMt {
* Returns profit value of the current account.
*/
static double AccountProfit() { return AccountInfoDouble(ACCOUNT_PROFIT); }
float GetProfit() {
float GetProfit() override {
// @todo: Adds caching.
// return UpdateStats(ACC_PROFIT, AccountProfit());
return (float)AccountMt::AccountProfit();
Expand All @@ -166,7 +167,7 @@ class AccountMt {
* Returns equity value of the current account.
*/
static double AccountEquity() { return AccountInfoDouble(ACCOUNT_EQUITY); }
float GetEquity() {
float GetEquity() override {
// @todo: Adds caching.
// return UpdateStats(ACC_EQUITY, AccountEquity());
return (float)AccountMt::AccountEquity();
Expand Down Expand Up @@ -198,7 +199,7 @@ class AccountMt {
* Returns free margin value of the current account.
*/
static double AccountFreeMargin() { return AccountInfoDouble(ACCOUNT_MARGIN_FREE); }
float GetMarginFree() {
float GetMarginFree() override {
// @todo: Adds caching.
// return UpdateStats(ACC_MARGIN_FREE, AccountFreeMargin());
return (float)AccountMt::AccountFreeMargin();
Expand Down Expand Up @@ -267,7 +268,7 @@ class AccountMt {
* Get account available margin.
*/
static double AccountAvailMargin() { return fmin(AccountFreeMargin(), AccountTotalBalance()); }
float GetMarginAvail() { return (float)AccountAvailMargin(); }
float GetMarginAvail() override { return (float)AccountAvailMargin(); }

/**
* Returns the calculation mode of free margin allowed to open orders on the current account.
Expand Down
25 changes: 24 additions & 1 deletion Indicator.enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ enum ENUM_INDICATOR_ACTION {
enum ENUM_INDICATOR_TYPE {
INDI_NONE = 0, // (None)
INDI_AC, // Accelerator Oscillator
INDI_ACCOUNT_STATS, // Account Stats
INDI_AD, // Accumulation/Distribution
INDI_ADX, // Average Directional Index
INDI_ADXW, // ADX by Welles Wilder
Expand Down Expand Up @@ -237,9 +238,24 @@ enum ENUM_INDI_VS_TYPE {
INDI_VS_TYPE_INDEX_8,
INDI_VS_TYPE_INDEX_9,
INDI_VS_TYPE_INDEX_FIRST = INDI_VS_TYPE_INDEX_0,
INDI_VS_TYPE_INDEX_LAST = INDI_VS_TYPE_INDEX_9
INDI_VS_TYPE_INDEX_LAST = INDI_VS_TYPE_INDEX_9,

// Account Stats.
INDI_VS_TYPE_ACCOUNT_STATS_DATE_TIME,
INDI_VS_TYPE_ACCOUNT_STATS_BALANCE,
INDI_VS_TYPE_ACCOUNT_STATS_CREDIT,
INDI_VS_TYPE_ACCOUNT_STATS_EQUITY,
INDI_VS_TYPE_ACCOUNT_STATS_PROFIT,
INDI_VS_TYPE_ACCOUNT_STATS_MARGIN_USED,
INDI_VS_TYPE_ACCOUNT_STATS_MARGIN_FREE,
INDI_VS_TYPE_ACCOUNT_STATS_MARGIN_AVAIL,
INDI_VS_TYPE_ACCOUNT_STATS_INDEX_FIRST = INDI_VS_TYPE_ACCOUNT_STATS_DATE_TIME,
INDI_VS_TYPE_ACCOUNT_STATS_INDEX_LAST = INDI_VS_TYPE_ACCOUNT_STATS_MARGIN_AVAIL,
};

#define INDI_VS_TYPE_ACCOUNT_STATS_BUFFERS_COUNT \
(INDI_VS_TYPE_ACCOUNT_STATS_INDEX_LAST - INDI_VS_TYPE_ACCOUNT_STATS_INDEX_FIRST + 1)

// Indicator flags.
enum ENUM_INDI_FLAGS {
INDI_FLAG_INDEXABLE_BY_SHIFT, // Indicator supports indexation by shift.
Expand Down Expand Up @@ -269,4 +285,11 @@ enum ENUM_INDI_DS_MODE_KIND {
INDI_DS_MODE_KIND_AP, // Mode is a value from ENUM_APPLIED_PRICE enumeration. It is used to retrieve value storage
// based on ENUM_INDI_VS_TYPE enumeration, e.g., PRICE_OPEN becomes ENUM_INDI_VS_PRICE_OPEN.
};

// Type of entry
enum ENUM_INDI_EMITTED_ENTRY_TYPE {
INDI_EMITTED_ENTRY_TYPE_PARENT, // Undetermined type of entry from direct parent indicator.
INDI_EMITTED_ENTRY_TYPE_TICK,
INDI_EMITTED_ENTRY_TYPE_CANDLE,
};
//+------------------------------------------------------------------+
28 changes: 22 additions & 6 deletions Indicator/IndicatorCandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
// Includes.
#include "../Buffer/BufferCandle.h"
#include "../Candle.struct.h"
#include "../Indicator.enum.h"
#include "../Indicator.mqh"
#include "../Storage/ValueStorage.price_median.h"
#include "../Storage/ValueStorage.price_typical.h"
Expand Down Expand Up @@ -310,7 +311,7 @@ class IndicatorCandle : public Indicator<TS> {
void EmitHistory() override {
for (DictStructIterator<long, CandleOCTOHLC<TV>> iter(icdata.Begin()); iter.IsValid(); ++iter) {
IndicatorDataEntry _entry = CandleToEntry(iter.Key(), iter.Value());
EmitEntry(_entry);
EmitEntry(_entry, INDI_EMITTED_ENTRY_TYPE_CANDLE);
}
}

Expand Down Expand Up @@ -342,7 +343,7 @@ class IndicatorCandle : public Indicator<TS> {
/**
* Adds tick's price to the matching candle and updates its OHLC values.
*/
void UpdateCandle(long _tick_timestamp, double _price) {
CandleOCTOHLC<double> UpdateCandle(long _tick_timestamp, double _price) {
long _candle_timestamp = CalcCandleTimestamp(_tick_timestamp);

#ifdef __debug_verbose__
Expand All @@ -368,6 +369,8 @@ class IndicatorCandle : public Indicator<TS> {
}

icdata.Add(_candle, _candle_timestamp);

return _candle;
}

/**
Expand All @@ -380,12 +383,25 @@ class IndicatorCandle : public Indicator<TS> {
/**
* Called when data source emits new entry (historic or future one).
*/
void OnDataSourceEntry(IndicatorDataEntry& entry) override {
// Updating candle from bid price.
UpdateCandle(entry.timestamp, entry[1]);
void OnDataSourceEntry(IndicatorDataEntry& entry,
ENUM_INDI_EMITTED_ENTRY_TYPE type = INDI_EMITTED_ENTRY_TYPE_PARENT) override {
Indicator<TS>::OnDataSourceEntry(entry, type);

if (type != INDI_EMITTED_ENTRY_TYPE_TICK) {
return;
}

long _candle_timestamp = CalcCandleTimestamp(entry.timestamp);

// Updating tick & bar indices.
counter.OnTick(CalcCandleTimestamp(entry.timestamp));
counter.OnTick(_candle_timestamp);

// Updating candle from bid price.
CandleOCTOHLC<double> _candle = UpdateCandle(entry.timestamp, entry[1]);

// Emitting candle for children.
IndicatorDataEntry _candle_entry = CandleToEntry(_candle_timestamp, _candle);
EmitEntry(_candle_entry, INDI_EMITTED_ENTRY_TYPE_CANDLE);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion Indicator/IndicatorTick.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class IndicatorTick : public Indicator<TS> {
void EmitHistory() override {
for (DictStructIterator<long, TickAB<TV>> iter(itdata.Begin()); iter.IsValid(); ++iter) {
IndicatorDataEntry _entry = TickToEntry(iter.Key(), iter.Value());
EmitEntry(_entry);
EmitEntry(_entry, INDI_EMITTED_ENTRY_TYPE_TICK);
}
}

Expand Down
12 changes: 7 additions & 5 deletions Indicator/tests/classes/IndicatorTfDummy.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ class IndicatorTfDummy : public IndicatorTf<IndicatorTfDummyParams> {

string GetName() override { return "IndicatorTfDummy(" + IntegerToString(iparams.spc) + ")"; }

void OnDataSourceEntry(IndicatorDataEntry& entry) override {
// When overriding OnDataSourceEntry() we have to remember to call parent
// method, because IndicatorCandle also need to invoke it in order to
// create/update matching candle.
IndicatorTf<IndicatorTfDummyParams>::OnDataSourceEntry(entry);
void OnDataSourceEntry(IndicatorDataEntry& entry,
ENUM_INDI_EMITTED_ENTRY_TYPE type = INDI_EMITTED_ENTRY_TYPE_PARENT) override {
IndicatorTf<IndicatorTfDummyParams>::OnDataSourceEntry(entry, type);

if (type != INDI_EMITTED_ENTRY_TYPE_TICK) {
return;
}

#ifdef __debug_indicator__
Print(GetFullName(), " got new tick at ", entry.timestamp,
Expand Down
16 changes: 8 additions & 8 deletions Indicator/tests/classes/IndicatorTickDummy.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ class IndicatorTickDummy : public IndicatorTick<IndicatorTickDummyParams, double
TickAB<double> _t7(4.2f, 4.21f);
TickAB<double> _t8(4.8f, 4.81f);

EmitEntry(TickToEntry(1000, _t1));
EmitEntry(TickToEntry(1500, _t2));
EmitEntry(TickToEntry(2000, _t3));
EmitEntry(TickToEntry(3000, _t4));
EmitEntry(TickToEntry(4000, _t5));
EmitEntry(TickToEntry(4100, _t6));
EmitEntry(TickToEntry(4200, _t7));
EmitEntry(TickToEntry(4800, _t8));
EmitEntry(TickToEntry(1000, _t1), INDI_EMITTED_ENTRY_TYPE_TICK);
EmitEntry(TickToEntry(1500, _t2), INDI_EMITTED_ENTRY_TYPE_TICK);
EmitEntry(TickToEntry(2000, _t3), INDI_EMITTED_ENTRY_TYPE_TICK);
EmitEntry(TickToEntry(3000, _t4), INDI_EMITTED_ENTRY_TYPE_TICK);
EmitEntry(TickToEntry(4000, _t5), INDI_EMITTED_ENTRY_TYPE_TICK);
EmitEntry(TickToEntry(4100, _t6), INDI_EMITTED_ENTRY_TYPE_TICK);
EmitEntry(TickToEntry(4200, _t7), INDI_EMITTED_ENTRY_TYPE_TICK);
EmitEntry(TickToEntry(4800, _t8), INDI_EMITTED_ENTRY_TYPE_TICK);
};
};
10 changes: 7 additions & 3 deletions IndicatorData.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -1616,10 +1616,10 @@ class IndicatorData : public IndicatorBase {
/**
* Sends entry to listening indicators.
*/
void EmitEntry(IndicatorDataEntry& entry) {
void EmitEntry(IndicatorDataEntry& entry, ENUM_INDI_EMITTED_ENTRY_TYPE type = INDI_EMITTED_ENTRY_TYPE_PARENT) {
for (int i = 0; i < ArraySize(listeners); ++i) {
if (listeners[i].ObjectExists()) {
listeners[i].Ptr().OnDataSourceEntry(entry);
listeners[i].Ptr().OnDataSourceEntry(entry, type);
}
}
}
Expand Down Expand Up @@ -1683,7 +1683,11 @@ class IndicatorData : public IndicatorBase {
/**
* Called when data source emits new entry (historic or future one).
*/
virtual void OnDataSourceEntry(IndicatorDataEntry& entry){};
virtual void OnDataSourceEntry(IndicatorDataEntry& entry,
ENUM_INDI_EMITTED_ENTRY_TYPE type = INDI_EMITTED_ENTRY_TYPE_PARENT) {
// Sending entry to all chilren listeners (from highest parent to lowest child).
EmitEntry(entry, type);
};

virtual void OnTick() {}

Expand Down
Loading

0 comments on commit 20a08a7

Please sign in to comment.