When it comes to programming, it must be inseparable from the API. For many non-programmer people, what is the API? In this section we will use the easiest language to explain what is the API, and introduce the APIs commonly used in the FMZ Quant tools.
If you google it, you will get the following results: API (Application Programming Interface) is a predefined function to provide applications and developers with the ability to access a set of routines based on a piece of software or hardware. Without having to access the source code, or understand the details of the internal working mechanism.
In fact, in daily life, we have a lot of similar API scenarios, such as: you go to a restaurant to eat, just look at the menu to order, you do not need to know how to make it. The menu name in the menu is the specific API, and the menu is the API document.
For example, If you need to get the opening price of the current variety today, you don’t need to know exactly how to get it. You only need to write “OPEN” in the code editor, just use it directly, “OPEN” is the API for the opening price in the M language.
Before we talk about the M language API, let’s first look at the common code structure and what features it has. This will help you better understand the API. Please see the following example:
AA:=OPEN; //Get the opening price of the latest k line and assign the result to the variable AA BB:=MA(ClOSE,10); //Get the 10-cycle moving average of the latest K line and assign the result to the variable BB
The code shown above:
AA is a variable, and the variable is a element that can be changed, just like the algebra of our junior high school. If the opening price is assigned to AA, then AA is the opening price; if the highest price is assigned to AA, then AA is the highest price. AA is just a custom name, you can also define it as BB or something else.
“:=” is the meaning of the assignment, that is, the value on the right side of “:=” is given to the variable on the left.
“OPEN” and “MA” are API of the M language. Note that the OPEN in the first line is the API for obtaining the closing price. It can be used directly. The MA in the second line is the API for getting the moving average. It needs to be passed by 2 parameters, which means you need to tell the FMZ Quant trading tool, what kind of moving average you need: If you want to get the 50-period moving average calculated at the opening price, you can write: MA (OPEN, 50); please make sure there is a comma between the parameters.
The yellow “//” is a comment character, and the English behind it is the comment content. These are all viewed by programmers to indicate what the line of code means. The comment will not processed at all while the program starts to run. Note that before the comment character, each line of code must have an semicolon as the end of the line.
With the basic code structure awareness, we will bring you some common grammar of M languages below, and we will use them in the future.
Example: AA:=OPEN; Get the opening price of the latest K line and assign the result to AA
Example: AA:=HIGH; Get the highest price of the latest K line and assign the result to AA
Example: AA:=LOW; Get the lowest price of the latest K line and assign the result to AA
Example: AA:=CLOSE; Get the closing price of the latest K line and assign the result to AA
Example: AA:=VOL; Get the volume of the latest K line and assign the result to AA
Example: REF(CLOSE,1); Get the opening price of the pervious K line
Example: MA(CLOSE, 10); //Get the 10-cycle moving average of the latest K-line
Example: CROSSUP (CLOSE, MA (C, 10)) // closing price up-cross the 10 cycle moving average price
Example: CROSSDOWN(CLOSE, MA(C,10)) // closing price down-cross the 10 cycle moving average price
Example: CLOSE>MA(CLOSE,5), BK; // Closing price is greater than 5 cycle moving average, open long position
Example: CLOSE<MA(CLOSE,5), SP; // Closing price is less than 5 cycle moving average, sell to close long position
Example: CLOSE<MA(CLOSE,5),SK; // Closing price is less than 5 cycle moving average, selling short open position
Example: CLOSE>MA(CLOSE,5), BP; // Closing price is greater than 5 cycle moving average, close short position
Example: CLOSE>MA(CLOSE,5), BPK; // when the closing price is greater than the 5-period moving average, close the short position and open the long position.
Example: CLOSE<MA(CLOSE,5), SPK; // when the closing price is less than the 5-period moving average, close the long position and open the short position.
Example: CLOSEOUT; close all positions that current holding.
var aa = exchange.GetRecords(); // get the K-line data var bb = exchange.SetContractType("This_week") // set the trading pair to weekly K-line
The code shown above:
Example: exchange.SetContractType(“this_week”); //Set the trading pair to weekly K-line
Example: exchange.GetTicker(); //Get Tick data
Example: exchange.GetRecords(); //Get K line data
Example: exchange.Buy(5000, 1); //Buy one unit at 5,000
Example: exchange.Sell (5000, 1); // Sell one unit at 5,000
Example: exchange.GetAccount(); //Get account information
Example: exchange.GetPosition(); //Get position information
exchange.SetDirection(“buy”); //Set the order type to open long positions
exchange.SetDirection(“closebuy”); //Set the order type to sell to close long positions
exchange.SetDirection(“sell”); //Set the order type to open short positions
exchange.SetDirection(“closesell”); //Set the order type to buy to cover to close short position
Example: Log( “hello, World” ); //output " hello world " in the log
Example: Sleep (1000); / / make the program pause for 1 second (1000 milliseconds equals 1 second)
Some people may have doubts, so many APIs above, how can i memorize it all? actually, you don’t need to remember any one of them, FMZ Quant official website has a detailed API documentation. Just like looking up a dictionary, anytime you use it, you only need check it through our documentation. Don’t be scared by the coding part. What we want is to organize our own strategies through these languages. Please remember that technology is never a threshold for quantitative trading. The excellent trading strategy is the most important of all.
The above is the most commonly used API in quantitative trading, basically including: obtaining data, calculating data, placing orders. These steps should be enough to deal with a simple quantitative trading strategy. futhermore, if you want to write a more complex strategy, you need go to the FMZ Quant website to get it.
1, try to write a M language 5 cycle average line up-cross 10 cycle moving average line sentence.
Programming is like assembling LEGO bricks. The API is like the various parts of a building block. The programming process is to make each Lego part a complete toy. In the next section, I will lead you to assemble a complete quantitative trading strategy using the M language API.