PB SAR バックテスト戦略

作者: リン・ハーンチャオチャン開催日:2023年10月11日 15:22:26
タグ:

概要

この戦略は,弾性停止損失指標をベースに,買いと売りの信号を設定し,長所と短所を操作する.指標が買い信号が出たとき,より多くのことをする;売り信号が出たとき,空にする.戦略は,追跡停止損失メカニズムを組み合わせて,リスクを効果的に制御する.

原則

この戦略は,主に柔軟性停止指標がトレンドの転換点を識別し,逆転操作を行う.指標内部では,真の範囲指標が極値価格を識別し,価格が極値を超えると異常突破として判断し,トレンド逆転の可能性を判断する.具体的には,指標内部では,極値価格 (EP) とトリガー価格 (TP) の2つの変数を維持する.EPは現在のトレンドの下の最高価格または最低価格を表す.TPはEPから計算される.

上向きの傾向では,価格がEPより高くなったとき,異常突破と判断され,このときEPは最高価格,TPは最低価格と更新される.価格がTPより低いとき,トレンドが逆転して売り信号を生成することを決定する.下向きの傾向では,原理は類似している.

この戦略は,ストップ損失を追跡するメカニズムを組み合わせ,ポジションが開かれたとき,最適なストップ損失価格をリアルタイムに追跡し,利益を保証しながらリスクを制御します.具体的には,多額で取引した後,ストップ損失ラインは閉店低点を追跡し,空いた後に,ストップ損失ラインは閉店高点を追跡します.

利点

この戦略には以下のような利点があります.

  1. 傾向の転換点を識別する指標は,簡単に拘束されることができません.

  2. ストップ損失の仕組みを追跡することで,利益をロックし,損失拡大を防ぐことができます.

  3. 指標のパラメータはシンプルで実現しやすい.

  4. 購入・販売のシグナルを設定して操作が簡単です.

  5. 戦略効果を全面的に評価するために,リテストサイクルを柔軟に配置できます.

リスク

この戦略にはいくつかのリスクもあります:

  1. この指標は遅れているため,トレンドの逆転の最適なポイントを見逃す可能性があります.

  2. ストップダースが激しくなり,短期間の価格変動によってストップされる可能性があります.

  3. 復習周期の選択は不適切で,戦略の効果を全面的に評価することができません.

  4. 取引コストが収益に与える影響について考える必要があります.

リスクに対応するには,以下の点から最適化することができます:

  1. 遅延を減らすために指標のパラメータを調整します.

  2. ストップダースアルゴリズムを最適化して,詐欺を避ける.

  3. 信頼性を確保するために適切な再テストサイクルを選択します.

  4. ポジション管理を最適化し,取引コストを削減する.

優化方向

この戦略は,以下の点からさらに最適化することができます:

  1. トレンド指標を組み合わせると,反転取引が套布されるのを避ける.

  2. ポジション管理アルゴリズムの最適化,例えば固定比例ポジション,ダイナミックポジションなど.

  3. ギャップによる誤った取引を避けるために,取引量フィルターを追加します.

  4. パラメータ最適化により,最適なパラメータ組み合わせを見つけます.

  5. 傾向が動いている間に,タイミングで停止する,停止戦略に参加します.

  6. 停止損失をよりスムーズにするために停止損失戦略を最適化します. 停止損失アルゴリズム,例えば Chandelier Exit を試してみることができます.

  7. 取引の種類や時間帯を最適化し,戦略的適応性を向上させる.

  8. 機械学習アルゴリズムを組み込むことで,戦略がより適応可能になります.

概要

この戦略は全体的に比較的シンプルで信頼性があり,弾性停止指標を用いて反転点を特定し,停止メカニズムを追跡してリスクを制御するために装備されており,短線反転戦略として使用できます.しかし,指標遅滞,停止損失が極端すぎるなどの問題には注意する必要があります.さらに最適化することで,より良い戦略効果を得ることが期待されます.

概要

この戦略は,ロングおよびショートポジションの買いと売却信号を生成するために,パラボリックSAR指標に基づいています.また,リスクを効果的に制御するために,トライリングストップロスのメカニズムも組み込みます.

原則

この戦略の核心は,対トレンド取引のためのパラボリックSAR指標を使用してトレンド逆転点を特定することです.指標は,極端な価格を検出するために真の範囲を使用します.価格が極端を超えると,それはブレイクアウトと潜在的なトレンド逆転の兆候とみなされます.特に,指標は,極端な価格 (EP) とトリガー価格 (TP) という2つの変数を維持します.EPは現在のトレンドの最高/最低価格を表し,TPはEPから派生します.

上昇傾向では,価格がEPより高くなったとき,それはブレイクアウトと見なされます. EPは最高価格に更新され,TPは最低価格に更新されます.価格がTPを下回ると,トレンド逆転が特定され,セールシグナルが生成されます.同じ原則はダウントレンドに適用されます.

