4
ध्यान केंद्रित करना
1271
समर्थक

आपको K-लाइन संश्लेषण फ़ंक्शन का पायथन संस्करण लिखना चरण दर चरण सिखाएगा

में बनाया: 2019-12-21 09:38:26, को अपडेट: 2024-12-15 15:59:54
comments   4
hits   2834

आपको K-लाइन संश्लेषण फ़ंक्शन का पायथन संस्करण लिखना चरण दर चरण सिखाएगा

आपको K-लाइन संश्लेषण फ़ंक्शन का पायथन संस्करण लिखना चरण दर चरण सिखाएगा

रणनीतियों को लिखते और उपयोग करते समय, कुछ असामान्य K-लाइन चक्र डेटा का अक्सर उपयोग किया जाता है। हालाँकि, एक्सचेंज और डेटा स्रोत इन अवधियों के लिए डेटा उपलब्ध नहीं कराते हैं। इसे केवल मौजूदा चक्रों से डेटा का उपयोग करके ही संश्लेषित किया जा सकता है। संश्लेषण एल्गोरिथ्म का पहले से ही एक जावास्क्रिप्ट संस्करण है (जोड़ना), वास्तव में, जावास्क्रिप्ट कोड को पायथन संस्करण में पोर्ट करना बहुत सरल है। इसके बाद, आइए K-लाइन संश्लेषण एल्गोरिथ्म का पायथन संस्करण लिखें।

जावास्क्रिप्ट संस्करण

  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 
  }

यदि कोई जावास्क्रिप्ट एल्गोरिथ्म है, तो उसे लाइन दर लाइन पायथन में अनुवादित और प्रत्यारोपित किया जा सकता है। जब जावास्क्रिप्ट के अंतर्निहित फ़ंक्शन या अंतर्निहित विधियों का सामना करना पड़ता है, तो बस पायथन में संबंधित विधियों की तलाश करें, इसलिए प्रत्यारोपण अपेक्षाकृत आसान है। एल्गोरिथ्म तर्क बिल्कुल वैसा ही है, बस जावास्क्रिप्ट फ़ंक्शन कॉलvar n = d.getTimezoneOffset()पायथन में पोर्ट करते समय, पायथन की टाइम लाइब्रेरी का उपयोग करेंn = time.altzoneप्रतिस्थापित करें। अन्य अंतर केवल भाषा के वाक्यविन्यास में हैं (जैसे कि फॉर लूप्स का उपयोग, बूलियन मानों में अंतर, लॉजिकल एंड, लॉजिकल नॉट, लॉजिकल ऑर आदि के उपयोग में अंतर)।

प्रत्यारोपित पायथन कोड:

import time

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

परीक्षा

हुओबी मार्केट चार्ट आपको K-लाइन संश्लेषण फ़ंक्शन का पायथन संस्करण लिखना चरण दर चरण सिखाएगा

सिंथेटिक 4-घंटे चार्ट का बैकटेस्टिंग आपको K-लाइन संश्लेषण फ़ंक्शन का पायथन संस्करण लिखना चरण दर चरण सिखाएगा

उपरोक्त कोड का उपयोग केवल सीखने के संदर्भ के रूप में किया जाता है। यदि किसी विशिष्ट रणनीति में इसका उपयोग किया जाता है, तो कृपया इसे अपनी आवश्यकताओं के अनुसार संशोधित करें और परीक्षण करें। यदि आपके पास कोई बग या सुधार के लिए सुझाव है, तो कृपया एक संदेश छोड़ दें, बहुत बहुत धन्यवाद o^_^o