Understanding Amibroker’s ExRem() and Flip() functions

Have not been posting much because I have been learning more about Amibroker. I re-read Howard Bandy’s book “Quantitative Trading Systems” and also watched some YouTube videos. Here’s a very useful video that I watched on how to scale in and out of a position:

Amibroker

When it comes to Amibroker, I think I have a much better understanding of it now.

Firstly, it is easier to understand Amibroker when you think of it like a spreadsheet. The arrays in Amibroker works similar to the columns in an Excel spreadsheet. However, one major difference between Amibroker and Excel is that it is easy to loop through the array, which is akin to reading the cells row-by-row in an Excel column. I’m not sure if Excel offers an easy way to do this (besides using Visual Basic, I guess), but it is definitely very easy to do this in Amibroker.

Next, it is important to understand that a Buy or Sell statement in AFL does not equate to actual buying and selling. In other words, when the program encounters a Buy statement, it does not executes the buy trade immediately. Rather, it simply changes the value of the Buy array at that index to 1.

Think of it as an Excel spreadsheet with two columns called “Day” and “Buy”. Suppose there is a buy signal on Day 5, Amibroker updates the value of the “Buy” column on Day 5 to 1.

If after the first Buy statement, the program encounters another Buy statement, this new Buy statement updates any previous signal. Suppose this new Buy statement says that we should not buy on Day 5, Amibroker will then update the value of the “Buy” cell on Day 5 to 0.

The ExRem() and Flip() functions

Understanding this has helped me immensely when trying to understand how the ExRem() and Flip() functions work in Amibroker.

Here’s a mockup that I did in Excel to show how the values of the “Buy” column changes (or more accurately, how the values in the Buy array changes) when Amibroker is presented with different Buy statements.

The columns labelled “First Buy Statement” and “Second Buy Statement” on the left show how a second Buy statement updates the values in “Buy” Column.

For instance, on Day 5, the value of the “Buy” column is 1 after the first Buy statement. After the second Buy statement, it changes to 0.

Next, on the right side of the table, we see the effects of ExRem() and Flip().

The Buy = ExRem(Buy, Sell) statement removes all the excessive buy signals between the first buy signal and the subsequent sell signal. It does so by changing the values of the “Buy” column on Days 10 to 12 (i.e., the blue cells) to 0.

In contrast, the Buy = Flip(Buy, Sell) statement does the opposite. It changes all the cells from the first buy signal to the subsequent sell signal to 1.

The highlighted grey cells show the difference between the ExRem() and Flip() functions.

Thoughts

Programming mechanical trading systems and backtesting them excites me a lot more than just practicing chart reading. Chart reading and discretionary trading stress me up.

I’ve failed previously when trying to develop mechanical systems, but back then, resources were scarce. Now, with YouTube and all the free online resources, I should have a better chance. Back then, I never fully understood Amibroker or Metatrader 4. I couldn’t really test the systems correctly. I also did not understand the ins and outs of backtesting, the statistics behind it, and so on. In addition, I was limited to my own brainstorming when it comes to coming up with trading systems to test.

Now, there are so many ‘gurus’ offering free trading systems. While most of these systems are likely crap, I can at least get some inspiration from them. There are also a lot of Wiley ebooks available on NLB’s website that I can read and learn from.

Moving forward, I intend to read and learn as much about backtesting and mechanical trading systems as possible. One day, I also plan to revisit the CQF content.

On another note, CNY is 2 days away. Perfect for a brand new start. I will also be going for a knee surgery soon. Sure hope this can help improve my walking and reduce the pain when walking long distances.

All the best and Happy Lunar New Year!!!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *