I did something like this. Keep track of all orders via $ amount. Assuming all your orders have the same magic number, first you have to figure your total profit by magic number. I created a function to do this:
PHP Code:
double OPBM(int intMagic)//OrderProfitByMagic
{
double dblProfit=0;
int intPOS=0;
bool boolTerm=false;
while(boolTerm==false)
{
if(OrderSelect(intPOS,SELECT_BY_POS))
{
if(OrderMagicNumber()==intMagic) dblProfit=dblProfit+OrderProfit();
intPOS++;
}
else
boolTerm=true;
}
return(dblProfit);
}
Insert this piece of code after the last ending bracket of your EA. Then anytime you need to get the total value of all open orders with a specific magic number simply call the function. Ex.
PHP Code:
OPBM(Your_Magic_Number_Here)
Next create a few external variables:
PHP Code:
extern bool Use_Trailing_Stop=true;//select true to use a trailing stop based on total $amount
extern double Trail_Start=10;//TS will start after this $Profit amount is reached
extern double TSLoss_Percent=50;//%Percentage of your HIGHEST profit you can lose before close all is performed
After your Trail_Start value is hit, you will need to keep track of your highest achieved profit value, and an allowed loss value, and store those values. I stored them as a Global Variables.
PHP Code:
Total_Profit=OPBM(Your_Magic_Number_Here);
if ((Use_Trailing_Stop==true) && (OPBM(Your_Magic_Number_Here)>Trail_Start))
{
Highest_Profit=GlobalVariableGet(TFX_Highest_Profit);
if (Total_Profit > Highest_Profit)
{
Highest_Profit=GlobalVariableSet(TFX_Highest_Profit,Total_Profit);
Highest_Profit=GlobalVariableGet(TFX_Highest_Profit);
Loss_Percent=TSLoss_Percent*0.01;
Print(" Loss percent = ",Loss_Percent);
Allowed_Loss=GlobalVariableGet(TFX_Highest_Profit)*Loss_Percent;
Allowed_Loss=GlobalVariableSet(TFX_Allowed_Loss,Allowed_Loss);
Allowed_Loss=GlobalVariableGet(TFX_Allowed_Loss);
Trail_Engaged=true;
Print(" Highest Profit = ",Highest_Profit);
Print(" Allowed Loss = ",Allowed_Loss);
}
}
Then you need code to close orders when you fall below your allowed loss value, maybe something like this:
PHP Code:
if ((Use_Trailing_Stop==true) && (Trail_Engaged==true))
{
if (OPBM(Your_Magic_Number_Here) <= ((GlobalVariableGet(TFX_Highest_Profit))-(GlobalVariableGet(TFX_Allowed_Loss))))
{
CBM(Your_Magic_Number_Here);
}
The CBM() is a Close By Magic function I made:
PHP Code:
int CBM(int intMagic)//CloseByMagic
{
int intOffset=0;
int Count = OTBM(intMagic);
while(OTBM(intMagic)>0 && Count > 0)
{
OrderSelect(intOffset,SELECT_BY_POS);
if(OrderMagicNumber()==intMagic)
{
if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Red);
else if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Orange);
Count--;
}
else {
intOffset++;
}
}
return(0);
}
To use the CBM function, you also will need this function:
PHP Code:
int OTBM(int intMagic)//OrdersTotalByMagic
{
int intCount=0;
int intPOS=0;
bool boolTerm=false;
while(boolTerm==false)
{
if(OrderSelect(intPOS,SELECT_BY_POS))
{
if(OrderMagicNumber()==intMagic) intCount++;
intPOS++;
}
else
boolTerm=true;
}
return(intCount);
}