Ballast Prop Rules
- librerie
- Versione: 1.0
- Attivazioni: 5
The prop-firm rules behind Ballast Prop Guard, as a library for your own Expert Advisors. Give it the firm's rules once. On every tick it tells your EA whether it may trade and how many lots still fit before the daily or total loss limit, and it tightens stops so that all open trades together cannot take the account past a limit.
It is not a trading strategy. It is the part a prop-firm EA needs and that ends most challenges: one bad day, or one trade too big.
- The rules firms use: daily loss measured from the day-start balance, equity or the higher of the two, with the day starting at any server hour; max loss fixed or trailing the highest equity or balance; profit target; consistency rule (best day at most X% of the total profit); a buffer that locks before a limit, not at it.
- Room that counts open risk: PR_OpenRisk adds up what every open position and pending order loses at its stop. PR_MaxLots gives the largest lot whose stop fits in what is left (10% is kept back for slippage), PR_CanOpen answers yes or no with the numbers. An open position without a stop blocks new trades: its risk is unknown.
- Stop fitting and locks: PR_FitStops gives every position a stop so that all of them together fit in the room. Stops are only tightened; a trade with no room even for the nearest allowed stop is closed. PR_Update returns a lock code: daily and consistency locks end with the next trading day, max loss and target locks stay until PR_ClearLock. PR_CloseAll closes by magic number or everything.
- Survives restarts: start balance, day-start base, trailing peak and lock are saved in terminal global variables under your id. The day-start balance is rebuilt from the deal history, so an EA started in the middle of the day still measures from the real day start. EAs on one account with the same id share the state and the locks.
Example
#include <BallastPropRules.mqh>
int OnInit()
{
//--- start 100000, daily 5% from the higher of balance/equity, day from 00:00 server,
//--- max loss 10% fixed, target 10%, lock 0.5% of the start BEFORE a loss limit
if(!PR_Setup(100000, 5, 2, 0, 10, 0, 10, 0.5, "phase1"))
return INIT_PARAMETERS_INCORRECT;
return INIT_SUCCEEDED;
}
void OnTick()
{
string why;
if(PR_Update(why) != PR_OK)
{
PR_CloseAll(0); // locked: flat, no new trades
return;
}
PR_FitStops(0); // every open stop fits in the room left
double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK), sl = ask - 300 * _Point;
double lots = MathMin(1.0, PR_MaxLots(_Symbol, ask, sl));
if(lots > 0 && PR_CanOpen(_Symbol, true, lots, sl, why))
{
// send your buy order with stop sl here
}
}Tested
- Five two-month runs on real ticks through the import, as a buyer uses it: EURUSD and Gold with a USD account, USDJPY with a EUR account, EURUSD with a balance base, trailing max loss and a day starting at 22:00, and Gold with an equity base, max loss trailing the balance and a 30% consistency rule. The test EA was built to fail: it wanted 2% risk per trade, up to 3 trades at once, and every 5th trade was oversized with no stop.
- 1,206 trades over 53 challenges (32 failed, 18 passed): 0 ticks with equity below a daily or max loss level. The closest equity came to a limit was 0.31% of the start balance (the lock was set at 0.5%). 21 daily and 20 consistency locks, 0 trades allowed while locked, all 259 no-stop trades given a stop or closed at once.
- Day-start base checked 255 times and a restart (reload of the saved state) 169 times: 0 wrong. PR_MaxLots was the largest volume that fits every time. 19 of 19 unit checks.
Notes
- The library does not trade or close anything by itself. Your EA calls PR_CloseAll when PR_Update returns a lock, and PR_FitStops when it wants stops fitted.
- Risk is the move to the stop from the current price (positions) or the order price (pendings), so the spread paid at entry is included. Commission, swap and slippage beyond the 10% kept back are not. A gap at the weekend or on news can jump past any stop.
- A trade your EA opens without asking (no stop, no check) can lose its spread before the library reacts. In the tests equity went below a level only within a minute of such a trade.
- A limit set to 0 is off. With both loss limits off, PR_Room and PR_FreeRisk return DBL_MAX and PR_FitStops does nothing.
- Check your firm's exact rules (when the day starts, balance or equity) and pass them to PR_Setup. The library follows what you set and is not affiliated with any firm.
- The Market installs the library in MQL5\Libraries\Market. The import line must match the file name there exactly (two backslashes in the path).
All functions (copy this into MQL5\Include\BallastPropRules.mqh)
//+------------------------------------------------------------------+
//| BallastPropRules.mqh - import block for Ballast Prop Rules. |
//| Save this file in MQL5\Include and add to your EA: |
//| #include <BallastPropRules.mqh> |
//| The Market installs the library in MQL5\Libraries\Market. |
//+------------------------------------------------------------------+
#define PR_NOT_SET -1 // PR_Setup not called
#define PR_OK 0 // trading allowed
#define PR_DAILY 1 // daily loss limit nearly reached: locked until the next trading day
#define PR_MAXLOSS 2 // max loss limit nearly reached: locked until PR_ClearLock
#define PR_TARGET 3 // profit target reached: locked until PR_ClearLock
#define PR_CONSIST 4 // consistency cap reached: locked until the next trading day
#import "Market\Ballast Prop Rules.ex5"
string PR_Version();
//--- setup (dailyBase: 0 balance, 1 equity, 2 higher of the two; trailMode: 0 fixed, 1 highest equity, 2 highest balance)
bool PR_Setup(double startBalance, double dailyLossPct, int dailyBase, int dayStartHour, double maxLossPct, int trailMode,
double targetPct, double bufferPct, string id);
void PR_SetConsistency(double bestDayPct);
//--- status (call PR_Update on every tick)
int PR_Update(string &why);
int PR_Status();
string PR_StatusText(int status);
void PR_ClearLock();
void PR_ResetState();
//--- levels
double PR_StartBalance();
double PR_DayBase();
double PR_DailyLimitLevel();
double PR_MaxLossLevel();
double PR_TargetLevel();
double PR_TodayProfit();
double PR_ConsistencyCap();
double PR_Room();
//--- risk
double PR_OpenRisk(string symbol, ulong magic, int &noStop);
double PR_FreeRisk();
double PR_MaxLots(string symbol, double entry, double sl);
bool PR_CanOpen(string symbol, bool buy, double lots, double sl, string &reason);
//--- actions
int PR_FitStops(ulong magic);
int PR_CloseAll(ulong magic);
int PR_TradingDays();
#import Works well with
- Ballast Prop Guard — keeps a prop-firm challenge inside the daily and total loss limits
- Ballast Prop Rules Checker — free: would your trading have passed a prop-firm challenge?
- Ballast Trade Journal — every closed trade in R, setups ranked, weekly table and equity curve
