Equity and Free Margin Protection EA I coded: Hope it helps you guys and gals

 

Attached is an EA I coded, which does need a Forex account that allows hedging for it to be useful. Putting it online here for others to use is simply to give something back to this forum and as a way to say thank you.

It is a "Trade Manager" type EA, not a trading strategy.

It works as follows:

For all 3 areas (Free Margin, Equity Profit and Equity Loss), when it starts up (or you restart MT4), it calculates the difference between sell and buy lots that are open as well as your opening equity and free margin levels.

At pre-set trigger points that you set in inputs, it will open a trade to balance lots.

Eg, With Equity Loss and Equity Profit, it makes the lots equal to lock in the profit or limit the loss. With free Margin, it opens an opposite order to leave 50% of the trade in favor of the direction you have.

The trigger points and lots info are shown on the chart so that you know where things are at.

Free Margin -

In inputs, you can select the % of Free Margin at which point the 50% balance trade gets opened. That is, "extern int FM = 0.3" means that when Free Margin gets down to 30%, it will open an opposite trade equal to 50% of the open lots. This leaves the majority of your open lots in favor of the direction you wanted, but prevents your account from any margin calls.

TakeProfitFM is the pips of profit this trade can be closed at.

Equity Loss -

In inputs, you can set the maximum loss of equity you will accept before an opposite order will balance your buy/sell lots. That is "extern double MaxLoss = 0.25" equals 25% loss of equity to my account will trigger a balancing trade that makes buy and sell lots equal.

When this trade is opened, it resets the MaxLoss level at the set % lower.

NOTE: This code tracks best price/equity. As your account improves equity/profit, it tracks and changes the MaxLoss tigger point according to the new/higher level.

TakeProfitML is the pips in profit this trade can be closed at.

Equity Profit -

In inputs, you can set the maximum increase of equity you will accept before an opposite order will balance your buy/sell lots. That is "extern double HeapsProfit = 0.25" equals 25% profit on equity to my account will trigger a balancing trade the makes buy and sell lots equal. This is to lock in a profit, which emotionally we often see disappear before our eyes before we take action.

When this trade is opened, it resets the HeapsProfit level at the set % higher.

TakeProfitHP is the pips in profit these trades can be closed at.

The way to use this EA is to attach it to any chart. It is not related to time frames. Any other EAs you use of course run on their own charts. That is, this is a "stand-alone EA" that I use not for the primary trades I open, but to cover my butt when things go good or bad.

Any comments welcome, but please don't think I'm interested in adding lots of functions - my coding skill is only a few rungs up the learning curve ladder.

equity_and_free_margin_manager.mq4

 
Very interesting, looking forward for more topics like these
 
@dancingphil, you deserve an acalade for your generousity.
 

Where would I insert this file to MT4?

 
Thank you
 

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

  1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce, the stop goes below the support.

  2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP, but it takes account of the exchange rates of the pair vs. your account currency.)

  3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
              MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
              Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

  4. You must normalize lots properly and check against min and max.

  5. You must also check Free Margin to avoid stop out

  6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

Reason: