Cryptocurrency Quantitative Trading for Beginners - Taking You Closer to Cryptocurrency Quantitative (3)
Tags: cryptocurrency, trading, fmz, backtest
Published: 2024-04-26 02:16:30

### Error message

In the previous articles, we have learned that the so-called programmatic and quantitative trading is a script program based on the data obtained from the exchange through a series of calculations, judgments, and triggers to make some operations, and operate the exchange account to trade. These actions of acquiring data and operating accounts are all carried out through the exchange API interface. Simply put, the script program interacts with the exchange. Since it is an interaction, there must be normal interaction and abnormal interaction. When an abnormal interaction occurs, the interface returns the exception message.

Of course, there are all kinds of error prompts and error messages in the programmatic and quantitative trading systems on the market, or in the programs developed by ourselves. These error messages are not limited to the error messages reported by the exchange API interface. There are also program runtime exception errors, configuration errors, program grammar errors and so on.

Error messages on the FMZ Quantitative Trading Platform also fall roughly into several categories:

- Strategy grammar error
  This type of error is the most common, because the beginners is unfamiliar with programming, and there are grammatical errors in the code writing during the learning and testing phase. For example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bv342zbmecavyxwxteye.png)  
  The code has missed characters like parentheses. Such errors can usually be seen on the strategy editing page, and the strategy cannot be run (an error will be reported directly at runtime, as shown in the figure below).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/khe0xfcfpgd1kyfp7nz9.png)
  So after writing the strategy, take a habitual glance on the platform strategy editing page to see if there is a red XX, if so, there must be an obvious error.

- Runtime program exceptions caused by strategy program BUG
  There is a bug in the program. When the program is running, triggering an exception will cause the program to stop abnormally and display such error messages.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6d5qt9qlm9tpymarekyl.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/csvq84yip4o8ca8lkz5i.png)

  Such errors will cause the program to be abnormal and the program will stop running.

- Errors caused by improper configuration and settings
  
  On FMZ platform, the trading pair is uniformly defined in the format of X_Y, where X represents the name of the trading currency, and Y represents the name of the denominated currency (the denominated currency of the futures currency-based contract trading pair is usually expressed in USD, as described in the previous articles), such as BTC_USDT, if I write the trading pair randomly, write it as BTC-USDT.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mpsb0f8otcyqtimrjza.png)

  An error is reported on the FMZ platform backtesting system:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qhku7ydwfwbr9fd3vaz.png)

  Furthermore, a mistake that beginners often encounters:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xnidc2mrr9y0ysh7ki53.png)
  
  This type of error is due to the modification of the password on the FMZ platform account, resulting in the invalidation of the API KEY in the configured exchange object (the user's API KEY is configured on the FMZ platform after being encrypted on the browser), and the strategy is unable to start, thus an error is reported.

- An error is reported in interface call
  
  Interface call errors are often encountered when running a strategy. In previous articles, we learned that the interfaces on the FMZ platform are divided into **interfaces that generate network requests** and **interfaces that do not generate network requests**. The interface error will not cause the strategy program to stop, usually due to interface call exceptions and the wrong data is returned, the strategy does not make fault tolerance, and the program exception error caused by the wrong data causes the program to stop (the concept of fault tolerance mentioned in previous articles).
  
  Here are several interface error messages that generate network requests:
  
  - Network timeout
    
    One of the frequent error messages encountered by beginners is the use of domestic network equipment (their own computer or domestic server). Since most exchanges are blocked, most exchanges in the domestic network are inaccessible, and the access interface will report a timeout (mentioned in previous articles).
    
  - http 429 error
    
    One of the classic error messages is that the exchange interface is called too frequently, exceeding the frequency limit of the exchange (mentioned in previous articles).
    Some beginners may say that I would like to apply for API KEY for more exchanges or I apply for more exchange accounts. We need to know that the frequency of interface access limits by exchanges is usually based on IP addresses. Simply put, as long as all requests sent to an IP address are counted on this IP address, the exchange server will deny access if the request exceeds the limit. 

  - Error reporting on business of the exchange interface
    
    The timeout and 429 mentioned above are network errors. If there is a problem on business of the exchange interface, an error will also be reported. For example, if I want to obtain the spot market price, but I set up a non-existing trading pair. I tested it in the debugging tool of the FMZ platform, the debugging tool is a very convenient testing tool, which is very suitable for real bot testing of function calls and data acquisition.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0kc90ifxrg4pt97foevh.png)
    
    Debugging tool execution results, there is no difference between debugging tool execution and real bot execution.
    
    ```
    Huobi	error	GetTicker: Invalid ticker: {"Info":{"err-code":"invalid-parameter","err-msg":"invalid symbol","status":"error","ts":1620872079355},"High":0,"Low":0,"Sell":0,"Buy":0,"Last":0,"Volume":0,"OpenInterest":0,"Time":0}
    ```
    The error message here means that the trading pair is invalid (as seen here "err-msg":"invalid symbol").
    For example, there are many such business-related errors, such as setting leverage when some exchanges do not support leverage values ​​with decimal parts, at this time, if the leverage value has a decimal part, it will also cause an error in the interface call.
  
  List an interface call that does not generate a network request
  
  - Set futures contract code
    Some interfaces just set some global variables in the system and do not generate network requests, for example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vfb8yjwtz3o33b3snr5f.png)
    
    However, if the parameter is passed incorrectly or scribbled, an error will be reported.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijsnte655hjb3gfl5yuy.png)
  
  But regardless of the type of error, the displayed error message is the key information to find the problem, and the problem can usually be seen from the error message. You can use translation tools to translate error messages and extract key information. For example, "err-msg":"invalid symbol" in the above example, the translation is: "err msg": "无效符号". It is probably known that the trading pair is set incorrectly, because English symbols are usually used to represent the trading code and trading pair.
  Regarding the error information, we briefly discuss these. For common problems, there is a post that will continue to be collected for query: https://www.fmz.com/bbs-topic/9158

### Backtesting system

The backtesting system is also the focus of a quantitative tool. The backtesting system can test strategy prototypes conveniently, potential bugs and logical problems in the preliminary test strategy. The backtesting system needs to be viewed rationally. The backtesting system can reflect some problems of the strategy to a certain extent.

The following is a brief description of the backtesting system on the FMZ platform from the level of different strategy languages ​​supported by FMZ. (Some introductions of the backtesting system were mentioned in the previous articles)

- JavaScript
  
  Backtesting in browser uses native hardware resources.

- Python

  When backtesting on a docker, you can choose which docker to assign to (either the docker deployed by yourself, or the public docker on FMZ platform). In view of the large load of public custodians on FMZ platform, it is recommended to use the local docker for backtesting (this will also be fast, when the public docker is backtesting, if the tasks exceed the load, some backtesting tasks will be canceled, resulting in backtesting interruption).

- C++

  Unlike scripting languages, C++ strategies need to be compiled and then executed. The C++ language strategy will be compiled first on the FMZ platform (server) (if there is a problem with the code, the compilation may not pass, and an error message will pop up directly). After the compilation is passed, it will be backtested on the FMZ platform (server).

- MyLanguage
  
  The underlying implementation is JavaScript, and backtesting is also performed on the browser.
  
- Visualization

  The underlying implementation is JavaScript, and backtesting is also performed on the browser.

The backtesting system on the FMZ Quant Trading Platform is divided into two backtesting modes (this is regardless of the strategy language, this is the backtesting setting, and the strategy backtesting in all languages ​​are the same).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3tyglvwpaaetuly4ntr7.png)

- 1. Simulation level backtesting
  In simple layman's terms, the simulation level backtesting refers to the price data of each time node is simulated and generated according to the K-line data.
  
  ```
  A bar in the K-line opens high and closes low, which constitutes a price framework, within which the prices are all in this price frame, so as long as the generated price opens high and closes low in this K-line frame within the range, the simulated price is reasonable.
  ```
  
  It's like a simulation like this:
  https://www.fmz.com/upload/asset//35c54e14e29601352720d51f75e2d7674415f92e.png
  Of course, when the actual backtesting system implements this simulation, the situation is slightly more complicated than that shown in the figure.
  Knowing this principle, it is important to note the drawbacks of the simulation-level backtesting, although the simulation-level backtesting is very fast (because the prices generated by the simulation are not real second-by-second prices released one by one). However, if the strategy fits the **simulated tick movement trend**, the strategy will perform very well (but in reality, the price may not be this trend, although the price is within the framework of this K-line bar).
  The K-line used to generate the simulated tick data here is called the bottom K-line, and the period of this K-line is called **the bottom K-line period**, which is set as shown on the strategy setting page:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8j52gelrnex63eftvkex.png)
  The setting of 1 minute here means that the K-line data with a period of 1 minute is used as the data source for generating the simulated tick.
  
  Another point is that for high-frequency strategies, it is obviously not appropriate to use simulation-level backtesting. However, for trend strategies, the use of simulation-level backtesting can still reflect the performance of the strategy to a certain extent.
  
