आप Sharpe अनुपात, अधिकतम ड्रॉडाउन, वापसी दर और रणनीति बैकटेस्टिंग में अन्य संकेतक एल्गोरिदम का विश्लेषण करने के लिए ले

लेखक:लिडिया, बनाया गयाः 2022-11-28 16:44:25, अद्यतन किया गयाः 2023-09-13 19:42:38

img

आप Sharpe अनुपात, अधिकतम ड्रॉडाउन, वापसी दर और रणनीति बैकटेस्टिंग में अन्य संकेतक एल्गोरिदम का विश्लेषण करने के लिए ले

रणनीति के कुछ प्रदर्शन संकेतक एल्गोरिदम अक्सर समूह के सदस्यों द्वारा चर्चा की जाती है, और एक एल्गोरिथ्म का खुलासा एफएमजेड के एपीआई दस्तावेज़ में भी किया गया है। हालांकि, यह टिप्पणियों के बिना समझना आसान नहीं है। इस लेख में, मैं आपको एल्गोरिथ्म का विश्लेषण करने के लिए ले जाऊंगा। मेरा मानना है कि आपको इस लेख को पढ़ने के बाद शार्प अनुपात, अधिकतम ड्रॉडाउन, रिटर्न दर और गणना तर्क की अवधारणाओं की स्पष्ट समझ होगी। हम स्रोत कोड के साथ शुरू करेंगे, जो जावास्क्रिप्ट भाषा में लिखा गया है। एफएमजेड की बैकटेस्टिंग प्रणाली भी स्वचालित रूप से बैकटेस्टिंग प्रदर्शन डेटा उत्पन्न करने के लिए इस एल्गोरिथ्म को अपनाती है।

returnविश्लेषण फ़ंक्शन

function returnAnalyze(totalAssets, profits, ts, te, period, yearDays)

https://www.fmz.com/api#Backtestingप्रणाली शार्प एल्गोरिथ्म

चूंकि यह एक गणना समारोह है, वहाँ इनपुट और आउटपुट होना चाहिए. चलो पहले फ़ंक्शन के इनपुट को देखेंः

totalAssets, profits, ts, te, period, yearDays
  • कुल परिसंपत्तियाँ यह पैरामीटर उस समय की प्रारंभिक कुल संपत्ति है जब रणनीति चलनी शुरू होती है।

  • मुनाफा यह पैरामीटर महत्वपूर्ण है, क्योंकि इस मूल डेटा के आधार पर प्रदर्शन संकेतकों की एक श्रृंखला की गणना की जाती है। यह पैरामीटर निम्न प्रारूप में एक दो आयामी सरणी हैः[[timestamp1, profit1], [timestamp2, profit2], [timestamp3, profit3],..., [timestampN, profitN]]. यह देखा जा सकता है कि returnAnalyze फ़ंक्शन को ऐसी डेटा संरचना की आवश्यकता होती है जो प्रत्येक समय पर रिटर्न के कालानुक्रमिक क्रम को रिकॉर्ड करती है. टाइमस्टैम्प1 से टाइमस्टैम्पN तक कालानुक्रमिक क्रम में दूर से निकट तक होते हैं. प्रत्येक समय बिंदु पर एक लाभ मूल्य होता है. उदाहरण के लिए, रिटर्न रिकॉर्ड में तीसरा समय बिंदु [टाइमस्टैम्प 3, लाभ] है। एफएमजेड की ऑनलाइन बैकटेस्टिंग प्रणाली में, लाभ सरणी के डेटा को बैकटेस्टिंग प्रणाली द्वारा इस फ़ंक्शन को प्रदान किया जाता है। बेशक, यदि आप स्वयं रिटर्न डेटा रिकॉर्ड करते हैं और ऐसी सरणी संरचना बनाते हैं, तो आप इसे परिणामों की गणना करने के लिए गणना फ़ंक्शन को भी प्रदान कर सकते हैं।

  • ण बैकटेस्ट का आरंभ समय-स्टैम्प।

  • क बैकटेस्ट के अंत का टाइमस्टैम्प।

अवधि मिलीसेकंड के स्तर पर गणना की अवधि।

  • वर्षदिन एक वर्ष में व्यापारिक दिन।

