আপনি বাজার সংগ্রাহক আপগ্রেড শেখান ব্যাকটেস্ট কাস্টম তথ্য উৎস

লেখক:ভাল, তৈরিঃ 2020-06-06 08:53:02, আপডেটঃ 2023-11-01 20:28:58

img

পূর্ববর্তী নিবন্ধআপনাকে শেখান কিভাবে একটি বাজার কোট সংগ্রহকারী বাস্তবায়নআমরা একটি রোবট প্রোগ্রাম বাস্তবায়ন করেছি যা একসাথে বাজার কোট সংগ্রহ করে।

আমরা এটি সংগ্রহ করার পরে কীভাবে বাজারের ডেটা ব্যবহার করব? এটি ব্যাকটেস্ট সিস্টেমের জন্য ব্যবহার করা হবে। এফএমজেড প্ল্যাটফর্ম ব্যাকটেস্ট সিস্টেমের কাস্টম ডেটা উত্স ফাংশনের উপর নির্ভর করে, আমরা সংগৃহীত ডেটা সরাসরি ব্যাকটেস্ট সিস্টেমের ডেটা উত্স হিসাবে ব্যবহার করতে পারি, যাতে আমরা ব্যাকটেস্ট সিস্টেমকে যে কোনও বাজারে ব্যবহার করতে পারি যেখানে আমরা ঐতিহাসিক ডেটা ব্যাকটেস্ট করতে চাই।

সুতরাং, আমরা মার্কেট কোট কালেক্টর একটি আপগ্রেড দিতে পারেন! মার্কেট কালেক্টর এছাড়াও ব্যাকটেস্ট সিস্টেমে তথ্য প্রদানের জন্য একটি কাস্টম তথ্য উৎস হিসাবে পরিবেশন করতে পারেন.

প্রস্তুত হও

এটি পূর্ববর্তী নিবন্ধের প্রস্তুতির কাজ থেকে আলাদা। শেষবার আমার স্থানীয় ম্যাক কম্পিউটারে একটি ডকার প্রোগ্রাম চলছে, ডাটাবেস পরিষেবা শুরু করতে মঙ্গোডব ডাটাবেস ইনস্টল করা। এই সময় আমরা অপারেটিং পরিবেশটি ভিপিএসে পরিবর্তন করেছি এবং আমাদের প্রোগ্রামগুলির সেট চালানোর জন্য আলিবাবা ক্লাউড লিনাক্স সার্ভার ব্যবহার করেছি।

  • Mongodb ডাটাবেজ

পূর্ববর্তী নিবন্ধের মত, আমাদের মঙ্গোডব ডাটাবেস ইনস্টল করতে হবে ডিভাইসে যেখানে মার্কেট কালেক্টর প্রোগ্রাম চলছে এবং পরিষেবাটি শুরু করতে হবে। এটি মূলত ম্যাক কম্পিউটারে মঙ্গোডব ইনস্টল করার মতোই। ইন্টারনেটে অনেক টিউটোরিয়াল রয়েছে, আপনি এটি গুগল করতে পারেন, এটি খুব সহজ।

  • পাইথন 3 ইনস্টল করুন

প্রোগ্রামটি পাইথন 3 ব্যবহার করে, কিছু পাইথন লাইব্রেরি ব্যবহারের দিকে মনোযোগ দিন, যদি তারা ইনস্টল না হয়, তাহলে আপনাকে প্রথমে তাদের ইনস্টল করতে হবে।

পিমঙ্গো http উর্লিব

  • ডকার

এফএমজেড ডকিং যথেষ্ট হবে।

মার্কেট কোট কালেক্টরকে রূপান্তর করুন

বাজার কোট সংগ্রহকারী এই হলঃhttps://www.fmz.com/strategy/199120(RecordsCollector) কৌশল.

এর কিছু পরিবর্তন করা যাক:

তথ্য সংগ্রহের জন্য প্রোগ্রামটি যখন লুপে প্রবেশ করে, তখন একটি মাল্টি-থ্রেডেড লাইব্রেরি ব্যবহার করা হয় এবং একই সাথে এক্সিকিউশনটি এফএমজেড প্ল্যাটফর্ম ব্যাকটেস্ট সিস্টেমের ডেটা অনুরোধ পর্যবেক্ষণের জন্য একটি পরিষেবা শুরু করে। (অন্যান্য বিবরণ উপেক্ষা করা যেতে পারে)

