Python中怎么实现一个K线合成函数
发表于:2025-02-01 作者:千家信息网编辑
千家信息网最后更新 2025年02月01日,Python中怎么实现一个K线合成函数,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。JavaScript版本 function Get
千家信息网最后更新 2025年02月01日Python中怎么实现一个K线合成函数
Python中怎么实现一个K线合成函数,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
JavaScript版本
function GetNewCycleRecords (sourceRecords, targetCycle) { // K线合成函数 var ret = [] // 首先获取源K线数据的周期 if (!sourceRecords || sourceRecords.length < 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 }
有JavaScript算法,对于Python其实逐行翻译移植就可以了,遇到JavaScript的内置函数,或者固有方法,对应的去Python中查找对应的方法即可,所以移植还是比较容易的。
算法逻辑完全一模一样,只是JavaScript的函数调用var n = d.getTimezoneOffset()
,移植到Python时,使用Python的time库中的n = time.altzone
代替。其它差异仅仅是语言语法方面的了(例如for循环的使用,布尔值的差别,逻辑与、逻辑非、逻辑或的使用差别等..)。
移植后的Python代码:
import timedef GetNewCycleRecords(sourceRecords, targetCycle): ret = [] # 首先获取源K线数据的周期 if not sourceRecords or len(sourceRecords) < 2 : return None sourceLen = len(sourceRecords) sourceCycle = sourceRecords[-1]["Time"] - sourceRecords[-2]["Time"] if targetCycle % sourceCycle != 0 : Log("targetCycle:", targetCycle) Log("sourceCycle:", sourceCycle) raise "targetCycle is not an integral multiple of sourceCycle." if (1000 * 60 * 60) % targetCycle != 0 and (1000 * 60 * 60 * 24) % targetCycle != 0 : Log("targetCycle:", targetCycle) Log("sourceCycle:", sourceCycle) Log((1000 * 60 * 60) % targetCycle, (1000 * 60 * 60 * 24) % targetCycle) raise "targetCycle cannot complete the cycle." multiple = targetCycle / sourceCycle isBegin = False count = 0 barHigh = 0 barLow = 0 barOpen = 0 barClose = 0 barTime = 0 barVol = 0 for i in range(sourceLen) : # 获取时区偏移数值 n = time.altzone if ((1000 * 60 * 60 * 24) - (sourceRecords[i]["Time"] * 1000) % (1000 * 60 * 60 * 24) + (n * 1000)) % targetCycle == 0 : isBegin = True if isBegin : if count == 0 : barHigh = sourceRecords[i]["High"] barLow = sourceRecords[i]["Low"] barOpen = sourceRecords[i]["Open"] barClose = sourceRecords[i]["Close"] barTime = sourceRecords[i]["Time"] barVol = sourceRecords[i]["Volume"] count += 1 elif count < multiple : barHigh = max(barHigh, sourceRecords[i]["High"]) barLow = min(barLow, sourceRecords[i]["Low"]) barClose = sourceRecords[i]["Close"] barVol += sourceRecords[i]["Volume"] count += 1 if count == multiple or i == sourceLen - 1 : ret.append({ "High" : barHigh, "Low" : barLow, "Open" : barOpen, "Close" : barClose, "Time" : barTime, "Volume" : barVol, }) count = 0 return ret # 测试def main(): while True: r = exchange.GetRecords() r2 = GetNewCycleRecords(r, 1000 * 60 * 60 * 4) ext.PlotRecords(r2, "r2") Sleep(1000)
测试
火币行情图表
回测合成4小时图表
看完上述内容,你们掌握Python中怎么实现一个K线合成函数的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!
函数
方法
逻辑
移植
内容
周期
图表
差别
数值
数据
时区
更多
算法
问题
偏移
测试
一模一样
束手无策
为此
代码
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
神思电子有网络安全概念
百姓部落互联网科技有限公司
上届奥运会男足比分数据库
手机测试服务器失败
软件开发工作很忙吗
网络安全初中主题班会照片
阿里云的数据库连不上
计算机软件开发图片
互联网软件开发时代
idc机房网络安全如何
徐州市贾汪区统计局数据库
cs1.6服务器管理页面
互联网十大高科技股
显示器软件开发招聘
嵌入式软件开发全套课程
超微服务器bmc口
数据库测试案例设计
网络技术职业规划人物访谈
重装上阵服务器自动运行
内蒙古数据中心服务器
数据库配置需求
服务器安装手机软件
如何把数据库变可疑
快快盾服务器
北京无线网络安全
数据库软件下载视频
首都网络安全日的口号是
数据库sql指针查询
csgo官匹韩国服务器脱机
手游2k20 无法连接服务器