RSI এবং ওয়েটেড মুভিং মিডিয়ার উপর ভিত্তি করে ট্রেন্ড অনুসরণকারী কৌশল

লেখক:চাওঝাং, তারিখ: ২০২৩-১২-২৫ 13:28:24
ট্যাগঃ

img

সারসংক্ষেপ

এই কৌশলটি দুটি সুপরিচিত সূচকঃ আপেক্ষিক শক্তি সূচক (আরএসআই) এবং ওজনযুক্ত চলমান গড় (ডাব্লুএমএ) এর উপর ভিত্তি করে। এটি বাজারের প্রবণতা সনাক্ত করে এবং এর দিক অনুসরণ করে। আরএসআই ওভারকুপ / ওভারসোল্ড স্তরগুলি বিচার করে, ডাব্লুএমএ মূল্য প্রবণতা নির্ধারণ করে। উভয়ের সমন্বয় কার্যকরভাবে অপ্রাসঙ্গিক সংকেতগুলি ফিল্টার করতে এবং লাভজনকতা উন্নত করতে পারে। এটি একটি মাঝারি / দীর্ঘমেয়াদী কৌশল, লাভ / ক্ষতির উপর ভিত্তি করে অবস্থানের আকার সামঞ্জস্য করার জন্য অর্থ পরিচালনার পদ্ধতির সাথে যুক্ত।

কৌশলগত যুক্তি

আরএসআই সূচক

আরএসআই হল সর্বাধিক পরিচিত ওভারকুপ/ওভারসোল্ড সূচকগুলির মধ্যে একটি। এর সূত্রটি হলঃ

$$আরএসআই=100 - \frac{100}{1+\frac{AvgGain}{AvgLoss}}$$

যেখানে AvgGain হল গত x দিনের মধ্যে বন্ধের চেয়ে বেশি খোলা দিনের গড়, এবং AvgLoss হল গত x দিনের মধ্যে বন্ধের চেয়ে কম খোলা দিনের পরম মানের গড়।

এই কৌশলটি প্রবণতা বিচার করার জন্য আরএসআই সময়কালকে 20 হিসাবে সেট করে। 60 এর উপরে আরএসআই দীর্ঘ সংকেত উত্পন্ন করে, 40 এর নীচে আরএসআই সংক্ষিপ্ত সংকেত উত্পন্ন করে।

ওজনযুক্ত চলমান গড় (WMA)

এসএমএর সাথে তুলনা করে, ডাব্লুএমএ সাম্প্রতিক মূল্যগুলিকে আরও ভারী করে। এর সূত্রটি হলঃ

$$WMA = \frac{\sum_{i=1}^n w_i x_i}{\sum_{i=1}^n w_i}$

যেখানে w হল ওজন, w ক্রমবর্ধমান i এর সাথে এক্সপোনেন্সিয়ালভাবে বৃদ্ধি পায়। এই কৌশলটি নিম্নলিখিত ওজন সূত্র ব্যবহার করেঃ

$$w = \begin{cases} 100/(4+(n-4)১.৩), & i <= ৩ \ ১.৩w, & i > 3 \end{case}$$

অর্থাৎ গত তিন দিনে ওজন সমান হয়েছে এবং প্রতি এক দিনে ১.৩ গুণ বাড়ছে।

এই কৌশলতে WMA এর দৈর্ঘ্য ২০।

কৌশলগত সংকেত

দীর্ঘ সংকেতঃ RSI > 60 এবং WMA এর 20 দিনের ROC < -1 সংক্ষিপ্ত সংকেতঃ RSI < 40 এবং WMA এর 20 দিনের ROC > 1

যেখানে WMA এর ২০ দিনের ROC গণনা করা হয়ঃ

$$ROC = (WMA_{today}/WMA_{20_days_ago} - 1) \times 100$$

সুবিধা

  • ট্রেন্ডের দিকনির্দেশনা নির্ধারণের জন্য আরএসআই ব্যবহার করুন, উইপসা বাজারে অর্থ হারাবেন না
  • ডব্লিউএমএ সাম্প্রতিক দামগুলিকে গোলমাল হ্রাস এবং প্রধান প্রবণতা সনাক্ত করার জন্য ওজন করে
  • RSI এবং WMA ROC এর সমন্বয় অপ্রাসঙ্গিক সংকেতগুলিকে ফিল্টার করে
  • একাধিক এটিআর অনুসরণকারী লাভ গ্রহণ, নমনীয় লাভ গ্রহণ
  • অর্থ ব্যবস্থাপনা লাভ/হানির উপর ভিত্তি করে অবস্থানের আকার সামঞ্জস্য করে, ঝুঁকি নিয়ন্ত্রণ করে

