आप पायथन संस्करण में एक K-लाइन संश्लेषण समारोह लिखने के लिए सिखाओ

लेखक:लिडिया, बनाया गयाः 2022-12-26 09:28:58, अद्यतन किया गयाः 2023-09-20 09:48:46

img

आप पायथन संस्करण में एक K-लाइन संश्लेषण समारोह लिखने के लिए सिखाओ

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

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

  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 
  }

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

माइग्रेटेड पायथन कोडः

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)

परीक्षण

हुओबी बाजार चार्ट

img

बैकटेस्ट संश्लेषण का 4 घंटे का चार्ट

img

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


संबंधित

अधिक