Chiến lược nhập cảnh và xuất cảnh ngẫu nhiên

Tác giả:ChaoZhang, Ngày: 2023-10-11 15:17:28
Tags:

Tổng quan

Chiến lược nhập và thoát ngẫu nhiên là một chiến lược quyết định ngẫu nhiên thời gian nhập và thoát trong quá trình giao dịch. Nó sử dụng máy phát số ngẫu nhiên để mô phỏng các quyết định nhập và thoát.

Chiến lược logic

Logic cốt lõi của chiến lược này là:

  1. Mỗi ngọn nến sẽ tạo ra một số lượng ngẫu nhiên từ 0 đến 100.

  2. Nếu số ngẫu nhiên thấp hơn ngưỡng nhập khẩu được thiết lập, một vị trí sẽ được mở. xác suất nhập khẩu mặc định là 10%.

  3. Nếu số ngẫu nhiên thấp hơn ngưỡng thoát đặt, vị trí sẽ được đóng. xác suất thoát mặc định là 3%.

  4. Có ba lựa chọn hướng: chỉ dài, chỉ ngắn hoặc chỉ hướng ngẫu nhiên.

  5. Năm bắt đầu cũng có thể được thiết lập để tránh những năm có biến động thị trường lớn.

Bằng cách thiết lập các kết hợp khác nhau của xác suất vào, xác suất ra và hướng, chúng ta có thể mô phỏng hành vi giao dịch ngẫu nhiên của các loại thương nhân khác nhau và kiểm tra hiệu suất giao dịch ngẫu nhiên trên các thị trường khác nhau.

Phân tích lợi thế

  • Mô phỏng việc ra quyết định ngẫu nhiên của thương nhân thực tế, gần với các tình huống thị trường thực tế.

  • Có thể kiểm tra sự khác biệt về hiệu suất của giao dịch ngẫu nhiên trên các thị trường khác nhau.

  • Có thể tìm ra thị trường nào có thể tạo ra lợi nhuận tích cực ngay cả với giao dịch ngẫu nhiên.

  • Có thể sử dụng giao dịch ngẫu nhiên như một chiến lược chuẩn để kiểm tra lợi thế của các chiến lược khác.

Phân tích rủi ro

  • Không thể lợi nhuận từ xu hướng thị trường, không thể xác định thời gian nhập khẩu tối ưu.

  • Việc thoát ngẫu nhiên có thể ngăn chặn lỗ ở mức không thuận lợi.

  • Hiệu suất kém ở các thị trường có thiên hướng hướng rõ ràng.

  • Cần tối ưu hóa xác suất nhập / ra để tránh giao dịch quá mức hoặc thời gian giữ không đủ.

  • Xem xét thêm stop loss để tránh các lỗ mở rộng.

Hướng dẫn tối ưu hóa

  • Điều chỉnh xác suất nhập / ra để tìm kết hợp phù hợp cho các thị trường khác nhau.

  • Thêm các chiến lược dừng lỗ để kiểm soát lỗ giao dịch duy nhất.

  • Tối ưu hóa kích thước vị trí để giảm rủi ro giao dịch duy nhất.

  • Chuyển sang xu hướng theo các chiến lược khi xu hướng rõ ràng.

  • Sử dụng phân tích thống kê để tìm ra thị trường nào thích giao dịch ngẫu nhiên.

Tóm lại

Chiến lược nhập và thoát ngẫu nhiên kiểm tra hiệu suất của các thị trường khác nhau dưới các quyết định thương nhân ngẫu nhiên mô phỏng. Logic chiến lược đơn giản và có thể phục vụ như một điểm chuẩn để kiểm tra các chiến lược khác. Tuy nhiên, nó có những nhược điểm như không nắm bắt xu hướng và thiếu quản lý stop loss thích hợp. Chúng ta có thể cải thiện chiến lược bằng cách điều chỉnh sự kết hợp các tham số, thêm các điểm dừng, tối ưu hóa kích thước vị trí vv, để biến nó thành một chiến lược giao dịch lượng tử khả thi.


/*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())

Thêm nữa