2

How to Convert an AFL to DLL? Step by Step Tutorial

For advanced Amibroker developers, it’s a common need to convert their AFL to DLL, so that they can distribute their proprietary indicators or trading systems without exposing the underlying logic. In this article, we’ll learn how to convert an AFL to DLL using Dot Net for Amibroker SDK.

Please refer to Part 1 of this article where we have explained in detail about DLL (dynamic link libraries), Dot net for Amibroker, and some of the benefits of converting your AFL into DLL code.

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

Also Read: Develop and Backtest Options Trading system in Amibroker

Steps to convert an AFL to DLL

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 convert AFL to DLL:

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

AFL to DLL 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

AFL to DLL 2

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

AFL to DLL 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.

AFL to DLL 4

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

Step 4: From your windows explorer, navigate to path ”\AmiBroker\.NET for AmiBroker\Tools” and launch the application “AflXCompilerx64.exe”.

Note: You’ll need a licensed version of .Net for Amibroker to efficiently use this application.

Step 5: When the application is launched click on File → Open AFL File, and then select the AFL file that you wish to convert into DLL.

AFL to DLL 5

We’ve chosen a simple AFL code that plots price as a candlestick chart and overlays a Simple moving average line on top of it

_SECTION_BEGIN("Price with Simple Moving Average");

Plot( Close, "Price", colorWhite, styleCandle );

SimpleMovAvg = MA(C,20);

Plot (SimpleMovAvg, "Simple Moving Average", colorRed, styleLine);

_SECTION_END();

Step 6: Once the AFL code is loaded in the AFLXComplier application, click on File → Compile AFL to C#, you will see the generated C# code in the right-hand pane. Errors (if any) will appear in the bottom pane.

AFL to DLL 6

Note: The conversion process is not 100% accurate, you may still need to review the generated C# code for any inaccuracies, hence elementary knowledge of C# is a must.

Step 7: Copy the C# code (requires developer edition license), and paste it into BasicSamples.cs class in the SamplePlugInCS_VS2010 project that you opened in Step 2.

Make sure you only copy the function definition and not the entire class, as we’ll be adding the function in the existing BasicSamples class.

Also, change the function name to a more readable format. Below is the function definition we copied from the generated C# code to the BasicSamples class

[ABMethod]

        public void PricewithSMA()

        {

            ATArray simpleMovAvg;

            AFMisc.SectionBegin("Price with Simple Moving Average");

            AFGraph.Plot(Close, "Price", (float)Color.White, (float)Style.Candle);

            simpleMovAvg = AFAvg.Ma(Close, 20f);

            AFGraph.Plot(simpleMovAvg, "Simple Moving Average",(float)Color.Red, (float) Style.Line) ;

            AFMisc.SectionEnd();

        }

Step 8: 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.

AFL to DLL 7

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.

AFL to DLL 8

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

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

_SECTION_BEGIN("Price with SMA DLL");

PricewithSMA();

_SECTION_END();

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 SMA chart as below.

AFL to DLL 10

If you noticed, multiple lines of code in C# have been encapsulated in one function call within AFL. And you won’t be able to know the underlying logic behind that function call unless you have access to the C# code.

So, if you want to distribute your AFL code to a wider audience without compromising on the code logic, you can do it through this process.

What’s next after Converting AFL to DLL?

Once you have converted your AFL code into a DLL plugin, it can be protected and licensed by commercial protection tools like IntelliLock, CliSecure Licensing, .NET Licensing Pro, etc. The protected plugins can be made public and only customers who were given licenses for their machines can use the protected .NET Plug-Ins.

We’ll come up with a tutorial in the future detailing all the steps to protect and license your DLL plugins.

Also, we’ll look at more complex examples of AFL to DLL conversion.

Until then, if you have any questions please feel free to put them in the comments section

Related Posts

2 Comments

  1. very excellent and usefull article
    waiting for article on cial protection tools like IntelliLock, CliSecure Licensing, .NET Licensing Pro, etc.
    do you have any class room coaching for these sir
    it will be very use full for advance users

    • Hi Shaik,

      Thanks for your kind words! Unfortunately we do not have any classroom coaching offering right now

Leave a Reply

Your email address will not be published.