পাইথন সংস্করণে সহজ গ্রিড কৌশল

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

img

পাইথন সংস্করণে সহজ গ্রিড কৌশল

কৌশল স্কোয়ারে অনেক পাইথন কৌশল নেই। এখানে গ্রিড কৌশলটির একটি পাইথন সংস্করণ লেখা আছে। কৌশলটির নীতিটি খুব সহজ। গ্রিড নোডগুলির একটি সিরিজ একটি মূল্য পরিসরের মধ্যে একটি নির্দিষ্ট মূল্য দূরত্ব দ্বারা উত্পন্ন হয়। যখন বাজার পরিবর্তন হয় এবং দাম একটি গ্রিড নোড মূল্য অবস্থানে পৌঁছে যায়, তখন একটি ক্রয় অর্ডার স্থাপন করা হয়। যখন অর্ডারটি বন্ধ হয়, অর্থাৎ, মুলতুবি অর্ডারের দাম প্লাস মুনাফা স্প্রেড অনুযায়ী, অবস্থানটি বন্ধ করার জন্য একটি বিক্রয় অর্ডার অপেক্ষা করুন। সেট মূল্য পরিসরের মধ্যে ওঠানামা ক্যাপচার করুন।

এটি বলার অপেক্ষা রাখে না যে গ্রিড কৌশলটির ঝুঁকি হ'ল যে কোনও গ্রিড-টাইপ কৌশল হ'ল বাজি যে দামটি একটি নির্দিষ্ট পরিসরে ওঠানামা করে। একবার দাম গ্রিড পরিসরের বাইরে ভেঙে গেলে এটি গুরুতর ভাসমান ক্ষতির কারণ হতে পারে। অতএব, এই কৌশলটি লেখার উদ্দেশ্য হ'ল পাইথন কৌশল লেখার ধারণা বা প্রোগ্রাম ডিজাইনের জন্য রেফারেন্স সরবরাহ করা। এই কৌশলটি কেবল শেখার জন্য ব্যবহৃত হয় এবং এটি বাস্তব বটে ঝুঁকিপূর্ণ হতে পারে।

কৌশলগত ধারণাগুলির ব্যাখ্যা সরাসরি কৌশল কোড মন্তব্যে লেখা আছে।

কৌশল কোড

'''backtest
start: 2019-07-01 00:00:00
end: 2020-01-03 00:00:00
period: 1m
exchanges: [{"eid":"OKEX","currency":"BTC_USDT"}]
'''

import json

# Parameters
beginPrice = 5000   # Grid interval begin price
endPrice = 8000     # Grid interval end price
distance = 20       # Price distance of each grid node
pointProfit = 50    # Profit spread per grid node
amount = 0.01       # Number of pending orders per grid node
minBalance = 300    # Minimum fund balance of the account (at the time of purchase)

# Global variables
arrNet = []
arrMsg = []
acc = None

def findOrder (orderId, NumOfTimes, ordersList = []) :
    for j in range(NumOfTimes) :
        orders = None
        if len(ordersList) == 0:
            orders = _C(exchange.GetOrders)
        else :
            orders = ordersList
        for i in range(len(orders)):
            if orderId == orders[i]["Id"]:
                return True
        Sleep(1000)
    return False

def cancelOrder (price, orderType) :
    orders = _C(exchange.GetOrders)
    for i in range(len(orders)) : 
        if price == orders[i]["Price"] and orderType == orders[i]["Type"]: 
            exchange.CancelOrder(orders[i]["Id"])
            Sleep(500)

def checkOpenOrders (orders, ticker) :
    global arrNet, arrMsg
    for i in range(len(arrNet)) : 
        if not findOrder(arrNet[i]["id"], 1, orders) and arrNet[i]["state"] == "pending" :
            orderId = exchange.Sell(arrNet[i]["coverPrice"], arrNet[i]["amount"], arrNet[i], ticker)
            if orderId :
                arrNet[i]["state"] = "cover"
                arrNet[i]["id"] = orderId                
            else :
                # Cancel
                cancelOrder(arrNet[i]["coverPrice"], ORDER_TYPE_SELL)
                arrMsg.append("Pending order failed!" + json.dumps(arrNet[i]) + ", time:" + _D())

def checkCoverOrders (orders, ticker) :
    global arrNet, arrMsg
    for i in range(len(arrNet)) : 
        if not findOrder(arrNet[i]["id"], 1, orders) and arrNet[i]["state"] == "cover" :
            arrNet[i]["id"] = -1
            arrNet[i]["state"] = "idle"
            Log(arrNet[i], "The node closes the position and resets to the idle state.", "#FF0000")


