Max Drawdown Kill Switch

Hello everyone, I've been working on a simple shut-off switch that triggers if max drawdown is hit. I've been running far too long without it and I felt naked without some kind of automated capital preservation system. It's been tested once on a paper server but if anyone would like to take a look to see if there are any glaring issues, please do. I left out any connection-related stuff so as not to be distracting. Here is the code:

/* 
Rough barebones framework for an automatic shut-off switch triggered by a max drawdown limit, user definable.

Only tested once on a paper server. Use at your own discretion. 

-Dude, Oct 2025
*/

turnAutoTradeOn = ParamTrigger( "turnAutoTradeOn", "TURN AUTO TRADE ON" );
turnAutoTradeOff = ParamTrigger( "turnAutoTradeOff", "TURN AUTO TRADE OFF" );

if (turnAutoTradeOn) StaticVarSet("autoTradeStatus", 1, True);
if (turnAutoTradeOff) StaticVarSet("autoTradeStatus", 0, True);

autoTradeOn = StaticVarGet("autoTradeStatus");
_TRACE("AutoTrade Status: " + autoTradeOn);


if (autoTradeOn) {

ibc = GetTradingInterface("IB");

	EquityHigh = Nz( StaticVarGet("HighWatermark"), 125000 ); // retrieve the historical equity high, or set initial if empty
	CurrentEquity = StrToNum( ibc.GetAccountValue("NetLiquidationByCurrency") ); // get current account equity
	if (CurrentEquity > EquityHigh) StaticVarSet( "HighWatermark", CurrentEquity, True ); // set new high watermark
	_TRACE("\nHigh Watermark: " + EquityHigh);
	_TRACE("\nCurrent Equity: " + CurrentEquity);

	MaxDrawDown = CurrentEquity != 0 AND CurrentEquity < EquityHigh * 0.80; // max drawdown set to 20%. CurrentEquity will show 0 if not connected. 

	
	if ( MaxDrawDown ) { // check to see if max drawdown triggered
		
		ibc.CancelAllPendingOrders(); // cancel pending orders
		ibc.CloseAllOpenPositions(); // liquidate all positions
		StaticVarSet("autoTradeStatus", 0, True); // shut off further auto trading
		
	}


	else {
	
		 //the rest of the auto-trading code here
		
	}



}


Why don't you just place Max Loss stop (part of the bracket order) to limit the downside on trade by trade basis.

Some food for thought:

1 Like

This is not about any individual trades, it's to automatically shut down the entire auto-trading system to prevent it from taking any further trades.