2.1 Use the API to access account information, market data, K-line data, and market depth

Author: The Little Dream, Created: 2016-11-05 16:41:04, Updated: 2019-08-01 09:31:23

2.1 Use the API to access account information, market data, K-line data, and market depth

Finally, it's time for chapter two, which should give you some insight into inventor quantization's various functions by taking a tour of the previous chapter.


  • Use the API to access account information

    Assuming that the visitor through the first chapter has already added the custodian, the exchange, of course we can use the inventor to quantify the simulation disk for code testing.

    So let's first create a strategy, let's call it test 1.img

    The strategy editor interfaceimg img

    The code is as follows:

function main() {
    Log(exchange.GetAccount()); // 看过API 文档后知道, exchange就是交易所对象,实际上策略有个全局变量
                                // exchanges 数组,这个数组存放的就是你创建机器人或者回测时添加的交易所(可以是多个)
                                // 添加1个交易所 exchanges 数组就只包含1个交易所对象,即添加的交易所对象。
                                // 那么 exchange 和 exchanges 是什么关系呢? 其实 exchange 就是 exchanges[0] ,
                                // exchange 就是 exchanges 数组的第一个元素(这里这个元素是交易所对象)。
                                
                                // Log()函数应该也不陌生吧,这个API 就是输出一条日志,日志内容就是Log括号里面的参数。
}

Creating a robot, also called Test1 binding, is a strategy called the "Ring Test1", quantified by the inventors of the Analog Disk Test.img img

The policy is executed instantly and displays an account message.img

It turns out that we're going to compare the account information with the analogue disk.img

  • Access to market data

function main() {
    Log(exchange.GetAccount()); // 已经知道怎么获取 主交易所 账户信息了
    //下面我们来试试 不停的获取行情数据。 
    while(true){ // 这里用一个无限循环的结构来不停的获取 交易所行情数据。
        Log("行情数据:", exchange.GetTicker()); // 哇! Log() 函数的括号里面可以写2个参数,第一个参数是: "行情数据:"
                                               // 第二个参数是 exchange.GetTicker() 这个函数的返回值。就是主交易所的行情数据。
                                               // 注意 Log() 函数的参数要用 逗号分隔。
        Sleep(1000);   // 咦~ 这个又是什么? 答:机器人程序 执行循环也需要休息!它可是执行很快的哦!(一秒N次)
                       // Sleep 函数的作用就是让程序暂停一会儿,括号里面的参数 1000 是 毫秒数, 1秒 = 1000毫秒。Sleep(1000);就是暂停1000毫秒。
                       // 不要小看这个参数,这个参数控制了程序的轮询频率,间接影响访问 交易所API 的频率,有些交易所API访问过于频繁可是会拒绝访问的。
    }
}

img Note: This is not the case.You may have noticed that the highest and lowest prices of the data obtained by the GetTicker function are very different, so the High and Low of the market data returned by GetTicker are the highest and lowest prices within the exchange's agreed cycle, depending on the exchange's setting.

  • Obtaining K-line data

    Let's first look at the description of the API documentation:
GetRecords(Period)	返回一个K线历史, K线周期在创建机器人时指定, Record数组结构
不加参数, 默认返回添加机器人时时指量的K线周期, 但也可以自定义K线周期
支持: PERIOD_M1 指1分钟, PERIOD_M5 指5分钟, PERIOD_M15 指15分钟, PERIOD_M30 指30分钟, PERIOD_H1 指1小时, PERIOD_D1 指一天

Let's write a piece of code to test the K-line data for the default cycle (five minutes).

function main() {
    Log(exchange.GetAccount()); // 已经知道怎么获取 主交易所 账户信息了
    //下面我们来试试 不停的获取行情数据。 
    var records = exchanges[0].GetRecords();  // 按照默认周期获取K线数据
    Log("records:", records);  // 在日志中输出 获取到的K线数据。
}

