संसाधन लोड हो रहा है... लोड करना...

एफएमजेड क्वांट ट्रेडिंग प्लेटफॉर्म के साथ रणनीति लेखन का प्राथमिक ट्यूटोरियल (पढ़ना अनिवार्य है)

लेखक:FMZ~Lydia, बनाया गयाः 2023-07-12 15:09:33, अद्यतनः 2024-02-05 20:05:43

'); // एकाधिक तालिकाओं को व्यवस्था के अनुसार ऊपर और नीचे प्रदर्शित किया जाता है


### Sleep 

Its paramert is the number of millisecond, such as ``Sleep(1000)``, indicating sleep for one second. Due to the limited access frequency of all platforms, sleep time should be added to the infinite loop in general strategies.

### _G 

After the bot is restarted, the program will restart. If you want to save some persistent information, ``_G`` is very convenient and practical, and it can save the JSON serialized content. The ``_G`` function is written in ``onexit()``, so that every time the strategy is stopped, the required information will be automatically saved.
If you want to save more formatted data, the _G function is not suitable, but you can use Python to write directly to the database.

फलन onexit ((() { _G ((लाभ, लाभ) } मुख्य कार्य _G(num, 1); // 1 सेकंड के मूल्य के साथ एक वैश्विक चर num सेट करें _G(num, ok); // एक वैश्विक चर num बदलें, जिसका मान स्ट्रिंग ok है _G(num, null); // वैश्विक चर num को मिटाएँ _G(num); // वैश्विक चर num का मान लौटाएँ; यदि यह मौजूद नहीं है, तो शून्य लौटाएँ

var profit = 0
if(_G('profit')){
    profit = _G('profit')
}

}


### _N 

When placing an order, the precisions of price and volume normally need to be controlled; FMZ has builted in the _N function to determine the decimal places to be saved; for example, the result of ``_N(4.253,2)`` is 4.25. 


### _C 

Calling the plaform API cannot guarantee the access is successful every time, and _C is an automatic retry function. It will always call the specified functions until it returns successfully (the function will retry if it returns null or false); for example, ``_C(exchange.GetTicker)``, with the default retry interval of 3 seconds, and you can call  _CDelay function to control the retry interval, such as _CDelay(1000), which means to change the _C function retry interval to 1 second. It is recommended to use  _C to operate fault tolerance in ``GetTicker()``, ``exchange.GetDepth``, ``GetTrade``, ``GetRecords``, `` GetAccount``, ``GetOrders`` and ``GetOrder`` to prevent the program interruption caused by the access failure.


``CancelOrder`` cannot use the _C function, because there are various reasons for the failure to cancel an order. If an order has been executed, then canceling the order will return a failure, and using the _C function will result in retrying all the time.
The _C function can also pass in parameters and is also used in custom functions.

मुख्य कार्य var टिकर = _C (विनिमय.Get टिकर) var गहराई = _C (विनिमय.GetDepth) var रिकॉर्ड = _C(exchange.GetRecords, PERIOD_D1) // पैरामीटर में पास करें }


### _D 

Calling ``_D()`` directly will return the current time string, such as: ``2019-08-15 03:46:14``. If it is called during the backtest, the backtest time will be returned. You can use the _D function to judge the time, such as: `` _D().slice(11) > '09:00:00':``.
``_D(timestamp, fmt)``, will convert the ms timestamp to a time string, such as ``_D(1565855310002)``. The fmt parameter is the time format, and the default is ``yyyy-MM-dd hh:mm:ss``.


### TA Indicator Functions 

For some commonly used indicator functions, such as MA\MACD\KDJ\BOLL and other common indicators, which have been directly built in by FMZ platform, and the specific supported indicators can be found in the API document.

Before using the indicator functions, it is best to judge the length of K-line. When the previous K-line length cannot meet the required period for calculation, the result is ``null``. For example, if the input K-line length is 100 and the period for calculating MA is 10, then the first 9 values are all null, and the calculation after the formar 9 values will be done normally.

JavaScript also supports the complete talib, as a third-party library, with invocation method such as ``talib.CCI(records)``. Please refer to http://ta-lib.org/function.html. For Python, you can install the talib library by yourself. Due to the need for compilation, you cannot simply use pip to install. You can search for the installation method by yourself.

**Indicator functions can not only pass the K-line data, but also pass any array**

मुख्य कार्य var रिकॉर्ड = exchange.GetRecords ((PERIOD_M30) यदि (रिकॉर्ड && records.length > 9) { var ma =TA.MA(रिकॉर्ड, 14) लॉग (ma) } }


### Commonly Used Functions in JavaScript 

Here we introdue some commonly used JavaScript functions in the bots.  

- ``Date.now()`` returns the current timestamp;
- ``parseFloat()`` transfers strings into numbers, such as ``parseFloat("123.21")``;
- ``parseInt()`` transfers strings into integers; 
- ``num.toString()`` transfers numbers into strings, with number variable num;
- ``JSON.parse()`` formats Json strings, such as ``JSON.parse(exchange.GetRawJSON())``;
- JavaScript has its own Math functions, such as the common math operations, inluding ``Math.max()``, ``Math.abs()`` and so on; reference: https://www.w3school.com.cn/jsref/jsref_obj_math.asp ;
- The third-party math library used by FMZ; reference: https://mathjs.org/ ;
- The third-party underscore library of JavaScript used by FMZ, which is recommended to have a knowledge of and which makes the tedious Js operations more convenient; reference: https://underscorejs.org/.

## Template

There are many situations that need to be considered when writing a bot strategy function. For example, a simple function such as buying 5 coins, we need to consider: Is the current balance enough? How much is the order price? What is the precision? Do you need to split orders to avoid impacting the market? How to deal with unfinished orders? And some details like that. In different strategies, these functions are the same, so you can make them into a template. Following the official templates, users can also write their own template strategies. Here we will introduce several very commonly used template class libraries officially released by FMZ, so that users can quickly write their own strategies.

The JavaScript cryptocurrency trading library and commodity futures trading library are built in by default and do not need to be copied. Other template libraries can be found at the strategy "Square" (https://www.fmz.com/square/20/1). Copy and save the template library, and check the library to be used when creating your own strategy.

JavaScript template functions all start with ``$``, while Python ones all start with ``ext``.

### Cryptocurrency Trading library 

Source Code Address: https://www.fmz.com/strategy/10989, which has already been built-in, so no need to copy. Specific funtion implementation can directly refer to the source code.

**Get Account:**

$.GetAccount प्राप्त करें

लॉग (($.GetAccount()); // गलती सहिष्णुता समारोह के साथ खाता जानकारी प्राप्त करें लॉग (($.GetAcccount ((एक्सचेंजों[1]));

**Order Placing & Canceling:**

$.बिक्री/बिक्री (e, राशि) $.Buy(0.3); // मुख्य मंच 0.3 सिक्का खरीदता है $.Sell(0.2); // मुख्य मंच 0.2 सिक्का बेचता है $.Sell(एक्सचेंजों[1], 0.1); // द्वितीयक मंच 0.1 सिक्का बेचता है $.रद्द करनाअपेक्षित ऑर्डर (e, orderType)

$.CancelPendingOrders(); // मुख्य मंच के सभी सौंपे गए आदेश रद्द करें $.CancelPendingOrders ((ORDER_TYPE_BUY); // मुख्य मंच के सभी खरीद आदेश रद्द करें $.CancelPendingOrders ((exchanges[1]); // माध्यमिक मंच के सभी आदेश रद्द करें $.CancelPendingOrders ((exchanges[1], ORDER_TYPE_SELL); // माध्यमिक मंच के सभी बिक्री आदेश रद्द करें

**Judge the Cross:**

$.क्रॉस (अवधि A, अवधि B) / $.क्रॉस (arr1, arr2);

var n = $.Cross ((15, 30); var m = $.Cross (([1,2,3,2.8,3.5], [3,1.9,2,5,0.6]) यदि n = 0, तो इसका अर्थ है कि सटीक 15 अवधि के ईएमए और 30 अवधि के ईएमए की वर्तमान कीमतें समान हैं। यदि n > 0, जैसे 5, तो इसका अर्थ है कि 15-पीरियड ईएमए 30-पीरियड ईएमए को 5 पीरियड (बार) तक पार करता है। यदि n < 0, जैसे -12, तो इसका अर्थ है कि 15-अवधि ईएमए 30-अवधि ईएमए को 12 अवधियों (बार) से नीचे पार करता है यदि यह क्रॉस को पारित एक सरणी नहीं है, तो फ़ंक्शन स्वचालित रूप से चलती औसत गणना के लिए K-लाइन प्राप्त करता है। यदि क्रॉस को एक सरणी दी जाती है, तो सीधे तुलना करें.


$.withdraw(e, currency, address, amount, fee, password) function:

$.withdraw ((विनिमय, btc, 0x..., 1.0, 0.0001, ***)

### Commodity Futures Trading library

For the use of the commodity futures trading library is very stable, it is recommended. Source code address: https://www.fmz.com/strategy/12961, which has already been built-in, so no need to copy. Specific funtion implementation can directly refer to the source code.

**CTA Library**

- The bot will automatically map the index to the main continuous contract;
- it Will automatically handle the move;
- you can specify the mapping for backtest, such as rb000/rb888, which is to map the k-line of rb index to trade the main continuous contract; 
- it can also be mapped to other contracts; for example, rb000/MA888 is to look at the K-line of the rb index to trade the MA main continuous contract. 

मुख्य फंक्शन $.CTA ((rb000,M000, कार्य ((r, mp) { यदि (r.length < 20) { लौटना } var emaSlow = TA.EMA(r, 20) var emaFast = TA.EMA(r, 5) var cross = $.Cross ((emaFast, emaSlow); यदि (mp <= 0 && क्रॉस > 2) { लॉग ((गोल्डन क्रॉस पीरियड, क्रॉस, मोमेंट पोजीशन, एमपी); रिटर्न 1 } अन्यथा यदि (mp >= 0 && क्रॉस < -2) { लॉग ((मृत्यु क्रॉस अवधि, क्रॉस, क्षण स्थिति, एमपी); रिटर्न -1 } }); }

**Invocation Example of library**

main() { var p = $.NewPositionManager(); p.OpenShort ((MA609, 1); p.OpenShort ((MA701, 1); लॉग ((p.GetPosition ((MA609, PD_SHORT)); लॉग ((p.GetAccount()); लॉग ((पी.खाता (()); नींद ((60000 * 10); p.CoverAll ((MA609); LogProfit ((p.Profit (())); लॉग (($.इस्ट ट्रेडिंग ((MA609)); // कई किस्में गैर अवरुद्ध व्यापार कार्य को पूरा करने के लिए व्यापार कतार का उपयोग करें var q = $.NewTaskQueue(); q.pushTask(विनिमय, MA701, buy, 3, कार्य(कार्य, ret) { लॉग ((कार्य.desc, ret) }) जबकि (सही) { // खाली समय में अधूरे कार्यों को निष्पादित करने के लिए poll कॉल करें q.poll (() नींद (१०००) } }


### Drawing Library 

For the raw functions for drawing are very complicated, which will be introduced in the next tutorial, we recommend beginners to use the drawing library, to draw very simple line charts and k-line charts, etc. The simple drawing library has been built in FMZ, which can be seen on the strategy editing page; if the library is not built in yet, users need to copy and save, to check and use the library in the strategy. 

![img](/upload/asset/2684f22ebd46dfee60435.png)

Copy address of Javascript version drawing library: https://www.fmz.com/strategy/27293
Copy address of Python version drawing library: https://www.fmz.com/strategy/39066

Specific example:

मुख्य फंक्शन जबकि (सही) { var टिकर = विनिमय.GetTicker() यदि (टिकर) { $.PlotLine ((Last, ticker.Last) // आप एक ही समय में दो लाइनें खींच सकते हैं, Last लाइन का नाम है $.PlotLine ((Buy, टिकर.Buy) } नींद ((6000) } }


## Strategy Parameter Settings 

Under the "Edit Strategy", there are strategy parameter settings, which are equal to the global variables of the strategy, and can be accessed at any location in the code. The strategy parameters can be modified on the bot page, and they will be valid after restart. Therefore, some variables can be set to the parameters, and the parameters can be modified without modifying the strategy.  
 ![img](/upload/asset/269577e72f4bc0612d1e1.png)
- **Varable Name**: namely the number, string and combox etc. in the above image,which can be directly used in the strategy group. 
- **Description**: the name of a parameters on strategy interface, more convenient for users to understand the meaning of the parameter.
- **Remark**: the detailed explanation of a parameter, which will be displayed accordingly when the mouse is over the parameter. 
- **Type**: the type of a parameter, which will be introduced in details later. 
- **Default**: the default of the a parameter.

It is very easy to undertand string type and number type. which are very commonly used types. The combo box will display the options in the box on the parameter interface. For example, you can set the SYMBOL paramater as ``BTC|USDT|ETH`` in the combo box; if you choose USDT in the box on the page, the SYMBOL value in the strategy is USDT index 1. Check options refers to an optional checkbox; check means true, while no check means false. 

There are more parameters for settings; referene:https://www.fmz.com/api.

## Strategy Backtest

When the quantization of a strategy is finished, you can test it by the history data, to check out the profit situation of the strategy in the history date. Of course, the backtest result is only for reference. FMZ Quant platform supports the backtests of cryptocurrency spot and futures, BitMEX perpetual contract, commodity futures, in which only mainstream cryptocurrencies are supported. 
The Javascript backtest is run on the browser; the Python backtest is on the docker, and our platform provides public dockers for users. The backtest of Mylanguage has more parameters for settings, and more details can be referred in the document of Mylanguage.  

### Baktest Mechanism 

Onbar backtest mechanism is based on K-line, that is, each K-line will generate one point in time for backtest. On the point in time, you can obtain the information including the open, close, highest and lowest prices and trading volume of the current K-line, as well as the history K-line information before the point. The shortcoming of this kind of mechanism is very obvious: only one purchase can be generated on one K-line; usually the referred price is the close price of thr K-line. also, one K-line can only obtain four prices, namely the close, open, highest and lowest prices; the information, icluding how the prices change in one K-line and whether the highest price or the lowest price changes first, cannot be obtained. Take the one-hour K-line as an example. The market quote information is surely obtained every several seconds in the bot, and trading commands will be conducted in the bot not after the K-line is finished. The advantage of onbar backtest mechanism is easy to understand, and fast to backtest. 

The backtest on FMZ contains two kinds, namely the simulation level backtest and the real market level backtest. The simulation level backtest can generate the simulated tick according to the underlayer K-line periods, and each underlayer K-line period will generate 14 backtest time points. **However, the real market level backtest will actually collect tick, every several seconds, and now it supports real depth (inluding 20 levels), and real execution trade by tarde.** The date volume is pretty huge, and the backtest speed is very slow, so the backtest cannot be run in a long time. FMZ backtest mechanism can realize multiple trades of the startegy on one K-line, to prevent the situation that the trading can only be executed by the close price, and also increasingly targeting at and taking care of the backtest speed. For more detailed instructions: https://www.fmz.com/bbs-topic/9126. 

The framework of backtest and bot are the same, both an infinite loop. Because the backtest is to skip to different backtest points, the backtest can be run without using "Sleep", and it will automatically skip to the next time point when one loop is over. However, Python, due to the program mechanism, needs a constraint of ``Sleep(10)``, to avoid being stuck. 

### Backtest Matching 

The backtest engine will match the order price placed by the user and the market price at the backtest time point. If the buy price is higher than sell one, the sell one will be executed. If the trading cannot be executed, a pending order will be generated. Slippage needs to be added to ensure the trading. If the position cannot be opened or closed during the backtest, check whether the position is frozen due to unfinished orders.

### Backtest Page Settings 
  ![img](/upload/asset/2698694cbcac4a55b22fa.png)  
- 1.choosing the page of "Backtest", on the left of which is "Edit Strategy" page;
- 2.the starting time and the ending time of the backtest; for the data might be incomplete, the backtest may directly start from the time when the data exists;
- 3.the default period of backtesting the ``GetRecords()`` function; you can also specify a period parameter in the code; 
- 4.choosing the backtest mechanism;
- 5.displaying or hiding more backtest settings; 
- 6.the maximums of log items, profit log items and chart log items, to prevent the browser from the breakdown caused by huge data amount; 
- 7.the period generated by the underlayer tick according to K-line; 
- 8.traing slippoint;
- 9.fault tolerance, which will simulate the situation when the API request is wrong, and test the fault tolerance capacity of the strategy; 
- 10.whether to draw the market chart; if a TA indicator function is used in the baacktest, the function will be automatically shown in the chart, and buying and selling also will be marked; 
- 11.service fee setting; 
- 12.adding  platforms - trading pair & assests; 
- 13.backtest parameter settings; if the parameters are numbers and also support one-key optimizaton, the parameters will be traversed automatically in a certain range in the backtest.  

  
### Differences Between Bot & Backtest 

- 1.the only valid market quotes in the backtest are only from GetTicker and GetRecords; others like GetDepth and GetTrades are not real (the data volume is huge, and although the real market level backtest now supports the data already, it only supports the latest data);
- 2.the platforms added in the backtest are all segregated accounts; right ow switching the trading pairs is not supported; therefore, you cannot operate two trading pairs in one account; 
- 3.netwrok request cannot be used in the backtest; 
- 4.IO extension cannot be used in the backtest, and only basic APIs can be operated; 
- 5.only standard data can be obtained in the backtest, and the data, like Info that is related to the bot, does not exist; 
- 6.tarding might be not executed in the backtest, and pay attention to the situation of frozen orders; 
- 7.in the backtest of commodity futures, market order is not supported. 

## Strategy Fault Tolerance & Common Errors 

As we mentioned earlier, Using an API interface in the bot may fail to access and return ``null``; if you still use the data of it, an error will be reported and the bot will stop. Therefore, strategies need to do well in fault tolerance. 

### Common Ways of Fault Tolenrance 

Common Causes:

- API access network error; the timeout of accessing the interface returns nunll, and an error will be reported.
- Platfrom limitation error, such as ip limitation, order precision, access frequency, parameter error, asset deficiency, market trading failure, the canceling of executed orders, etc.; the details can be queried in the API document according to the wrong codes. 
- Platform return data error; it happens sometimes, such as returning null depth, delayed account information and delayed order status, etc. 

- Program logic error. 

Before you use the returned data of API, you should judge whether the data is null, and the common methods are introduced as follows:

//1. आंकड़ा शून्य है और संभाल न्याय var टिकर = exchange.GetTicker(); while(ticker == null) { लॉग ((टिकर त्रुटि प्राप्त करें); टिकर = विनिमय.GetTicker(); } लॉग ((टिकर.अंतिम); // 2. आंकड़ा शून्य नहीं है न्याय, और उपयोग var टिकर = exchange.GetTicker(); यदि (!टिकर) { लॉग ((टिकर.अंतिम); } // 3.retry _C() फ़ंक्शन var ticker = _C(exchange.GetTicker); लॉग ((टिकर.अंतिम); // 4.Cache दोष सहिष्णुता की कोशिश करें कोशिश{ var टिकर = exchange.GetTicker(); लॉग ((टिकर.अंतिम); } पकड़ना लॉग ((टिकर त्रुटि प्राप्त करें); }

If you want to obtain the information of erors, you can use ``GetLastError()``, and the strings of the last time error information will be returned, and the errors can be processed by differences. 

### FAQ

Common error summary in the top posts of the forums: https://www.fmz.com/bbs-topic/9158. Here we introduce it in brief; you can use ctrl+F to search, when you have problems.

> How to deploy the docker?

There is a detailed introdution about it in the section of adding a docker.

> Can I ask someone to ghostwrite strategies for me? 

on https://www.fmz.com/markets , there are some people who provide services of writing strategies for others, or you can ask in the chatting groups; notice that those kind of services shall be contacted by yourself, and you should be aware that the risk shall also be borne by yourself. 

> All interfaces prompt timeout when acessed

it refers to the timeout of the accessed platform interface; if the timeout happens occasionally, it is not a problem; if the timeout is prompted all the time, that means all networks cannot be accessed and an overseas server is needed. 

> ERR_INVALID_POSITION 

If the backtest reports an error, it is generally a writing error; when you try to place an order to close a position, when there is no position or the position volume is not enough, an error will be reported. 

> Symbol not set

There is no contract set in the code, durig the backtests of futures platforms. Refer to exchange.SetContractType function. 

> BITMEX 429error,{"error":{"message":"Rate limit exceeded retry in 1 seconds ……"}}

The access frequency of the platform interface is too high. 

> {"status":6004,"msg":"timestamp is out of range"}

The timestamp of the server exceeds the time range of updating the server, and the exceeded time cannot be too long. 

> GetOrder(455284455): Error: invalid order id or order cancelled. 

If the order of a platform is cancelled, the platform will not maintain the order information anymore, so the information cannot be obtained.

> GetOrders: 400: {"code":-1121,"msg":"Invalid symbol."}

Invalid trading pair; check out if the tradig pair setting is wrong. 

> Secret key decrypt failed

The APIKEY parsing fails. If the FMZ password has been modified after the APIKEY is configured, try to add a platform page on FMZ and reconfigure the platform APIKEY.

> Signature not valid: invalid submission time or incorrect time format

suggest you use Linux server, or install time synchronization software on these Windows systems where this problem occurs.

> Why the docker still cannot access the platform API when a global proxy is set? 

The global proxy does not have a proxy docker network port. Due to the delay problem, it is best to deploy the docker of an overseas server.

> How to save a strategy locally, not to upload it to FMZ?

Using Python, and you can import local files, save the strategy normally written by FMZ API as a file and put it in the execution path on your own server, and you can directly read and execute it.

पायथन2.7

def run ((runfile): चलाने के लिए के साथ open ((runfile,r) के रूप में f: exec ((f.read (())

मुख्य परिभाषाः चलनाmy.py’)


> How to the testnet of a platform or how to change API base address? 

Use exchange.SetBase() to directly switch to the corresponding API base address. For example:

विनिमय.सेटबेस(https://www.okex.me”)



अधिक