পাইথন সংস্করণে একটি কে-লাইন সংশ্লেষণ ফাংশন লিখতে শেখান

লেখক:লিডিয়া, সৃষ্টিঃ ২০২২-১২-২৬ ০৯ঃ২৮ঃ৫৮, আপডেটঃ ২০২৩-০৯-২০ ০৯ঃ৪৮ঃ৪৬

img

পাইথন সংস্করণে একটি কে-লাইন সংশ্লেষণ ফাংশন লিখতে শেখান

কৌশল লেখার এবং ব্যবহার করার সময়, আমরা প্রায়শই কিছু বিরলভাবে ব্যবহৃত কে-লাইন সময়কালের ডেটা ব্যবহার করি। তবে এক্সচেঞ্জ এবং ডেটা উত্সগুলি এই সময়কালের ডেটা সরবরাহ করে না। এটি কেবল বিদ্যমান সময়ের সাথে ডেটা ব্যবহার করে সংশ্লেষিত করা যেতে পারে। সংশ্লেষিত অ্যালগরিদমটির ইতিমধ্যে একটি জাভাস্ক্রিপ্ট সংস্করণ রয়েছে (লিঙ্ক) আসলে, জাভাস্ক্রিপ্ট কোডের একটি টুকরা পাইথনে ট্রান্সপ্ল্যান্ট করা সহজ। এরপরে, কে-লাইন সংশ্লেষণ অ্যালগরিদমের একটি পাইথন সংস্করণ লিখুন।

জাভাস্ক্রিপ্ট সংস্করণ

  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অন্যান্য পার্থক্য শুধুমাত্র ভাষা ব্যাকরণের ক্ষেত্রে (যেমন for loops, Boolean values, logical AND, logical NOT, logical OR ইত্যাদি) ।

মাইগ্রেটেড পাইথন কোডঃ

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


সম্পর্কিত

আরো