Display the output: records: [{Time:1478260200000,Open:4765.14,High:4773,Low:4764.54,Close:4769.47,Volume:5211.539999999999}, which is the first time I've seen this movie. I've been trying to get a hold of you for a while, but I can't. I've been trying to get a hold of you for a while, but I can't. This is the first time I've ever seen a movie with a female protagonist. It is the most commonly used type of anti-inflammatory drug. This is a list of all the different ways Openum is credited in the database.

As you can see, the variable records is a structural array that is arranged in K-line time sequence from far (index0) to close (indexrecords.length-1). Let's get to know the K-line by the way: (some graphs are red for the sun, green for the vagina, some are the opposite color.)imgSee below for an example of a K-line chart with a 5-minute cycle on the platform.img Please note:A K-line cycle is completed and its value is determined. In practical use, we call var records = exchanges. GetRecords (); the last element in the data records array returned is records[records.length-1], which is constantly changing until its cycle is completed.

The GetRecords function returns data according to the default cycle set by the policy without adding parameters. It is also possible to pass parameters to specify the cycle of the K-line. Currently, the retrieval system supports the GetRecords pass parameters to specify the cycle (return data according to the default cycle without adding parameters), so that the policy can be retrieved using different cycles at the same time.

  • Get in-depth information on the market

    GetDepth returns a depth structure
function main() {
    var depth = exchanges[0].GetDepth();   //获取市场深度信息,  返回订单薄信息,一个对象包含2个属性,每个属性是一个对象数组。
    Log("depth:", depth);    // 日志中输出,一下的输出是 整理过的格式,是方便读者理解,实际上是所有内容都在一行显示的。
}

The above code retest shows:

depth: 
{"Asks":[{"Price":4726.07,"Amount":15},   // 卖单数组,回测时,数据都是模拟出来的,所以Amount 都是 15,索引为0的是卖一,依次类推。
         {"Price":4726.08,"Amount":15},
         {"Price":4726.09,"Amount":15},
         {"Price":4726.1,"Amount":15},
         {"Price":4726.11,"Amount":15},
         {"Price":4726.12,"Amount":15},
         {"Price":4726.13,"Amount":15},
         {"Price":4726.14,"Amount":15},
         {"Price":4726.15,"Amount":15},
         {"Price":4726.16,"Amount":15},
         {"Price":4726.17,"Amount":15}],
"Bids":[{"Price":4726.05,"Amount":15},   //  买单数组,索引为0的是买一, 向后依次类推。
        {"Price":4726.04,"Amount":15},
        {"Price":4726.03,"Amount":15},
        {"Price":4726.02,"Amount":15},
        {"Price":4726.01,"Amount":15},
        {"Price":4726,"Amount":15},
        {"Price":4725.99,"Amount":15},
        {"Price":4725.98,"Amount":15},
        {"Price":4725.97,"Amount":15},
        {"Price":4725.96,"Amount":15},
        {"Price":4725.95,"Amount":15}]
}

The corresponding order density is as follows ((here are the actual data for OKCoin)); market depth information during the actual process ((order density) is changing quickly, interested students can register for OKCoin and then log in to see it.

img

What is the use of deep market information (discount data)? Discount data has many uses, for example, for eating lists (of course there are also hanging lists).

function main() {    
    var depth = exchanges[0].GetDepth();    // 获取市场深度
    Log("depth:", depth);                   // 日志输出显示
    Log(exchanges[0].GetAccount());         // 输出 吃单前的 账户信息
    var buyPrice = depth.Asks[0].Price;     // 设置吃卖单的价格,即卖一,
                                            // 有时为确保吃单成功,这样处理:var buyPrice = depth.Asks[0].Price + slidePrice;
    var buyAmount = depth.Asks[0].Amount;   // 吃卖单的量
    exchanges[0].Buy(buyPrice, buyAmount);  // 执行买入操作, 吃掉卖一 这个单子
    Log(exchanges[0].GetAccount());         // 显示买入后的  账户信息,对比初始账户信息。可以对比出 买入操作的成交的数量。
}

The results of the inventor's quantified analog drive run:img


More

flydogThe second thing I found was that after I selected the US public host, the bot didn't have that warning symbol in the list, but when I ran it, it was already no one was exporting any information in the logs, suggesting no log information.

flydogAdd to the problem the finding that in the list of bots, the status is that there is an exclamation mark in the orange triangle, as if it were an exceptional cue.

flydogHi, I'm testing with the platform simulation, first step is to test the account information, no log output, error prompt, don't know where the problem is? The robot test code is: function main (() { Log ((exchange.GetAccount (())); Log (("test"); I'm not sure. The result: Test one This is a list of all the different ways Test 1 is credited in the database. Dates: Created on 2018-09-13 14:07:57 Recently started on 2018-09-13 14:40:24 Stopped on 2018-09-13 14:40:25 Status: K line cycle 1 minute There is an error Hosted in China: 42.236.82.38 - linux/amd64 (public), ID: 118025 The exchange is called BotVS/BTC_USD.

flydogHi, I'm testing with the platform simulation, first step is to test the account information, no log output, error prompt, don't know where the problem is? The robot test code is: function main (() { Log ((exchange.GetAccount (())); Log (("test"); I'm not sure. The result: Test one This is a list of all the different ways Test 1 is credited in the database. Dates: Created on 2018-09-13 14:07:57 Recently started on 2018-09-13 14:40:24 Stopped on 2018-09-13 14:40:25 Status: K line cycle 1 minute There is an error Hosted in China: 42.236.82.38 - linux/amd64 (public), ID: 118025 The exchange is called BotVS/BTC_USD.

hokshelatoGetTicker: timeout**, please tell me why. GetTicker: timeout**, please tell me why.

dyhhuHi, is the real-time simulation data from that exchange real-time data?

bijiasuo xuexizhong

shandianliyuPlease exchange.GetDepth (()) returns all the pending information from the current exchange?

maohbaoGetRecords ((Period)) returns a K-line history, but how many K-line bars does this K-line history data contain, and where does this specify?

penglihengOkay, I got the key wrong, I fixed it.

penglihengGetAccount: Signatures not matched How did you do it, Monk?

Andy2simpleWhy is the amount of analogue disc depth always 15? Is the analogue disc the past data taken or the randomly generated data? If it is past data, the amount can be a value.

cjz140What is length? is depth an object? what is the difference between an object and a function? for example, depth (asks, price, amount) where depth is an object and asks is an array? price and amount are attributes?

FangBeiWhat is slidePrice?

The Little DreamIf you have any questions, you can add the official QQ group @administrator.

flydogThank you so much for your reply, I followed the advice and tried again.

The Little DreamI don't know if it's a public host problem, you can test it with a private host, a private host should be possible, you deploy one on your own computer and it works.

The Little DreamYou can try it with your own private host.

The Little DreamThis may be specific to what kind of execution logic the policy is, or it may be that the policy is running normally, but without triggering any action.

The Little DreamA red flag indicates that the robot is running an error report, and that you need to check the robot log or the administrator log information.

The Little DreamRecent network problems, in addition to the fact that the analog disk is out of date when the behavior is not updated, can be adjusted using the exchange.IO ("mode") behavior acquisition mode.

The Little DreamThe simulated market is a 24-hour simulated market that follows the market trends of several mainstream exchanges. All users of the simulated market are participants in this simulated market.

shandianliyuOkay, I see. Thank you.

The Little DreamThis is a real-time scale of measurement, buying and selling 1 file is real data.

shandianliyuThank you very much. Also, please ask, when simulating the real-disc simulation of the analogue retest, exchange.GetDepth (()) returns data that is BotVS-compatible or real historical data?

The Little DreamReturning file quantity See how much the exchange API interface provides, some exchanges return more, some less, and some exchanges interface support Deep Merge, some do not support, specifically depending on the exchange implementation, BotVS is packaged with default returning data, if you want to call directly, you can use the exchange.IO function, set parameters Call ((This function is free of signature, see the API documentation for details)).

The Little DreamThis does not specify how many return K lines are specifically pushed by the exchange, each exchange may have a different number of pushes, some other exchanges do not provide a K line interface, the custodian will collect the transaction records of the exchange itself, generating K lines, which are accumulated from the first root.

The Little Dream^^ At the beginning, I was wrong too~

The Little DreamWell, let's think about it, there are two main points: - 1, the depth of data is so huge, it changes every second, and it changes so fast, that the actual dish we see is just a slice of data. - 2, even if actual depth data is done, it is not possible to accurately simulate ordered shallow-depth environments, because the participants in the retest at this time are only ourselves and there are no other participants. But we will consider optimizing as close to reality as possible, thank you for your suggestions ^^

Andy2simpleDeep data is not yet happening, and price curves have happened in the past. Individuals believe that data from the past and the future together is more reliable for judging current buying and selling. It is strongly recommended to record deep data as well.

The Little DreamThe depth data is too large. If all the data are recorded, there will be too many.

The Little Dreamrecords is a variable that is used to receive K-line data returned by the API function GetRecords, length is the length property of an array type variable in the JS language, representing the length of the array (i.e. the number of elements in it), depth is similar to records is also a variable that is used to receive depth data returned by the GetDepth function, the data structure of these records, depth, ticker can be found in the API documentation.

The Little DreamThe price of the food is a little bit higher than the price of the food, which is easier to negotiate.