Summary

C++ is a very difficult programming language. The hard part mainly is to learn in depth, but if you just write strategy logic by C++, it won't need much deep knowledge, as long as it is not a very complicated strategy. Learning some the basics will be enough.

Why should we choose C++ for quantitative trading?

For some people using C++ doesn't mean it is suitable for everyone to use it. the reason is, programming language is just a tool. In quantitative trading, C++ is not the "must-required". If you prefer to use the script programming language, such as Python or C++, you can save a lot of time and energy , which can be used for focusing on the strategy design.

But among quantitative investment institutions, most of the underlying quantitative trading system software was wrote by C++, because its unique language specificity makes it more efficient and faster than other languages ​​in some aspects, especially in numerical calculations. This also means that C++ is more suitable for financial derivatives and high-frequency trading. So, if you want to use a faster programming language, it must be the the C++.

Complete Strategy

In order to help everyone understand the content of this section more quickly, before introducing the C++ language, let's look at the strategy that wrote by the C++, so that you have a preliminary understanding of the noun concept in this section. Let's take the simplest MACD strategy as an example:

We all know that the MACD has two curves, namely the fast line and slow line, let's design a trading logic based on these two lins. When the fast line up cross the slow line, open long position; when the fast line down cross the slow line, open short position. The complete strategy logic are:

  • Long position open: If there is currently no position, and the fast line is greater than the slow line.
  • Short position open: If there is currently no position, and the fast line is less than the slow line.
  • close Long position: If currently holding long position, and the fast line is less than the slow line.
  • close Short position: If current holding short position, and the fast line is greater than the slow line.

Using the C++ language to write the above strategy logic, will be like :

double position = 0;  //Position status parameter, the default position is 0
uint64_t lastSignalTime = 0; // Last signal trigger time

bool onTick(string symbol) { // onTick function, inside the function is the strategy logic
    auto ct = exchange.SetContractType(symbol); // set the trading variety
    if (ct == false) { // if setting the trading variety is not successful 
        return false; // return false
    }
    auto r = exchange.GetRecords(); // get the k line array
    if (!r.Valid || r.sizeO < 20) { // if get the k line array is not successful or the number of k line is less than 20 
        return false; // return false
    }
    auto signalTime = r[r.size() - 2].Time; // get the previous k line time
    if (signalTime <= lastSignalTime) { // if the previous k line time is less than or equal to the last trigger signal time
        return false; // return false
    }
    auto macd = TA.MACD(r); // calculate the MACD indicator
    auto slow = macd[0][macd[0].size() - 2]; // get the previous k line MACD value 
    auto fast = macd[l][macd[l].size() - 2]; // get the previous k line MACD average value
    string action; // define a string variable action
    if (fast >= slow && position <= 0) { // if the previous k line macd value is greater than or equal to the previous k line macd average value, and there are no long position holding
        action = "buy"; // assign buy to the variable action
    } else if (fast <= slow && position >= 0) { // if the previous k line macd value is less than or equal to the previous k line macd average value, and there are no short position holding
        action = "sell"; // assign sell to the variable action
    }
    if (actton.size() > 0) { // If there are orders for placing order
        If (position != 0) { // If there are holding position
            ext::Trade("cover", symbol); // call the C++ trading class library and close all position
        }
        position = ext::Trade(action, symbol, 1); // call the C++ trading class library, placing orders according the direction of variable "action", and renew the position status
        lastSignalTime = signalTime; // reset the time of last trigger signal
    }
    return true; // return true
}

void main() { // program starts from here
    while (true) { // enter the loop
        if (exchange.IO("status") == 0) { // if the connection with the exchange is not successful
            Sleep(1000); // pause for 1 second
            continue; // skip this loop, continue to the next loop
        }
        if (!onTtck("this_week")) { // if the connection is ok, enter the if loop and start to execute the onTick function
            Sleep(1000); // pause for 1 second
        }
    }
}

The code above is a complete quantitative trading strategy written in C++. It can be apply in the real market and will automatically placing orders. In terms of code size, it is more complicated than other languages. Because the C++ language in mainly for high-frequency strategy development on FMZ Quant platform.

Although the coding part is a bit more than before, so many unnecessary trading class libraries have been reduced already, and most of the underlying system level processing is packaged by FMZ Quant platform.

For beginners, the design process of the whole strategy remains unchanged: setting the market variety, obtaining K-line data, obtaining position information, calculating trading logic, and placing orders.

Identifier

The identifier is also the name. The variables and function names in C++ are case-sensitive, which means that the variable name test and the variable name Test are two different variables. The first character of the identifier must be a letter, an underscore "_", and the following characters can also be numbers, as shown in the following:

mohd    zara    abc    move_name    a_123
myname50    _temp    j    a23b9    retVal

Comment

