How to creat equity trail stop

 

Looking for a way my EA can use a trailing stop based on equity for all trades via magic number EA.

I would like it to work something like this:

If equity profit = + 200, set equity stop (or equity trailing stop) at +100, target profit = +500.

So if I do not hit my profit target of 500 cause market retraced, at least I can guarantee that all trades will be closed out with +100 to my equity/balance.

Thank you

 

any smart people that can help code this?

 
  1. creat a function to defind how much you can get from 1 pip , due to your entry lotsize
  2. creat another function to defind how many pips you gain or loss from your equity
  3. and that by your pips-gain-from-equity function you will know how many pips far from current price to mark as a new sl

but this will more difficult if you trade more than 1 order at a time

hope this idea helps you

 

This would be extremely difficult to code if you're actually trying to set the stoploss values on the orders. If you can leave your EA running at all times, as I would think you do, then it's not too difficult.

Please see the attached code. I haven't had time to test it, so be sure to look at it prior to use.

Settings:

equity_lock How much you want to ensure you keep, once lock_start has been hit.

equity_lock_start At what profit do we start locking in equity?

equity_target At this amount of profit we close all orders

Example:

100,130,500 - If we get to $130 profit, then we'll lock in $100. If we get to $500 profit, we close all orders.

Realize that because this is a stand-alone EA, it's not very friendly. It ignores magic numbers and manual orders and will take everything into account.

Files:
 
AirforceMook:
This would be extremely difficult to code if you're actually trying to set the stoploss values on the orders. If you can leave your EA running at all times, as I would think you do, then it's not too difficult.

Please see the attached code. I haven't had time to test it, so be sure to look at it prior to use.

Settings:

equity_lock How much you want to ensure you keep, once lock_start has been hit.

equity_lock_start At what profit do we start locking in equity?

equity_target At this amount of profit we close all orders

Example:

100,130,500 - If we get to $130 profit, then we'll lock in $100. If we get to $500 profit, we close all orders.

Realize that because this is a stand-alone EA, it's not very friendly. It ignores magic numbers and manual orders and will take everything into account.

Very nice idea.. I'll try it..

Thank you

 

I give as a bonus with my differents offers a CloseAll with global $ amount. One of the features is a global trailing stop in $ amount. In other words, it works exactly like the standard MT4 trailing stop but this one is in $ and for a global profit (all order at the same time).

All targets are in $ and are hidden.

Not sure that is what you need.

FerruFx

 

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:

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);

}[/PHP]

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.

OPBM(Your_Magic_Number_Here)[/PHP]

Next create a few external variables:

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 [/PHP]

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.

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]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]

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]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);

}

 
hellkas:
Very nice idea.. I'll try it..

Did you test the EquityWatch (have it locked the profit), hellkas ?

I try for some times, but no effects..

Anyone tested it ?(or am I doing smth wrong)

 

 *** didn't take profit at the equity_target

 
Chatel Hill:  *** didn't take profit at the equity_target
  1. Do you really expect that they are still waiting for a solution after eleven (11) years?
    Don't resurrect old threads without a very good reason. A lot has changed since Build 600 and Higher. 2014.02.03

  2. There is no TP for equity when there are multiple open trades.
Reason: