আপনি Sharpe অনুপাত বিশ্লেষণ নিতে, সর্বোচ্চ ড্রাউনডাউন, রিটার্ন হার এবং কৌশল ব্যাক টেস্টিং অন্যান্য সূচক অ্যালগরিদম

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

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: শার্প অনুপাত
  • volatility: volatility (অস্থিরতা)
  • maxDrawdown: সর্বাধিক ড্রাউন
  • 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.

  • স্ট্যান্ডার্ড ডিভিয়েশনঃ বৈচিত্র্যের গাণিতিক বর্গমূল গণনা করুন, যা মান বিচ্যুতি।

  • অস্থিরতা: যখন গণনার স্কেলটি বার্ষিক হয়, তখন অস্থিরতা মানক বিচ্যুতি।

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

তুমি কি এটা শিখেছ?


আরো