Comments include single-line comments and block-level comments. Single-line comments begin with two slashes, beginning with a slash and an asterisk ( /* ), ending with an asterisk and a slash ( */ ), as shown in the following:

// this is a single-line comment

/*
 * this is a multiple-line comment
 * block-level comments
 *
 */

Semicolon & Statement Block

In C++, the semicolon is the statement terminator. That is, each statement must end with a semicolon. It indicates the end of a logical entity. For example, here are three different statements:

x = y;
y = y + 1;
add(x, y);

Variable

A variable is a operational storage area. To define a variable in C++, you must first define the type of the variable. In the development of quantitative trading strategies, we commonly using types are: integer ( int ), float ( double ), string ( string ) and automatic derivation type ( auto ).

Integers can be understood as integer numbers; floating point types can be understood as numbers with decimal points; strings are literals, can be English or other language characters.

Sometimes when we call an API , but we don't know if this API will Give us what type of data to return, so using the automatic derivation type ( auto ) will help us automatically determine the data type. As shown below:

int numbers = 10; // use int to define a integer variable and assign 10 to this variable
double PI = 3.14159; // use double to define a float variable and assign 10 to this variable
string name = "FMZ Quant"; // use string to define a string variable and assign "FMZ Quant" to this variable
auto bar = exchange.GetRecords(); // use auto to define a variable (automatic derivation type) and assign k line array to this variable

Array

An array is a container for storing data. A C++ array can store a fixed-order collection of elements of the same type with a fixed size. So in C++ , to declare an array, you need to specify the type of element and the number of elements. All arrays have an index of 0 as their first element. To get the first data in the array is " [0] ", the second data is " [1] ", and so on, as follows shows:

// define a array, array name is balance. there are 5 floating(double) type data inside it
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

double salary = balance[0];  // get the first data in the array, the result is : 1000.0
double salary = balance[1];  // get the second data in the array, the result is : 2.0

Function

A function is a set of statements that execute a task together. The declaration of a function includes: the name of the function, the type of the return, and the parameters. The return type is the data type returned when the function is run when I call this function; the parameter is optional, and the function can also contain no parameters. When the function is called, you can also pass a parameter to the function. Look at the following example:

// create a function called "max"
// when this function is called, it returns the int type data
// this function has 2 parameters, and they both are int type
// this function is for passing 2 int type numbers, and return the bigger one

int max(int num1, int num2) {
    int result; // define a int variable result
    if (num1 > num2) // if the num1 > num2
        result = num1; // assign the value of num1 to result
    else // otherwise
        result = num2; // assign the value of num2 to result
    return result; // return the value of result
}

Operator

Using C++ to write quantitative trading strategies, there are three commonly used operators: arithmetic operators, relational operators, logical operators, and assignment operators. The arithmetic operator is the mathematical operation of adding, subtracting, multiplying, and dividing. The relational operator can compare whether the two values ​​are smaller or bigger. The logical operators mainly include: logical AND, logical OR, and logical non. The assignment operator is the variable assignment we talked about earlier. As shown below:

int main() {
    // arithmetic operator
    int a = 10;
    int b = 5;
    a + b; // the result is 15
    a - b; // the result is 5
    a * b; // the result is 50
    a / b; // the result is 2
    a % b; // the result is 0
    a++; // the result is 11
    a--; // the result is 9

    // relational operators
    a == b; // the result is false
    a != b; // the result is true
    a >= b; // the result is true
    a <= b; // the result is false

    logical operators
    true && true // the result is true
    true && false // the result is false
    false || false // the result is false
    true || false // the result is true
    !(true) // the result is false
    !(false) // the result is true
    return 0;
}

Priority

If there is a 100*(10-1)/(10+5) expression, which step is the program first calculated? Middle school mathematics tells us: If it is the same level of operation, it is generally calculated from left to right; If there are additions and subtractions, and multiplication and division, first calculate the multiplication and division, then add and subtract; If there are brackets , first calculate the inside of the brackets; If the law of operation is met, the calculation law can be used for the calculation. the same above principle for C++, as shown below:

auto num = 100*(10-1)/(10+5); // the value of num is 60
1 > 2 && (2 > 3 || 3 < 5); // the result is : false
1 > 2 && 2 > 3 || 3 < 5; // the result is : true

Conditional Statements

Usually when we writing code, we always need to perform different actions for different decisions. we can use conditional statements in our code to accomplish this task. In C++, we can use the following conditional statements:

  • If statement - Use this statement to execute code only if the specified condition is true
  • If...else statement - execute code if the specified condition is true, the other code executed when the condition is false
  • If...else if....else statement - use this statement to select one of multiple code blocks to execute
  • Switch statement - Use this statement to select one of multiple code blocks to execute

If statement