इसके बाद, चलो इस फ़ंक्शन के आउटपुट को एक साथ देखते हैंः

return {
        totalAssets: totalAssets,
        yearDays: yearDays,
        totalReturns: totalReturns,
        annualizedReturns: annualizedReturns,
        sharpeRatio: sharpeRatio,
        volatility: volatility,
        maxDrawdown: maxDrawdown,
        maxDrawdownTime: maxDrawdownTime,
        maxAssetsTime: maxAssetsTime,
        maxDrawdownStartTime: maxDrawdownStartTime,
        winningRate: winningRate
    }
  • कुल परिसंपत्तियाँ: कुल परिसंपत्तियाँ
  • वर्षदिनः व्यापार के दिन
  • कुल रिटर्न: कुल रिटर्न
  • वार्षिक रिटर्न: वार्षिक रिटर्न
  • sharpeRatio: शार्प अनुपात
  • अस्थिरता: अस्थिरता
  • अधिकतम निकासीः अधिकतम निकासी
  • maxDrawdownTime: अधिकतम ड्रॉडाउन पर टाइमस्टैम्प
  • maxAssetsTime: अधिकतम परिसंपत्तियों पर टाइमस्टैम्प
  • maxDrawdownStartTime: अधिकतम ड्रॉडाउन का प्रारंभ समय
  • winningRate: जीतने की दर

इनपुट और आउटपुट को जानते हुए, हम समझ सकते हैं कि फ़ंक्शन का उपयोग किस लिए किया जाता है। इसे सरलता से रखने के लिए, यह फ़ंक्शन को कुछ मूल रिकॉर्ड देता है, जैसे कि रिटर्न सांख्यिकी सरणी। फ़ंक्शन आपको बैकटेस्ट के प्रदर्शन को दिखाने के लिए एक परिणाम देगा।

अब, आइए देखें कि कोड की गणना कैसे की जाती हैः

