ランダムなエントリーとエグジット戦略


作成日: 2023-10-11 15:17:28 最終変更日: 2023-10-11 15:17:28
コピー: 0 クリック数: 966
1
フォロー
1628
フォロワー

概要

ランダムな入場・出場戦略は,取引過程でランダムに入場・出場時間を決定することによって行われる戦略である.この戦略は,ランダムな数生成器を使用して入場・出場の決定を模倣する.

戦略原則

この戦略の核心的な論理は:

  1. K線は0から100までの数字をランダムに生成します.

  2. ランダムな数が設定された入場確率の値より小さい場合は,入場する.入場確率のデフォルトは10%である.

  3. ランダムな数字が設定された出場確率の値より小さい場合は,平仓出場する. 既定出場確率は3%である.

  4. 3つの方向から選べます. 余分なだけ,空きだけ,ランダムな方向.

  5. また,取引開始年を設定して,特大変動を回避することもできます.

異なる入場確率,出場確率,方向パラメータを設定することで,異なるタイプのトレーダーによるランダムな取引行動を模倣し,ランダムな取引下で異なる市場のパフォーマンスを調べることができる.

優位分析

  • リアルな取引先のランダムな意思決定を模倣し,現実の市場状況に近付く.

  • ランダムな取引で異なる市場のパフォーマンスの違いをテストできます.

  • ランダムな取引でも正の利益を得られる市場を見つけましょう.

  • ランダムな取引は,他の戦略の優位性をテストするための基準戦略として使用できます.

リスク分析

  • 市場動向から利益を得られず,入場時期を決められない.

  • ランダムな出場は不利な位置で止まるかもしれない.

  • 市場がはっきりとした方向性がある場合, 市場がうまく機能しない.

  • 入場・出場の確率を最適化して,頻繁に出場したり,短期間出場したりしないようにする.

  • 損失の拡大を防ぐために,損失防止メカニズムを導入することも検討できます.

最適化の方向

  • 市場を調整し,異なる市場に適した組み合わせを見つけます.

  • 単一損失をコントロールするストップ・ロズ戦略に参加する.

  • ポジション管理を最適化し,単発リスクを低減する.

  • トレンドがはっきりした場合は,トレンドフォロー戦略に変更できます.

  • 統計分析と組み合わせて,ランダムな取引がどの市場でより効果的かを調べる.

要約する

ランダムな入場出場戦略は,トレーダーのランダムな意思決定を模倣して,ランダムな取引下での異なる市場のパフォーマンスをテストする.この戦略の原理はシンプルで,他の戦略の効果をテストする基準として使用できます.しかし,その自己の存在は,トレンドを捕捉できないこと,ストップ管理の不完全さなどの問題を抱えています.この戦略を,パラメータの組み合わせを調整し,ストップを追加し,ポジション管理の方法を最適化することによって改善して,実際の価値のある量化取引戦略にすることができます.

ストラテジーソースコード
/*backtest
start: 2022-10-04 00:00:00
end: 2023-10-10 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
args: [["v_input_1",2]]
*/

