ডিজিটাল মুদ্রা ফিউচার ট্রেডিং ক্যাটাগরি (পরীক্ষামূলক সংস্করণ)

লেখক:ছোট্ট স্বপ্ন, তারিখঃ ২০২০-০৪-২৯ ১১ঃ১৯ঃ৩৯
ট্যাগঃ

ডিজিটাল মুদ্রা ফিউচার ট্রেডিং ক্যাটাগরি (পরীক্ষামূলক সংস্করণ)

  • main ফাংশনটি একটি পরীক্ষা ফাংশন, উদাহরণস্বরূপ কল করুন

আপনার কোন প্রশ্ন থাকলে মন্তব্য করতে পারেন।


// 参数
/*
var MinAmount = 1
var SlidePrice = 5
var Interval = 500
*/

function GetPosition(e, contractType, direction) {
    e.SetContractType(contractType)
    var positions = _C(e.GetPosition);
    for (var i = 0; i < positions.length; i++) {
        if (positions[i].ContractType == contractType && positions[i].Type == direction) {
            return positions[i]
        }
    }

    return null
}

function Open(e, contractType, direction, opAmount) {
    var initPosition = GetPosition(e, contractType, direction);
    var isFirst = true;
    var initAmount = initPosition ? initPosition.Amount : 0;
    var nowPosition = initPosition;
    var directBreak = false 
    var preNeedOpen = 0
    var timeoutCount = 0
    while (true) {
        var ticker = _C(e.GetTicker)
        var needOpen = opAmount;
        if (isFirst) {
            isFirst = false;
        } else {
            nowPosition = GetPosition(e, contractType, direction);
            if (nowPosition) {
                needOpen = opAmount - (nowPosition.Amount - initAmount);
            }
            // 检测directBreak 并且持仓未变的情况
            if (preNeedOpen == needOpen && directBreak) {
                Log("疑似仓位数据延迟,等待30秒", "#FF0000")
                Sleep(30000)
                nowPosition = GetPosition(e, contractType, direction);
                if (nowPosition) {
                    needOpen = opAmount - (nowPosition.Amount - initAmount);
                }
                /*
                timeoutCount++
                if (timeoutCount > 10) {
                    Log("连续10次疑似仓位延迟,下单失败!", "#FF0000")
                    break
                }
                */
            } else {
                timeoutCount = 0
            }
        }
        if (needOpen < MinAmount) {
            break;
        }
        
        var amount = needOpen;
        preNeedOpen = needOpen
        e.SetDirection(direction == PD_LONG ? "buy" : "sell");
        var orderId;
        if (direction == PD_LONG) {
            orderId = e.Buy(ticker.Sell + SlidePrice, amount, "开多仓", contractType, ticker);
        } else {
            orderId = e.Sell(ticker.Buy - SlidePrice, amount, "开空仓", contractType, ticker);
        }

        directBreak = false
        var n = 0
        while (true) {
            Sleep(Interval);
            var orders = _C(e.GetOrders);
            if (orders.length == 0) {
                if (n == 0) {
                    directBreak = true
                }
                break;
            }
            for (var j = 0; j < orders.length; j++) {
                e.CancelOrder(orders[j].Id);
                if (j < (orders.length - 1)) {
                    Sleep(Interval);
                }
            }
            n++
        }
    }

    var ret = {
        price: 0,
        amount: 0,
        position: nowPosition
    };
    if (!nowPosition) {
        return ret;
    }
    if (!initPosition) {
        ret.price = nowPosition.Price;
        ret.amount = nowPosition.Amount;
    } else {
        ret.amount = nowPosition.Amount - initPosition.Amount;
        ret.price = _N(((nowPosition.Price * nowPosition.Amount) - (initPosition.Price * initPosition.Amount)) / ret.amount);
    }
    return ret;
}