ঝুঁকি

  • অনুপযুক্ত প্যারামিটার সেটিং ট্রেডিং ফ্রিকোয়েন্সি বৃদ্ধি করতে পারে
  • ভুল স্টপ লস সেটিং ক্ষতি বাড়াতে পারে
  • প্রবণতা অনুসরণকারী কৌশল হিসাবে, পরিসীমা-সীমাবদ্ধ বাজারের জন্য উপযুক্ত নয়
  • ম্যাক্রো পরিবেশের পরিবর্তনগুলিতে মনোযোগ দিন, প্রয়োজন হলে ম্যানুয়াল হস্তক্ষেপ করুন

উন্নতির দিকনির্দেশ

  • সর্বোত্তম প্যারামিটার সেট খুঁজে পেতে RSI দৈর্ঘ্য, WMA দৈর্ঘ্য, ROC প্রান্তিক পরীক্ষা করুন
  • সেরা পজিশন সমন্বয় পরিকল্পনা খুঁজে পেতে বিভিন্ন অর্থ ব্যবস্থাপনা পদ্ধতি পরীক্ষা করুন
  • আরও সিগন্যাল ফিল্টারিংয়ের জন্য অন্যান্য সূচক যোগ করুন
  • ট্রেডিংয়ের জন্য হ্রাস ঝুঁকি সীমাবদ্ধ করার জন্য স্টপ লস কৌশল অন্তর্ভুক্ত করুন
  • ট্রেন্ড চলাকালীন মুনাফা সর্বাধিকীকরণের জন্য মুনাফা গ্রহণের কৌশলটি অনুকূল করুন

সিদ্ধান্ত

এই কৌশলটি ট্রেন্ডের দিকনির্দেশ নির্ধারণের জন্য আরএসআই এবং ডাব্লুএমএকে একত্রিত করে, যার লক্ষ্য মধ্যম / দীর্ঘমেয়াদে প্রধান প্রবণতা থেকে লাভ অর্জন করা। ঝুঁকি নিয়ন্ত্রণের জন্য অর্থ পরিচালনা এবং মুনাফা গ্রহণের কৌশলগুলিও ব্যবহৃত হয়। এটির ব্যবহারিক মূল্য রয়েছে তবে আরও ভাল ফলাফলের জন্য পরামিতি সেটিং এবং স্টপ লস প্রক্রিয়াটির অবিচ্ছিন্ন পরীক্ষা এবং অপ্টিমাইজেশনের প্রয়োজন। বিনিয়োগকারীদের পরিস্থিতি মূল্যায়ন করা উচিত, প্রয়োজনে ম্যানুয়ালি হস্তক্ষেপ করা উচিত এবং নিয়ন্ত্রণযোগ্য ঝুঁকি নিশ্চিত করা উচিত।