def onTick () :
    global arrNet, arrMsg, acc

    ticker = _C(exchange.GetTicker)    # Get the latest current ticker every time
    for i in range(len(arrNet)):       # Iterate through all grid nodes, find out the position where you need to pend a buy order according to the current market, and pend a buy order.
        if i != len(arrNet) - 1 and arrNet[i]["state"] == "idle" and ticker.Sell > arrNet[i]["price"] and ticker.Sell < arrNet[i + 1]["price"]:
            acc = _C(exchange.GetAccount)
            if acc.Balance < minBalance :     # If there is not enough money left, you can only jump out and do nothing.
                arrMsg.append("Insufficient funds" + json.dumps(acc) + "!" + ", time:" + _D())
                break

            orderId = exchange.Buy(arrNet[i]["price"], arrNet[i]["amount"], arrNet[i], ticker) # Pending buy orders
            if orderId : 
                arrNet[i]["state"] = "pending"   # Update the grid node status and other information if the buy order is successfully pending
                arrNet[i]["id"] = orderId
            else :
                # Cancel h/the order
                cancelOrder(arrNet[i]["price"], ORDER_TYPE_BUY)    # Cancel orders by using the cancel function
                arrMsg.append("Pending order failed!" + json.dumps(arrNet[i]) + ", time:" + _D())
    Sleep(1000)
    orders = _C(exchange.GetOrders)    
    checkOpenOrders(orders, ticker)    # Check the status of all buy orders and process them according to the changes.
    Sleep(1000)
    orders = _C(exchange.GetOrders)    
    checkCoverOrders(orders, ticker)   # Check the status of all sell orders and process them according to the changes.

    # The following information about the construction status bar can be found in the FMZ API documentation.
    tbl = {
        "type" : "table", 
        "title" : "grid status",
        "cols" : ["node index", "details"], 
        "rows" : [], 
    }    

    for i in range(len(arrNet)) : 
        tbl["rows"].append([i, json.dumps(arrNet[i])])

    errTbl = {
        "type" : "table", 
        "title" : "record",
        "cols" : ["node index", "details"], 
        "rows" : [], 
    }

    orderTbl = {
     	"type" : "table", 
        "title" : "orders",
        "cols" : ["node index", "details"], 
        "rows" : [],    
    }

    while len(arrMsg) > 20 : 
        arrMsg.pop(0)

    for i in range(len(arrMsg)) : 
        errTbl["rows"].append([i, json.dumps(arrMsg[i])])    

    for i in range(len(orders)) : 
        orderTbl["rows"].append([i, json.dumps(orders[i])])

    LogStatus(_D(), "\n", acc, "\n", "arrMsg length:", len(arrMsg), "\n", "`" + json.dumps([tbl, errTbl, orderTbl]) + "`")


def main ():         # Strategy execution starts here
    global arrNet
    for i in range(int((endPrice - beginPrice) / distance)):        # The for loop constructs a data structure for the grid based on the parameters, a list that stores each grid node, with the following information for each grid node:
        arrNet.append({
            "price" : beginPrice + i * distance,                    # Price of the node
            "amount" : amount,                                      # Number of orders
            "state" : "idle",    # pending / cover / idle           # Node Status
            "coverPrice" : beginPrice + i * distance + pointProfit, # Node closing price
            "id" : -1,                                              # ID of the current order related to the node
        })
        
    while True:    # After the grid data structure is constructed, enter the main strategy loop
        onTick()   # Processing functions on the main loop, the main processing logic
        Sleep(500) # Control polling frequency

কৌশলটির মূল নকশা ধারণাটি হ'ল বর্তমান তালিকাটি তুলনা করা।GetOrdersইন্টারফেসটি আপনার দ্বারা রক্ষণাবেক্ষণ করা গ্রিড ডেটা কাঠামোর সাথে সামঞ্জস্যপূর্ণ। মুলতুবি অর্ডারগুলির পরিবর্তনগুলি বিশ্লেষণ করুন (তারা বন্ধ হোক বা না হোক), গ্রিড ডেটা কাঠামো আপডেট করুন এবং পরবর্তী ক্রিয়াকলাপগুলি করুন। এছাড়াও, মুলতুবি অর্ডারগুলি লেনদেনটি সম্পন্ন না হওয়া পর্যন্ত বাতিল করা হবে না, এমনকি যদি দামটি বিচ্যুত হয়, কারণ ডিজিটাল মুদ্রা বাজারে প্রায়শই পিনের পরিস্থিতি থাকে, এই মুলতুবি অর্ডারগুলি পিনের অর্ডারগুলিও গ্রহণ করতে পারে (যদি এক্সচেঞ্জে মুলতুবি অর্ডারের সংখ্যা সীমিত হয় তবে এটি সামঞ্জস্য করা হবে) ।

কৌশল ডেটা ভিজ্যুয়ালাইজেশন ব্যবহার করেLogStatusরিয়েল টাইমে স্ট্যাটাস বারে তথ্য প্রদর্শন করার ফাংশন।

    tbl = {
        "type" : "table", 
        "title" : "grid status",
        "cols" : ["node index", "details"], 
        "rows" : [], 
    }    

    for i in range(len(arrNet)) : 
        tbl["rows"].append([i, json.dumps(arrNet[i])])

    errTbl = {
        "type" : "table", 
        "title" : "record",
        "cols" : ["node index", "details"], 
        "rows" : [], 
    }

    orderTbl = {
     	"type" : "table", 
        "title" : "orders",
        "cols" : ["node index", "details"], 
        "rows" : [],    
    }

তিনটি টেবিল তৈরি করা হয়। প্রথম টেবিলটি বর্তমান গ্রিড ডেটা কাঠামোর প্রতিটি নোডের তথ্য প্রদর্শন করে, দ্বিতীয় টেবিলটি অস্বাভাবিক তথ্য প্রদর্শন করে এবং তৃতীয় টেবিলটি এক্সচেঞ্জের প্রকৃত তালিকা তথ্য প্রদর্শন করে।

ব্যাকটেস্ট

img img img

কৌশল ঠিকানা

কৌশল ঠিকানা

কৌশলটি শুধুমাত্র শেখার এবং ব্যাকটেস্টিং উদ্দেশ্যে, এবং আপনি আগ্রহী হলে এটি অপ্টিমাইজ এবং আপগ্রেড করা যেতে পারে।


সম্পর্কিত

আরো