Ballast Trade Safety
- librerie
- Versione: 1.0
- Attivazioni: 5
The order handling behind every Ballast tool, as a library for your own Expert Advisors. One call opens a trade sized to a percentage of the account, refuses to send it without a stop, checks every broker limit first and retries requotes, so your EA never sends an order the broker or the Market validator rejects.
It is not a trading strategy. It is the part every EA needs and most get wrong: how many lots, is this order valid, what if the broker says no.
- Lot size from risk that does not trust the broker's tick value alone. On CFDs priced in another currency the tick value can be far off (seen: Gold about 10% high, UK 100 16% low, Japan 225 175 times high). The library uses the terminal's own profit calculation, then contract size times exchange rate, and the tick value only as a last resort. Lots are always rounded down to the volume step.
- Pre-trade checks with a reason in plain words: connection, Algo Trading, account and symbol permissions, trading session, volume (min, max, step), stop and target distance (stops level and freeze level), free margin, symbol volume limit.
- Orders: market orders need a stop loss (an order without one is refused), prices are rounded to the tick size, the filling mode is chosen for the symbol, and requotes and price changes are retried up to 3 times at the new price. Modify stops, close all or part of a position.
- Account protection: money lost if every stop is hit now (and how many positions have no stop), profit closed today, the balance when the day began, the room left before a daily loss limit and before a maximum loss limit, the way prop firms count them.
Example
#include <BallastTradeSafety.mqh>
void OnTick()
{
double sl = SymbolInfoDouble(_Symbol, SYMBOL_BID) - 300 * _Point;
string why;
ulong ticket = BT_OpenByRisk(_Symbol, true, sl, 0, 1.0, 123, "my EA", why); // buy, risk 1% to the stop
if(ticket == 0)
Print("not opened: ", why); // e.g. "not enough free margin", "market closed"
}Tested
- Three two-month runs on real ticks through the import, as a buyer uses it: EURUSD with a USD account, Gold with a USD account, USDJPY with a EUR account. A test EA opened 396 trades sized to 0.5% risk, moved stops to break-even, closed halves, and tried 9 invalid orders on each run.
- Lot size versus the terminal's own calculation: 0.0000% difference on every check. All 27 invalid orders refused with the right reason, 0 requests rejected by the broker in all three runs.
- Loss of a stopped trade versus the planned risk (swap not included): worst 1.002x on Gold, 1.014x on USDJPY with a EUR account. On EURUSD some stops were filled further away during US news at 12:30 and the rollover (up to 2.1x the plan): no stop can prevent that slippage.
Notes
- The Market installs the library in MQL5\Libraries\Market. The import line must match the file name there exactly (two backslashes in the path).
- Risk is the move from the entry to the stop. Swap, commission and slippage are not included.
- Days for the daily functions begin at the server hour you pass (0 = midnight server time). Deposits and withdrawals are not counted as profit.
All functions (copy this into MQL5\Include\BallastTradeSafety.mqh)
//+------------------------------------------------------------------+ //| BallastTradeSafety.mqh - import block for Ballast Trade Safety. | //| Save this file in MQL5\Include and add to your EA: | //| #include <BallastTradeSafety.mqh> | //| The Market installs the library in MQL5\Libraries\Market. | //+------------------------------------------------------------------+ #import "Market\\Ballast Trade Safety.ex5" string BT_Version(); //--- money and sizing double BT_LossPerLot(string symbol, double priceDistance); double BT_NormalizeLots(string symbol, double lots); double BT_NormalizePrice(string symbol, double price); double BT_LotsForRisk(string symbol, double entry, double stop, double riskMoney); double BT_LotsForRiskPercent(string symbol, double entry, double stop, double riskPercent); //--- checks bool BT_CanTrade(string symbol, bool buy, string &reason); double BT_MinStopDistance(string symbol); bool BT_StopsValid(string symbol, bool buy, double sl, double tp); bool BT_CheckOrder(string symbol, bool buy, double lots, double sl, double tp, string &reason); //--- orders (a stop loss is required; requotes are retried) ulong BT_Open(string symbol, bool buy, double lots, double sl, double tp, ulong magic, string comment, string &reason); ulong BT_Buy(string symbol, double lots, double sl, double tp, ulong magic, string comment, string &reason); ulong BT_Sell(string symbol, double lots, double sl, double tp, ulong magic, string comment, string &reason); ulong BT_OpenByRisk(string symbol, bool buy, double sl, double tp, double riskPercent, ulong magic, string comment, string &reason); bool BT_SetStops(ulong position, double sl, double tp, string &reason); bool BT_Close(ulong position, double lots, string &reason); //--- account protection double BT_RiskToStops(string symbol, ulong magic, int &noStop); double BT_TodayClosedProfit(int dayStartHour); double BT_DayStartBalance(int dayStartHour); double BT_DailyLossRoom(double limitPercent, double referenceBalance, int dayStartHour); double BT_MaxLossRoom(double limitPercent, double baseBalance); #import
