Discussing the article: "Building Your Personal Expert Advisor (Part 3): Risk Management II—Margin and Allowable Risk"

 

Check out the new article: Building Your Personal Expert Advisor (Part 3): Risk Management II—Margin and Allowable Risk.

Risk-based lot sizing can still exceed what free margin allows. The article adds a margin-aware cap using OrderCalcMargin(), an optional adaptive cap that scales with ACCOUNT MARGIN LEVEL, and a single pre-trade validation gate that unifies position limits, risk sizing, and margin checks. Readers get concrete code to prevent order rejections and over-committing margin, with clear logs when a trade is reduced or skipped.

The article assumes you are working from the EA produced in Part 2 and are comfortable with what CalculateLotSize() does. As always, the crossover logic stays untouched because this is not a strategy article.

Scope of This Article

By the end of this part, we will have the following:

  • Added a margin check that caps the risk-based lot to what free margin can actually support.
  • Made that cap configurable, with a hard floor where the trade is skipped rather than shrunk.
  • Added an optional adaptive layer that tightens the cap as the account's margin level deteriorates.
  • Collected the position check, risk sizing, and margin check into one named pre-trade validation gate.

None of this is specific to a moving average crossover. Any EA that calculates a lot size before sending an order needs the same check.

Author: Solomon Anietie Sunday