Add also Support Opposit Signal Generating by Position to XSignal struct ...
This commit is contained in:
+429
-378
@@ -126,6 +126,384 @@ enum ENUM_X_GUARD_ACTIONS
|
||||
//
|
||||
// Structs ...
|
||||
|
||||
//
|
||||
// Model an Open Position ...
|
||||
struct XPosition
|
||||
{
|
||||
//
|
||||
// Props ...
|
||||
|
||||
//
|
||||
// Magic Number ...
|
||||
ulong magic;
|
||||
|
||||
//
|
||||
// Ticket ID ...
|
||||
ulong ticket;
|
||||
|
||||
//
|
||||
// Tradinng Symbol ...
|
||||
string symbol;
|
||||
|
||||
//
|
||||
// Trading Period ...
|
||||
ENUM_TIMEFRAMES period;
|
||||
|
||||
//
|
||||
// Position Type ...
|
||||
ENUM_POSITION_TYPE type;
|
||||
|
||||
//
|
||||
// Take Profit ...
|
||||
double tp;
|
||||
|
||||
//
|
||||
// Stop Loss ...
|
||||
double sl;
|
||||
|
||||
//
|
||||
// Position Current Profit ...
|
||||
double profit;
|
||||
|
||||
//
|
||||
// Position Open Price ...
|
||||
double entry;
|
||||
|
||||
//
|
||||
// Current Symbol Price ...
|
||||
double price;
|
||||
|
||||
//
|
||||
// Position Swap ...
|
||||
double swap;
|
||||
|
||||
//
|
||||
// Position Commission ...
|
||||
double commission;
|
||||
|
||||
//
|
||||
// Volume ...
|
||||
double volume;
|
||||
|
||||
//
|
||||
// Position Open Time ...
|
||||
datetime openAt;
|
||||
|
||||
//
|
||||
// Sifnal Provider ...
|
||||
string provider;
|
||||
|
||||
//
|
||||
// Position Comment ...
|
||||
string comment;
|
||||
|
||||
//
|
||||
// Constructor ...
|
||||
XPosition()
|
||||
{
|
||||
Clean();
|
||||
}
|
||||
|
||||
//
|
||||
// Initializers ...
|
||||
|
||||
bool ByIndex(int index)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
static CPositionInfo mPositionInfo;
|
||||
|
||||
//
|
||||
result = mPositionInfo.SelectByIndex(index);
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
ulong mTicket = mPositionInfo.Ticket();
|
||||
|
||||
//
|
||||
result = ByTicket(mTicket);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
bool ByTicket(ulong mTicket)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
static CPositionInfo mPositionInfo;
|
||||
|
||||
//
|
||||
result = mPositionInfo.SelectByTicket(mTicket);
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
swap = mPositionInfo.Swap();
|
||||
magic = mPositionInfo.Magic();
|
||||
openAt = mPositionInfo.Time();
|
||||
sl = mPositionInfo.StopLoss();
|
||||
ticket = mPositionInfo.Ticket();
|
||||
symbol = mPositionInfo.Symbol();
|
||||
tp = mPositionInfo.TakeProfit();
|
||||
profit = mPositionInfo.Profit();
|
||||
volume = mPositionInfo.Volume();
|
||||
comment = mPositionInfo.Comment();
|
||||
entry = mPositionInfo.PriceOpen();
|
||||
type = mPositionInfo.PositionType();
|
||||
price = mPositionInfo.PriceCurrent();
|
||||
commission = mPositionInfo.Commission();
|
||||
|
||||
//
|
||||
period = ExtractPeriod(comment);
|
||||
provider = ExtractProvider(comment);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Tools ...
|
||||
|
||||
//
|
||||
// Cleanup ...
|
||||
void Clean()
|
||||
{
|
||||
magic = 0;
|
||||
ticket = 0;
|
||||
tp = 0;
|
||||
sl = 0;
|
||||
swap = 0;
|
||||
entry = 0;
|
||||
price = 0;
|
||||
profit = 0;
|
||||
volume = 0;
|
||||
openAt = 0;
|
||||
commission = 0;
|
||||
|
||||
//
|
||||
type = NULL;
|
||||
period = NULL;
|
||||
|
||||
//
|
||||
symbol = NULL;
|
||||
comment = NULL;
|
||||
provider = NULL;
|
||||
}
|
||||
|
||||
//
|
||||
// Validate ...
|
||||
bool IsValid()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
result =
|
||||
//
|
||||
IsValid(symbol) &&
|
||||
NotEmpty(ticket)
|
||||
//
|
||||
;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Entry Price ...
|
||||
double GetEntry()
|
||||
{
|
||||
//
|
||||
double result = 0;
|
||||
|
||||
//
|
||||
if (!IsValid())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result = GetEntry(
|
||||
symbol,
|
||||
type
|
||||
//
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Exit ...
|
||||
double GetExit()
|
||||
{
|
||||
//
|
||||
double result = 0;
|
||||
|
||||
//
|
||||
if (!IsValid())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result = GetExit(
|
||||
symbol,
|
||||
type
|
||||
//
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Get Spread by Point ...
|
||||
double GetSpread()
|
||||
{
|
||||
//
|
||||
double result = 0;
|
||||
|
||||
//
|
||||
if (!IsValid())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result = PriceToPoint(GetSpread(symbol));
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Age ...
|
||||
int GetAge(
|
||||
ENUM_TIMEFRAMES mPeriod // Calculate Age based on this period
|
||||
)
|
||||
{
|
||||
return GetAge(this, mPeriod);
|
||||
}
|
||||
|
||||
//
|
||||
// Check this instance is Same os Given instance or not ...
|
||||
bool IsSameAs(
|
||||
const XPosition &value // instance for Compare
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
result =
|
||||
//
|
||||
type == value.type &&
|
||||
magic == value.magic &&
|
||||
entry == value.entry &&
|
||||
ticket == value.ticket &&
|
||||
volume == value.volume &&
|
||||
symbol == value.symbol &&
|
||||
provider == value.provider
|
||||
//
|
||||
;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Find index in a Collection ...
|
||||
int FindIndex(
|
||||
const XPosition &values[] // Collection to Find
|
||||
)
|
||||
{
|
||||
//
|
||||
return FindIndex(
|
||||
this,
|
||||
values
|
||||
//
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// Check Items Passed Specific Filters or not ...
|
||||
bool IsFiltersPassed(
|
||||
string mSymbol = NULL, // Trading Symbol
|
||||
string mProvider = NULL, // Signal Provider
|
||||
ENUM_TIMEFRAMES mPeriod = NULL, // Trading Timeframe
|
||||
ENUM_POSITION_TYPE mType = NULL, // Long (Buy), Short (Sell)
|
||||
ulong mMagic = NULL, // Magic Number
|
||||
bool mFilterByMagic = true // Get Only Self Open Positions
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
result =
|
||||
//
|
||||
// Magic Filter ...
|
||||
IsMagicFilterPassed(
|
||||
mMagic,
|
||||
mFilterByMagic,
|
||||
this
|
||||
//
|
||||
)
|
||||
//
|
||||
&&
|
||||
//
|
||||
// Symbol Filter ...
|
||||
IsSymbolFilterPassed(
|
||||
mSymbol,
|
||||
this
|
||||
//
|
||||
)
|
||||
//
|
||||
&&
|
||||
//
|
||||
// Provider Filter ...
|
||||
IsProviderFilterPassed(
|
||||
mProvider,
|
||||
this
|
||||
//
|
||||
)
|
||||
//
|
||||
&&
|
||||
//
|
||||
// Period Filter ...
|
||||
IsPeriodFilterPassed(
|
||||
mPeriod,
|
||||
this
|
||||
//
|
||||
)
|
||||
//
|
||||
&&
|
||||
//
|
||||
// Type Filter ...
|
||||
IsTypeFilterPassed(
|
||||
mType,
|
||||
this
|
||||
//
|
||||
)
|
||||
//
|
||||
;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Model a Signal ...
|
||||
struct XSignal
|
||||
@@ -704,6 +1082,57 @@ struct XSignal
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Prepare Opposit Signal based On a Position ...
|
||||
bool PrepareOpposit(
|
||||
XPosition &source // Source Data
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
ENUM_POSITION_TYPE mType = GetOpposit(source.type);
|
||||
double mEntry = GetEntry(source.symbol, mType);
|
||||
|
||||
//
|
||||
bool isLong = IsLong(mType);
|
||||
|
||||
//
|
||||
double risk = MathAbs(source.entry - source.sl);
|
||||
double reward = MathAbs(source.tp - source.entry);
|
||||
|
||||
//
|
||||
double mSL = isLong
|
||||
? mEntry - risk
|
||||
: mEntry + risk;
|
||||
|
||||
//
|
||||
double mTP = isLong
|
||||
? mEntry + reward
|
||||
: mEntry - reward;
|
||||
|
||||
//
|
||||
return Prepare(
|
||||
source.symbol,
|
||||
source.provider,
|
||||
source.period,
|
||||
mType,
|
||||
X_ORDER_MODE_MARKET,
|
||||
mEntry,
|
||||
source.volume,
|
||||
X_SIGNAL_USE_NOTHING,
|
||||
mSL,
|
||||
mTP //
|
||||
);
|
||||
|
||||
//
|
||||
result = IsValid();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Cleanup ...
|
||||
void Clean()
|
||||
@@ -1269,384 +1698,6 @@ struct XSignal
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Model an Open Position ...
|
||||
struct XPosition
|
||||
{
|
||||
//
|
||||
// Props ...
|
||||
|
||||
//
|
||||
// Magic Number ...
|
||||
ulong magic;
|
||||
|
||||
//
|
||||
// Ticket ID ...
|
||||
ulong ticket;
|
||||
|
||||
//
|
||||
// Tradinng Symbol ...
|
||||
string symbol;
|
||||
|
||||
//
|
||||
// Trading Period ...
|
||||
ENUM_TIMEFRAMES period;
|
||||
|
||||
//
|
||||
// Position Type ...
|
||||
ENUM_POSITION_TYPE type;
|
||||
|
||||
//
|
||||
// Take Profit ...
|
||||
double tp;
|
||||
|
||||
//
|
||||
// Stop Loss ...
|
||||
double sl;
|
||||
|
||||
//
|
||||
// Position Current Profit ...
|
||||
double profit;
|
||||
|
||||
//
|
||||
// Position Open Price ...
|
||||
double entry;
|
||||
|
||||
//
|
||||
// Current Symbol Price ...
|
||||
double price;
|
||||
|
||||
//
|
||||
// Position Swap ...
|
||||
double swap;
|
||||
|
||||
//
|
||||
// Position Commission ...
|
||||
double commission;
|
||||
|
||||
//
|
||||
// Volume ...
|
||||
double volume;
|
||||
|
||||
//
|
||||
// Position Open Time ...
|
||||
datetime openAt;
|
||||
|
||||
//
|
||||
// Sifnal Provider ...
|
||||
string provider;
|
||||
|
||||
//
|
||||
// Position Comment ...
|
||||
string comment;
|
||||
|
||||
//
|
||||
// Constructor ...
|
||||
XPosition()
|
||||
{
|
||||
Clean();
|
||||
}
|
||||
|
||||
//
|
||||
// Initializers ...
|
||||
|
||||
bool ByIndex(int index)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
static CPositionInfo mPositionInfo;
|
||||
|
||||
//
|
||||
result = mPositionInfo.SelectByIndex(index);
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
ulong mTicket = mPositionInfo.Ticket();
|
||||
|
||||
//
|
||||
result = ByTicket(mTicket);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
bool ByTicket(ulong mTicket)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
static CPositionInfo mPositionInfo;
|
||||
|
||||
//
|
||||
result = mPositionInfo.SelectByTicket(mTicket);
|
||||
if (!result)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
swap = mPositionInfo.Swap();
|
||||
magic = mPositionInfo.Magic();
|
||||
openAt = mPositionInfo.Time();
|
||||
sl = mPositionInfo.StopLoss();
|
||||
ticket = mPositionInfo.Ticket();
|
||||
symbol = mPositionInfo.Symbol();
|
||||
tp = mPositionInfo.TakeProfit();
|
||||
profit = mPositionInfo.Profit();
|
||||
volume = mPositionInfo.Volume();
|
||||
comment = mPositionInfo.Comment();
|
||||
entry = mPositionInfo.PriceOpen();
|
||||
type = mPositionInfo.PositionType();
|
||||
price = mPositionInfo.PriceCurrent();
|
||||
commission = mPositionInfo.Commission();
|
||||
|
||||
//
|
||||
period = ExtractPeriod(comment);
|
||||
provider = ExtractProvider(comment);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Tools ...
|
||||
|
||||
//
|
||||
// Cleanup ...
|
||||
void Clean()
|
||||
{
|
||||
magic = 0;
|
||||
ticket = 0;
|
||||
tp = 0;
|
||||
sl = 0;
|
||||
swap = 0;
|
||||
entry = 0;
|
||||
price = 0;
|
||||
profit = 0;
|
||||
volume = 0;
|
||||
openAt = 0;
|
||||
commission = 0;
|
||||
|
||||
//
|
||||
type = NULL;
|
||||
period = NULL;
|
||||
|
||||
//
|
||||
symbol = NULL;
|
||||
comment = NULL;
|
||||
provider = NULL;
|
||||
}
|
||||
|
||||
//
|
||||
// Validate ...
|
||||
bool IsValid()
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
result =
|
||||
//
|
||||
IsValid(symbol) &&
|
||||
NotEmpty(ticket)
|
||||
//
|
||||
;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Entry Price ...
|
||||
double GetEntry()
|
||||
{
|
||||
//
|
||||
double result = 0;
|
||||
|
||||
//
|
||||
if (!IsValid())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result = GetEntry(
|
||||
symbol,
|
||||
type
|
||||
//
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Retrieve Exit ...
|
||||
double GetExit()
|
||||
{
|
||||
//
|
||||
double result = 0;
|
||||
|
||||
//
|
||||
if (!IsValid())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result = GetExit(
|
||||
symbol,
|
||||
type
|
||||
//
|
||||
);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Get Spread by Point ...
|
||||
double GetSpread()
|
||||
{
|
||||
//
|
||||
double result = 0;
|
||||
|
||||
//
|
||||
if (!IsValid())
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result = PriceToPoint(GetSpread(symbol));
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Calculate Age ...
|
||||
int GetAge(
|
||||
ENUM_TIMEFRAMES mPeriod // Calculate Age based on this period
|
||||
)
|
||||
{
|
||||
return GetAge(this, mPeriod);
|
||||
}
|
||||
|
||||
//
|
||||
// Check this instance is Same os Given instance or not ...
|
||||
bool IsSameAs(
|
||||
const XPosition &value // instance for Compare
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
result =
|
||||
//
|
||||
type == value.type &&
|
||||
magic == value.magic &&
|
||||
entry == value.entry &&
|
||||
ticket == value.ticket &&
|
||||
volume == value.volume &&
|
||||
symbol == value.symbol &&
|
||||
provider == value.provider
|
||||
//
|
||||
;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Find index in a Collection ...
|
||||
int FindIndex(
|
||||
const XPosition &values[] // Collection to Find
|
||||
)
|
||||
{
|
||||
//
|
||||
return FindIndex(
|
||||
this,
|
||||
values
|
||||
//
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// Check Items Passed Specific Filters or not ...
|
||||
bool IsFiltersPassed(
|
||||
string mSymbol = NULL, // Trading Symbol
|
||||
string mProvider = NULL, // Signal Provider
|
||||
ENUM_TIMEFRAMES mPeriod = NULL, // Trading Timeframe
|
||||
ENUM_POSITION_TYPE mType = NULL, // Long (Buy), Short (Sell)
|
||||
ulong mMagic = NULL, // Magic Number
|
||||
bool mFilterByMagic = true // Get Only Self Open Positions
|
||||
)
|
||||
{
|
||||
//
|
||||
bool result = false;
|
||||
|
||||
//
|
||||
result =
|
||||
//
|
||||
// Magic Filter ...
|
||||
IsMagicFilterPassed(
|
||||
mMagic,
|
||||
mFilterByMagic,
|
||||
this
|
||||
//
|
||||
)
|
||||
//
|
||||
&&
|
||||
//
|
||||
// Symbol Filter ...
|
||||
IsSymbolFilterPassed(
|
||||
mSymbol,
|
||||
this
|
||||
//
|
||||
)
|
||||
//
|
||||
&&
|
||||
//
|
||||
// Provider Filter ...
|
||||
IsProviderFilterPassed(
|
||||
mProvider,
|
||||
this
|
||||
//
|
||||
)
|
||||
//
|
||||
&&
|
||||
//
|
||||
// Period Filter ...
|
||||
IsPeriodFilterPassed(
|
||||
mPeriod,
|
||||
this
|
||||
//
|
||||
)
|
||||
//
|
||||
&&
|
||||
//
|
||||
// Type Filter ...
|
||||
IsTypeFilterPassed(
|
||||
mType,
|
||||
this
|
||||
//
|
||||
)
|
||||
//
|
||||
;
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Model an Order ...
|
||||
struct XOrder
|
||||
|
||||
Reference in New Issue
Block a user