PythonのバージョンでK線合成関数を書くことを教える

作者: リン・ハーンリディア作成日:2022-12-26 09:28:58 更新日:2023-09-20 09:48:46 更新日:2020-09-20 更新日:2020-09-20 09:48:46 更新日:2020-09-20 更新日:2020-09-20 更新日:2020-09-20 更新日:2020-09-20 更新日:2020-09-20 更新日:2020-09-20 更新日:2020-09-20 更新日:2020-09-20 更新日:2020-09-20 更新日:2020-09-20 更新日:2020-09-20 更新日:2020-09-20

img

PythonのバージョンでK線合成関数を書くことを教える

戦略を書くときや使うとき,私たちはしばしば,ほとんど使われないK線期間のデータを使います.しかし,交換やデータソースは,これらの期間のデータを提供しません.既存の期間のデータを使用することによってのみ合成できます.合成アルゴリズムには既にJavaScriptバージョンがあります (リンク) 実際,JavaScriptコードを Python に移植するのは簡単です.次に,K線合成アルゴリズムの Python 版を書こう.

JavaScript バージョン

  function GetNewCycleRecords (sourceRecords, targetCycle) {    // K-line synthesis function
      var ret = []
      
      // Obtain the period of the source K-line data first
      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++) {
          // Get the time zone offset value
          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 に移行する際には,n=time.altzonePythonの時間ライブラリでは,その代わりに使用される.他の違いは言語文法 (for for ループ,ブール値,論理 AND,論理 NOT,論理 ORなど) のみである.

移行された Python コード:

import time

def GetNewCycleRecords(sourceRecords, targetCycle):
    ret = []

    # Obtain the period of the source K-line data first
    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) : 
        # Get the time zone offset value
        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 

# Test
def main():
    while True:
        r = exchange.GetRecords()
        r2 = GetNewCycleRecords(r, 1000 * 60 * 60 * 4)      

        ext.PlotRecords(r2, "r2")                                 
        Sleep(1000)

テスト

Huobi市場グラフ

img

バックテスト合成の4時間グラフ

img

上記コードは参照のみです.特定の戦略で使用される場合は,特定の要件に従って変更およびテストしてください. バグや改善の提案があれば メッセージを残してください ありがとうございました o^_^ o


関連性

もっと