/*backtest
start: 2022-12-24 00:00:00
end: 2023-12-06 05:20:00
period: 1d
basePeriod: 1h
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/
// © gsanson66


//This code is based on RSI and a backed weighted MA
//@version=5
strategy("RSI + MA BACKTESTING", overlay=true, initial_capital=1000, default_qty_type=strategy.fixed, commission_type=strategy.commission.percent, commission_value=0.18, slippage=3)


//------------------------TOOL TIPS---------------------------//

t1 = "Choice between a Standard MA (SMA) or a backed-weighted MA (RWMA) which permits to minimize the impact of short term reversal. Default is RWMA."
t2 = "Value of RSI to send a LONG or a SHORT signal. RSI above 60 is a LONG signal and RSI below 40 is a SHORT signal."
t3 = "Rate of Change Value of selected MA to send a LONG or a SHORT signal. By default : ROC MA below -1 is a LONG signal and ROC MA above 1 is a SHORT signal"
t4 = "Threshold value to trigger trailing Take Profit. This threshold is calculated as a multiple of the ATR (Average True Range)."
t5 = "Percentage value of trailing Take Profit. This Trailing TP follows the profit if it increases, remaining selected percentage below it, but stops if the profit decreases."
t6 = "Each gain or losse (relative to the previous reference) in an amount equal to this fixed ratio will change quantity of orders."
t7 = "The amount of money to be added to or subtracted from orders once the fixed ratio has been reached."


//------------------------FUNCTIONS---------------------------//

//@function which calculate a retro weighted moving average to minimize the impact of short term reversal
rwma(source, length) =>
    sum = 0.0
    denominator = 0.0
    weight = 0.0
    weight_x = 100/(4+(length-4)*1.30)
    weight_y = 1.30*weight_x
    for i=0 to length - 1
        if i <= 3
            weight := weight_x
        else
            weight := weight_y
        sum := sum + source[i] * weight
        denominator := denominator + weight
    rwma = sum/denominator

//@function which permits the user to choose a moving average type
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "RWMA" => rwma(source, length)

//@function Displays text passed to `txt` when called.
debugLabel(txt, color) =>
    label.new(bar_index, high, text = txt, color=color, style = label.style_label_lower_right, textcolor = color.black, size = size.small)

//@function which looks if the close date of the current bar falls inside the date range
inBacktestPeriod(start, end) => (time >= start) and (time <= end)


//--------------------------------USER INPUTS-------------------------------//

//Technical parameters
rsiLengthInput = input.int(20, minval=1, title="RSI Length", group="RSI Settings")
maTypeInput = input.string("RWMA", title="MA Type", options=["SMA", "RWMA"], group="MA Settings", inline="1", tooltip=t1)
maLenghtInput = input.int(20, minval=1, title="MA Length", group="MA Settings", inline="1")
rsiLongSignalValue = input.int(60, minval=1, maxval=99, title="RSI Long Signal", group="Strategy parameters", inline="3")
rsiShortSignalValue = input.int(40, minval=1, maxval=99, title="RSI Short Signal", group="Strategy parameters", inline="3", tooltip=t2)
rocMovAverLongSignalValue = input.float(-1, maxval=0, title="ROC MA Long Signal", group="Strategy parameters", inline="4")
rocMovAverShortSignalValue = input.float(1, minval=0, title="ROC MA Short Signal", group="Strategy parameters", inline="4", tooltip=t3)
//TP Activation and Trailing TP
takeProfitActivationInput = input.float(5, minval=1.0, title="TP activation in multiple of ATR", group="Strategy parameters", tooltip=t4)
trailingStopInput = input.float(3, minval=0, title="Trailing TP in percentage", group="Strategy parameters", tooltip=t5)
//Money Management
fixedRatio = input.int(defval=400, minval=1, title="Fixed Ratio Value ($)", group="Money Management", tooltip=t6)
increasingOrderAmount = input.int(defval=200, minval=1, title="Increasing Order Amount ($)", group="Money Management", tooltip=t7)
//Backtesting period
startDate = input(title="Start Date", defval=timestamp("1 Jan 2018 00:00:00"), group="Backtesting Period")
endDate = input(title="End Date", defval=timestamp("1 July 2024 00:00:00"), group="Backtesting Period")


//------------------------------VARIABLES INITIALISATION-----------------------------//

float rsi = ta.rsi(close, rsiLengthInput)
float ma = ma(close, maLenghtInput, maTypeInput)
float roc_ma = ((ma/ma[maLenghtInput]) - 1)*100
float atr = ta.atr(20)
var float trailingStopOffset = na
var float trailingStopActivation = na
var float trailingStop = na
var float stopLoss = na
var bool long = na
var bool short = na
var bool bufferTrailingStopDrawing = na
float theoreticalStopPrice = na
bool inRange = na
equity = math.abs(strategy.equity - strategy.openprofit)
strategy.initial_capital = 50000
var float capital_ref = strategy.initial_capital
var float cashOrder = strategy.initial_capital * 0.95


//------------------------------CHECKING SOME CONDITIONS ON EACH SCRIPT EXECUTION-------------------------------//

//Checking if the date belong to the range
inRange := true

//Checking performances of the strategy
if equity > capital_ref + fixedRatio
    spread = (equity - capital_ref)/fixedRatio
    nb_level = int(spread)
    increasingOrder = nb_level * increasingOrderAmount
    cashOrder := cashOrder + increasingOrder
    capital_ref := capital_ref + nb_level*fixedRatio
if equity < capital_ref - fixedRatio
    spread = (capital_ref - equity)/fixedRatio
    nb_level = int(spread)
    decreasingOrder = nb_level * increasingOrderAmount
    cashOrder := cashOrder - decreasingOrder
    capital_ref := capital_ref - nb_level*fixedRatio

//Checking if we close all trades in case where we exit the backtesting period
if strategy.position_size!=0 and not inRange
    debugLabel("END OF BACKTESTING PERIOD : we close the trade", color=color.rgb(116, 116, 116))
    strategy.close_all()
    bufferTrailingStopDrawing := false
    stopLoss := na
    trailingStopActivation := na
    trailingStop := na
    short := false
    long := false


//------------------------------STOP LOSS AND TRAILING STOP ACTIVATION----------------------------//

// We handle the stop loss and trailing stop activation 
if (low <= stopLoss or high >= trailingStopActivation) and long
    if high >= trailingStopActivation
        bufferTrailingStopDrawing := true
    else if low <= stopLoss
        long := false
    stopLoss := na
    trailingStopActivation := na
if (low <= trailingStopActivation or high >= stopLoss) and short
    if low <= trailingStopActivation
        bufferTrailingStopDrawing := true
    else if high >= stopLoss
        short := false
    stopLoss := na
    trailingStopActivation := na


//-------------------------------------TRAILING STOP--------------------------------------//

// If the traling stop is activated, we manage its plotting with the bufferTrailingStopDrawing
if bufferTrailingStopDrawing and long
    theoreticalStopPrice := high - trailingStopOffset * syminfo.mintick
    if na(trailingStop)
        trailingStop := theoreticalStopPrice
    else if theoreticalStopPrice > trailingStop
        trailingStop := theoreticalStopPrice
    else if low <= trailingStop
        trailingStop := na
        bufferTrailingStopDrawing := false
        long := false
if bufferTrailingStopDrawing and short
    theoreticalStopPrice := low + trailingStopOffset * syminfo.mintick
    if na(trailingStop)
        trailingStop := theoreticalStopPrice
    else if theoreticalStopPrice < trailingStop
        trailingStop := theoreticalStopPrice
    else if high >= trailingStop
        trailingStop := na
        bufferTrailingStopDrawing := false
        short := false


//---------------------------------LONG CONDITION--------------------------//

if rsi >= 60 and roc_ma <= rocMovAverLongSignalValue and inRange and not long
    if short
        bufferTrailingStopDrawing := false
        stopLoss := na
        trailingStopActivation := na
        trailingStop := na
        short := false
    trailingStopActivation := close + takeProfitActivationInput*atr
    trailingStopOffset := (trailingStopActivation * trailingStopInput/100) / syminfo.mintick
    stopLoss := close - 3*atr
    long := true
    qty = cashOrder/close
    strategy.entry("Long", strategy.long, qty)
    strategy.exit("Exit Long", "Long", stop = stopLoss, trail_price = trailingStopActivation,
                 trail_offset = trailingStopOffset)


//--------------------------------SHORT CONDITION-------------------------------//

if rsi <= 40 and roc_ma >= rocMovAverShortSignalValue and inRange and not short
    if long
        bufferTrailingStopDrawing := false
        stopLoss := na
        trailingStopActivation := na
        trailingStop := na
        long := false
    trailingStopActivation := close - takeProfitActivationInput*atr
    trailingStopOffset := (trailingStopActivation * trailingStopInput/100) / syminfo.mintick
    stopLoss := close + 3*atr
    short := true
    qty = cashOrder/close
    strategy.entry("Short", strategy.short, qty)
    strategy.exit("Exit Short", "Short", stop = stopLoss, trail_price = trailingStopActivation,
                 trail_offset = trailingStopOffset)


//--------------------------------PLOTTING ELEMENT---------------------------------//

// Plotting of element in the graph
plotchar(rsi, "RSI", "", location.top, color.rgb(0, 214, 243))
plot(ma, "MA", color.rgb(219, 219, 18))
plotchar(roc_ma, "ROC MA", "", location.top, color=color.orange)
// Visualizer trailing stop and stop loss movement
plot(stopLoss, "SL", color.red, 3, plot.style_linebr)
plot(trailingStopActivation, "Trigger Trail", color.green, 3, plot.style_linebr)
plot(trailingStop, "Trailing Stop",  color.blue, 3, plot.style_linebr)


আরো