
この戦略は,時間梯子の加仓方法を利用して量化取引を行う簡単な戦略である.戦略の主な考え方は,毎日の固定時間にポジションを開設し,複数のポジションを確立し,その後,各ポジションに対して異なるストップ・ロスの条件を設定し,それによって分批のストップ・ロスを達成することである.
この戦略は,以下の3つの鍵となる論理に基づいています.
活用するsessionTimeパラメータは,1日間の取引時間帯を設定し,この時間帯の間,毎日の開市時にFIXED梯子式で段階的にポジションを追加し,ポジションの数は,資金プール最大のポジションの数の平均分配である.
ストップポイントを設定します.takeProfitストップポイントstopLossオーダーごとに独立したストップ・ストップ・ロジックがあり,そのため,分批のストップ・ストップ・ロジックを実現します.
当日の取引期間が終了すると,その期間中に未止めたすべてのオーダーを平仓するか選択できます.
この戦略の利点は以下の通りです.
リスク分散,資金プール内の資金等を異なる注文に分配し,単一の注文の損失を効果的に制御する.
各順序のストップ・損失は,各順序に独立したストップ・損失の論理があり,すべての順序が同時に停止することを防ぐ.
柔軟な配置で,最大加仓回数,毎日の取引時間,ストップ・ストップ・損失比率などのパラメータをカスタマイズできます.
戦略の論理はシンプルで明快だ.
この戦略にはリスクもあります.
隠されたリスクがあり,もしすべての注文が止まりラインに達しないのに,先ず対応する止まりラインを触発した場合,大きな損失が生じます.合理的な配置の止まりライン比率で回避できます.
1日の開設総額を制限することはできません.特殊な状況に遭遇した場合,過剰な注文が同時に開設され,資金の承受能力を超えることがあります. 1日の開設総額の最大制限を追加することを考慮することができます.
タイミングを正しく設定しない場合,取引の機会を逃す可能性があります. タイミングを設定すると,対象の取引品種のアクティブ期間を参照することをお勧めします.
この戦略は以下の方向から最適化できます.
ポジション開設条件判断の論理を追加し,特定の技術指標信号を満たす時にのみポジションを開設し,盲目加仓を避ける.
貯蓄池の支える能力を超えることを防ぐために,毎日の貯蓄総額を制限する.
異なる注文に対して異なるストップ・ロスの比率を設定し,差分ストップ・ロスを実現する.
オーダー数を増やして,資金池の余剰と結びつけることで,オーダー数を増やして,資金利用量と結びつけます.
この戦略全体は,時間階層加仓の考え方を活用して量化取引を行う非常に単純な戦略模板であり,戦略の論理は明確であり,同時に一定のリスクと最適化スペースが存在し,開発者は,この基礎で適切な最適化を行うことができ,より安定した信頼性の高い量化戦略となる.
/*backtest
start: 2022-12-20 00:00:00
end: 2023-12-26 00:00: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/
// © A3Sh
//@version=5
strategy("Simple_Pyramiding", overlay=true, pyramiding=99, initial_capital=500, default_qty_type=strategy.percent_of_equity, commission_type=strategy.commission.percent, commission_value=0.075, close_entries_rule='FIFO')
// Study of a Simple DCA strategy that opens a position every day at a specified time.
// A position is opened at the start time of the Timeframe.
// Positions exit individually when the take profit level is triggered.
// Option to activate Stop Loss and/or Position exit at the end of the Timeframe
// Backtest Window
start_time = input(defval=timestamp("01 April 2021 20:00"), group = "Backtest Window", title="Start Time")
end_time = input(defval=timestamp("01 Aug 2022 20:00"), group = "Backtest Window", title="End Time")
window() => true
// Inputs
posCount = input.int (6, group = "Risk", title = "Max Amount of DCA Entries")
takeProfit = input.float (2.5, group = "Risk", title = "Take Profit %")
slSwitch = input.bool (true, group = "Risk", title = "Activate Stop Loss")
stopLoss = input.float (9, group = "Risk", title = "Stop Loss %")
sessionTime = input("1800-1700", group = "DCA Settings", title = "DCA Order Timeframe", tooltip="Open order at the start/If ativated, close order at the end")
exitDCA = input.bool (false, group = "DCA Settings", title = "Exit DCA Entry at end of Timeframe")
// Order size based on max amount of pyramid orders
q = (strategy.equity / posCount) / open
// Timeframe for opening and closing a DCA order
// example taken from https://stackoverflow.com/questions/69230164/pinescript-basic-question-open-a-trade-at-a-set-time-each-day
t = time("D", sessionTime)
isStart = na(t[1]) and not na(t) or t[1] < t
isEnd = na(t) and not na(t[1]) or t[1] < t
bgcolor(t ? color.new(color.blue,95) : na, title = " TimeFrame Color")
// Create DCA Entries
entry_price = 0.0
if isStart and window()
for i = 0 to strategy.opentrades
if strategy.opentrades == i
entry_price := close
entry_id = "PE_" + str.tostring(i + 1)
strategy.entry(id = entry_id, direction=strategy.long, limit=entry_price, qty=q)
if strategy.opentrades == posCount
break
//Exit DCA Entries when take profit or stop loss is triggered
if strategy.opentrades > 0 and window()
for i = 0 to strategy.opentrades
exit_from = "PE_" + str.tostring(i + 1)
exit_id = "Exit_" + str.tostring(i + 1)
strategy.exit(id= exit_id, from_entry= exit_from, profit = close * takeProfit / 100 / syminfo.mintick, loss = slSwitch ? close * stopLoss /100 / syminfo.mintick :na)
//Exit DCA Entries at end of DCA Timeframe
if strategy.opentrades > 0 and exitDCA and isEnd and window()
for i = 0 to strategy.opentrades
exit_from = "PE_" + str.tostring(i + 1)
exit_id = "Exit_" + str.tostring(i + 1)
strategy.exit(id= exit_id, from_entry= exit_from, stop = close)