日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python k线合成_手把手教你写一个Python版的K线合成函数

發(fā)布時間:2024/10/14 python 77 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python k线合成_手把手教你写一个Python版的K线合成函数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

手把手教你寫一個Python版的K線合成函數(shù)

在編寫、使用策略時,經(jīng)常會使用一些不常用的K線周期數(shù)據(jù)。然而交易所、數(shù)據(jù)源又沒有提供這些周期的數(shù)據(jù)。只能通過使用已有周期的數(shù)據(jù)進行合成。合成算法已經(jīng)有一個JavaScript版本了(鏈接),其實移植一段JavaScript代碼為Python版本很簡單。接下來我們一起寫一個Python版本的K線合成算法。

JavaScript版本

function GetNewCycleRecords (sourceRecords, targetCycle) { // K線合成函數(shù)

var ret = []

// 首先獲取源K線數(shù)據(jù)的周期

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++) {

// 獲取 時區(qū)偏移數(shù)值

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的內(nèi)置函數(shù),或者固有方法,對應的去Python中查找對應的方法即可,所以移植還是比較容易的。

算法邏輯完全一模一樣,只是JavaScript的函數(shù)調(diào)用var n = d.getTimezoneOffset(),移植到Python時,使用Python的time庫中的n = time.altzone代替。其它差異僅僅是語言語法方面的了(例如for循環(huán)的使用,布爾值的差別,邏輯與、邏輯非、邏輯或的使用差別等…)。

移植后的Python代碼:

import time

def GetNewCycleRecords(sourceRecords, targetCycle):

ret = []

# 首先獲取源K線數(shù)據(jù)的周期

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) :

# 獲取時區(qū)偏移數(shù)值

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小時圖表

以上代碼僅作為學習參考使用,如果用于具體策略中,請根據(jù)需求修改、測試。

如有BUG或者改進建議,歡迎留言,十分感謝 o^_^o

總結(jié)

以上是生活随笔為你收集整理的python k线合成_手把手教你写一个Python版的K线合成函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。