2

How to create an Amibroker AFL DLL Plugin? Step by Step Tutorial

Plugins help extend the functionalities of Amibroker beyond all the good things it can do out of the box. You can build your custom code in C++ or C# programming environment, package it as DLL, and then integrate it with Amibroker using Amibroker Development Kit (ADK). In this article, we’ll learn how to create an Amibroker AFL DLL plugin in a step-by-step fashion.

Amibroker AFL DLL Plugin

In the second part of this article, you’ll learn how to convert any AFL into a DLL.

Before going to the tutorial, let’s understand what is DLL?

Quoting Techopedia, “A dynamic link library (DLL) is a shared program module with ordered code, methods, functions, enums, and structures that may be dynamically called by an executing program during run time. A DLL usually has a file extension ending in .dll “

In simple words, A DLL contains a set of programs with some underlying logic that is packaged as a single executable file. This file can be invoked or integrated with external applications like Amibroker. If you are from the Java world, a JAR file is more or less equivalent to DLL

Also Read: Amibroker Custom Backtester: Step by Step Tutorial

Introducing Dot Net for Amibroker

For this tutorial, and any subsequent tutorials on this topic we’ll be using an SDK called Dot Net for Amibroker.

It is a wrapper on top of the standard Amibroker development kit (ADK) to simplify your development experience. With this tool, you can choose your favorite .NET language! All programmable AmiBroker features can be used from .NET code using C#, VB.NET, VC.NET, F#, or any .NET language. No need to hassle with native C/C++.

The intent is not to stop you from using ADK, but rather to keep your development easier by using a lot of reusable interfaces.

Dot Net for Amibroker saves a lot of development efforts and time, and hence it comes with a price tag. Starts from $59 for a standard license at the time of writing this article. But there is an option to use their free and trial plans as well.

Let’s dig into the prerequisites and detailed steps for creating your first Amibroker AFL DLL plugin.

Prerequisites

  • Download and Install the latest version of Amibroker. Refer to this link.
  • Download and Install the Visual studio community edition here
  • Download and Install .Net for Amibroker from this link. Remember to choose the correct installation package compatible with your Amibroker version

You can refer to the below video to install .Net for Amibroker, it references the old version of Amibroker but its still valid

https://www.youtube.com/watch?v=UtMNLGqWbsU

Apart from the above, you’ll need to have an elementary knowledge of Amibroker AFL as well as visual C# to successfully complete this tutorial

Steps to create an Amibroker AFL DLL Plugin

Please make sure that you finish the prerequisites above before moving on. Otherwise, you won’t be able to proceed.

Follow the below steps to create a fresh Amibroker AFL DLL plugin:

Step 1: Verify if you are able to see a folder named “.Net for Amibroker” inside your Amibroker installation directory.

Amibroker AFL DLL Plugin 1

If you aren’t able to see it, then your .Net for Amibroker installation is not successful, and you may need to redo it.

Note: The actual Amibroker installation path may differ from PC to PC, the above screenshot is just for illustration purposes

Step 2: Launch Microsoft Visual Studio, and then click on ‘Open a Project or Solution’ from the Get Started Menu

Amibroker AFL DLL Plugin 2

Navigate to path “\AmiBroker\.NET for AmiBroker\Samples\Sources\AFL Plug-ins\SamplePlugInCS” and select project file SamplePlugInCS_VS2010.csproj. Click on Open.

Amibroker AFL DLL Plugin 3

We have used the Visual Studio 2022 community edition for creating this tutorial. The menu options may differ for other versions.

Step 3: The sample project would be loaded in the solution explorer menu.

Amibroker AFL DLL Plugin 4

You should be now able to navigate through the different folders inside the sample project

Step 4: To start with, click on Basic Samples.cs from solution explorer and go through the C# code

Amibroker AFL DLL Plugin 5

All the classes in the sample project are inherited from the IndicatorBase class which hides the complexities of AmiBroker’s site interface (ADK) and tries to provide a programming environment that resembles AFL a lot.

Also, it uses AmiBroker.PlugInHost.dll referenced assembly

Step 5: Paste the below function code inside BasicSample class:

[ABMethod]
        public void PriceWithEMA()

        {
            ATArray myFastEma = AFAvg.Ema(Close, 20);

            AFGraph.Plot(myFastEma, "Ema5", Color.DarkBlue, Style.Line);

            AFGraph.PlotOHLC(Open, High, Low, Close, "Close", Color.Red, Style.Candle);
        }

 

This is a simple function that plots a candlestick chart along with a 20 period exponential moving average.

PriceWithEMA is a member function attributed by [ABMethod].

AFAvg, AFGraph, Plot, etc are inbuilt classes present in the Amibroker namespace. They are C# equivalents of similar functions in AFL. You can find documentation of all these functions inside the Help file in the .Net for Amibroker folder.

Step 6: Build the solution. From the top-level menu bar click on Build → Build Solution, or use Ctrl+Shift+B shortcut key.

In the output window, you’ll see a “Build Successful” message, or error messages if any.

Amibroker AFL DLL Plugin 6

The project has a customized post build event to automatically deploy the required files to AmiBroker’s folders. After each build: 

  • It copies the compiled assembly of the project to .NET for AmiBroker’s Assemblies folder (\Amibroker\.NET for AmiBroker\Assemblies\).
  • It copies the AFL script files from the Scripts folder to a project directory in AmiBroker’s Formulas folder 

So for this particular sample project, post build, you’ll see a file named AmiBroker.Samples.SamplePlugInCS.dll in the \Amibroker\.NET for AmiBroker\Assemblies\ folder.

Amibroker AFL DLL Plugin 7

That’s your DLL file which can now be referenced from the AFL code.

Step 7: Close and re-launch Amibroker. Create a new AFL formula and call function PriceWithEMA() that you created in Step 5 above

_SECTION_BEGIN("Price with EMA DLL");

PriceWithEMA();

_SECTION_END();

Amibroker AFL DLL Plugin 8

As you can see, we are calling the C# function from AFL code, and since the function call is colored in blue, AFL recognizes it all fine.

Save this AFL code and load it on a chart. You’ll see a Price with EMA chart as below.

Amibroker AFL DLL Plugin 9


Those were all the steps to create an Amibroker AFL DLL Plugin. This was one of the simplest examples but should get you started.

We would recommend looking at more examples inside the sample project “SamplePlugInCS_VS2010” to become familiar with the advanced functions, classes, and other coding constructs.

As always, please let us know in the comments section if you are stuck somewhere or need any further help.

Related Posts

2 Comments

Leave a Reply

Your email address will not be published.