//@version=4
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © gregoirejohnb
//
// "tHe MaRkEtS aRe RaNdOm", say moron academics.
//
// The purpose of this study is to show that most markets are NOT random! Most markets show a clear bias where we can make such easy money, that a random number generator can do it.
// 
// === HOW THE INDICATOR WORKS ===
// 
// -The study will randomly enter the market
// -The study will randomly exit the market if in a trade
// -You can choose a Long Only, Short Only, or Bidirectional strategy
//
// === DEFAULT VALUES AND THEIR LOGIC ===
// 
// Percent Chance to Enter Per Bar: 10%
// Percent Chance to Exit Per Bar: 1%
// Direction: Long Only
// Commission: 0
//
// Each bar has a 10% chance to enter the market. Each bar has a 1% to exit the market [if in a trade]. It will only enter long.
//
// I included zero commission for simplication. It's a good exercise to include a commission/slippage to see just how much trading fees take from you.
// 
// === TIPS ===
//
// -Increasing "Percent Chance to Exit" will shorten the time in a trade. You can see the "Avg # Bars In Trade" go down as you increase. If "Percent Chance to Exit" is too high, the study won't be in the market long enough to catch any movement, possibly exiting on the same bar most of the time.
// -If you're getting the red screen, that means the strategy lost so much money it went broke. Try reducing the percent equity on the Properties tab.
// -Switch the start year to avoid black swan events like the covid drop in 2020.
// -
// === FINDINGS ===
//
// Most markets lose money with a "Random" direction strategy.
// Most markets lose ALL money with a "Short Only" strategy.
// Most markets make money with a "Long Only" strategy.
// 
// Try this strategy on: Bitcoin (BTCUSD) and the NASDAQ (QQQ).
//
// There are two popular memes right now: "Bitcoin to the moon" and "Stocks only go up". Both are seemingly true. Bitcoin was the best performing asset of the 2010's, gaining several billion percent in gains. The stock market is on a 100 year long uptrend. Why? BECAUSE FIAT CURRENCIES ALWAYS GO DOWN! This is inflation. If we measure the market in terms of others assets instead of fiat, the Long Only strategy doesn't work anymore.
// Try this strategy on: Bitcoin/GLD (BTCUSD/GLD), the Eurodollar (EURUSD), and the S&P 500 measured in gold (SPY/GLD).
// 
// Bitcoin measured in gold (BTCUSD/GLD) still works with a Long Only strategy because Bitcoin increased in value over both USD and gold.
// The Eurodollar (EURUSD) generally loses money no matter what, especially if you add any commission. This makes sense as they are both fiat currencies with similar inflation schedules.
// Gold and the S&P 500 have gained roughly the same amount since ~2000. Some years will show better results for a long strategy, while others will favor a short strategy. Now look at just SPY or GLD (which are both measured in USD by default!) and you'll see the same trend again: a Long Only strategy crushes even when entering and exiting randomly.
//
// === "JUST TELL ME WHAT TO DO, YOU NERD!" ===
//
// Bulls always win and Bears always lose because fiat currencies go to zero.
//
strategy(title="Random Entries Work", shorttitle="REW", overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=100, currency=currency.USD,commission_type=strategy.commission.percent,commission_value=0)

// === GENERAL INPUTS ===
strategy = input(defval="Long Only",title="Direction",options=["Long Only", "Short Only", "Random"])
enter_frequency = input(defval=10,minval=1,maxval=100,title="Percent Chance to Enter")
exit_frequency = input(defval=3, minval=0,maxval=100,title="Percent Chance to Exit",tooltip="This should be much lower than Percent Chance to Enter. Higher values decrease time in market. Lower values increase time in market.")
start_year = input(defval=2020, title="Start Year")


// === LOGIC ===
r = random(0,100)
enter = enter_frequency > r[0]
exit = exit_frequency > r[0]
direction = random(0,100) >= 50

// === STRATEGY - LONG POSITION EXECUTION ===
enterLong() =>
    strategy.opentrades == 0 and enter and (strategy == "Long Only" or (strategy == "Random") and direction) and 
       time > timestamp(start_year, 01, 01, 01, 01)
exitLong() =>
    exit
strategy.entry(id="Long", long=strategy.long, when=enterLong())
strategy.close(id="Long", when=exitLong())
// === STRATEGY - SHORT POSITION EXECUTION ===
enterShort() =>
    strategy.opentrades == 0 and enter and (strategy == "Short Only" or (strategy == "Random" and not direction)) and 
       time > timestamp(start_year, 01, 01, 01, 01)
exitShort() =>
    exit
strategy.entry(id="Short", long=strategy.short, when=enterShort())
strategy.close(id="Short", when=exitShort())