ڈائنامک آر ایس آئی آسکیلیٹر پولینومیئل فٹنگ انڈیکیٹر ٹرینڈ مقداری تجارتی حکمت عملی

RSI DRSI QR EMA RMSE MSE
تخلیق کی تاریخ: 2024-12-11 15:32:23 آخر میں ترمیم کریں: 2024-12-11 15:32:23
کاپی: 4 کلکس کی تعداد: 463
1
پر توجہ دیں
1617
پیروکار

ڈائنامک آر ایس آئی آسکیلیٹر پولینومیئل فٹنگ انڈیکیٹر ٹرینڈ مقداری تجارتی حکمت عملی

یہ حکمت عملی آر ایس آئی کے متحرک oscillators پر مبنی ایک مقداری تجارتی نظام ہے۔ آر ایس آئی اشارے پر کثیر مقصود اور وقت کی ترتیب کا تجزیہ کرکے ، آر ایس آئی میں تبدیلی کی شرح کا حساب کتاب کرکے مارکیٹ کی حرکیات کو پکڑنے کے لئے۔ حکمت عملی اعلی ریاضی کے طریقوں جیسے سگنل پروسیسنگ جیسے کیو آر ڈس انکولیشن کا استعمال کرتی ہے ، اور تجارت کے فیصلوں کے لئے یکساں لکیری نظام کے ساتھ مل کر۔

حکمت عملی کا اصول

