How to creat equity trail stop

To add comments, please log in or register
jpsdyb
110
jpsdyb  

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

jpsdyb
110
jpsdyb  

any smart people that can help code this?

phoenix
439
phoenix  
  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

Jason
155
Jason  

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:
hellkkas
676
hellkkas  
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

Eric Venturi-Bloxs
1068
Eric Venturi-Bloxs  

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

wolfe
1037
wolfe  

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

}

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

To add comments, please log in or register