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.
any smart people that can help code this?
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.
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
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..
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.
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:
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.
Next create a few external variables:
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.
if ((Use_Trailing_Stop==true) && (OPBM(Your_Magic_Number_Here)>Trail_Start))
if (Total_Profit > Highest_Profit)
Print(" Loss percent = ",Loss_Percent);
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))))
The CBM() is a Close By Magic function I made:
int CBM(int intMagic)//CloseByMagic
int Count = OTBM(intMagic);
while(OTBM(intMagic)>0 && Count > 0)
else if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Orange);
To use the CBM function, you also will need this function:
[PHP]int OTBM(int intMagic)//OrdersTotalByMagic
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)