戦略と買取・保有リターンの視覚的な比較

作者: リン・ハーンチャオチャン,日付: 2024-01-05 15:27:26
タグ:

img

概要

この戦略は,ある戦略と取引される証券の買いと保持の収益を詳細で視覚的に比較します.

戦略の論理

この戦略の基本的な論理は,与えられた戦略とbuy&hold戦略の比較のための4つの重要な要素を図示することです.

  1. 戦略P/L:戦略純利益+戦略オープン利益
  2. 購入・保有P/L: 実現されていない利益
  3. 違い:戦略P/L - Buy & Hold P/L
  4. ストラテジー vs 買い 保持 統計
    • バール戦略P/Lの割合は,Buy&Holdより高い
    • バール戦略のP/Lの割合は,Buy & Holdより低い
    • 全時平均差

上記の4つの要素を比較することで,我々の戦略が シンプルな買取・保有戦略よりも優れているか劣っているかを 直感的に理解することができます.

利点分析

この戦略は,単純に"buy&hold"の収益を比較すると,以下の利点があります.

  1. より包括的で詳細な比較指標,バー別比較と全体的な統計比較を含むので,私たちの戦略がいつ,どこで買い&ホールに勝ったり負けたりするかを明確に知ることができます.

  2. 戦略のP/L,buy&holdP/Lとその間の差をグラフ化する直感的な視覚チャートです. それは私たちの戦略のパフォーマンスをより早く視覚的に伝えることができます.

  3. 特定の期間で戦略を比較することに焦点を当てることができます.

  4. シンプルで使いやすい. 比較論理は内蔵されているので,戦略スクリプトのセクションを自分のものに置き換えるだけです. 比較論理を自分でコードする必要はありません.

リスク分析

この戦略は,主に比較のための取引プラットフォームの内蔵のbuy&holdリターンメトリクスに依存している.そのベンチマークに対する偏見は最終結果に影響を与える.また,この戦略の統計計算には,比較を正確に反映できない欠陥がある可能性があります.

さらに検証のために,より多くのベンチマークと統計方法が導入できます.取引プラットフォームがbuy&holdメトリックに重要な変更を導入した場合,ここで比較論理も調整する必要があります.

オプティマイゼーションの方向性

この戦略は,次の側面からさらに最適化できます.

  1. 3方向または多方向の比較のためのより多くのベンチマークを導入します.例えば,インデックスや業界同級者との比較です.

  2. 戦略を複数の次元から評価するために,年間勝利率,最大引き上げ期間差など,より多くの統計指標を含みます.

  3. バランスマークやメトリックなどのコンポーネントを ユーザーにカスタマイズできるようにします

  4. 図表の可視化を改善します. 簡単な線図では,特定のバーの詳細な比較を検出することが困難になります. 列グラフや追加のマークが役立ちます.

結論

詳細な比較メトリックと直感的な視覚チャートにより,この戦略は,カスタム戦略が単純なバイ&ホールアプローチとどこでどのように異なるかを非常に明確に把握することができます.カスタマイズ可能な日付範囲は,異なる期間に戦略のメリットとデメリットを分析する柔軟性も提供します.

ベンチマーク,メトリック,視覚化により,戦略分析のための非常に強力なツールキットに変えることができます.戦略分析と改善をはるかに効率化するためのテンプレートとフレームワークを提供します.