RecordsCollector (কস্টম ডেটা উৎস ফাংশন প্রদানের জন্য আপগ্রেড)

import _thread
import pymongo
import json
import math
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import parse_qs, urlparse

def url2Dict(url):
    query = urlparse(url).query  
    params = parse_qs(query)  
    result = {key: params[key][0] for key in params}  
    return result

class Provider(BaseHTTPRequestHandler):
    def do_GET(self):
        try:
            self.send_response(200)
            self.send_header("Content-type", "application/json")
            self.end_headers()

            dictParam = url2Dict(self.path)
            Log("The custom data source service receives the request, self.path:", self.path, "query parameter:", dictParam)
            
            # At present, the backtesting system can only select the exchange name from the list. When adding a custom data source, set it to Binance, that is: Binance
            exName = exchange.GetName()                                     
            # Note that period is the bottom K-line period
            tabName = "%s_%s" % ("records", int(int(dictParam["period"]) / 1000))  
            priceRatio = math.pow(10, int(dictParam["round"]))
            amountRatio = math.pow(10, int(dictParam["vround"]))
            fromTS = int(dictParam["from"]) * int(1000)
            toTS = int(dictParam["to"]) * int(1000)
            
            
            # Connect to the database
            Log("Connect to the database service to obtain data, the database:", exName, "table:", tabName)
            myDBClient = pymongo.MongoClient("mongodb://localhost:27017")
            ex_DB = myDBClient[exName]
            exRecords = ex_DB[tabName]
            
            
            # Request data
            data = {
                "schema" : ["time", "open", "high", "low", "close", "vol"],
                "data" : []
            }
            
            # Construct query condition: greater than a certain value{'age': {'$gt': 20}} Less than a certain value{'age': {'$lt': 20}}
            dbQuery = {"$and":[{'Time': {'$gt': fromTS}}, {'Time': {'$lt': toTS}}]}
            Log("Query conditions:", dbQuery, "Number of inquiries:", exRecords.find(dbQuery).count(), "Total number of databases:", exRecords.find().count())
            
            for x in exRecords.find(dbQuery).sort("Time"):
                # Need to process data accuracy according to request parameters round and vround
                bar = [x["Time"], int(x["Open"] * priceRatio), int(x["High"] * priceRatio), int(x["Low"] * priceRatio), int(x["Close"] * priceRatio), int(x["Volume"] * amountRatio)]
                data["data"].append(bar)
            
            Log("data:", data, "Respond to backtest system requests.")
            # Write data reply
            self.wfile.write(json.dumps(data).encode())
        except BaseException as e:
            Log("Provider do_GET error, e:", e)


def createServer(host):
    try:
        server = HTTPServer(host, Provider)
        Log("Starting server, listen at: %s:%s" % host)
        server.serve_forever()
    except BaseException as e:
        Log("createServer error, e:", e)
        raise Exception("stop")