ストップロスのメカニズムも組み込まれています.ポジションを開いた後,最適なストップロスの価格をリアルタイムで追跡し,リスクを制御しながら利益をロックします.特に,ロングエントリー後,ストップロスは閉じる低値を追跡します.ショートエントリー後,閉じる高値を追跡します.

利点

この戦略の主な利点は以下の通りです.

  1. 傾向の逆転点を指標で特定し,傾向に囚われないようにします.

  2. ストップロスは利益を固定し より大きな損失を防ぐ

  3. シンプルな指標パラメータで 簡単に実行できます

  4. 設定可能な買い/売る信号のアラート

  5. 柔軟なバックテスト期間設定で徹底的な評価を行う.

リスク

考慮すべきリスクもいくつかあります.

  1. インディケーターの遅延は,最適な逆転点を見逃す可能性があります.

  2. 短期の変動によって 攻撃的なストップが停止される可能性があります

  3. バックテスト期間を不適切に選択することで 戦略を完全に評価することはできません

  4. トランザクションコストは利益を損なう可能性があります.

リスクに対処する方法は以下の通りです

  1. パラメータを最適化して遅延を減らす

  2. ストップ・ロスのアルゴリズムを改良して 不必要にストップを避ける

  3. 信頼性に関する適切なバックテスト期間を選択する.

  4. 取引コストを下げるためにポジションサイズを最適化します

強化

戦略をさらに最適化する方法:

  1. 逆動向に囚われないように MA のような傾向指標を組み込む.

  2. ポジションサイズのアルゴリズムを最適化する.例えば固定分数,ダイナミック.

  3. 音量フィルターを追加して 隙間からの偽信号を避ける.

  4. パラメータの最適化により 最適な組み合わせを見つけます

  5. 傾向から利益を得るために 利益を得る戦略を導入する.

  6. スムーズなストップをするためにストップ損失アルゴリズムを改良します. チェンデリア出口などで実験します.

  7. 製品や時間枠などで最適化して適応性を向上させる.

  8. 機械学習を組み込むことで 適応性が向上します

概要

概要すると,これは逆転を特定し,リスクを制御するためにストップロスを追跡するためにパラボリックSARを使用するシンプルで堅牢な戦略です.これは短期的な平均逆転戦略として機能できます.しかし,指標遅延と過敏なストップは対処する必要があります.さらなる最適化はパフォーマンスの向上につながります.


