The Structure of a Strategy Program in LUA

Hi all,

I continue investiaging another area of knowledge of humankind, which is FXCM trading Station.

So, if you need to write strategy, you'll need following methods to be defined in your strategy: 

  1. Init()
  2. Prepare(nameOnly)
  3. Update()
  4. ReleaseInstance()
  5. AsyncOperationFinished(cookie, successful, message, message1, message2)
  6. CheckParameters(parameters)
  7. ChangeParameters()

Now little bit details:

The Init() function of the strategy is called once, when the host application has just loaded the strategy into the memory. In this function the strategy must create profile. Profile is the information which is required for creation of the strategy: strategy name and description and parameters.

When the function is being executed, there is a global variable called strategy. You must use the table kept in this variable to provide all necessary information.

Note: Values of any global variables, which are defined in the Init() function will not be available in the other strategies functions. Because the user can apply multiple instance of the same strategy at the same time, the new instance of the strategy is created every time when the user applies the strategy.

Prepare function is called every time when the user applies the strategy. At this moment, a copy of the strategy code is created. This copy is called strategy instance. At one moment, there can be as many strategy instances as many times the strategy is applied.

In this function you must:

  • Prepare all common data for further work of the strategy logic.

When the function is being executed, there is a global variable called instance. You must use this to access to the instrument and parameters data.

Parameter nameOnly indicates whether this function was called just for creating instance's name.

Important: strategy instance is always created in a new context. The Init() function is not called for an instance, so there is no way to pass data from the Init() function to the Prepare() function.

Update is a most important function in the strategy. The update function is called every time when a new bid or ask price appears on the instrument to which the strategy is attached.

Use the terminal table to get access to the trading application functions.

Release function is optional, you can omit it in your strategy.

In case this function exists, it is called before the strategy instance is destroyed. You can use this function to dispose objects created in the Prepare function.

 

Other functions will be described in another post

Lua for FXCM Trade Station

Hello everybody,

today I will share with you adventure of learning writing something for Trade Station from FXCM and some other trading companies.

I got one request from client to write for him expert advisor, and it should be for Trade Station in lua, and not in mql4.

I can't say that I was happy, but customer is sacred think, so I started.

Initially I read the manual and realized that lua is simple language with C like syntax. AFAIK C, I decided to look how to develop EA.

So I created file "matrade.lua" which was empty. 

Then in trade station I made the following:

Pressed at button import and pointed to my file:

and as result got error message

For me it meant following. In some way I need to add function Init to code of my strategy. I digged in the folder of fxcm trade station and found interesting folder which had name Strategies, and there I found file strategies.cab. When I looked inside of it, found interesting list of files, and among them was file macross.lua.

as you can see from screenshot there was function Init(), and I added it to my file and tried to open again. Imagine my surprise, when Trading Station notified me that my strategy was installed. Of course, with only function Init which is empty my strategy is useless, but now I can continue to look into existing code and develop my strategy.