function returnAnalyze(totalAssets, profits, ts, te, period, yearDays) {
    // force by days
    period = 86400000                  // The number of milliseconds in a day, that is 60 * 60 * 24 * 1000
    if (profits.length == 0) {         // If the length of the array of profits is 0, it cannot be calculated and it will return to the null value directly
        return null
    }
    var freeProfit = 0.03              // Risk-free interest rate, which can also be set according to the demand, such as 3% annualized national debt
    var yearRange = yearDays * 86400000          // Milliseconds of all accumulated trading days in a year
    var totalReturns = profits[profits.length - 1][1] / totalAssets      // Total return rate
    var annualizedReturns = (totalReturns * yearRange) / (te - ts)       // The annualized rate of return, the expected rate of return obtained by scaling the time of profit statistics to the scale of one year

    // MaxDrawDown
    var maxDrawdown = 0           // Initialize the maximum drawdown variable to 0
    var maxAssets = totalAssets   // Initialize maximum asset variable with initial net value assignment
    var maxAssetsTime = 0         // Timestamp of the time when the maximum asset is initialized
    var maxDrawdownTime = 0       // Timestamp of the time when the maximum drawdown is initialized
    var maxDrawdownStartTime = 0  // Timestamp of the time when the maximum start time is initialized
    var winningRate = 0           // Initialized win rate is 0
    var winningResult = 0         // Record the number of win time
    for (var i = 0; i < profits.length; i++) {      // Traverse the return array
        if (i == 0) {
            if (profits[i][1] > 0) {                // If the first return record point, the return is greater than 0, it means the profit
                winningResult++                     // The number of win times accumulates 1 
            }
        } else {                                    // If it is not the first returns record point, as long as the returns of the current point is greater than the returns of the previous moment (return point), it means profit, and the number of win times accumulates 1 
            if (profits[i][1] > profits[i - 1][1]) {
                winningResult++
            }
        }
        if ((profits[i][1] + totalAssets) > maxAssets) {    // If the return plus the initial net value at that moment is greater than the largest asset recorded to have occurred, the value of the largest asset is updated and the timestamp of this moment is recorded
            maxAssets = profits[i][1] + totalAssets
            maxAssetsTime = profits[i][0]
        }
        if (maxAssets > 0) {                                // When the maximum asset value recorded is greater than 0, the drawdown is calculated
            var drawDown = 1 - (profits[i][1] + totalAssets) / maxAssets
            if (drawDown > maxDrawdown) {                   // If the current drawdown is greater than the recorded maximum drawdown, update the maximum drawdown, maximum drawdown time, etc
                maxDrawdown = drawDown
                maxDrawdownTime = profits[i][0]
                maxDrawdownStartTime = maxAssetsTime
            }
        }
    }
    if (profits.length > 0) {                            // Calculate the winning rate
        winningRate = winningResult / profits.length
    }
    // trim profits
    var i = 0
    var datas = []
    var sum = 0
    var preProfit = 0
    var perRatio = 0
    var rangeEnd = te
    if ((te - ts) % period > 0) {
        rangeEnd = (parseInt(te / period) + 1) * period     // Process rangeEnd as an integer multiple of period
    }
    for (var n = ts; n < rangeEnd; n += period) {
        var dayProfit = 0.0
        var cut = n + period
        while (i < profits.length && profits[i][0] < cut) {    // Ensure that when the timestamp is not out of bounds, the array length is also not out of bounds
            dayProfit += (profits[i][1] - preProfit)           // Calculate the daily returns
            preProfit = profits[i][1]                          // Record yesterday's returns
            i++                                                // Accumulate i for accessing the next profits node
        }
        perRatio = ((dayProfit / totalAssets) * yearRange) / period   // Calculate the annualized rate of return at that time
        sum += perRatio                                               // Accumulation
        datas.push(perRatio)                                          // Put in the array datas
    }

    var sharpeRatio = 0                    // Initial Sharpe ratio is 0
    var volatility = 0                     // Initial volatility is 0
    if (datas.length > 0) {
        var avg = sum / datas.length;      // Find the mean value
        var std = 0;
        for (i = 0; i < datas.length; i++) {
            std += Math.pow(datas[i] - avg, 2);      // The std is used to calculate the following variance. The following std/datas.length is the variance, and the square root of the number is the standard deviation
        }
        volatility = Math.sqrt(std / datas.length);  // In terms of years, volatility is the standard deviation
        if (volatility !== 0) {
            sharpeRatio = (annualizedReturns - freeProfit) / volatility   // Sharpe formula to calculate Sharpe ratio: (annualized return rate - risk-free rate) / standard deviation
        }
    }

    return {
        totalAssets: totalAssets,
        yearDays: yearDays,
        totalReturns: totalReturns,
        annualizedReturns: annualizedReturns,
        sharpeRatio: sharpeRatio,
        volatility: volatility,
        maxDrawdown: maxDrawdown,
        maxDrawdownTime: maxDrawdownTime,
        maxAssetsTime: maxAssetsTime,
        maxDrawdownStartTime: maxDrawdownStartTime,
        winningRate: winningRate
    }
}

कुल मिलाकर, एल्गोरिथ्म जटिल नहीं है, और कई अवधारणाएं हो सकती हैं जिन्हें पहले से समझने की आवश्यकता है।

  • भिन्नता: इसे रिटर्न डेटा के एक सेट के रूप में समझा जा सकता है। नमूनों के समूह, 1, 2, 3, 4 और 5, का औसत मान है (1+2+3+4+5) / 5 = 3, जबकि विचलन प्रत्येक डेटा और इसके योग के बीच के अंतर के वर्गों के योग का औसत मूल्य है, जो क्रमशः [(1-3) ^ 2 + (2-3) ^ 2 + (3-3) ^ 2 + (4-3) ^ 2 + (5-3) ^ 2) / 5 = 2 है, और विचलन 2 है।

  • मानक विचलन: भिन्नता के अंकगणितीय वर्गमूल की गणना करें, जो मानक विचलन है।

  • अस्थिरता: जब गणना पैमाने को वार्षिक किया जाता है, तो अस्थिरता मानक विचलन है।

इन अवधारणाओं और गणना सूत्रों की समझ के साथ, फ़ंक्शन का शार्प गणना भाग एक नज़र में स्पष्ट हो जाएगा। शार्प अनुपात की गणना के लिए शार्प सूत्रः (वार्षिक प्रतिफल दर - जोखिम मुक्त दर) / मानक विचलन

क्या तुमने इसे अभी तक सीखा है?


अधिक