/*backtest
start: 2023-09-10 00:00:00
end: 2023-10-10 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

//@version=4
strategy("PB SAR BackTest - Colorbar", overlay=false)

// Full credit to Sawcruhteez, Lucid Investment Strategies LLC, Casey Bowman and Peter Brandt.
// This is a strategy version of the Peterbolic SAR indicator created by the above-mentioned parties.
// Original version of the indicator: https://www.tradingview.com/script/6nYrH3Vm-Peterbolic-SAR/

// SAR #1
// Lucid Sar
// Branded under the name "Lucid SAR"
// as agreed to with Lucid Investment Strategies LLC on July 9, 2019
// https://lucidinvestmentstrategies.com/
// see branch "lucid"

// SAR #2
// Peterbolic Sar
// Using the name "Peterbolic SAR"
// as agreed to by Peter Brandt on October 2, 2019
// - https://twitter.com/PeterLBrandt/status/1179365590668075008
// in response to request from Sawcruhteez
// - https://twitter.com/Sawcruhteez/status/1179213105705836544
// Sawcruhteez gives credit to @CrazyGabey for coming up with the name
// - https://twitter.com/Sawcruhteez/status/1179213196583940097
// see branch "peterbolic"

// SAR #3
// Sawcruhteez Sar
// Branded under the name "Sawcruhteez SAR"
// as agreed to with Sawcruhteez on September 11, 2019
// see branch "sawcruhteez"

// Open Source on github
// https://github.com/casey-bowman/sar/blob/peterbolic/peterbolic.pine

// Created by Casey Bowman on July 4, 2019

// MIT License

// Copyright (c) 2019 Casey Bowman

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.



TSR() =>

    // start with uptrend
    var uptrend = true
    var EP = high       // extreme price - high or low depending on trend
    var SP = low        // setup price
    var TP = float(na)  // trigger price


    var setup   = low
    var trigger = float(na)

    if barstate.isnew
        setup := low
        trigger = float(na)

    extreme_candle = false
    first_extreme_candle = false
    setup_candle = false
    trigger_candle = false

    waiting_for_setup = false
    waiting_for_trigger = false

    var since_extreme = 0
    var since_setup = 0

    waiting_for_setup   := not extreme_candle and not na(SP)
    waiting_for_trigger := not na(TP)

    if not barstate.isfirst
        if barstate.isnew and extreme_candle[1]
            trigger := float(na)
        if barstate.isnew and setup_candle[1]
            setup := float(na)
        if barstate.isnew and waiting_for_trigger
            since_setup := since_setup + 1
            trigger := TP
        if barstate.isnew and waiting_for_setup
            since_extreme := since_extreme + 1
            setup := SP
        if uptrend

            if extreme_candle
                EP := high
                SP := low
            else
                if high > EP
                    extreme_candle := true
                    EP := high
                    SP := low
                    since_extreme := 0
                    since_setup   := 0
                else
                    if waiting_for_setup
                        if barstate.isconfirmed
                            if close < SP
                                setup_candle := true
                                SP := float(na)
                                TP := low
            if waiting_for_trigger
                if low < TP
                    trigger_candle := true
                    extreme_candle := true
                    EP := low
                    SP := high
                    TP := float(na)
                    uptrend := false
                    since_extreme := 0
                    since_setup := 0
                else
                    if barstate.isconfirmed and extreme_candle
                        TP := float(na)
                        trigger := float(na)

        else
            if extreme_candle
                EP := low
                SP := high
            else
                if low <  EP
                    extreme_candle := true
                    EP := low
                    SP := high
                    since_extreme := 0
                    since_setup   := 0
                else
                    if waiting_for_setup
                        if barstate.isconfirmed
                            if close > SP
                                setup_candle := true
                                SP := float(na)
                                TP := high
            if waiting_for_trigger
                if high > TP
                    trigger_candle := true
                    extreme_candle := true
                    EP := high
                    SP := low
                    TP := float(na)
                    uptrend := true
                    since_extreme := 0
                    since_setup := 0
                else
                    if barstate.isconfirmed and extreme_candle
                        TP := float(na)
                        trigger := float(na)


    [trigger_candle, trigger, since_setup, setup_candle, setup, since_extreme, extreme_candle, uptrend]


[TC, T, SS, SC, S, SE, EC, up] = TSR()

// Make input options that configure backtest date range
StartMonth = input(title="Start Month", type=input.integer,
     defval=1, minval=1, maxval=12)
StartDate = input(title="Start Date", type=input.integer,
     defval=1, minval=1, maxval=31)
StartYear = input(title="Start Year", type=input.integer,
     defval=(2019), minval=1800, maxval=2100)

EndMonth = input(title="End Month", type=input.integer,
     defval=1, minval=1, maxval=12)
EndDate = input(title="End Date", type=input.integer,
     defval=1, minval=1, maxval=31)
EndYear = input(title="End Year", type=input.integer,
     defval=(2020), minval=1800, maxval=2100)
     
// Look if the close time of the current bar falls inside the date range
inDateRange = true

buytrigger = (TC and up)
selltrigger = (TC and not up)
buysetup = (SC and not up)
sellsetup = (SC and up)

IntBuy = buytrigger ? 1 : 0
IntSB = buysetup ? 0.5 : 0

IntSell= selltrigger ? -1 : 0
IntSS = sellsetup ? -0.5 : 0

bgcolor = buytrigger ? color.green : selltrigger ? color.red : buysetup ? color.yellow : sellsetup ? color.orange : color.black
trans = buytrigger ? 20 : selltrigger ? 20 : 100

bgcolor(bgcolor, 30)

NUM = IntBuy + IntSB + IntSell + IntSS
linecolor = color.orange
plot(NUM, color=linecolor, linewidth=2)

alertcondition(NUM > 0.5, title="Buy Signal", message="Buy Alert")
alertcondition(NUM < -0.5, title="Sell Signal", message="Sell Alert")

alertcondition(NUM == 0.5, title="Buy Setup", message="Buy Setup")
alertcondition(NUM == -0.5, title="Sell Setup", message="Sell Setup")

//Switch on for strategy moves

if(inDateRange and buytrigger)
    strategy.exit("SHORT", "SHORT_SL", comment="Short_Exit")
    strategy.entry("LONG", strategy.long, comment="")
if(inDateRange and selltrigger)
    strategy.exit("LONG", "LONG_SL", comment="Long_Exit")
    strategy.entry("SHORT", strategy.short, comment="")
if (not inDateRange)
    strategy.close_all()

// plotshape(SC and not up, color = color.yellow, style = shape.triangleup, location = location.belowbar, size = size.auto, transp = 0, title = "Setup to Buy")
// plotshape(TC and up, color = color.green, style = shape.triangleup, location = location.belowbar, size = size.auto, title = "Trigger to Buy")
// plotshape(SC and up, color = color.yellow, style = shape.triangledown, location = location.abovebar, size = size.auto, transp = 0, title = "Setup to Sell")
// plotshape(TC and not up, color = color.red, style = shape.triangledown, location = location.abovebar, size = size.auto, title = "Trigger to Sell")


もっと