In the last article, we learned some basic concepts and common sense of commodity futures trading. In this paper, we continue to study the basic design of programmatic trading strategy for commodity futures. The CTP protocol is used as an example.

Prerequisite for all operations – connect to the futures company’s front machine

One important concept we’ve learned is that our trading programs are not directly connected to commodity futures exchanges. However, our trading program is connected to the front-end server of the futures company. When our strategy program triggers trading, a series of operations, such as placing orders, withdrawing orders, querying account assets, and obtaining quotations, interact with the front-end server of the futures company. Then some students may ask: “Our trading program under the order submitted to the futures company front-machine server, then how finally to the exchange? After all, exchanges are where the deals are ultimately made.”

Our trading strategy program interacts with the futures company’s front desk. As for how the order and withdrawal request finally reaches the exchange, that is the business scope of the futures company’s front desk and the exchange, which can be ignored. Of course, this is just a brief description. The actual situation is like CTP protocol, when the strategy program and the futures company front machine establish a connection, the futures company front machine will actively push prices and some messages. We are not going to study the CTP protocol much, but only here.

The first concept to keep in mind when designing a commodity futures trading strategy on FMZ is “make sure you connect to the futures company’s front desk”. All operations in the strategy program must be based on a successful connection to the futures company’s front machine. So there is the basic framework for commodity futures strategies (using JavaScript as an example) described in the FMZ API documentation:

function main(){
    while(true) {// Exchange.IO("status") returns true
        if(exchange.IO("status")){
            exchange.SetContractType("MA000")
            var ticker = exchange.GetTicker()
            Log("MA000 ticker:", ticker)
            LogStatus(_D(), "CTP already connected!")}else {
            LogStatus(_D(), "CTP not connected!")}}}Copy the code

Some of you might just look at the code and feel like your head is blowing up. Fear not, this article will take you through the code.

Function first let’s talk about the word function. This word is a JavaScript keyword. A keyword is a name defined in the programming language that is used for only one purpose. Function is used to create a function. So the cute new student asks, “What is a function?” . Basically, a function is you give it some parameters, it gives you some data back, or it does some processing, or it performs some logic.

So function is used in JavaScript to create a function. And then after function, there’s another word: main. This main is not a keyword, just a name that means “main.” Function followed by main means that we’ve created a function called main. If you’re creating a function that needs to pass arguments, you can write formal arguments inside the parentheses. We won’t go into that here, but we’ll just know. The function we created called main has no arguments, so the inside of () is empty. Looking further back, the red marks in the image below:

Another pair of symbols, but here is a pair of {} braces. We call the curly braces and the code inside them the body of the function, that’s what you want the function to do, but we won’t go into that. Now that we’ve learned how to create a function, let’s get our hands on it and use the FMZ backtest system to easily learn the JavaScript language.

Use a backtest system to test and learn

After logging into FMZ, click control Center, then Click Policy Library, then click New Policy button. Then set the language to JavaScript, as shown below:

When creating a policy, the default code given is:

function main() {
    return exchange.GetName();
}
Copy the code

The default code is a main function that executes a single line of code: return exchange.getName (); Let’s not get into that. (By the way, remember where we started? Commodity futures must be determined to connect to the front-end server) we clear the code edit area.

To practice creating a function, we type in the code edit area:

function main() {

}
Copy the code

So we’ve created a function in the policy called main that we can run.

Click the simulated backtest to switch to the backtest page and use the backtest system to test.

  • 1. Set the exchange object for backtest toCommodity futures.
  • 2. Add the set exchange object.
  • 3. The display is added.
  • 4. Click the back test button to test the policy code.

Found nothing moving!! Because we didn’t write anything in the function body! If we write in the body of main an output statement Log(” Hello FMZ! ), for example:

Function main() {Log(" hello FMZ! )}Copy the code

Then click the simulated test back button again to run, you can see the log output.

And what is the Log of this? This statement is used to print log information. Detailed usage will be explained later.

The main function of main

We created a strategy through practice. A function named main is created in the policy. Some of you might ask what if I created a function that wasn’t called Main? Will the policy still run when testing back?

The answer must be: no. On FMZ, the policy must be executed from the main function as an entry. If you do not create a main function in your policy, you will get an error. So we learned again that a policy must have a main function.

In the function bodywhilestatements

While is another programming language keyword to learn. This keyword means to execute a loop. The while keyword is followed by a parenthesis (). The expression inside the parenthesis is a condition for whether the while loop can be executed. Here you can see the parentheses () reads a true, true is a Boolean value and true value, which means “this whether can perform condition” is true, the cycle in the absence of other jump out of the loop statement, would have been performed (if it is in the loop body, there is no other jump out of the loop statement, The loop is called an infinite loop because the body of the loop is executed over and over again. Immediately after the condition of the () loop in the while statement is the body of the loop, which is surrounded by {} curly braces.

The term “expression” is simply a combination of variables, operators, etc., which has a value. It can be a combination of various conditions and calculations. When used as a condition of a cycle, the value of this expression determines whether the cycle is executed or not. It will be encountered in the future programming learning of quantitative trading strategy. The lines of code inside the curly braces are the lines of code to be executed each time through the loop.

If (conditional expression) {code executed when triggered} else {code executed when triggered}statements

After the while loop, let’s look at another keyword in the body of the loop: if… The else. The if statement can be used alone or in conjunction with if else or else.

Let’s look at the use of if. The if statement means exactly what the word literally means: “if.” If followed by a parenthesis () we’re all familiar with this, but we also need to write an expression inside the parenthesis. That’s okay. Let’s do it. Using what we have learned above, it is easy to write a main function that can be used to test learning the if statement in a backtest system.

Function main() {if (1) {Log("1 ")} if (0) {Log("0 ")}}Copy the code

You can see that the value 1 can also be used as an expression, which has a true value. So in the figure above, the code executing the if condition is triggered by Log(“1 is true, so the condition is triggered “). As you can see, this line of code is also wrapped in {}. In Javascript, {} is wrapped in a block of code, which can be one line of code or N lines of code.

In the test, the first if determines that since the condition is true (the value 1 is always true), the code in the code block carried by the if statement is executed. The second if determines that because the condition is false (0 is always false), the code in the code block carried by the if statement, {}, is not executed.

So if statement, what about else statement? What’s it for? Else is used in conjunction with if. In combination, if conditions are not triggered and the code block following the else is executed. Consider this example:

Function main() {if (0) {Log("0 = false >_< ")} else {Log("0 = false >_< ")} else {Log(" Have a look at the code ( ̄ ii  ̄;) Suction ( ̄ \"  ̄;) ")}}Copy the code

I’ll stop here and continue in the next chapter.