complete trailing ...
This commit is contained in:
@@ -4100,15 +4100,93 @@ public:
|
||||
mSaveTrades = value;
|
||||
}
|
||||
|
||||
//
|
||||
// Trailling ...
|
||||
|
||||
//
|
||||
bool AllowTrailStops()
|
||||
{
|
||||
return mAllowTrailStops;
|
||||
}
|
||||
|
||||
//
|
||||
void AllowTrailStops(bool value)
|
||||
{
|
||||
mAllowTrailStops = value;
|
||||
}
|
||||
|
||||
//
|
||||
bool IgnoreTPOnTrail()
|
||||
{
|
||||
return mIgnoreTPOnTrail;
|
||||
}
|
||||
|
||||
//
|
||||
void IgnoreTPOnTrail(bool value)
|
||||
{
|
||||
mIgnoreTPOnTrail = value;
|
||||
}
|
||||
|
||||
//
|
||||
double TrailStartInPoint()
|
||||
{
|
||||
return mTrailStartInPoint;
|
||||
}
|
||||
|
||||
//
|
||||
void TrailStartInPoint(double value)
|
||||
{
|
||||
//
|
||||
if (value < 0)
|
||||
{
|
||||
value = 0;
|
||||
}
|
||||
|
||||
//
|
||||
mTrailStartInPoint = value;
|
||||
}
|
||||
|
||||
//
|
||||
double TrailStepInPoint()
|
||||
{
|
||||
return mTrailStepInPoint;
|
||||
}
|
||||
|
||||
//
|
||||
void TrailStepInPoint(double value)
|
||||
{
|
||||
//
|
||||
if (value < 0)
|
||||
{
|
||||
value = 0;
|
||||
}
|
||||
|
||||
//
|
||||
mTrailStepInPoint = value;
|
||||
}
|
||||
|
||||
//
|
||||
bool TrailBasedOnProfit()
|
||||
{
|
||||
return mTrailBasedOnProfit;
|
||||
}
|
||||
|
||||
//
|
||||
void TrailBasedOnProfit(bool value)
|
||||
{
|
||||
mTrailBasedOnProfit = value;
|
||||
}
|
||||
|
||||
//
|
||||
// Tools ...
|
||||
|
||||
//
|
||||
// First Step of Trade Handling ...
|
||||
// Since Must Call when a Signal Executed ...
|
||||
void AddExecutedSignal(
|
||||
XSignal &signal, // Executed Signal
|
||||
double commission = 0 // Commission
|
||||
void AddData(
|
||||
XSignal &signal, // Executed Signal
|
||||
double commission = 0, // Commission
|
||||
bool forceTrail = false //
|
||||
)
|
||||
{
|
||||
//
|
||||
@@ -4129,6 +4207,24 @@ public:
|
||||
//
|
||||
item.commission = commission;
|
||||
|
||||
//
|
||||
// Trailling Configuration ...
|
||||
|
||||
//
|
||||
// Force Trail used to implement Trailing for
|
||||
// Custom Positions ...
|
||||
if (mAllowTrailStops || forceTrail)
|
||||
{
|
||||
//
|
||||
item.allowTrailStop = true;
|
||||
|
||||
//
|
||||
item.ignoreTPOnTrail = mIgnoreTPOnTrail;
|
||||
item.trailStepInPoint = mTrailStepInPoint;
|
||||
item.trailStartInPoint = mTrailStartInPoint;
|
||||
item.trailBasedOnProfit = mTrailBasedOnProfit;
|
||||
}
|
||||
|
||||
//
|
||||
Add(item);
|
||||
|
||||
@@ -4277,6 +4373,225 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Do all Protections ...
|
||||
void HandleProtection()
|
||||
{
|
||||
//
|
||||
int count = Count();
|
||||
if (!IsValidSize(count))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
// Do Hedging ...
|
||||
|
||||
//
|
||||
// Do Force Closing ...
|
||||
|
||||
//
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
//
|
||||
ulong ticket = mData[i].ticket;
|
||||
double profit = mData[i].profit;
|
||||
double volume = mData[i].volume;
|
||||
string symbol = mData[i].symbol;
|
||||
bool isLong = IsLong(mData[i].type);
|
||||
ENUM_TIMEFRAMES period = mData[i].period;
|
||||
double points = GetPoints(mData[i].symbol);
|
||||
bool ignoreTPOnTrail = mData[i].ignoreTPOnTrail;
|
||||
double trailStepInPoints = mData[i].trailStepInPoint;
|
||||
double trailStartInPoint = mData[i].trailStartInPoint;
|
||||
bool trailBasedOnProfit = mData[i].trailBasedOnProfit;
|
||||
|
||||
//
|
||||
XPosition iPosition;
|
||||
bool hasPosition = mTrader.GetPosition(
|
||||
ticket,
|
||||
iPosition //
|
||||
);
|
||||
if (!hasPosition)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
// Trail ...
|
||||
if (mData[i].allowTrailStop)
|
||||
{
|
||||
//
|
||||
// Do Trail ...
|
||||
|
||||
//
|
||||
// Only Can Trail if Position In Profit ...
|
||||
if (profit > 0)
|
||||
{
|
||||
//
|
||||
// Current Profit ...
|
||||
double profitInPoints = (profit / volume);
|
||||
|
||||
//
|
||||
// Check Trailling Method ...
|
||||
if (trailBasedOnProfit)
|
||||
{
|
||||
//
|
||||
// if Trailling Method is Based on Profits Growing
|
||||
// these Values must Greater than 0 ...
|
||||
if (trailStartInPoint > 0 && trailStepInPoints > 0)
|
||||
{
|
||||
//
|
||||
double requiredProfit = trailStartInPoint + (mData[i].trailLevel * trailStepInPoints);
|
||||
bool isProfitPassed = profitInPoints >= requiredProfit;
|
||||
if (isProfitPassed)
|
||||
{
|
||||
//
|
||||
// Calculate new Level SL ...
|
||||
double sl =
|
||||
isLong
|
||||
? iPosition.entry + (trailStepInPoints * points)
|
||||
: iPosition.entry - (trailStepInPoints * points);
|
||||
sl = NormalizePrice(
|
||||
sl,
|
||||
symbol //
|
||||
);
|
||||
|
||||
//
|
||||
// Calculate TP ...
|
||||
double tp =
|
||||
ignoreTPOnTrail
|
||||
? 0
|
||||
: iPosition.tp;
|
||||
|
||||
//
|
||||
if (sl != iPosition.sl)
|
||||
{
|
||||
//
|
||||
bool isModified = mTrader.Modify(
|
||||
ticket,
|
||||
sl,
|
||||
tp //
|
||||
);
|
||||
if (isModified)
|
||||
{
|
||||
//
|
||||
// Increase Trail Level ...
|
||||
mData[i].trailLevel++;
|
||||
|
||||
//
|
||||
string msg = "Position (" + ToString(ticket) + ") Stops in Level: " + ToString(mData[i].trailLevel) + " Trailed Successfully ...";
|
||||
Alert(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
// Handle Trailling Based On Bars ...
|
||||
|
||||
//
|
||||
bool canTrail = true;
|
||||
if (trailStartInPoint > 0)
|
||||
{
|
||||
//
|
||||
// Check Profit ...
|
||||
canTrail = profitInPoints >= trailStartInPoint;
|
||||
}
|
||||
|
||||
//
|
||||
if (canTrail)
|
||||
{
|
||||
//
|
||||
// Zero Index Bar ...
|
||||
XOHCL zBar;
|
||||
bool isInited = zBar.Init(
|
||||
symbol,
|
||||
period,
|
||||
0 //
|
||||
);
|
||||
if (isInited)
|
||||
{
|
||||
//
|
||||
// Get Previous Bar ...
|
||||
XOHCL cBar;
|
||||
isInited = zBar.GetPreviousBar(cBar);
|
||||
if (isInited)
|
||||
{
|
||||
//
|
||||
// Get Prev of Previous Bar ...
|
||||
XOHCL pBar;
|
||||
isInited = cBar.GetPreviousBar(pBar);
|
||||
if (isInited)
|
||||
{
|
||||
//
|
||||
// Here we have to Check Bar Closes for
|
||||
// Trailling Stops ...
|
||||
canTrail =
|
||||
//
|
||||
isLong
|
||||
? cBar.IsBullish() &&
|
||||
cBar.close > pBar.open
|
||||
: cBar.IsBearish() &&
|
||||
cBar.close < pBar.open
|
||||
//
|
||||
;
|
||||
if (canTrail)
|
||||
{
|
||||
//
|
||||
// Calculate new Level SL ...
|
||||
double sl =
|
||||
isLong
|
||||
? cBar.low
|
||||
: cBar.high;
|
||||
sl = NormalizePrice(
|
||||
sl,
|
||||
symbol //
|
||||
);
|
||||
|
||||
//
|
||||
// Calculate TP ...
|
||||
double tp =
|
||||
ignoreTPOnTrail
|
||||
? 0
|
||||
: iPosition.tp;
|
||||
|
||||
//
|
||||
if (sl != iPosition.sl)
|
||||
{
|
||||
//
|
||||
bool isModified = mTrader.Modify(
|
||||
ticket,
|
||||
sl,
|
||||
tp //
|
||||
);
|
||||
if (isModified)
|
||||
{
|
||||
//
|
||||
// Increase Trail Level ...
|
||||
mData[i].trailLevel++;
|
||||
|
||||
//
|
||||
string msg = "Position (" + ToString(ticket) + ") Stops in Level: " + ToString(mData[i].trailLevel) + " Trailed Successfully ...";
|
||||
Alert(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Grid ...
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Protected ...
|
||||
protected:
|
||||
@@ -4291,6 +4606,14 @@ protected:
|
||||
XSCTrade *mTrader; // Instance of Trader Class
|
||||
XTradeCollector *mCollector; // Instance of Trade Collector Class
|
||||
|
||||
//
|
||||
// Trailling ...
|
||||
bool mAllowTrailStops; // Trail All Trades Stops
|
||||
bool mIgnoreTPOnTrail; // Ignore SL Trailed TP
|
||||
bool mTrailBasedOnProfit; // Do Trail Based on Profit Points
|
||||
double mTrailStartInPoint; // Trail Starts in Points of Profit
|
||||
double mTrailStepInPoint; // Profit Must Grows Point to do Trailling
|
||||
|
||||
//
|
||||
XTradeData mData[]; // Hold Trade Data
|
||||
|
||||
|
||||
Reference in New Issue
Block a user