Skip to main content

Introduction to Stop Loss and Amibroker’s ApplyStop() Function

Ever wondered how to safeguard your trades against unexpected market dips? Enter the Stop Loss strategy, a lifesaver in the trading world. It’s like your safety net, set to sell off your stocks if prices start to plummet, ensuring you don’t lose more than you can bear. Think of it as a crucial tool in your risk management arsenal.

Let’s talk about Amibroker, a favored software among traders for crafting and testing trading strategies. Its heart and soul for implementing stop losses? The ApplyStop() function. Whether you prefer a fixed percentage stop loss or a more dynamic trailing stop loss, ApplyStop() has got you covered.

The magic lies in its three parameters: stopType, stopMode, and stopPrice. It’s like telling the function, “Hey, here’s the kind of stop loss I want, and here’s how I want it calculated.” Curious for more? Check out this detailed guide on ApplyStop().

By integrating ApplyStop() in your Amibroker strategy, you’re not just trading; you’re trading smart, with a solid backup plan for those just-in-case moments.

Also Read: All you wanted to know about Stop Loss Strategies

Stop Loss Illustration Amibroker AFL Code

				
					//------------------------------------------------------
//
//  Formula Name:    Stop Loss Illustraton
//  Website:         https://zerobrokerageclub.com/
//------------------------------------------------------

_SECTION_BEGIN("Stop Loss Illustraton");


// Define inputs
SetTradeDelays( 0, 0, 0, 0 ); // No trade delays
SetOption("InitialEquity", 100000); // Set initial equity to 100,000
SetOption("MinShares", 1); // Minimum shares to trade
SetOption("CommissionMode", 0.02); // Commission rate

// Define strategy
Buy = Cross( MACD(), Signal() ); // Buy when MACD crosses above Signal
Sell = Cross( Signal(), MACD() ); // Sell when MACD crosses below Signal

// Define stop loss types
StopLossPercent = 1.0; // Define a stop loss as a percentage of the entry price
StopLossATR = 2.0; // Define a stop loss as a multiple of the Average True Range
StopLossTrailing = 3.0; // Define a trailing stop loss as a multiple of the Average True Range

// Define stop loss conditions
StopLossPercentCondition = BuyPrice * ( 1 - StopLossPercent/100 ); // Define stop loss as a percentage of the entry price
StopLossATRCondition = BuyPrice - StopLossATR * ATR(14); // Define stop loss as a multiple of the Average True Range
StopLossTrailingCondition = HHV( High, 10 ) - StopLossTrailing * ATR(14); // Define trailing stop loss as a multiple of the Average True Range

// Apply stop loss conditions
ApplyStop( stopTypeLoss, stopModePercent, StopLossPercentCondition ); // Apply percentage stop loss
ApplyStop( stopTypeLoss, stopModePoint, StopLossATRCondition ); // Apply ATR stop loss
ApplyStop( stopTypeNBar, stopModePoint, StopLossTrailingCondition ); // Apply trailing stop loss
				
			

Decoding the AFL

This code, crafted in Amibroker AFL (AmiBroker Formula Language), is your key to custom trading strategies and indicators. Think of it as your programming wand in the world of Amibroker.

It kicks off with some basic setups for your strategy. The SetTradeDelays function tells Amibroker, “Let’s not delay, execute trades right away!” Meanwhile, SetOption functions are like setting the stage – your starting capital, how many shares to trade, and what’s the cut for the broker.

Then, we get into the crux of the strategy – Buy and Sell decisions based on the MACD indicator’s dance with its signal line.

Next up, we’ve got three stop loss superstars – percentage-based, ATR (Average True Range) based, and the crowd-favorite trailing stop loss. It’s like having different safety nets depending on how you like to play the game.

Last but not least, the ApplyStop function steps in to bring these stop loss strategies to life in your trades. It’s the final touch, ensuring your trades know when to hold on and when to let go.

In a nutshell, this AFL code is your toolkit for blending different stop loss strategies into your Amibroker escapades, making your trading journey not just about profits, but also about smart risk management.

Also Read: Trailing Stop Loss – All you wanted to know about it

Leave a Reply