سادہ گرڈ کی حکمت عملی پائیتھون ورژن میں

مصنف:لیدیہ, تخلیق: 2022-12-23 21:00:45, تازہ کاری: 2023-09-20 11:17:48

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

حکمت عملی کا پتہ

حکمت عملی کا پتہ

حکمت عملی صرف سیکھنے اور بیک ٹیسٹنگ کے مقصد کے لئے ہے، اور اگر آپ دلچسپی رکھتے ہیں تو اسے بہتر اور اپ گریڈ کیا جا سکتا ہے.


متعلقہ

مزید