Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with K-line data in programmed transactions with Go language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how Go language deals with K-line data in programmed transactions". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "Go language how to deal with K-line data in programmed transactions" can help you solve your doubts.

Preface

When writing a programmed trading strategy, using K-line data, there is often a need to use some non-standard periodic K-line data, such as 12-minute cycle K-line data and 4-hour K-line cycle data. usually this kind of non-standard period can not be obtained directly. So how do we deal with this kind of demand?

There must be a way to answer.

Non-standard periods can be obtained by merging and synthesizing data with smaller periods. You can imagine that the highest price in multiple cycles is counted as the highest price after synthesis, and the lowest price is counted as the lowest price after synthesis, and the opening price will not change. On the basis of the first opening price of the raw material data of the synthetic K line, the closing price corresponds to the closing price of the last raw material data of the synthetic K line. Time is the time of the opening price, and the trading volume is calculated by summing the trading volume of the raw material data.

Train of thought

We take the blockchain asset market BTC_USDT as an example, which takes 1 hour to synthesize into 4 hours.

You can see that the data are consistent.

Write code to implement

After verifying the preliminary idea, you can start to write code to initially realize this requirement. Directly release the code, the code is for reference only:

Function GetNewCycleRecords (sourceRecords, targetCycle) {/ / K line synthesis function var ret = [] / / first get the period if (! sourceRecords | | sourceRecords.length) of the source K line data

< 2) { return null } var sourceLen = sourceRecords.length var sourceCycle = sourceRecords[sourceLen - 1].Time - sourceRecords[sourceLen - 2].Time if (targetCycle % sourceCycle != 0) { Log("targetCycle:", targetCycle) Log("sourceCycle:", sourceCycle) throw "targetCycle is not an integral multiple of sourceCycle." } if ((1000 * 60 * 60) % targetCycle != 0 && (1000 * 60 * 60 * 24) % targetCycle != 0) { Log("targetCycle:", targetCycle) Log("sourceCycle:", sourceCycle) Log((1000 * 60 * 60) % targetCycle, (1000 * 60 * 60 * 24) % targetCycle) throw "targetCycle cannot complete the cycle." } var multiple = targetCycle / sourceCycle var isBegin = false var count = 0 var high = 0 var low = 0 var open = 0 var close = 0 var time = 0 var vol = 0 for (var i = 0 ; i < sourceLen ; i++) { // 获取 时区偏移数值 var d = new Date() var n = d.getTimezoneOffset() if (((1000 * 60 * 60 * 24) - sourceRecords[i].Time % (1000 * 60 * 60 * 24) + (n * 1000 * 60)) % targetCycle == 0) { isBegin = true } if (isBegin) { if (count == 0) { high = sourceRecords[i].High low = sourceRecords[i].Low open = sourceRecords[i].Open close = sourceRecords[i].Close time = sourceRecords[i].Time vol = sourceRecords[i].Volume count++ } else if (count < multiple) { high = Math.max(high, sourceRecords[i].High) low = Math.min(low, sourceRecords[i].Low) close = sourceRecords[i].Close vol += sourceRecords[i].Volume count++ } if (count == multiple || i == sourceLen - 1) { ret.push({ High : high, Low : low, Open : open, Close : close, Time : time, Volume : vol, }) count = 0 } } } return ret } // 测试 function main () { while (true) { var r = exchange.GetRecords() // 原始数据,作为合成K线的基础K线数据,例如要合成4小时K线,可以用1小时K线作为原始数据。 var r2 = GetNewCycleRecords(r, 1000 * 60 * 60 * 4) // 通过 GetNewCycleRecords 函数 传入 原始K线数据 r , 和目标周期, 1000 * 60 * 60 * 4 即 目标合成的周期 是4小时K线数据。 $.PlotRecords(r2, "r2") // 策略类库栏 可以勾选画线类库,调用 $.PlotRecords 画线类库 导出函数 画图。 Sleep(1000) // 每次循环间隔 1000 毫秒,防止访问K线接口获取数据过于频繁,导致交易所限制。 } } 其实要合成K线,就需要两个东西,第一是需要原料数据,即小周期的K线数据,例子中 var r = exchange.GetRecords()。获取的小周期K线数据。第二是需要明确合成为多大的周期,即K线数据合成的目标周期。然后通过GetNewCycleRecords函数的算法,就可以最后返回一个合成出来的K线数组结构的数据了。实盘运行了一下: 对比交易所图表 需要注意的是:目标周期不能小于你传入GetNewCycleRecords 函数作为数据原料的K线的周期,因为无法用小周期去合成更小的周期的数据。设置的目标周期必须是周期闭合的。例如 12分钟周期的K线,从每个小时的0分0秒开始(以0时举例),第一个周期是00:00:00 ~ 00:12:00,第二个周期是00:12:00 ~ 00:24:00,第三个周期是00:24:00 ~ 00:36:00,第四个周期是00:36:00 ~ 00:48:00,第五个周期是00:48:00 ~ 01:00:00 ,正好组成一个完整的1小时。如果是 13分钟周期,就是不闭合的周期,这样的周期算出的数据不唯一,因为根据合成的数据起始点不同,合成出来的数据有差异。 使用K线数据构造需要的数据结构 经常有群友提问,我想计算每根K线的最高价的均线,怎么办?通常,我们计算均线都是计算的收盘价的均值,组成均线,但是也有时候有需求计算最高价、最低价、开盘价等等。这个时候就不能直接把exchange.GetRecords() 函数返回的K线数据直接传入 指标计算函数了。例如:talib.MA 均线指标计算函数有两个参数,第一个参数是需要传入的数据,第二个参数是指标周期参数。例如我们要算如下图的指标: K线周期是4小时,在交易所图表上,已经设置好了一条均线,均线周期参数为9。并且设置计算的数据源是每根Bar的最高价。 即这条均线是9个4小时周期K线Bar的最高价平均计算出的均值,组成的指标均线。我们自己动手构造一个数据算下,看是不是和交易所的图表计算得出的一样。 var highs = []for (var i = 0 ; i < r2.length ; i++) { highs.push(r2[i].High)} 既然要计算每根Bar的最高价的均值得出均线指标。那么就需要先构造一个数组,其中每个数据元素都是对应每根Bar的最高价。可以看到 highs 变量初始为一个空数组,然后我们遍历 r2 这个K线数据变量(不记得r2了?看下上面合成4小时K线的main函数中的代码)。读取r2每根Bar的最高价(即 r2[i].High , i取值范围 从 0 到 r2.length - 1 ),然后 push 进highs 。这样就构造了一个和K线数据Bar一一对应的数据结构。此时 highs 就可以传入 talib.MA函数计算出均线了。 完整的例子: function main () { while (true) { var r = exchange.GetRecords() var r2 = GetNewCycleRecords(r, 1000 * 60 * 60 * 4) if (!r2) { continue } $.PlotRecords(r2, "r2") // 画出K线 var highs = [] for (var i = 0 ; i < r2.length ; i++) { highs.push(r2[i].High) } var ma = talib.MA(highs, 9) // 用均线指标函数 talib.MA 计算 均线指标 $.PlotLine("high_MA9", ma[ma.length - 2], r2[r2.length - 2].Time) // 使用画线类库把均线指标画在图表上 Sleep(1000) }} 回测运行: 可以看到 图中鼠标停留位置的均线指标值均为 11466.9289。以上代码可以复制到策略中运行测试,记得勾选「画线类库」后保存! 数字货币市场的K线数据获取方式 发明者量化交易平台已经有封装好的接口,即 exchange.GetRecords 函数,即可获取K线数据。下面着重讲解的是直接访问交易所K线数据接口获取数据,因为有时候需要指定参数获取更多的K线,封装的GetRecords 接口一般是返回 100根。如果遇到策略初始需要超过100根的K线时,就需要收集等待。 为了让策略尽快进行运作,可以自己封装一个函数,直接访问交易所K线接口,指定参数获取更多的K线数据。以火币币币交易 BTC_USDT 交易对为例,我们实现这个需求:找到交易所的API文档,查看K线接口描述: https://api.huobi.pro/market/history/kline?period=1day&size=200&symbol=btcusdt 参数:

Test the code:

Function GetRecords_Huobi (period, size, symbol) {var url = "https://api.huobi.pro/market/history/kline?" +" period= "+ period +" & size= "+ size +" & symbol= "+ symbol var ret = HttpQuery (url) try {var jsonData = JSON.parse (ret) var records = [] for (var I = jsonData.data.length-1; I > = 0 ) {records.push ({Time: jsonData.data [I] .id * 1000, High: jsonData.data [I] .high, Open: jsonData.data [I] .open, Low: jsonData.data [I] .low, jsonData.data [I] .close Volume: jsonData.data [I] .vol,}} return records} catch (e) {Log (e)}} function main () {var records = GetRecords_Huobi ("1day", "300"," btcusdt ") Log (records.length) $.PlotRecords (records," K ")}

You can see that on the log, the print records.length is 300, that is, the number of records K-line data bar is 300.

Read here, the "Go language how to deal with programmed transactions in the K-line data" article has been introduced, want to master the knowledge of this article still need to practice and use in order to understand, if you want to know more related articles, welcome to pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report