def main():
    LogReset(1)
    exName = exchange.GetName()
    period = exchange.GetPeriod()
    Log("collect", exName, "Exchange K-line data,", "K line cycle:", period, "second")
    
    # Connect to the database service, service address mongodb://127.0.0.1:27017 See the settings of mongodb installed on the server
    Log("Connect to the mongodb service of the hosting device, mongodb://localhost:27017")
    myDBClient = pymongo.MongoClient("mongodb://localhost:27017")   
    # Create a database
    ex_DB = myDBClient[exName]
    
    # Print the current database table
    collist = ex_DB.list_collection_names()
    Log("mongodb ", exName, " collist:", collist)
    
    # Check if the table is deleted
    arrDropNames = json.loads(dropNames)
    if isinstance(arrDropNames, list):
        for i in range(len(arrDropNames)):
            dropName = arrDropNames[i]
            if isinstance(dropName, str):
                if not dropName in collist:
                    continue
                tab = ex_DB[dropName]
                Log("dropName:", dropName, "delete:", dropName)
                ret = tab.drop()
                collist = ex_DB.list_collection_names()
                if dropName in collist:
                    Log(dropName, "failed to delete")
                else :
                    Log(dropName, "successfully deleted")
    
    # Start a thread to provide a custom data source service
    try:
        # _thread.start_new_thread(createServer, (("localhost", 9090), ))     # local computer test
        _thread.start_new_thread(createServer, (("0.0.0.0", 9090), ))         # Test on VPS server
        Log("Open the custom data source service thread", "#FF0000")
    except BaseException as e:
        Log("Failed to start the custom data source service!")
        Log("Error message:", e)
        raise Exception("stop")
    
    # Create the records table
    ex_DB_Records = ex_DB["%s_%d" % ("records", period)]
    Log("Start collecting", exName, "K-line data", "cycle:", period, "Open (create) the database table:", "%s_%d" % ("records", period), "#FF0000")
    preBarTime = 0
    index = 1
    while True:
        r = _C(exchange.GetRecords)
        if len(r) < 2:
            Sleep(1000)
            continue
        if preBarTime == 0:
            # Write all BAR data for the first time
            for i in range(len(r) - 1):
                bar = r[i]
                # Write line by line, you need to determine whether the data already exists in the current database table, based on timestamp detection, if there is the data, then skip, if not write in
                retQuery = ex_DB_Records.find({"Time": bar["Time"]})
                if retQuery.count() > 0:
                    continue
                
                # Write bar to the database table
                ex_DB_Records.insert_one({"High": bar["High"], "Low": bar["Low"], "Open": bar["Open"], "Close": bar["Close"], "Time": bar["Time"], "Volume": bar["Volume"]})                
                index += 1
            preBarTime = r[-1]["Time"]
        elif preBarTime != r[-1]["Time"]:
            bar = r[-2]
            # Check before writing data, whether the data already exists, based on time stamp detection
            retQuery = ex_DB_Records.find({"Time": bar["Time"]})
            if retQuery.count() > 0:
                continue
            
            ex_DB_Records.insert_one({"High": bar["High"], "Low": bar["Low"], "Open": bar["Open"], "Close": bar["Close"], "Time": bar["Time"], "Volume": bar["Volume"]})
            index += 1
            preBarTime = r[-1]["Time"]
        LogStatus(_D(), "preBarTime:", preBarTime, "_D(preBarTime):", _D(preBarTime/1000), "index:", index)
        # adding drawing display
        ext.PlotRecords(r, "%s_%d" % ("records", period))
        Sleep(10000)

পরীক্ষা

রোবট কনফিগার করুন

img

রোবট চালাও, বাজার কোট সংগ্রহকারী চালাও।

img

ব্যাকটেস্টের জন্য একটি টেস্ট কৌশল খুলুন। উদাহরণস্বরূপঃ

function main() {
    Log(exchange.GetRecords())
    Log(exchange.GetRecords())
    Log(exchange.GetRecords())
    Log(exchange.GetRecords())
    Log(exchange.GetRecords())
    Log(exchange.GetRecords())
    Log(exchange.GetRecords().length)
}

ব্যাকটেস্ট অপশনটি কনফিগার করুন, এক্সচেঞ্জটি বিন্যান্সে সেট করুন কারণ অস্থায়ী কাস্টম ডেটা উত্সটি এখনও নিজের দ্বারা এক্সচেঞ্জের নাম তৈরি করতে পারে না, আপনি কেবল তালিকার এক্সচেঞ্জের কনফিগারেশনগুলির মধ্যে একটি ধার নিতে পারেন, ব্যাকটেস্টটি দেখায় যে বিন্যান্স, প্রকৃত এটি উইক্সঅ্যাপের সিমুলেশন বাজারের ডেটা।

img

একটি কাস্টম ডেটা উত্স হিসাবে বাজার কোট সংগ্রাহক উপর ভিত্তি করে ব্যাকটেস্ট সিস্টেম দ্বারা উত্পন্ন চার্ট wexApp বিনিময় পৃষ্ঠায় 1 ঘন্টা কে-লাইন চার্ট সঙ্গে একই কিনা তুলনা করুন।

img img

এই ভাবে, ভিপিএসের রোবট নিজেই কে-লাইন ডেটা সংগ্রহ করতে পারে, এবং আমরা যে কোন সময় সংগ্রহ করা ডেটা পেতে পারি এবং সরাসরি ব্যাকটেস্ট সিস্টেমে ব্যাকটেস্ট করতে পারি।

উদাহরণস্বরূপ, আপনি রিয়েল-লেভেল ব্যাকটেস্ট কাস্টম ডেটা উত্স, এবং মাল্টি-বৈচিত্র্য, মাল্টি-মার্কেট ডেটা সংগ্রহ এবং অন্যান্য ফাংশন চেষ্টা করে সম্প্রসারণ চালিয়ে যেতে পারেন।


সম্পর্কিত

আরো