/*backtest
start: 2023-12-28 00:00:00
end: 2024-01-04 00:00:00
period: 3m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy("VS Buy Hold", precision=2)

bnh_info_panel = input(true, title='Enable Info Panel')
bnh_indicator_panel = input(true, title='Enable Indicator Panel')

//COMPARISON DATE RANGE//
bnh_FromYear = input(1970, title="From Year", minval=1970)
bnh_FromMonth = input(1, title="From Month", minval=1, maxval=12)
bnh_FromDay = input(1, title="From Day", minval=1, maxval=31)

bnh_ToYear = input(2050, title="To Year", minval=2010)
bnh_ToMonth = input(12, title="To Month", minval=1, maxval=12)
bnh_ToDay = input(31, title="To Day", minval=1, maxval=31)

bnh_start = timestamp(bnh_FromYear, bnh_FromMonth, bnh_FromDay, 00, 00)
bnh_finish = timestamp(bnh_ToYear, bnh_ToMonth, bnh_ToDay, 23, 59)
bnh_timeCond = time >= bnh_start and time <= bnh_finish ? true: false
    
//Note: If you are going to use the COMPARISON DATE RANGE above, apply bnh_timeCond
//      to your strategy parameters.


/////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////STRATEGY SCRIPT START//////////////////////////////////

//=========================PLACEHOLDER MA CROSS STRATEGY=========================//
fastLength = 50
slowLength = 200
price = close

mafast = sma(price, fastLength)
maslow = sma(price, slowLength)
strategy.initial_capital = 50000
positionSize = strategy.initial_capital / close

if (crossover(mafast, maslow) and bnh_timeCond) // <= bnh_timeCond added as a condition
    strategy.entry("MA2CrossLE", strategy.long, positionSize, comment="MA2CrossLE")

if (crossunder(mafast, maslow) and bnh_timeCond) // <= bnh_timeCond added as a condition
    strategy.entry("MA2CrossSE", strategy.short, positionSize, comment="MA2CrossSE")

//////////////////////////////STRATEGY SCRIPT END////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////



//STRATEGY EQUITY
strategy_pnl = strategy.netprofit + strategy.openprofit
bnh_strategy_pnl_pcnt = (strategy_pnl / strategy.initial_capital) * 100


//BUY AND HOLD EQUITY
float bnh_start_bar = na
bnh_start_bar := na(bnh_start_bar[1]) and bnh_timeCond? close : bnh_start_bar[1]
bnl_buy_hold_equity = ((close - bnh_start_bar)/bnh_start_bar) * 100


//STRATEGY VS BUY AND HOLD STATS
bnh_vs_diff = bnh_strategy_pnl_pcnt - bnl_buy_hold_equity

bnh_bar_counter = 0
bnh_bar_counter := bnh_vs_diff > 0 ? nz(bnh_bar_counter[1]) + 1 : bnh_bar_counter[1]

bnh_bar_counter2 = 0
bnh_bar_counter2 := bnh_vs_diff <= 0 ? nz(bnh_bar_counter2[1]) + 1 : bnh_bar_counter2[1]

bnh_pcnt_above = (bnh_bar_counter/(bnh_bar_counter + bnh_bar_counter2))*100
bnh_pcnt_below = (bnh_bar_counter2/(bnh_bar_counter + bnh_bar_counter2))*100

bnh_average_diff = cum(bnh_vs_diff) / (bnh_bar_counter + bnh_bar_counter2)


//PLOTS AND LABELS
bnh_diff_color = bnh_vs_diff > 0 ? color.green : color.red
plot(bnh_vs_diff, style=plot.style_columns, color=bnh_diff_color, transp=60, title='SvB')
plot(bnh_strategy_pnl_pcnt, color=color.yellow, linewidth=2, title="SR")
plot(bnl_buy_hold_equity, color=color.blue, title="BHR")

// draw_IndicatorLabel(_text, _x, _y, label_color, font_color)=>
//     string_text = _text
//     var label la_indicator = na
//     label.delete(la_indicator)
//     la_indicator := label.new(
//          x=_x, y=_y, 
//          text=string_text, xloc=xloc.bar_index, yloc=yloc.price, 
//          color=label_color, style=label.style_labeldown, textcolor=font_color, size=size.small)

// draw_InfoPanel(_text, _x, _y, font_size)=>
//     var label la_panel = na
//     label.delete(la_panel)
//     la_panel := label.new(
//          x=_x, y=_y, 
//          text=_text, xloc=xloc.bar_time, yloc=yloc.price, 
//          color=color.new(#383838, 5), style=label.style_labelup, textcolor=color.white, size=font_size)

// if bnh_indicator_panel         
//     draw_IndicatorLabel("Difference", bar_index, bnh_vs_diff, color.new(color.gray, 40), color.white)
//     draw_IndicatorLabel("Strategy P/L", bar_index, bnh_strategy_pnl_pcnt, color.new(color.yellow, 50), color.white)
//     draw_IndicatorLabel("Buy & Hold P/L", bar_index, bnl_buy_hold_equity, color.new(color.blue, 50), color.white)

// info_panel_x = time_close + round(change(time) * 200)
// info_panel_y = max(max(bnl_buy_hold_equity, bnh_strategy_pnl_pcnt), bnh_vs_diff) + abs(bnh_vs_diff * 0.25)


// title = "STRATEGY vs BUY & HOLD STATS"
// row0 = "-----------------------------------------------------"
// row1 = 'Bars Above Buy & Hold: ' + tostring(bnh_pcnt_above, '#.##') + '%'
// row2 = 'Bars Below Buy & Hold: ' + tostring(bnh_pcnt_below, '#.##') + '%'
// row3 = 'All Time Ave. Difference: ' + tostring(bnh_average_diff, '#.##') + '%'

// panel_text = '\n' + title + '\n' + row0 + '\n' + row1 + '\n\n' + row2 + '\n\n' + row3 + '\n'

// if bnh_info_panel
//     draw_InfoPanel(panel_text, info_panel_x, info_panel_y, size.normal)



もっと