AmiBroker will automatically rank all available buy signals on any given bar by their PositionScore and execute the top 10 highest-ranked assets. Smart Parameter Optimization
// --- Backtest Settings --- SetPositionSize(1, spsShares); // 1 share for testing SetOption("InitialEquity", 100000); SetOption("FuturesMode", False); SetOption("PriceBoundChecking", True);
: Search through thousands of symbols simultaneously to find stocks meeting specific quantitative or fundamental criteria.
Always document your logic using comments. Good documentation saves hours of troubleshooting down the road. amibroker afl code
Be careful when using functions like Ref(Close, 1) . Referencing a positive index looks forward in time, making your backtest look highly profitable but impossible to execute in real life.
Ensure the code is robust and efficient by checking the following: AFL Syntax and Logic: Verify that the code follows proper AFL syntax and that logic for is correctly defined. Variable Scope:
// Example: Calculating a custom stop-loss using a loop stop_level = Null; in_trade = False; for( i = 0; i < BarCount; i++ ) if( Buy[i] ) in_trade = True; stop_level[i] = Close[i] * 0.95; // 5% stop loss else if( Sell[i] ) in_trade = False; stop_level[i] = Null; else if( in_trade ) // Maintain the stop level from the previous bar stop_level[i] = stop_level[i-1]; Plot(stop_level, "Stop Loss", colorOrange, styleDots); Use code with caution. 6. Best Practices for Writing AFL Code AmiBroker will automatically rank all available buy signals
Typical Use Cases
AFL is written in optimized C++, allowing it to process millions of data points per second.
// Entry Buy = Cross( C, MA(C, 50) );
Unlike standard procedural languages like C++ or Python, . This means it processes entire arrays of data (such as an entire history of closing prices) in a single operation, making execution incredibly fast. Key Characteristics of AFL:
The feature in AmiBroker allows you to scan thousands of stocks simultaneously to find assets meeting your setup criteria. To convert AFL code into a scanner, utilize the Filter variable and the AddColumn() function. Here is an AFL script to scan for Volume Breakouts :
While vectorized operations are preferred for speed, loops are occasionally necessary. Access individual array elements using [] syntax: Good documentation saves hours of troubleshooting down the
: Right-click on a blank chart and select your saved formula from the Indicators menu to see it in action.