start implementing XTrade Handler Class ...
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
//
|
||||
// Imports ...
|
||||
#include "../Classes/x-saherelm.base.class.mq5"
|
||||
#include "../Classes/x-saherelm.xalert.class.mq5"
|
||||
#include "../Libraries/x-saherelm.xtrade.lib.mq5"
|
||||
#include "../Classes/x-saherelm.xaccount.class.mq5"
|
||||
|
||||
@@ -3772,4 +3773,314 @@ private:
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Trade Handler Class ...
|
||||
class XSCXTradeHandler : public XSCBaseAlert
|
||||
{
|
||||
//
|
||||
// Public ...
|
||||
public:
|
||||
//
|
||||
|
||||
//
|
||||
// Constructor(s) ...
|
||||
void XSCXTradeHandler(XSCTrade *trader)
|
||||
{
|
||||
//
|
||||
mTrader = trader;
|
||||
}
|
||||
|
||||
//
|
||||
// Deconstructor ...
|
||||
void ~XSCXTradeHandler()
|
||||
{
|
||||
//
|
||||
delete mTrader;
|
||||
}
|
||||
|
||||
//
|
||||
// First Step of Trade Handling ...
|
||||
// Since Must Call when a Signal Executed ...
|
||||
void AddExecutedSignal(
|
||||
XSignal &signal, // Executed Signal
|
||||
double commission = 0 // Commission
|
||||
)
|
||||
{
|
||||
//
|
||||
// Check Signal Valid ...
|
||||
if (!signal.IsValid())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
XTradeData item;
|
||||
bool isFilled = item.Fill(signal);
|
||||
if (!isFilled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
item.commission = commission;
|
||||
|
||||
//
|
||||
Add(item);
|
||||
}
|
||||
|
||||
//
|
||||
// this Method call's by a Timer,
|
||||
// or in OnTick for Update Positions,
|
||||
// Data ...
|
||||
void UpdateData()
|
||||
{
|
||||
//
|
||||
XPosition positions[];
|
||||
int count = mTrader.GetPositions(positions);
|
||||
if (!IsValidSize(count))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
//
|
||||
XPosition iPosition = positions[i];
|
||||
|
||||
//
|
||||
int idx = -1;
|
||||
bool isExists = HasItem(
|
||||
iPosition.ticket,
|
||||
idx //
|
||||
);
|
||||
if (!isExists)
|
||||
{
|
||||
//
|
||||
// Add New Data ...
|
||||
|
||||
//
|
||||
// Find Executed Signal Deal History ...
|
||||
XDeal deals[];
|
||||
double commission = 0;
|
||||
int dealsCount = mTrader.GetDeals(
|
||||
deals,
|
||||
iPosition.symbol,
|
||||
iPosition.provider,
|
||||
iPosition.period,
|
||||
NULL,
|
||||
DEAL_ENTRY_IN //
|
||||
);
|
||||
if (IsValidSize(dealsCount))
|
||||
{
|
||||
//
|
||||
for (int i = 0; i < dealsCount; i++)
|
||||
{
|
||||
//
|
||||
XDeal iDeal = deals[i];
|
||||
if (iDeal.positionId == iPosition.ticket)
|
||||
{
|
||||
//
|
||||
commission = iDeal.commission;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
XTradeData iData;
|
||||
iData.Fill(
|
||||
iPosition //
|
||||
);
|
||||
|
||||
//
|
||||
iData.ticket = iPosition.ticket;
|
||||
|
||||
//
|
||||
Add(iData);
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
// Update Exists ...
|
||||
mData[idx].Update(iPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Protected ...
|
||||
protected:
|
||||
//
|
||||
XSCTrade *mTrader; // Instance of Trader Class
|
||||
|
||||
//
|
||||
XTradeData mData[]; // Hold Trade Data
|
||||
|
||||
//
|
||||
// Private ...
|
||||
private:
|
||||
//
|
||||
// Props ...
|
||||
|
||||
//
|
||||
// Count Data ...
|
||||
int Count()
|
||||
{
|
||||
return ArraySize(mData);
|
||||
}
|
||||
|
||||
//
|
||||
// Add Item ...
|
||||
bool Add(XTradeData &item)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
result = item.IsValid();
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
int index = -1;
|
||||
if (item.ticket > 0)
|
||||
{
|
||||
//
|
||||
result = !HasItem(
|
||||
item.ticket,
|
||||
index //
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
result = !HasItem(
|
||||
item.symbol,
|
||||
item.provider,
|
||||
item.period,
|
||||
item.type,
|
||||
index //
|
||||
);
|
||||
}
|
||||
if (!result || IsValidIndex(index))
|
||||
{
|
||||
//
|
||||
result = false;
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
AddRef(
|
||||
item,
|
||||
mData //
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Find Item Index ...
|
||||
bool HasItem(
|
||||
ulong ticket,
|
||||
int &index //
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
index = -1;
|
||||
|
||||
//
|
||||
int count = Count();
|
||||
result = IsValidSize(count);
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
//
|
||||
result = mData[i].IsOwn(ticket);
|
||||
if (result)
|
||||
{
|
||||
//
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Find Item Index ...
|
||||
bool HasItem(
|
||||
string symbol,
|
||||
string provider,
|
||||
ENUM_TIMEFRAMES period,
|
||||
ENUM_X_POSITION_TYPES type,
|
||||
int &index //
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
index = -1;
|
||||
|
||||
//
|
||||
// Validate ...
|
||||
result =
|
||||
//
|
||||
IsValid(symbol) &&
|
||||
IsValid(period) &&
|
||||
IsValid(provider) &&
|
||||
type != X_POSITION_TYPE_ALL &&
|
||||
type != X_POSITION_TYPE_NONE
|
||||
//
|
||||
;
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
int count = Count();
|
||||
result = IsValidSize(count);
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
//
|
||||
result = mData[i].IsOwn(
|
||||
symbol,
|
||||
provider,
|
||||
period,
|
||||
type //
|
||||
);
|
||||
if (result)
|
||||
{
|
||||
//
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
Reference in New Issue
Block a user