The resource loading... loading...

Quantifying the coin circle is a whole new thing - it brings you closer to quantifying the coin circle.

Author: Inventors quantify - small dreams, Created: 2021-04-19 14:16:21, Updated: 2024-12-04 21:21:43

币圈量化交易萌新看过来–带你走近币圈量化(二)

The coin circle quantification transaction is a new look and brings you closer to the coin circle quantification.

In the previous article we talked about programmatic trading scripts. In fact, the trading strategy is a trading script. The article mainly talks about the trading script that needs to have a hardware carrier (where the program runs), which can be written in the computer programming language (listed three programming languages used in the inventor's quantitative trading platforms, of course, the programmatic trading itself can be implemented using any programming language implementation strategy).

Programmed transaction scripts

  • Types of trading strategies Beginners to programmatic trading and quantitative trading may be confused by the terms trend strategy, arbitrage strategy, high-frequency strategy, grid strategy, etc. In fact, programmatic trading and quantitative trading are common strategies in several directions.
    • How to hedge against leverage Simply put, a strategy such as holding a long position and a short position can be classified as a leverage strategy. There are many specific types, such as spot cross-market, futures cross-period, spot leverage, cross-variety leverage, and so on.
    • Trending strategies In simple terms, it is a strategy of tracking the trend of holding orders, double even lines, MACD and so on.
    • The return strategy For example, grid strategies that take advantage of price fluctuations in volatile markets.
    • High frequency strategy Simply put, it is a strategy to conduct high-frequency trading by using algorithms to uncover the market's microstructure, patterns, opportunities, etc.

The above is divided from the point of view of trading strategies, and from the point of view of strategy design on inventors' quantitative trading platforms, strategies can also be divided into:

  • The single-variety strategy This means that the strategy only operates on one variety, such as BTC trading or ETH trading.

  • Multicultural strategies In simple terms, it is a strategy to operate multiple varieties according to a single logic.

  • Multi-account policy Simply put, it is a real-time configuration of multiple exchange objects (the concept of the exchange has been introduced in the previous article, the exchange object configuration of the API KEY represents an exchange account); for example, some listing policies, multiple accounts follow operations together (may be the same exchange, or may be different exchanges), in short, one real-time management of multiple exchange objects (accounts).

  • Multiple logical strategies For example, the MACD strategy, the straight line strategy, the grid strategy, etc. are designed simultaneously on a real disk (of course, operating different exchange objects, operating the same exchange objects to see if the specific strategy is logically conflicted).

  • Exchange API interface The answer is the API interface that is opened through the exchange. In the previous article, we discussed REST, Websocket interfaces in general. Here we add a bit of concept from the policy level. Exchange interfaces are divided according to whether they are validated (REST, Websocket are both validated and non-validated).

    • Interfaces that do not require authentication Commonly referred to as public interface pins, these types of interfaces do not require verificationAPI KEYThese interfaces are generally market interfaces, such as asking for deep market, asking for K-line data, asking for capital rates, asking for information about trade varieties, asking for exchange server timing, etc. Simply put, an interface that is basically unrelated to your account can be roughly determined to be a public interface (no authentication required).
      In the inventor's quantified trading platform, when calling an unauthenticated API function (wrapping exchange non-authenticated interface, public interface) even if the API KEY is configured incorrectly, the data returned to the interface can be normally retrieved.

    • Interfaces that need to be verified Simply put, the interface that needs to be authenticated (authentication via API KEY) is called a private interface. These interfaces are usually associated with some operation or information about your account, such as querying account assets, querying account holdings, querying hang ups, query transfers, transfers, currency adjustment leverage, setting holdings mode, etc. These operations must be verified. In the inventor's quantified trading platform, the API function that needs to be validated when called (interface that needs to be validated, private interface of the encapsulated exchange) will return a blank value if the API KEY is configured incorrectly, and an error will occur when the interface is called.

So how are these interfaces used on inventors' quantitative trading platforms?

The inventor quantified trading platform encapsulates the behavior of the exchange and defines consistent interfaces (e.g. K-line interfaces, deep market interfaces, current asset query interfaces, downlink interfaces, withdrawal interfaces, etc.), which are called the inventor quantified trading platform API functions on the inventor quantified trading platform and can be accessed by querying the API documentation.https://www.fmz.com/api )。

So how do some of the behavioral, non-uniformly defined exchange interfaces work on inventors' quantitative trading platforms?

These interfaces are used for examples: asset allocation, conditional assignment, bulk ordering, bulk withdrawal, modification of orders, etc. These interfaces are available on some exchanges and not on others, and the functionality and usage details may vary greatly, so these interfaces are passed on to the inventor's quantitative trading platform.exchange.IOThis function can be accessed by visiting the inventor's API documentation:https://www.fmz.com/api#exchange.io…)。在发明者量化交易平台策略广场上也有些实用IO的范例策略。

Will all the API functions in the inventor's API documentation generate network requests?

Let's say that the exchange API interface has a frequency limitation (e.g. 5 times a second), access cannot be too frequent or it will report an HTTP 429 error and access is refused (most exchanges report 429). Not all inventors' API functions for quantitative trading platforms generate network requests, and some inventors' API functions simply modify some local settings, such as setting the current trading pair, setting the contract code, calculating the indicator function, obtaining the name of the exchange object, etc. Basically, the function's use can be used to determine whether a network request is being made, as long as the network request is generated to obtain exchange data, operations on exchange accounts, etc. These interfaces need to pay attention to call frequency.

  • Here are a few common questions and experiences that inventors have when using API functions for their quantitative trading platforms.

    • Let it go. This is the most common mistake, which has been troubling countless newbies, often the tactic is to check back and everything is fine, why is the hard drive running for a while (can trigger at any time) and the hard drive hangs off ~

    币圈量化交易萌新看过来–带你走近币圈量化(二)

    When writing a policy, we need to verify the data returned by the interface, for example, to get the following line of code on the inventor's quantitative trading platform (it is the same to write your own program to directly access the exchange interface):var ticker = exchange.GetTicker()And if we need to use this,tickerVariables (see structure returned by the GetTicker function)Last(Recent prices) This data, we need to use.var newPrice = ticker.LastThis will get the data ((newPrice is what? new: latest, Price: price, yes! combined!) at this point, ifGetTicker()The function returns normal data, but if there is a request overtime, network error, exchange disconnect, cable cutting, bear kid pulling the plug, etc.GetTicker()The function returnsnull❖ At this timetickerThe value isnullI'll visit it again.LastThis is the first time that a policy has been suspended due to a procedural error. It appears that the interface call failure (GetTicker call failure returns null) is not the direct cause of the policy disk stop (the direct cause is access to anullA variable's attribute), an interface call failure report is not a failure that causes the disk to stop (strike point). So what can we do to prevent the disk from stopping abnormally? The answer is to allow errors to be made in the data that the interface returns, and it's very simple to just judge whether the data is returned or not.null(JavaScript language example, other languages are basically the same) Write a short code paragraph to explain (this is just an illustration, running directly is a breeze!)

      var ticker = exchange.GetTicker()
      if (ticker) {
          var newPrice = ticker.Last
          Log("打印最新价格:", newPrice)
      } else {
          // 数据为null,不做操作就不会出问题
      }
    

    It's not justGetTickerInterfaces that have network requests need to be error tolerant (if you use a function's return value) There are many ways to forgive mistakes._C()Functions (see FMZ API documentation), write their own error tolerance functions, design their own error tolerance mechanisms, logic. About the_C()The use of functions, and many of the new students are probably using them wrong, be careful._C()The parameters of a function are function references, not function calls.

      ```_C(funcName(param1, param2))```,调用错误,通常萌新没认真看FMZ API文档都会这么写。
     - 现货市价单买单的下单量
      现货市价单买单的下单量也是很多萌新容易搞错的,上一篇中已经讲过现货市价单买单的下单量通常是金额(极个别的交易所可能是其它设定,一般FMZ上这些特殊的交易所设定都会在FMZ API文档上说明),例如我用OKEX V5 模拟盘测试:
      交易对设置为:```LTC_USDT```
    

    function main (() { exchange.IO ((simulate, true) // Switched to OKEX exchange.Buy ((-1, 1) // price is −1, indicating that the order is a market price list, the quantity is 1 indicating that the order is 1USDT I'm not sure.

      由于交易所一般都有下单金额限制,小于限制的订单是不予报单的(例如币安现货要求每单大于5USDT才可以报单成功)。所以这样下单会报错:
    

    Error Buy ((-1, 1): map[code:1 data:[map[clOrdId: ordId: sCode:51020 sMsg:Order amount should be greater than the min available amount. tag:]] msg:]

    
     - 期货下单时的方向
      在做期货策略时下单方向也是萌新们经常搞错导致出问题的,以在发明者量化交易平台上编写策略为例。
      我们先看下API文档上的描述:
      https://www.fmz.com/api#exchange.setdirection...
    
    
      ![币圈量化交易萌新看过来--带你走近币圈量化(二)](/upload/asset/16d065f0dfda87640aaf.png) 
    
    
      由于下单函数就只有```Buy```,```Sell```。然而期货(现货当然没问题了,现货只有买卖)有开多、平多、开空、平空这些方向,那么显然Buy/Sell表示不了这么多个方向的操作,这时就需要引入设置期货交易方向这个函数```exchange.SetDirection()```。
      在FMZ上
      ```exchange.SetDirection("buy")```(先设置方向)和```exchange.Buy```配合使用,就表示下的单子是开多仓的订单。
      以此类推:
      ```exchange.SetDirection("sell")```和```exchange.Sell```配合使用,就表示下的单子是开空仓的订单。
      ```exchange.SetDirection("closebuy")```和```exchange.Sell```配合使用,就表示下的单子是平多仓的订单。
      ```exchange.SetDirection("closesell")```和```exchange.Buy```配合使用,就表示下的单子是平空仓的订单。
      通常萌新会```exchange.SetDirection("sell")```和```exchange.Buy```配合使用,或者其它的错误组合。然后就报错了(回测可能不报错,但是这个明显是逻辑错误,强迫症不能忍....的)。
      另一个萌新经常犯的错误
    

    function main (() { exchange.SetContractType ((quarter tip) // Set the current contract as a quarterly contract exchange.SetDirection (sell button) var id = exchange.Sell ((-1, 1)
    Log ((Hey look at my market price order, transaction is done, there is a stock exchange, exchange.GetPosition)))
    exchange.SetDirection ((closebuy) // closebuy and Sell are used together, that's fine~ exchange.Sell ((-1, 1) I'm not sure.

      ![币圈量化交易萌新看过来--带你走近币圈量化(二)](/upload/asset/168d332fddad25f0859e.png)       
      看到这里会问:“为什么我有持仓并且closebuy和Sell也是搭配使用的,怎么就报错,不能平仓了?”。我会回答:“平错方向了!平的是多头仓位”
      以上这个报错还可能出现的一种情况是:平仓方向设置正确、下单函数使用也正确、也持有这个方向的持仓,但是还是报这个错误。
      原因是可能你的程序下了多次单,开始的订单并没成交,平仓单在盘口挂着等待成交,这个时候程序继续去平仓,就会提示超出平仓头寸的错误。
    
     - 日志输出、交易信息展示
      设计编写程序化、量化交易策略就离不开“数据显示”,“操作日志输出”等人机交互的设计。通常使用原生编程语言编写实盘脚本、策略程序。直接使用当前语言的输出函数。
      例如:
      python用```print```。
      javascript用```console.log```。
      Golang用```fmt.Println()```。
      C++用```cout```
    
    
      再来说下FMZ平台上的信息显示,在发明者量化交易平台上,显示信息的地方有两个主要位置。
         - 状态栏
        在实盘运行起来之后,实盘页面如图
    
    
        ![币圈量化交易萌新看过来--带你走近币圈量化(二)](/upload/asset/16bafc3d4df6dfa18102.png) 
    
    
        显示部分为状态栏信息,状态栏主要是为了显示一些实时变动的数据(因为实时变动需要实时观察,又不能每次都打印成日志,所以这类数据可以在状态栏显示,如果每条都打印日志会很多重复无意义的数据,影响查询)。
        状态栏上显示数据使用```LogStatus```函数,具体可以参看FMZ的API文档。
    
         - 日志栏
        同样在实盘页面,如图所示:
    
    
        ![币圈量化交易萌新看过来--带你走近币圈量化(二)](/upload/asset/16cf9d61e66384022a76.png) 
    
    
        显示部分为日志栏,日志栏主要是为了永久记录某个时刻某些数据,或者记录某个时候策略的某项操作。
        日志分为多种类型:
        1、普通日志,FMZ的策略中使用Log函数输出、打印在策略日志。
    
    
           ![币圈量化交易萌新看过来--带你走近币圈量化(二)](/upload/asset/16ddc72e1f7d07dcfa5a.png) 
    
    
        2、下单日志,FMZ的策略中使用```exchange.Sell```/```exchange.Buy```会自动在日志输出记录。
    
    
           ![币圈量化交易萌新看过来--带你走近币圈量化(二)](/upload/asset/172aac2089e93865e3c2.png) 
    
    
        3、撤单日志,FMZ的策略中使用```exchange.CancelOrder```,会自动在日志输出撤单日志。
    
    
           ![币圈量化交易萌新看过来--带你走近币圈量化(二)](/upload/asset/15e90c7be742743c7421.png) 
    
    
        4、错误日志,FMZ的策略运行时,进行网络请求的接口发生调用错误时,抛出异常时(例如throw之类的语句),会自动在日志中输出错误日志。        
    
    
           ![币圈量化交易萌新看过来--带你走近币圈量化(二)](/upload/asset/166196451439434a800f.png) 
    
    
      FMZ的API函数,可以产生日志输出的函数比如Log(...),exchange.Buy(Price, Amount),exchange.CancelOrder(Id)等都可以在必要参数后跟一些附带输出参数,比如:exchange.CancelOrder(orders[j].Id, orders[j])这样就是在取消orders[j]这个订单时,附带输出这个订单信息。
    

    function main (() { Log ( data 1, data 2, data 3,...) var data2 is equal to 200 var id = exchange.Sell ((100000, 0.1, and so on with the attached data 1 ton, data 2, and so on...) exchange.CancelOrder ((id, attached data 1, data 2, ...) LogProfit ((100, logs attached to data 1, data 2, logs... logs)) I'm not sure.

    
     - 指标函数的使用
      在说指标函数之前,我们先弄懂什么是指标,简单说就是均线、MACD、ATR之类的线。
      问:这些指标怎么来的?
      答:当然是计算出来的。
      问:是依据什么计算的呢?
      答:根据K线数据计算。
      问:举个例子?
      答:以最简单的指标均线指标举例,如果我们使用日K线(就是一根阳线或者阴线代表一天)数据作为指标计算的数据源。均线指标参数为10,那么计算出的均线指标就是10日均线。
      问:如果K线BAR数量不够10根,可以算出均线指标么?
      答:不仅是均线指标无法算出,任何指标在K线数据BAR数量不满足指标周期参数时都无法算出有效指标值,算出的数组对应位置上就会用空值填充,例如```JavaScript```语言策略打印算出的指标数据时就会显示```null```。
    
    
      正好策略广场上有个教学例子:https://www.fmz.com/strategy/125770
      回测这个教学例子策略,可以看到回测系统生成的图表以及10周期的均线:
    
    
      ![币圈量化交易萌新看过来--带你走近币圈量化(二)](/upload/asset/16626b033d3b5ccafb3b.png) 
    
    
      策略自定义画图,画出的K线以及均线图表:
    
    
      ![币圈量化交易萌新看过来--带你走近币圈量化(二)](/upload/asset/16ba98eec859abb30366.png) 
    
    
      问:如果我要10小时均线呢?
      答:K线数据用小时周期的K线数据就可以了。
    
    
      通俗说,我们看到的K线,我们把它数据化之后就是一个数组(数组概念不了解,可以百度下),其中每个元素就是一个K线柱,是按顺序排列的,数组第一个元素是距离当前时间最远的,数组最后一个元素是距离当前时间最近的。
      通常K线数据最后一个线柱是当前周期的线柱,是实时变动的,是未完成的(登录一个交易所的页面观察他的K线就能观察出来变动)。计算出的指标也是和K线柱一一对应的,上面的例子可以看到一个指标数值对应一个K线柱。注意最后一个K线柱是实时变动的,算出的指标也是会跟随K线柱变化而变化的。
    
    
      在发明者量化交易平台上,可以使用TA库(FMZ平台实现的库,集成在托管者,各种语言都可以直接使用)或者使用talib库(talib老牌指标库,JS、C++集成,Python需要自行安装)。
      例如以上例子中计算计算均线:
      使用TA库:
    

    function main (() { var records = exchange.GetRecords var ma = TA.MA ((records, 10) Log ((ma) // printed as a straight line I'm not sure.

      使用talib库:
    

    function main (() { var records = exchange.GetRecords var ma = talib.MA ((records, 10)) is a music video game based on the video game series of the same name. Log ((ma) // printed as a straight line I'm not sure.

      算出的指标数据ma是一个数组,每个元素一一对应K线数组(records),即```ma[ma.length -1]```对应```records[records.length - 1]```,以此类推。
    
    
      其它再复杂的指标也是同理,需要注意MACD这类指标。
    

    var macd = TA.MACD ((records) // This only passes data in the K-line, not the indicator parameters, the indicator parameters are the default, the other indicator functions are also symmetric

      此时macd这个变量是一个二维数组(不了解概念可以百度),二维数组简单说就是一个数组它的每个元素也是一个数组。
      问:为什么macd指标数据是个二维数组呢?
      答:因为macd指标是由两条线(dif线,dea线)和一组量柱组成(macd量柱,其实这个量柱数据也可以看成是一条线)。所以macd变量可以拆分为:
    

    var dif = macd[0] var dea = macd[1] var macdColumn = macd[2] What's up? Here's another example of a ready-made teaching tool, and an interesting study:https://www.fmz.com/strategy/151972

    币圈量化交易萌新看过来–带你走近币圈量化(二)


Related

More