function Cover(e, contractType, opAmount, direction) {
    var initPosition = null;
    var position = null;
    var isFirst = true;

    while (true) {
        while (true) {
            Sleep(Interval);
            var orders = _C(e.GetOrders);
            if (orders.length == 0) {
                break;
            }
            for (var j = 0; j < orders.length; j++) {
                e.CancelOrder(orders[j].Id);
                if (j < (orders.length - 1)) {
                    Sleep(Interval);
                }
            }
        }

        position = GetPosition(e, contractType, direction)
        if (!position) {
            break
        }
        if (isFirst == true) {
            initPosition = position;
            opAmount = Math.min(opAmount, initPosition.Amount)
            isFirst = false;
        }

        var amount = opAmount - (initPosition.Amount - position.Amount)
        if (amount <= 0) {
            break
        }

        var ticker = _C(e.GetTicker)
        if (position.Type == PD_LONG) {
            e.SetDirection("closebuy");
            e.Sell(ticker.Buy - SlidePrice, amount, "平多仓", contractType, ticker);
        } else if (position.Type == PD_SHORT) {
            e.SetDirection("closesell");
            e.Buy(ticker.Sell + SlidePrice, amount, "平空仓", contractType, ticker);
        }

        Sleep(Interval)
    }

    return position
}

$.OpenLong = function(e, contractType, amount) {
    if (typeof(e) == "string") {
        amount = contractType
        contractType = e
        e = exchange
    }

    return Open(e, contractType, PD_LONG, amount);
}

$.OpenShort = function(e, contractType, amount) {
    if (typeof(e) == "string") {
        amount = contractType
        contractType = e
        e = exchange
    }

    return Open(e, contractType, PD_SHORT, amount);
};

$.CoverLong = function(e, contractType, amount) {
    if (typeof(e) == "string") {
        amount = contractType
        contractType = e
        e = exchange
    }

    return Cover(e, contractType, amount, PD_LONG);
};

$.CoverShort = function(e, contractType, amount) {
    if (typeof(e) == "string") {
        amount = contractType
        contractType = e
        e = exchange
    }

    return Cover(e, contractType, amount, PD_SHORT);
};


function main() {
    Log(exchange.GetPosition())
    var info = $.OpenLong(exchange, "quarter", 100)
    Log(info, "#FF0000")

    Log(exchange.GetPosition())
    info = $.CoverLong(exchange, "quarter", 30)
    Log(exchange.GetPosition())
    Log(info, "#FF0000")

    info = $.CoverLong(exchange, "quarter", 80)
    Log(exchange.GetPosition())
    Log(info, "#FF0000")
}

আরো

হাড়ের ছুরিসমস্যা ড্রিমহেন, প্রধান ফাংশনে রেফারেন্সের পদ্ধতি সম্পর্কে একটু ব্যাখ্যা করুন। উদাহরণস্বরূপ, যদি আপনি একটি বিটিসি/ইউএসডিটি কোয়ার্টার শুরু করেন, তাহলে আপনি কিভাবে লিখবেন? জীবিত, প্রধান ফাংশন ব্যাখ্যা করুন var info = $.OpenLong ((exchange, "quarter", 100) এটা কি প্রতি কোয়ার্টার চুক্তির মাল্টিপ্লেক্স? তাহলে এক্সচেঞ্জ ১০০ কি?

উচ্চ নিমজ্জন নিমজ্জনস্লাইড প্রাইস হচ্ছে দামের অনুপাত।

ছোট্ট স্বপ্ন$.OpenLong ((exchange, "quarter", 100) এই এক্সচেঞ্জের বিষয়বস্তু, কয়েদো, ত্রৈমাসিক চুক্তি, ১০০টি।

ছোট্ট স্বপ্নহাইফেক্স একটি পৃথক লেনদেন পদ্ধতি লিখতে পরামর্শ দেয়, এই শ্রেণীর লাইব্রেরিতে কিছু লেনদেনের অবশিষ্ট পরিমাণ সনাক্তকরণ রয়েছে, যা সময় সাপেক্ষে। হাইফেক্স সরাসরি লেনদেনের যুক্তি কাস্টমাইজ করার পরামর্শ দেয়।

উচ্চ নিমজ্জন নিমজ্জনআমি এই OpenLong DOT ((৪টি ছুরি) ব্যবহার করি, এবং আমাকে ৯টি ছুরি ((৪+৫) দিয়ে একটি তালিকা দিন। এই শ্রেণীতে Sleep (() খুব দীর্ঘ, উচ্চ ফ্রিকোয়েন্সি ব্যবহার করা যাবে না, আমি পরিবর্তন করব:)

ছোট্ট স্বপ্ননা, এটা স্লাইড মূল্য, অথবা এক ঘন্টার বেশি খাওয়ার জন্য একটি সামান্য প্রিমিয়াম।