complete break even risk free ...

This commit is contained in:
2024-12-24 16:29:31 +03:30
parent 7c4cfd4321
commit 8d6c48ede5
9 changed files with 2306 additions and 2795 deletions
+312 -72
View File
@@ -677,6 +677,91 @@ struct XPosition
return result;
}
//
// Calculate TP Levels ...
int CalculateTPLevels(double &tps[])
{
//
int result = 0;
//
Clean(tps);
//
bool isValid = IsValid();
if (!isValid)
{
return result;
}
//
double risk = GetRisk();
int r2r = (int)GetRiskRewardRatio();
bool isLong = IsLong(type);
for (int i = 0; i < r2r; i++)
{
//
double iValue =
isLong
? entry + risk + (i * risk)
: entry - risk - (i * risk);
//
Add(
iValue,
tps //
);
}
//
result = ArraySize(tps);
//
return result;
}
//
// Calculate Reached TP Levels ...
void CalculateReahedTP(
double &reachedLevel,
double &reachedPrice //
)
{
//
reachedLevel = -1;
reachedPrice = 0;
double tps[];
bool isLong = IsLong(type);
double exitPrice = GetExit();
int tpLevels = CalculateTPLevels(tps);
if (!IsValidSize(tpLevels) || exitPrice <= 0)
{
return;
}
//
for (int i = 0; i < tpLevels - 1; i++)
{
//
double cTP = tps[i];
double nTP = tps[i + 1];
//
bool isReached =
isLong
? exitPrice > cTP &&
exitPrice < nTP
: exitPrice < cTP &&
exitPrice > nTP;
if (isReached)
{
//
reachedLevel = i + 1;
reachedPrice = cTP;
}
}
}
//
// Calculate Age ...
int GetAge(
@@ -830,6 +915,25 @@ struct XSignal
int pushers; // Signal Pushers ...
string conditions; // Signal Conditions ...
//
// Additional ...
//
// TP ...
double fullTPLevel; // Full TP ...
double partialCloseMultiplier; // Amount of Partial Close ...
double partialCloseOnTPLevel; // Do Partial Close on TP Level ...
//
// RF ...
double tpLevelForBreakEven; // TP Level for Break Even ...
bool riskFreeOnBreakEven; // Risk Free Signal On Break Even ...
//
// SL Trail ...
bool trailSL; // Do SL Trail ...
double trailSLStartOnReachTPLevel; // Start SL Trail After Reached TP Level ...
//
// Constructor ...
XSignal()
@@ -906,6 +1010,141 @@ struct XSignal
return result;
}
//
bool PrepareComplex(
string mSymbol, // Trading Symbol
string mProvider, // Signal Provider
ENUM_TIMEFRAMES mPeriod, // Trading TimeFrame
ENUM_POSITION_TYPE mType, // Long (Buy) or Short (Sell)
ENUM_X_ORDER_MODES mMode, // Execution Mode
double mEntry, // Entry Price
double mVolume, // Volume
double mSL = 0, // Stop Loss
//
// TP ...
double mFullTPLevel = 0, // Full TP Level
double mPartialCloseOnTPLevel = 0, // Partial Close On TP Level
double mPartialCloseMultiplier = 0, // Partial Close Multiplier
//
// RF ...
bool mRiskFreeOnBreakEven = false, // Risk Free Signal On Break Even ...
double mTpLevelForBreakEven = 0, // TP Level for Break Even ...
//
// SL Trail ...
bool mTrailSL = false, // Trail SL
double mTrailSLStartOnReachTPLevel = 0 // Trail SL Starts after Reached TP Level
)
{
//
bool result = false;
//
// Validate Args ...
result =
//
// Common ...
mSL > 0 &&
mEntry > 0 &&
mVolume > 0 &&
IsValid(mSymbol) &&
IsValid(mPeriod) &&
IsValid(mProvider) &&
mMode != X_ORDER_MODE_NOTHING
//
&&
//
// TP ...
mFullTPLevel > 0
//
;
if (!result)
{
return result;
}
//
// Prepare Regular Signal Values ...
double _risk = MathAbs(mEntry - mSL);
double _reward = _risk * mFullTPLevel;
//
bool isLong = IsLong(mType);
//
sl = mSL;
type = mType;
mode = mMode;
entry = mEntry;
symbol = mSymbol;
period = mPeriod;
volume = mVolume;
provider = mProvider;
fullTPLevel = mFullTPLevel;
tp = isLong
? mEntry + _reward
: mEntry - _reward;
//
result = Normalize();
if (!result)
{
return result;
}
//
// Calculate Other Conditions ...
//
bool canPartialClose =
//
mPartialCloseOnTPLevel > 0 &&
mPartialCloseMultiplier > 0 &&
mPartialCloseOnTPLevel < mFullTPLevel
//
;
if (canPartialClose)
{
//
partialCloseOnTPLevel = mPartialCloseOnTPLevel;
partialCloseMultiplier = mPartialCloseMultiplier;
}
//
bool canRFOnBEP =
//
mRiskFreeOnBreakEven &&
mTpLevelForBreakEven > 0
//
;
if (canRFOnBEP)
{
//
riskFreeOnBreakEven = mRiskFreeOnBreakEven;
tpLevelForBreakEven = mTpLevelForBreakEven;
}
//
bool canTrailSL =
//
mTrailSL &&
mTrailSLStartOnReachTPLevel > 0 &&
mTrailSLStartOnReachTPLevel < mFullTPLevel
//
;
if (canTrailSL)
{
//
trailSL = mTrailSL;
trailSLStartOnReachTPLevel = mTrailSLStartOnReachTPLevel;
}
//
result = IsValid();
//
return result;
}
//
bool PrepareLong(
string mSymbol, // Trading Symbol
@@ -1247,6 +1486,17 @@ struct XSignal
provider = NULL;
conditions = NULL;
//
trailSL = false;
riskFreeOnBreakEven = false;
//
fullTPLevel = 0;
tpLevelForBreakEven = 0;
partialCloseOnTPLevel = 0;
partialCloseMultiplier = 0;
trailSLStartOnReachTPLevel = 0;
//
Clean(supports);
}
@@ -1368,74 +1618,6 @@ struct XSignal
);
}
//
// 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;
}
//
// Find Risk ...
double GetRisk()
@@ -1496,6 +1678,61 @@ struct XSignal
return result;
}
//
// Check Can Partial Close on Specified TP Level ...
bool CanPartialClose()
{
//
bool result = false;
//
result =
IsValid() &&
fullTPLevel > 0 &&
partialCloseOnTPLevel > 0 &&
partialCloseMultiplier > 0 &&
partialCloseOnTPLevel < fullTPLevel;
//
return result;
}
//
// Check Can Risk Free On Break Even Point ...
bool CanRiskFreeOnBreakEvenPoint()
{
//
bool result = false;
//
result =
IsValid() &&
riskFreeOnBreakEven &&
tpLevelForBreakEven > 0;
//
return result;
}
//
// Check Can Trail SL ...
bool CanTrailSL()
{
//
bool result = false;
//
result =
IsValid() &&
trailSL &&
fullTPLevel > 0 &&
trailSLStartOnReachTPLevel > 0 &&
trailSLStartOnReachTPLevel < fullTPLevel;
//
return result;
}
//
string GetTag()
{
@@ -3341,21 +3578,24 @@ struct XSymbolPositions
};
//
struct XSymbolLastPosition {
struct XSymbolLastPosition
{
//
string symbol;
string provider;
datetime time;
//
XSymbolLastPosition() {
XSymbolLastPosition()
{
Clean();
}
//
// Tools ...
void Clean() {
void Clean()
{
//
time = NULL;
symbol = NULL;