In this post, we’ll go through a Stop Loss Illustration Amibroker AFL Code. The AFL code can be downloaded free of cost and imported into Amibroker.
Understanding Stop Loss and Amibroker ApplyStop() Function
A Stop Loss is an order placed with a broker to sell a security when it reaches a certain price. It is a risk management tool that traders use to limit their losses if a trade goes against them. Stop losses can be implemented in different ways, such as as a percentage of the entry price or a multiple of the Average True Range.
Amibroker is a popular technical analysis software that allows traders to develop and backtest trading strategies. The ApplyStop() function in Amibroker is used to apply stop loss orders to a trading strategy. It can be used to apply different types of stop losses, including regular stop losses and trailing stop losses.
The ApplyStop() function takes three arguments: stopType, stopMode, and stopPrice. The stopType argument specifies the type of stop loss to apply, and can be set to either stopTypeLoss for a regular stop loss or stopTypeTrailing for a trailing stop loss. The stopMode argument specifies the mode of the stop loss, and can be set to stopModePercent for a percentage stop loss or stopModePoint for an ATR or trailing stop loss. The stopPrice argument specifies the stop loss condition to use. Check this documentation link for the detailed description of this function.
By using the ApplyStop() function, traders can implement a stop loss order as part of their trading strategy in Amibroker, helping them manage their risk and protect their capital in case of unexpected market movements.
Also Read: All you wanted to know about Stop Loss Strategies
Stop Loss Illustration Amibroker AFL Code Download
Please download the AFL code from this link. It can be readily imported into Amibroker.
AFL Explanation
The code above is written in Amibroker AFL (AmiBroker Formula Language), a proprietary programming language used to create trading systems and custom indicators within Amibroker software.
The first few lines of the code define some inputs and parameters for the strategy. The SetTradeDelays
function sets the trade execution delay to zero, meaning that trades will be executed immediately when the entry signal is generated. The SetOption
functions set the initial equity, minimum shares to trade, and commission rate.
The strategy itself is defined using the Buy
and Sell
variables, which are set to true
when the MACD indicator crosses above or below its signal line, respectively.
Next, three different types of stop loss are defined: a percentage stop loss, an ATR stop loss, and a trailing stop loss. The stop loss conditions are then defined based on these types, where the percentage stop loss is calculated as a percentage of the entry price, the ATR stop loss is calculated as a multiple of the Average True Range, and the trailing stop loss is calculated as a multiple of the ATR from the highest high of the previous 10 bars.
Finally, the ApplyStop
function is used to apply each of these stop loss conditions to the strategy. The first argument to ApplyStop
specifies the type of stop loss to apply (stopTypeLoss
for a regular stop loss or stopTypeNBar
for a trailing stop loss), the second argument specifies the mode of the stop loss (stopModePercent
for a percentage stop loss or stopModePoint
for an ATR or trailing stop loss), and the third argument specifies the stop loss condition to use.
Overall, this code shows how to implement different types of stop losses in Amibroke
Also Read: Trailing Stop Loss – All you wanted to know about it
Disclaimer
All the AFLs posted in this section are for learning purposes. Trading Tuitions does not necessarily own these and we don’t have any intellectual property rights on them. We might copy useful AFLs from public forums and post them in this section in a presentable format. The intent is not to copy anybody’s work but to share knowledge. If you find any misleading or non-reproducible content then please inform us at support@tradingtuitions.com
Dear Sir,
The AFL Link is Broken. No AFL to Download. Please check and sort it out.
Hi Anil,
Thanks for reporting this. The broken links are restored now
What is the percentage to be applied on Entry price for arriving at the Stop loss
Hi Prasad,
The % depends on your risk capacity and trading system rules
The link is not wokring. Please give correct link.
Corrected the link
Dear Sir,
as of today The AFL Link is Broken. No AFL to Download. Please check and sort it out.
This is fixed. Thanks for reporting!