Ballast Trade Safety MT4
- 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 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 = Bid - 300 * _Point;
string why;
int 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
- Six months EURUSD H1 (every tick) through the import: 369 trades sized to 0.5% risk, break-even moves and partial closes, 9 invalid orders refused with the right reason, 0 requests rejected by the broker, lot size equal to the tick value calculation (0.0000%), worst stopped loss 1.007x the planned risk.
Notes
- The Market installs the library in MQL4\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.
- MT4 gives the rest of a partly closed trade a new ticket: BT_Close returns it (0 = all closed, -1 = failed).
- Days for the daily functions begin at the server hour you pass (0 = midnight server time).
All functions (copy this into MQL4\Include\BallastTradeSafety.mqh)
//+------------------------------------------------------------------+ //| BallastTradeSafety.mqh - import block for Ballast Trade Safety MT4.| //| Save this file in MQL4\Include and add to your EA: | //| #include <BallastTradeSafety.mqh> | //| The Market installs the library in MQL4\Libraries\Market. | //+------------------------------------------------------------------+ #import "Market\\Ballast Trade Safety MT4.ex4" 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). Tickets: -1 = failed int BT_Open(string symbol, bool buy, double lots, double sl, double tp, int magic, string comment, string &reason); int BT_Buy(string symbol, double lots, double sl, double tp, int magic, string comment, string &reason); int BT_Sell(string symbol, double lots, double sl, double tp, int magic, string comment, string &reason); int BT_OpenByRisk(string symbol, bool buy, double sl, double tp, double riskPercent, int magic, string comment, string &reason); bool BT_SetStops(int ticket, double sl, double tp, string &reason); int BT_Close(int ticket, double lots, string &reason); // returns the ticket of the part still open, 0 = all closed, -1 = failed //--- account protection double BT_RiskToStops(string symbol, int 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