اس حکمت عملی کا مرکز ڈیلٹا-آر ایس آئی آسکیلیٹر ہے ، جو مندرجہ ذیل اقدامات کے ذریعے حاصل کیا جاتا ہے:

  1. سب سے پہلے، روایتی RSI کے اعداد و شمار کے طور پر بنیادی اعداد و شمار کا حساب لگائیں.
  2. RSI کو ہموار کرنے اور شور کو کم کرنے کے لئے کثیر مقصود فٹ کا استعمال کریں
  3. RSI کے ٹائم کوڈینٹیول کو ڈیلٹا-RSI سے حاصل کیا جاتا ہے جو RSI کی تبدیلی کی شرح کو ظاہر کرتا ہے
  4. ڈیلٹا-آر ایس آئی کا موازنہ اس کی حرکت پذیری اوسط کے ساتھ ٹریڈنگ سگنل تیار کرتا ہے
  5. مساوی جڑ کی غلطی ((RMSE) کا استعمال کرتے ہوئے فٹنس کے معیار کا اندازہ اور فلٹرنگ

ٹرانزیکشن سگنل تین طریقوں سے پیدا ہوتے ہیں:

  • صفر لائن کراسنگ: ڈیلٹا-آر ایس آئی منفی سے درست ہونے پر زیادہ ہے ، مثبت سے منفی ہونے پر خالی ہے
  • سگنل لائن کراسنگ: ڈیلٹا-آر ایس آئی کو اس کی چلتی اوسط سے اوپر / نیچے جانے پر زیادہ / کم کرنا
  • سمت میں تبدیلی: ڈیلٹا-آر ایس آئی نے منفی علاقوں میں اضافے کا آغاز کیا اور مثبت علاقوں میں کمی کا آغاز کیا

اسٹریٹجک فوائد

  1. ریاضی کی مضبوط بنیاد: سگنل پروسیسنگ کے لئے اعلی درجے کی ریاضی کے طریقوں جیسے QR تجزیہ کا استعمال کرتے ہوئے ، نظریاتی بنیاد قابل اعتماد ہے
  2. سگنل ہموار: ملٹی میٹم کو مؤثر طریقے سے مارکیٹ کے شور کو فلٹر کرنے اور سگنل کے معیار کو بہتر بنانے کے لئے
  3. لچکدار: مختلف مارکیٹ کے حالات کے مطابق سگنل جنریشن کے مختلف طریقوں اور پیرامیٹرز کے انتخاب کی پیش کش
  4. خطرے کو کنٹرول کیا جاسکتا ہے: آر ایم ایس ای فلٹرنگ میکانزم پر مشتمل ہے ، جو اعلی وشوسنییتا سگنل کو فلٹر کرتا ہے
  5. اعلی کمپیوٹنگ کارکردگی: میٹرکس آپریشنز بہتر الگورتھم استعمال کرتے ہیں ، جو اعلی کارکردگی کا مظاہرہ کرتے ہیں

اسٹریٹجک رسک

  1. پیرامیٹر حساس: متعدد کلیدی پیرامیٹرز کو احتیاط سے ایڈجسٹ کرنے کی ضرورت ہے ، اور پیرامیٹرز کا غلط انتخاب حکمت عملی کی کارکردگی کو شدید متاثر کرسکتا ہے
  2. تاخیر: سگنل کی ہموار پروسیسنگ کچھ تاخیر کا باعث بنتی ہے ، جس سے تیز رفتار کو یاد کیا جاسکتا ہے
  3. جعلی بریک: ہلچل مچانے والی مارکیٹوں میں جعلی سگنل پیدا کرنے اور ٹرانزیکشن کی لاگت میں اضافے کا خطرہ
  4. پیچیدہ حساب کتاب: زیادہ میٹرکس آپریشنز میں ملوث ، اعلی تعدد تجارت میں ممکنہ کارکردگی کا مسئلہ
  5. اوور فٹ: پیرامیٹرز کو بہتر بنانے کے لئے احتیاط کی ضرورت ہے کہ تاریخ کے اعداد و شمار کو زیادہ سے زیادہ فٹ نہ کیا جائے

حکمت عملی کی اصلاح کی سمت

  1. ایڈجسٹمنٹ پیرامیٹرز: مارکیٹ میں اتار چڑھاؤ کی رفتار کے مطابق آر ایس آئی کے دورانیے اور فٹ ہونے والے مرحلے کو ایڈجسٹ کیا جاسکتا ہے
  2. کثیر ٹائم سائیکل: زیادہ ٹائم سائیکل کے ساتھ مل کر سگنل کو کراس کی توثیق کریں
  3. اتار چڑھاؤ کی شرح فلٹرنگ: ATR جیسے اتار چڑھاؤ کی شرح کے اشارے شامل کرنے کے لئے سگنل فلٹرنگ
  4. مارکیٹ کی درجہ بندی: مارکیٹ کی مختلف حالتوں کے لئے مختلف سگنل جنریشن قواعد کا استعمال کرتے ہوئے ((رجحان / ہلچل)
  5. اسٹاپ نقصان کی اصلاح: مزید ذہین اسٹاپ نقصان کے طریقہ کار کو شامل کرنا ، جیسے مزاحمت کی حمایت پر مبنی متحرک اسٹاپ نقصان

خلاصہ کریں۔

یہ ایک اچھی طرح سے تشکیل شدہ ، مضبوط نظریاتی بنیادوں والی ایک مقداری تجارت کی حکمت عملی ہے۔ آر ایس آئی کی متحرک خصوصیات کے تجزیہ کے ذریعہ ، جدید ریاضی کے طریقوں کے ساتھ مل کر سگنل پروسیسنگ کے ذریعہ ، مارکیٹ کے رجحانات کو بہتر طور پر پکڑنے کے قابل ہے۔ اگرچہ کچھ پیرامیٹرز کی حساسیت اور حساب کتاب کی پیچیدگی کے مسائل موجود ہیں ، لیکن معقول پیرامیٹرز کے انتخاب اور اصلاحی بہتری کے ذریعہ ، اس حکمت عملی میں اچھی اطلاق کی قیمت ہے۔ یہ مشورہ دیا جاتا ہے کہ خطرے پر قابو پانے ، معقول پوزیشنوں کی ترتیب اور حکمت عملی کی کارکردگی کی مستقل نگرانی پر توجہ دی جائے۔

حکمت عملی کا ماخذ کوڈ
/*backtest
start: 2024-11-10 00:00:00
end: 2024-12-09 08:00:00
period: 4h
basePeriod: 4h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tbiktag
//
// Delta-RSI Oscillator Strategy
//
// A strategy that uses Delta-RSI Oscillator (© tbiktag) as a stand-alone indicator:
// https://www.tradingview.com/script/OXQVFTQD-Delta-RSI-Oscillator/
//
// Delta-RSI is a smoothed time derivative of the RSI, plotted as a histogram 
// and serving as a momentum indicator. 
// 
// Input parameters:
// RSI Length: The timeframe of the RSI that serves as an input to D-RSI.
// Length: The length of the lookback frame used for local regression.
// Polynomial Order: The order of the local polynomial function used to interpolate the RSI.
// Signal Length: The length of a EMA of the D-RSI series that is used as a signal line.
// Trade signals are generated based on three optional conditions:
// - Zero-crossing: bullish when D-RSI crosses zero from negative to positive values (bearish otherwise)
// - Signal Line Crossing: bullish when D-RSI crosses from below to above the signal line (bearish otherwise)
// - Direction Change: bullish when D-RSI was negative and starts ascending (bearish otherwise)
//
// Since D-RSI oscillator is based on polynomial fitting of the RSI curve, there is also an option
// to filter trade signal by means of the root mean-square error of the fit (normalized by the sample average).
// 
//@version=5
strategy(title='Delta-RSI Oscillator Strategy-QuangVersion', shorttitle='D-RSI-Q', overlay=true)

// ---Subroutines---
matrix_get(_A, _i, _j, _nrows) =>
    // Get the value of the element of an implied 2d matrix
    //input: 
    // _A :: array: pseudo 2d matrix _A = [[column_0],[column_1],...,[column_(n-1)]]
    // _i :: integer: row number
    // _j :: integer: column number
    // _nrows :: integer: number of rows in the implied 2d matrix
    array.get(_A, _i + _nrows * _j)

matrix_set(_A, _value, _i, _j, _nrows) =>
    // Set a value to the element of an implied 2d matrix
    //input: 
    // _A :: array, changed on output: pseudo 2d matrix _A = [[column_0],[column_1],...,[column_(n-1)]]
    // _value :: float: the new value to be set
    // _i :: integer: row number
    // _j :: integer: column number
    // _nrows :: integer: number of rows in the implied 2d matrix
    array.set(_A, _i + _nrows * _j, _value)

transpose(_A, _nrows, _ncolumns) =>
    // Transpose an implied 2d matrix
    // input:
    // _A :: array: pseudo 2d matrix _A = [[column_0],[column_1],...,[column_(n-1)]]
    // _nrows :: integer: number of rows in _A
    // _ncolumns :: integer: number of columns in _A
    // output:
    // _AT :: array: pseudo 2d matrix with implied dimensions: _ncolums x _nrows
    var _AT = array.new_float(_nrows * _ncolumns, 0)
    for i = 0 to _nrows - 1 by 1
        for j = 0 to _ncolumns - 1 by 1
            matrix_set(_AT, matrix_get(_A, i, j, _nrows), j, i, _ncolumns)
    _AT

multiply(_A, _B, _nrowsA, _ncolumnsA, _ncolumnsB) =>
    // Calculate scalar product of two matrices
    // input: 
    // _A :: array: pseudo 2d matrix
    // _B :: array: pseudo 2d matrix
    // _nrowsA :: integer: number of rows in _A
    // _ncolumnsA :: integer: number of columns in _A
    // _ncolumnsB :: integer: number of columns in _B
    // output:
    // _C:: array: pseudo 2d matrix with implied dimensions _nrowsA x _ncolumnsB
    var _C = array.new_float(_nrowsA * _ncolumnsB, 0)
    int _nrowsB = _ncolumnsA
    float elementC = 0.0
    for i = 0 to _nrowsA - 1 by 1
        for j = 0 to _ncolumnsB - 1 by 1
            elementC := 0
            for k = 0 to _ncolumnsA - 1 by 1
                elementC += matrix_get(_A, i, k, _nrowsA) * matrix_get(_B, k, j, _nrowsB)
                elementC
            matrix_set(_C, elementC, i, j, _nrowsA)
    _C

vnorm(_X, _n) =>
    //Square norm of vector _X with size _n
    float _norm = 0.0
    for i = 0 to _n - 1 by 1
        _norm += math.pow(array.get(_X, i), 2)
        _norm
    math.sqrt(_norm)

qr_diag(_A, _nrows, _ncolumns) =>
    //QR Decomposition with Modified Gram-Schmidt Algorithm (Column-Oriented)
    // input:
    // _A :: array: pseudo 2d matrix _A = [[column_0],[column_1],...,[column_(n-1)]]
    // _nrows :: integer: number of rows in _A
    // _ncolumns :: integer: number of columns in _A
    // output:
    // _Q: unitary matrix, implied dimenstions _nrows x _ncolumns
    // _R: upper triangular matrix, implied dimansions _ncolumns x _ncolumns
    var _Q = array.new_float(_nrows * _ncolumns, 0)
    var _R = array.new_float(_ncolumns * _ncolumns, 0)
    var _a = array.new_float(_nrows, 0)
    var _q = array.new_float(_nrows, 0)
    float _r = 0.0
    float _aux = 0.0
    //get first column of _A and its norm:
    for i = 0 to _nrows - 1 by 1
        array.set(_a, i, matrix_get(_A, i, 0, _nrows))
    _r := vnorm(_a, _nrows)
    //assign first diagonal element of R and first column of Q
    matrix_set(_R, _r, 0, 0, _ncolumns)
    for i = 0 to _nrows - 1 by 1
        matrix_set(_Q, array.get(_a, i) / _r, i, 0, _nrows)
    if _ncolumns != 1
        //repeat for the rest of the columns
        for k = 1 to _ncolumns - 1 by 1
            for i = 0 to _nrows - 1 by 1
                array.set(_a, i, matrix_get(_A, i, k, _nrows))
            for j = 0 to k - 1 by 1
                //get R_jk as scalar product of Q_j column and A_k column:
                _r := 0
                for i = 0 to _nrows - 1 by 1
                    _r += matrix_get(_Q, i, j, _nrows) * array.get(_a, i)
                    _r
                matrix_set(_R, _r, j, k, _ncolumns)
                //update vector _a
                for i = 0 to _nrows - 1 by 1
                    _aux := array.get(_a, i) - _r * matrix_get(_Q, i, j, _nrows)
                    array.set(_a, i, _aux)
            //get diagonal R_kk and Q_k column
            _r := vnorm(_a, _nrows)
            matrix_set(_R, _r, k, k, _ncolumns)
            for i = 0 to _nrows - 1 by 1
                matrix_set(_Q, array.get(_a, i) / _r, i, k, _nrows)
    [_Q, _R]

pinv(_A, _nrows, _ncolumns) =>
    //Pseudoinverse of matrix _A calculated using QR decomposition
    // Input: 
    // _A:: array: implied as a (_nrows x _ncolumns) matrix _A = [[column_0],[column_1],...,[column_(_ncolumns-1)]]
    // Output: 
    // _Ainv:: array implied as a (_ncolumns x _nrows) matrix _A = [[row_0],[row_1],...,[row_(_nrows-1)]]
    // ----
    // First find the QR factorization of A: A = QR,
    // where R is upper triangular matrix.
    // Then _Ainv = R^-1*Q^T.
    // ----
    [_Q, _R] = qr_diag(_A, _nrows, _ncolumns)
    _QT = transpose(_Q, _nrows, _ncolumns)
    // Calculate Rinv:
    var _Rinv = array.new_float(_ncolumns * _ncolumns, 0)
    float _r = 0.0
    matrix_set(_Rinv, 1 / matrix_get(_R, 0, 0, _ncolumns), 0, 0, _ncolumns)
    if _ncolumns != 1
        for j = 1 to _ncolumns - 1 by 1
            for i = 0 to j - 1 by 1
                _r := 0.0
                for k = i to j - 1 by 1
                    _r += matrix_get(_Rinv, i, k, _ncolumns) * matrix_get(_R, k, j, _ncolumns)
                    _r
                matrix_set(_Rinv, _r, i, j, _ncolumns)
            for k = 0 to j - 1 by 1
                matrix_set(_Rinv, -matrix_get(_Rinv, k, j, _ncolumns) / matrix_get(_R, j, j, _ncolumns), k, j, _ncolumns)
            matrix_set(_Rinv, 1 / matrix_get(_R, j, j, _ncolumns), j, j, _ncolumns)
    //
    _Ainv = multiply(_Rinv, _QT, _ncolumns, _ncolumns, _nrows)
    _Ainv

norm_rmse(_x, _xhat) =>
    // Root Mean Square Error normalized to the sample mean
    // _x.   :: array float, original data
    // _xhat :: array float, model estimate
    // output
    // _nrmse:: float
    float _nrmse = 0.0
    if array.size(_x) != array.size(_xhat)
        _nrmse := na
        _nrmse
    else
        int _N = array.size(_x)
        float _mse = 0.0
        for i = 0 to _N - 1 by 1
            _mse += math.pow(array.get(_x, i) - array.get(_xhat, i), 2) / _N
            _mse
        _xmean = array.sum(_x) / _N
        _nrmse := math.sqrt(_mse) / _xmean
        _nrmse
    _nrmse


diff(_src, _window, _degree) =>
    // Polynomial differentiator
    // input:
    // _src:: input series
    // _window:: integer: wigth of the moving lookback window
    // _degree:: integer: degree of fitting polynomial
    // output:
    // _diff :: series: time derivative
    // _nrmse:: float: normalized root mean square error
    //
    // Vandermonde matrix with implied dimensions (window x degree+1)
    // Linear form: J = [ [z]^0, [z]^1, ... [z]^degree], with z = [ (1-window)/2 to (window-1)/2 ] 
    var _J = array.new_float(_window * (_degree + 1), 0)
    for i = 0 to _window - 1 by 1
        for j = 0 to _degree by 1
            matrix_set(_J, math.pow(i, j), i, j, _window)
    // Vector of raw datapoints:
    var _Y_raw = array.new_float(_window, na)
    for j = 0 to _window - 1 by 1
        array.set(_Y_raw, j, _src[_window - 1 - j])
    // Calculate polynomial coefficients which minimize the loss function
    _C = pinv(_J, _window, _degree + 1)
    _a_coef = multiply(_C, _Y_raw, _degree + 1, _window, 1)
    // For first derivative, approximate the last point (i.e. z=window-1) by 
    float _diff = 0.0
    for i = 1 to _degree by 1
        _diff += i * array.get(_a_coef, i) * math.pow(_window - 1, i - 1)
        _diff
    // Calculates data estimate (needed for rmse)
    _Y_hat = multiply(_J, _a_coef, _window, _degree + 1, 1)
    float _nrmse = norm_rmse(_Y_raw, _Y_hat)
    [_diff, _nrmse]

/// --- main ---
degree = input.int(title='Polynomial Order', group='Model Parameters:', inline='linepar1', defval=2, minval=1)
rsi_l = input.int(title='RSI Length', group='Model Parameters:', inline='linepar1', defval=21, minval=1, tooltip='The period length of RSI that is used as input.')
window = input.int(title='Length ( > Order)', group='Model Parameters:', inline='linepar2', defval=21, minval=2)
signalLength = input.int(title='Signal Length', group='Model Parameters:', inline='linepar2', defval=9, tooltip='The signal line is a EMA of the D-RSI time series.')
islong = input.bool(title='Buy', group='Show Signals:', inline='lineent', defval=true)
isshort = input.bool(title='Sell', group='Show Signals:', inline='lineent', defval=true)
showendlabels = input.bool(title='Exit', group='Show Signals:', inline='lineent', defval=true)
buycond = input.string(title='Buy', group='Entry and Exit Conditions:', inline='linecond', defval='Zero-Crossing', options=['Zero-Crossing', 'Signal Line Crossing', 'Direction Change'])
sellcond = input.string(title='Sell', group='Entry and Exit Conditions:', inline='linecond', defval='Zero-Crossing', options=['Zero-Crossing', 'Signal Line Crossing', 'Direction Change'])
endcond = input.string(title='Exit', group='Entry and Exit Conditions:', inline='linecond', defval='Zero-Crossing', options=['Zero-Crossing', 'Signal Line Crossing', 'Direction Change'])
usenrmse = input.bool(title='', group='Filter by Means of Root-Mean-Square Error of RSI Fitting:', inline='linermse', defval=false)
rmse_thrs = input.float(title='RSI fitting Error Threshold, %', group='Filter by Means of Root-Mean-Square Error of RSI Fitting:', inline='linermse', defval=10, minval=0.0) / 100


src = ta.rsi(close, rsi_l)
[drsi, nrmse] = diff(src, window, degree)
signalline = ta.ema(drsi, signalLength)

// Conditions and filters
filter_rmse = usenrmse ? nrmse < rmse_thrs : true
dirchangeup = drsi > drsi[1] and drsi[1] < drsi[2] and drsi[1] < 0.0
dirchangedw = drsi < drsi[1] and drsi[1] > drsi[2] and drsi[1] > 0.0
crossup = ta.crossover(drsi, 0.0)
crossdw = ta.crossunder(drsi, 0.0)
crosssignalup = ta.crossover(drsi, signalline)
crosssignaldw = ta.crossunder(drsi, signalline)

//Signals
golong = (buycond == 'Direction Change' ? dirchangeup : buycond == 'Zero-Crossing' ? crossup : crosssignalup) and filter_rmse
goshort = (sellcond == 'Direction Change' ? dirchangedw : sellcond == 'Zero-Crossing' ? crossdw : crosssignaldw) and filter_rmse
endlong = (endcond == 'Direction Change' ? dirchangedw : endcond == 'Zero-Crossing' ? crossdw : crosssignaldw) and filter_rmse
endshort = (endcond == 'Direction Change' ? dirchangeup : endcond == 'Zero-Crossing' ? crossup : crosssignalup) and filter_rmse
plotshape(golong and islong ? low : na, location=location.belowbar, style=shape.labelup, color=color.new(#2E7C13, 0), size=size.small, title='Buy')
plotshape(goshort and isshort ? high : na, location=location.abovebar, style=shape.labeldown, color=color.new(#BF217C, 0), size=size.small, title='Sell')
plotshape(showendlabels and endlong and islong ? high : na, location=location.abovebar, style=shape.xcross, color=color.new(#2E7C13, 0), size=size.tiny, title='Exit Long')
plotshape(showendlabels and endshort and isshort ? low : na, location=location.belowbar, style=shape.xcross, color=color.new(#BF217C, 0), size=size.tiny, title='Exit Short')

alertcondition(golong, title='Long Signal', message='D-RSI: Long Signal')
alertcondition(goshort, title='Short Signal', message='D-RSI: Short Signal')
alertcondition(endlong, title='Exit Long Signal', message='D-RSI: Exit Long')
alertcondition(endshort, title='Exit Short Signal', message='D-RSI: Exit Short')

strategy.entry('long', strategy.long, when=golong and islong)
strategy.entry('short', strategy.short, when=goshort and isshort)
strategy.close('long', when=endlong and islong)
strategy.close('short', when=endshort and isshort)