This statement executes the code only if the specified condition is true. Please use a lowercase if. Using a capital letter ( IF ) will generate a C++ error! As shown below:

// grammar
if (condition) {
    //execute code only if the condition is true
}
 
//example
if (time<20) { // if current time is less than 20:00
    x = "Good day"; // when the time is less that 20:00, assign the "good day" to x
}

If...else statement

execute code if the specified condition is true, the other code executed when the condition is false, as shown below:

//grammar
if (condition) {
    // execute code if the condition is true
} else {
    // the other code executed when the condition is false
}
 
//example
if (time<20) { // if current time is less than 20:00
    x = "Good day"; // when the time is less that 20:00, assign the "good day" to x
} else { // otherwise
    x = "Good evening"; // assign the "Good evening" to x
}

Switch Statement

Use this statement to select one of multiple code blocks to execute

switch (condition)
{
    case 1: // code to be executed if condition = 1;
        break;
    case 2: // code to be executed if condition = 2;
        break;
    default: // code to be executed if condition doesn't match any cases
}

For loop

The For loop can execute N times of code blocks repeatedly, and its execution flow is like this (as shown below):

for (int a = 10; a < 20; a++){
    // code block
}
  • Step 1 : Execute int a = 0 and only execute once. Its purpose is to declare an integer variable and initialize it to 0 to control the for loop.
  • Step 2 : Execute a<20. If it is true, execute the code block of line 2 .
  • Step 3: Execute a++, after execute a++, a becomes 11.
  • Step 4 : Execute a<20 again, and the second, third, and fourth steps will execute repeatedly. Until a<20 is false, if it is false, the code block of line 2 will not executed, and the whole for loop is finished.

While Loop

We all know that the market is constantly changing. If you want to get the latest K-line array, you have to constantly run the same code over and over again. Then use the while loop is the best choice. As long as the specified condition is true, the loop will continue to get the latest k-line array data.

void main() {
    auto ct = exchange.SetContractType(symbol); //set the trading variety
    while(true) {
        auto r = exchange.GetRecords(); // constantly getting k-line arrays
    }
} 

Break Statement

Loops have preconditions. Only when this precondition is " true ", the loop will start doing something repeatedly, until the precondition is " false ", the loop will end. But using the break statement can jump out of the loop immediately during the execution of the loop;

# including <iostream>
using namespace std;
int main() {
    for(int a = 0; a < 5; a++) {
        if(a == 2) break;
        cout << a << endl;
    }
    return 0;
}

// print out : 0, 1

Continue Statement

The continue statement also jumps out of the loop, but it doesn't jump out of the whole loop. Instead, interrupt a loop and continue to the next loop. As shown in the below, when a is equal to 2 , the loop is interrupted, and the next loop is continued until the precondition of the loop is " false " to jump out of the whole loop.

# including <iostream>
using namespace std;
int main() {
    for(int a = 0; a < 5; a++) {
        if(a == 2) continue;
        cout << a << endl;
    }
    return 0;
}

// print out : 0, 1, 3, 4

Return Statement

The return statement terminates the execution of the function and returns the value of the function. The return statement can only appear in the body of the function, and any other place in the code will cause a syntax error!

# including <iostream>
using namespace std;

int add(int num1, int num2) {
    return num1 + num2;  // The add function returns the sum of two parameters
}
 
int main()
{
    cout << add(5, 10); // call the add function, and print out the result:50
    return 0;
}

Strategy Architecture

On the FMZ Quant platform, it would be very convenient to write a strategy by C++. The FMZ Quant have a lots of officially built-in standard strategy frameworks and trading class libraries, such as follows:

bool onTick() { //onTick function
    // strategy logic
}

void main() { // program starts from here
    while (true) { // enter the loop
        if (exchange.IO("status") == 0) { // if the exchange connection is not stable
            sleep(1000); // pause for 1 second
            continue; // skip this loop, enter the next loop
        }
        if (!onTick()) { // if the exchange connection is stable, enter this if statement, start to execute the onTick function
            sleep(1000);// pause for 1 second
        }
    }
}

As shown the above, this is a standard strategy framework, and these formats are fixed. Use the framework to write a strategy. You only need to write the strategy logic from the second line. Other market acquisition and order processing are handled by the framework and the trading class libraries, you just need focus on strategy development.

To sum up

The above is the content of the C++ language quick start. If you need to write more complex strategy, please refer to the FMZ Quant platform C++ language API documentation, or directly consult the official customer service about the writing service

Next section notice

The key to quantitative trading is the trading strategy, not trading tools (programming language). In the next section, let's write a feasible C++ trading strategy. Still using the simplest technical analysis indicators as an example.

After-school Exercises

  • Try to get historical K-line data using the C++ language on the FMZ Quant platform.
  • Try to write down the strategy code at the beginning of this section and followed by specified comments.

Leave a Reply

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