This tutorial’s purpose is to educate you about the Amibroker and Amibroker Data feed. This is not an exhaustive Amibroker lesson, but it does cover all of the things that I feel are most important for trading.
Amiprofits provides an Amibroker Data Feed that has an uptime of 99.5% with a library of over 1500 symbols for stock, futures, commodities, indices, currencies, and options.
Get 500+ Amibroker AFLs for intraday and positional trading, as well as our Amibroker Data Feeder, for free from Amiprofits.
We at Amiprofits provide an Amibroker data feed at a reasonable price. For further information, please visit our website Amiprofits.
Amibroker And Amibroker Data Feed Tutorial – Tips And Tricks
Simple Method for Creating Amibroker AFL:
Starting with a spreadsheet is a good way to get started with Amibroker AFL code. Lay up all of the data in rows to get a sense of how Amibroker<span< a=””> style=”font-weight: 400;”> looks at the data and calculates it.</span<>
For example, in Excel, create 10 columns with 6 rows each: open, high, low, close, volume, and open interest. Additional rows can be added beneath this to contain your computations.
Often, simple math will suffice to perform the necessary computations. Always use the Amibroker live data function reference, which has a wealth of pre-built functions to help you get things done faster.
Amibroker data feed allows you to do a wide range of complicated computations, such as standard deviation. With only one line of code, you can calculate deviation, moving average, MACD, and RSI.
When you’ve mastered Amibroker, you may progress to more complex features such as the custom backtester, which gives you complete control over the Amibroker data feed backtest engine.
For the most part, you may accomplish the majority of your objectives without delving too far. The beauty of arrays and built-in functions is that complex formulae may be expressed in only a few lines of code.
When learning Amibroker, one of the most essential things to understand is that there is always more than one way to accomplish something. So don’t be overly concerned with finding the ideal answer.
Determining what you require on a sheet of paper and working backwards is sometimes the best approach. To work through your steps, you can utilize a spreadsheet.
Examples of Amibroker’s AFL Code:
Before we continue, here are a few formulae and functions to keep in mind:
Min() and Max() are functions that may be used to find the minimum and maximum value in two arrays.
For example, Min (Closing, Open) returns the least of the close or open price. If it were Max (), it would return the larger of the two arrays.
HHV () and LLV () return the highest or lowest value in an array.
The formula 100DayHigh = HHV (C, 100) yields the greatest closing value over 100 bars. This is how you would begin coding a breakout system.
For example, Purchase = C>Ref(HHV(C,100),-1); instructs Amibroker to buy when the close is greater than the highest close in the previous 100 bars.
StDev() calculates a moving standard deviation of a collection
Sum() may be used to add up all of the values over the last n bars.
For example, Sum(H, 10), for example, will sum the last ten highs together. That result can be used in another formula.
Cum () computes the cumulative sum of the values in an array since the array’s inception.
This function may be used to add up events. For example, from the first bar, you may tally the number of times the close has finished higher than the open.
UpCloses = Cum (C > O); this will tell you how many upcloses there have been in the entire data set.
LastValue() returns the last value in an array. If you wish, you can utilize this to instruct the Amibroker data feed to exit a stock on the final bar. For instance, if it is delisted.
Ref () is used to refer to previous or subsequent bars. So, recalling the notion of data put out in a spreadsheet, you may use the ref function to either go back or ahead along each bar.
Ref(Low,-20); returns the low price from 20 bars ago, for example.
It’s normally OK to use the Ref function to look backwards, but while designing a trading strategy, keep in mind the risks of referring to future bars. You don’t want to make rules based on facts that you don’t have.
BarsSince() returns the number of bars that have passed since the array returned true. For instance, if you type BarsSince (RSI (14) > 80), you will get more than 80 bars that have passed since the RSI indicator was activated.
Learn Amibroker Indicator Formulas:
Amibroker has a plethora of pre-built indicators that you may use in your code.
MA() is a basic moving average, thus to create a 50-period moving average of the close, simply type MA() (C,50).
This also implies that you can compute a moving average for whatever array you choose. For example, MA(H, 50) computes a moving average of the previous 50 high prices rather than the closing.
EMA () is the exponential moving average, and it is written in the same manner as the simple moving average.
There’s also the double (DEMA) and triple (TEMA), as well as the weighted moving average (WMA), and a few more. (You may discover which moving average works best by clicking here.)
ATR() returns the average true range, therefore ATR(10) returns the real range average over ten periods
The relative strength index (RSI) is a common momentum oscillator. It is also frequently used in mean reversion methods.
Because BBandTOP () and BBandBot () yield the top and bottom of a bollinger band, if you write Buy = C > BBandTop (20,2), you’ll buy when the close is higher than the top of the bollinger band.
MACD () stands for moving average convergence divergence and is a MACD indicator.
MFI (MFI) is the money flow indicator, which is comparable to RSI but additionally takes volume into account. We’ve done some research on the RSI vs. MFI here.
ROC () returns the rate of change, which is just the percentage change over n bars.
Percentile (%) is a very useful function that can be used in a variety of situations. It can be used to return the array’s rank percentile value. Buy = Close > Percentile (Close, 100, 99) will, for example, deliver a buy signal if the close price is in the top 1% of closes over the past 100 bars.
Common Error
Let’s have a look at some frequent Amibroker errors and how to prevent them in the last segment of this Amibroker data feed lesson.
Errors in Amibroker Coding:
One of the first errors newcomers make while learning Amibroker is misusing the equals symbol “=”. In Amibroker, “=” denotes assignment rather than equality. In other words, you use “=” to assign a value to anything.
If you desire equality, use two equals signs (“==”) to indicate that you’re seeking an exact match.
For example, if you type RSIValue == 70, the RSI value must be exactly 70 in order for the result to be true; otherwise, the result will be false.
It’s not going to work if you write RSIValue = 70. It will just set 70 to the RSIValue variable. That’s probably not what you’re looking for.
Another typical error is to write something like: “Result = IIf (RSI (14 = 70, High, Low); when you have an IIF statement. /INACCURATE
That’s incorrect since an amibroker isn’t searching for an exact match if you simply use one equals sign. It just assigns a value to your variable. What you actually want to accomplish is:
If(RSI(14) == 70, High, Low); / PERFECT
If you wish to return a true value when something is not equal, you must use “!=”.
For example, Result = RSI (14)! = 70 will return true anytime the RSI does not equal 70.
You may also use the NOT function to do this. other logical operators, such as AND and OR, as well. The Amibroker data feed reference handbook has a complete list of operators.
Look Into The Future (Future Leak):
A big blunder to avoid is allowing your system to glimpse into the future. Make certain that you are not using future values in your code that you will not be aware of at the time of trade.
A classic example is when your system trades on the open but depends on an indicator (such as the RSI) that utilizes the closing price. For example, if you write “Buy” = RSI (14) > 80 and use “BuyPrice” = Open with no trade delays, you are committing a future leak mistake. The system buys on the open while the indication is based on the close.
In this case, you might wish to use SetTradeDelays () to postpone your entry until the next bar opens. Looking forward to errors like this can sometimes be sneaky and hard to spot, so caution is needed.
Always remember, if your results look too good to be true, there could be some issue, like a future leak somewhere in the code.
Using Unrealistic Entry and Exit Prices:
Similarly to the future error, you must ensure that your system uses realistic entry and exit pricing. You may encounter a circumstance in which you receive a trade signal but execute an unreasonable price entry. When utilizing limit orders, for example.
Using a limit price like BuyPrice = Open * 0.95 instructs Amibroker to purchase at a 5% discount to the open price. When using adjusted stock data, however, this sort of limit price may present a problem.
Amibroker data feed may occasionally trade at the precise low of the day, leaving very little room for error. If the data is altered, this may be an implausible price fill. It also takes into account your market influence on the bid-ask spread.
That is to say, if your system frequently enters the day’s identical low or high, it’s making incorrect assumptions. It is preferable to change the formula above to something like this.
This adds some more realistic slippage to the entry price to ensure that your entry is filled in real-time trading:
LimitPrice = Open * 0.95;
Slippage = Low < (Limit Price * 0.995);
BuyPrice = LimitPrice AND Slippage;
In this scenario, we’re still assuming a 5% discount from the open. However, we ensure that the day’s low is at least 0.5 percent lower than the limit price.
This guarantees that the backtest enters a realistic price range and gives some margin of safety.
Amibroker may trade on the very same day if we do not utilize something like this low point of the bar.
We’d obtain fantastic backtest results, but they wouldn’t translate to real-world trading.
Using Incorrect Data:
It is critical to understand the data you are utilizing while backtesting or doing analysis in Amibroker data feed.
- Is the data modified to account for stock splits?
- Is it dividend and distribution adjusted?
- Is the data inclusive of previous members?
- How detailed is the data? How accurate is it in terms of your timeframe?
- Is the data for futures back-adjusted or spliced together?
- In the case of forex, does the data reflect the bid, ask, or midpoint? From which broker does the information come?
These are some of the questions to consider when selecting a data supplier and before backtesting.
There will be many price gaps to be aware of if you utilize data that has not been corrected for stock splits.
This may be acceptable if you can trade intraday, but you won’t get reliable backtest results for longer-term strategies.
Data that has not been adjusted for dividends will almost certainly underperform in the backtest.
Adjusted data, on the other hand, may result in incorrect entry signals (since the price is adjusted and not the actual price). It is best to test techniques on both sorts of data.
Delisted stocks and past members must also be included in the data. Stocks are constantly being withdrawn from the market, and backtesting without these securities results in survivorship bias and biased findings.
If the data were only available on a daily basis, evaluating intraday price fills would likely be impossible. Intraday systems often require one minute or less of data to perform reliable simulations.
When using basic information, Be mindful of the numerous changes and accounting problems that might arise after the fact.
For example, the Fed may publish an unemployment rate and then revise it to something else when they have more data. For an accurate backtest, the starting data point, not the correction, is required.
Similarly, a data point may have a published date as well as a reported date. The reported date may be when the incident occurred, whereas the published date is when it became public knowledge.
You must be aware of which one you are dealing with since you cannot trade on information that has not yet entered the public domain.
Not Getting Rid of Delisted Stocks:
Another blunder can arise when an Amibroker data feed has an open transaction in a delisted securities. In most cases, you should sell any stock that has been delisted or amalgamated with another firm.
If you see that your backtest results include a small number of trades and a large number of open positions, check to see if any of them are delisted stocks.
The following code employs BarIndex() to force close positions on the database’s final bar:
bi = BarIndex();
exitLastBar = bi == LastValue( bi );
Sell = exitLastBar;
If you employ one-bar trade delays, you’ll want to trigger the exit one bar ahead of time:
SetTradeDelays (1,1,1,1);
bi = BarIndex();
exitLastBar = bi == LastValue( bi – 1 );
Sell = ExitLastBar;
If you utilize a third-party database, such as Norgate Data, you should pay attention to the delisting date that has already been specified for each delisted security in the database:
OnSecondLastBarOfDelistedSecurity = !IsNull(GetFnData(“DelistingDate”)) AND (BarIndex() == (LastValue(BarIndex()) -1) OR DateTime() >= GetFnData(“DelistingDate”) );
Not Setting Position Size Constraints:
Finally, make certain that whatever method you devise does not purchase an excessive quantity of shares.
If a company has a daily volume of 100,000 shares and you intend to purchase 50,000 shares, your order will have a significant influence on the price and the bid-ask spread when you place your order.
In genuine trading, you’re unlikely to get filled at the price you desire. A backtest, on the other hand, is unaware of this and will fail.
Unless you instruct it differently, it will enter your whole position at the market price.
Set volume limitations so that the backtest only ever purchases a tiny portion of the daily volume. This may be done in the backtest options box.
I normally set this to 2 percent of the average daily volume. This means that my backtest entry cannot be more than 2% of the stock’s daily volume.
Conclusion
Transparency is vital to us, in contrast to other data suppliers. We let you test our software before you buy it. Our Amiprofits LiveFeed trial on Amibroker data feed provides full assistance. When you’ve finished downloading it, install it.
Get 500+ Amibroker AFLs for intraday and positional trading, as well as our Amibroker Data Feed, for free from Amiprofits.
Do you have any queries about installation? We are always here to help you via chat and WhatsApp.