资源加载中... loading...

2.3 Common API explanations

Author: , Created: 2019-06-25 12:03:15, Updated: 2023-11-13 19:49:04

img

Common API explanations

Summary

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.

What is API?

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.

What is the API in the quantitative trading?

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.

Common M language API

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.

  • OPEN - Get the opening price of the latest K line

Example: AA:=OPEN; Get the opening price of the latest K line and assign the result to AA

  • HIGH - get the highest price of the latest K line

Example: AA:=HIGH; Get the highest price of the latest K line and assign the result to AA

  • LOW - get the lowest price of the latest K line

Example: AA:=LOW; Get the lowest price of the latest K line and assign the result to AA

  • CLOSE - Get the closing price of the latest K line. When the k line is not finished, get the latest price.

Example: AA:=CLOSE; Get the closing price of the latest K line and assign the result to AA

  • VOL - Get the volume of the latest K line

Example: AA:=VOL; Get the volume of the latest K line and assign the result to AA

  • REF(X,N) - refers to the value of X before N cycles.

Example: REF(CLOSE,1); Get the opening price of the pervious K line

  • MA(X,N) - find the simple moving average of X in N cycles

Example: MA(CLOSE, 10); //Get the 10-cycle moving average of the latest K-line

  • CROSSUP(A,B)——Represents that when A passes B from below, it returns 1 (Yes), otherwise it returns 0 (No)

Example: CROSSUP (CLOSE, MA (C, 10)) // closing price up-cross the 10 cycle moving average price

  • CROSSDOWN(A,B)——Represents that when A passes B from above, it returns 1 (Yes), otherwise it returns 0 (No)

Example: CROSSDOWN(CLOSE, MA(C,10)) // closing price down-cross the 10 cycle moving average price

  • BK - buying long open position

Example: CLOSE>MA(CLOSE,5), BK; // Closing price is greater than 5 cycle moving average, open long position

  • SP - sell to close long position

Example: CLOSE<MA(CLOSE,5), SP; // Closing price is less than 5 cycle moving average, sell to close long position

  • SK - sell short open position

Example: CLOSE<MA(CLOSE,5),SK; // Closing price is less than 5 cycle moving average, selling short open position

  • BP - buy to cover to close short position

Example: CLOSE>MA(CLOSE,5), BP; // Closing price is greater than 5 cycle moving average, close short position

  • BPK - close short position and open long positions (reverse open 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.

  • SPK - close long position and open short position (reverse open 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.

  • CLOSEOUT - close all positions, it is recommended to use in the addition and subtraction position operation

Example: CLOSEOUT; close all positions that current holding.

Commonly used JavaScript language API

Before explaining the JavaScript language API, let’s take a look at what the common code structure is and what features it has. This will help you better understand the API. Please see the following example:

var aa = exchange.GetRecords(); // get the K-line data
var bb = exchange.SetContractType("BTC_USDT") // set the trading pair to Bitcoin to USDT

The code shown above:

  • Creating variables in the JavaScript language is often referred to as a “declaration” variable. we use the “var” keyword to declare the variable, the variable name is : “aa”.

  • In the JavaScript language, assign an equal sign, that is, give the value on the right side of “=” to the variable on the left.

  • The code “exchange” is the exchange object. The exchange here refers to the exchange you set up on platform page. This is a fixed format, that is, you must specify the exchange object when you call the API of the JavaScript language.

  • The green code is the API of the JavaScript language. When we call it, it is actually calling the function in the exchange object. Note that the point behind the code is also a fixed format. The function here is the same meaning of the function we learned in middle school. If the function does not need to specify a parameter, it is represented by an empty parenthesis; if the function must pass in a parameter, the parameter is written in parentheses.

After using the case to understand the basic structure of the code, let’s take a look at a few JavaScript language APIs that you will use in the future.

  • SetContractType ( “variety code” ) - set the trading pair, which is the trading variety that you want to trade

Example: exchange.SetContractType(“BTC_USDT”); //Set the trading pair to Bitcoin to USDT

  • GetTicker - Get Tick data

Example: exchange.GetTicker(); //Get Tick data

  • GetRecords - get K line data

Example: exchange.GetRecords(); //Get K line data

  • Buy - buy long

Example: exchange.Buy(5000, 1); //Buy one unit at 5,000

  • Sell ​​- sell short

Example: exchange.Sell (5000, 1); // Sell one unit at 5,000

  • GetAccount - get account information

Example: exchange.GetAccount(); //Get account information

  • GetPosition - Get position information

Example: exchange.GetPosition(); //Get position information

  • SetDirection - set to do long or short order type

example:

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

  • Log - output a message in the log

Example: Log( “hello, World” ); //output " hello world " in the log

  • Sleep - pause the program for a while

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.

To sum up

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.

After-school exercises

1, try to write a M language 5 cycle average line up-cross 10 cycle moving average line sentence.

2, try to use the JavaScript language “GetAccount” to get your account information, and use “Log” to print it to the log.

Next section notice

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.


Related

More