- 2. Real bot level backtesting
  After talking about simulation level backtesting, let's talk about real bot level backtesting. Simply put, backtesting at the real bot level is the real release of price data every second during backtesting. It allows the strategy to retrace the price per second of the market. This mode of backtesting allows you to backtest strategies with high trading frequency and get a certain degree of reference value.
  The disadvantage is that the amount of backtesting data at the real bot level is too large to be backtested in a large time range (the time is usually less than 1 day). You can lower the depth data level by closing **split data** (transaction tick-by-tick data, and market depth data also has second-by-second snapshots in the real bot backtesting, so the amount of real bot backtesting data is huge) to increase the range of backtesting appropriately, as shown in the figure:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8zoz9swt7agtp27m3do1.png)

Where is the data source of the FMZ Quant Trading Platform backtesting system? The backtesting system uses the data of the data center of the FMZ platform by default. The data center of the FMZ platform collects the set market data of each currency of each exchange automatically and provides it to the backtesting system on the platform.

- 1. Use data from FMZ data center by default
  As mentioned in previous articles: https://www.fmz.com/bbs-topic/9536
  The backtesting data provided by the platform only supports a limited number of trading pairs (the backtesting data of the whole market and all currencies is an astronomical figure, and it is unrealistic to collect all of them. Our platform has collected market data of mainstream exchanges and mainstream currencies).
  
- 2. Use custom data source data
  You can use the options on the backtesting page to set a custom data source. Simply put, if you have data from an exchange, you can provide it to the backtesting system of the FMZ platform for backtesting according to the format requirements on the FMZ platform.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/csj77iu2k2hnt07gwe9y.png)

  There are also some notes about custom data sources in the FMZ API documentation:
  https://www.fmz.com/api#custom-data-source

### Learning, testing, thinking

You can't do programmatic and quantitative trading without **learning**, **testing**, and **thinking**.
Thinking about problems cannot be thought in vain, that is inefficient. The most effective way to solve problems and think about problems is to **find information**, then **try it out**, **think and analyse**, if the problem is not solved, please repeat the above actions.

But when a beginner encounters problems, he or she will feel:

> "Oops~ it's too hard to program, quantify, and write strategies".
> "After watching it for a long time, I'm still stunned!"
> "I want to give up before I get started!"
> ....

Getting started on the FMZ platform is very simple actually. First of all, you must be good at searching information. There is a lot of information available in FMZ Quant Trading Platform strategy square, community and library.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/abqk0y9qs6xx2bxdxv1z.png)
The second is the hands-on ability, which can be easily tested by using the backtesting system and debugging tools. This does not mean to test a complete strategy. In fact, you can even learn the basics of JavaScript programs on the FMZ Quant backtesting system if you are completely basic.

This is the tutorial website where I often learn JS: https://www.runoob.com/js/js-loop-for.html, it does not limited to JS, all kinds of IT knowledge can be queried and learned here.
For example, I don't know how to use the regular expressions of JS, what should I do?
Of course, search the information first, and then try to do it ~

I saw an example of this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/swesdrb2cz1rzll29y8c.png)
I want to test it, and I can use the backtesting system of the FMZ platform to test and learn.

Set up a random exchange on the backtesting system

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ews2fzylz5varcci2yti.png)

Test the following code:
```
function IsEmail(str) {
    var reg=/^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/;
    return reg.test(str);
}

function main() {
    var strEmailAddress1 = "13512345678"
    Log(strEmailAddress1, " Is it an email address? ", " Answer: ", IsEmail(strEmailAddress1))
    
    var strEmailAddress2 = "123456789@qq.com"
    Log(strEmailAddress2, " Is it an email address? ", " Answer: ", IsEmail(strEmailAddress2))
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/850otlnww8b7wijjvxtu.png)

Look ~ what a learning tool!
For example, I want to learn how to write the loop logic of the JavaScript language, and try it out:

Loop through the elements of an array variable in the order in which they appear in the array:
```
function main() {
    var arr = [{coinName: "BTC", price: 10000}, {coinName: "LTC", price: 100}, {coinName: "ETH", price: 2000}, {coinName: "ETC", price: 500}]
    for (var i = 0 ; i < arr.length ; i++) {
        Log(arr[i])
    }
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/de7mdh0qvpv00bri8w83.png)

Do you feel motivated to learn in an instant?
In fact, on FMZ, you can learn the basics of JavaScript on the backtesting system while watching the JavaScript tutorial. The JavaScript grammar is almost mastered, and entering the next stage, you need to use the exchange interface to obtain data testing. You can also use the **debugging tool** of the FMZ platform to conduct real interface testing.

Then it is to think more, draw inferences from one case, test verification, comparative analysis, etc. This makes learning to get started very fast.

From: https://blog.mathquant.com/2022/08/03/cryptocurrency-quantitative-trading-for-beginners-taking-you-closer-to-cryptocurrency-quantitative-3.html