무작위 진입 및 종료 전략


생성 날짜: 2023-10-11 15:17:28 마지막으로 수정됨: 2023-10-11 15:17:28
복사: 0 클릭수: 966
avatar of ChaoZhang ChaoZhang
1
집중하다
1628
수행원

개요

무작위 입출장 전략은 거래 과정에서 무작위로 입출장 시간을 결정하여 입출장하는 전략이다. 이 전략은 무작위 수 생성기를 사용하여 입출장 결정을 모방한다.

전략 원칙

이 전략의 핵심 논리는 다음과 같습니다.

  1. 각 K줄은 0에서 100 사이의 숫자를 무작위로 생성한다.

  2. 만약 무작위 숫자가 설정된 출입 확률의 임계값보다 작다면, 입장을 개시한다. 기본 출입 확률은 10%이다.

  3. 만약 무작위 숫자가 설정된 출전 확률의 약값보다 작다면, 평점 출전한다. 기본 출전 확률은 3%이다.

  4. 3가지 방향에서 선택할 수 있습니다. 그냥 더 많이, 그냥 빈, 무작위 방향. 은 그냥 더 많이.

  5. 거래가 시작되는 해를 설정할 수도 있고, 특히 거래가 급격하게 변하는 해를 피할 수도 있습니다.

다른 입문 확률, 출구 확률 및 방향 파라미터를 설정하여, 다른 유형의 거래자의 무작위 거래 행동을 모의할 수 있으며, 무작위 거래 하에서 다른 시장의 성과를 살펴볼 수 있다.

우위 분석

  • 실제 거래자의 무작위적 의사 결정을 모방하여 실제 시장 상황에 가깝습니다.

  • 다양한 시장의 성과 차이를 무작위 거래에서 테스트 할 수 있습니다.

  • 어떤 시장에서 우연한 거래가 긍정적 인 수익을 창출 할 수 있는지 알아보십시오.

  • 무작위 거래는 다른 전략의 장점을 테스트하는 기준 전략으로 사용될 수 있습니다.

위험 분석

  • 시장의 추세에서 이익을 얻을 수 없고, 진입 시기를 결정할 수 없습니다.

  • 무작위 출전이 불리한 위치에서 중단될 수 있다.

  • 명확한 방향이 있는 시장에서는 좋지 않은 성적을 거뒀습니다.

  • 출전 확률을 최적화하여 너무 자주 출전하지 않도록 한다.

  • 손실을 막기 위한 제도를 고려할 수 있다.

최적화 방향

  • 입출장 확률을 조정하여 다른 시장에 적합한 조합을 찾습니다.

  • 단독 손실을 통제하기 위해 Stop Loss Strategies에 가입하십시오.

  • 포지션 관리를 최적화하고 단편 리스크를 줄입니다.

  • 트렌드가 명확할 때 트렌드 추적 전략으로 변경할 수 있습니다.

  • 통계 분석과 결합하여 어떤 시장이 더 나은 무작위 거래 효과를 볼 수 있는지 찾아보십시오.

요약하다

무작위 입출장 전략은 거래자의 무작위 결정을 모방하여 무작위 거래 하에서 다른 시장의 성능을 테스트합니다. 이 전략의 원칙은 간단하며 다른 전략의 효과를 검사하는 기준으로 사용할 수 있습니다. 그러나 그 자체는 추세를 잡지 못하고, 스톱 관리 불완전한 문제와 같은 문제를 가지고 있습니다. 우리는 파라미터 포지션을 조정하고, 스톱을 추가하고, 포지션 관리 방식을 최적화하여 이 전략을 개선하여 실제 가치있는 정량 거래 전략으로 만들 수 있습니다.

전략 소스 코드
/*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())