Chiến lược vị thế qua đêm theo xu hướng dài và ngắn trên thị trường chéo dựa trên chỉ báo EMA

EMA MA
Ngày tạo: 2024-11-12 10:49:00 sửa đổi lần cuối: 2024-11-12 10:49:00
sao chép: 2 Số nhấp chuột: 483
1
tập trung vào
1617
Người theo dõi

Chiến lược vị thế qua đêm theo xu hướng dài và ngắn trên thị trường chéo dựa trên chỉ báo EMA

Chiến lược này là một chiến lược giữ vị trí qua đêm trên thị trường dựa trên các chỉ số kỹ thuật EMA nhằm nắm bắt các cơ hội giao dịch trước và sau khi thị trường đóng cửa. Chiến lược này thực hiện giao dịch thông minh trong các môi trường thị trường khác nhau thông qua kiểm soát thời gian chính xác và lọc các chỉ số kỹ thuật.

Tổng quan về chiến lược

Chiến lược này chủ yếu thu được lợi nhuận bằng cách nhập vào một thời gian nhất định trước khi thị trường đóng cửa và thoát ra một thời gian nhất định sau khi mở cửa vào ngày hôm sau. Kết hợp với chỉ số EMA để xác nhận xu hướng, tìm kiếm cơ hội giao dịch trên nhiều thị trường toàn cầu. Chiến lược cũng tích hợp chức năng giao dịch tự động, thực hiện hoạt động không người giám sát.

Nguyên tắc chiến lược

  1. Kiểm soát thời gian: tùy thuộc vào thời gian giao dịch của các thị trường khác nhau, nhập vào thời gian cố định trước khi đóng cửa, xuất ra thời gian cố định sau khi mở cửa
  2. Bộ lọc EMA: sử dụng các chỉ số EMA tùy chọn để xác minh tín hiệu nhập cảnh
  3. Lựa chọn thị trường: hỗ trợ thời gian giao dịch tự điều chỉnh cho 3 thị trường lớn ở Mỹ, châu Á và châu Âu
  4. Bảo vệ cuối tuần: Cần phải giảm bớt vị thế trước khi đóng cửa vào thứ Sáu để tránh rủi ro cuối tuần

Lợi thế chiến lược

  1. Khả năng thích ứng đa thị trường: có thể điều chỉnh thời gian giao dịch linh hoạt theo các đặc điểm thị trường khác nhau
  2. Kiểm soát rủi ro tốt: bao gồm cơ chế bảo vệ vị thế cuối tuần
  3. Mức độ tự động hóa cao: hỗ trợ kết nối giao diện giao dịch tự động
  4. Các tham số linh hoạt: Thời gian giao dịch và tham số chỉ số kỹ thuật có thể được tùy chỉnh
  5. Chi phí giao dịch: bao gồm phí và thiết lập điểm trượt

Rủi ro chiến lược

  1. Rủi ro biến động thị trường: Giữ vị trí qua đêm có thể gây rủi ro
  2. Tùy thuộc thời gian: hiệu quả chiến lược bị ảnh hưởng bởi lựa chọn thời gian của thị trường
  3. Hạn chế về chỉ số kỹ thuật: chỉ số EMA đơn lẻ có thể bị tụt hậu Đề xuất: Thiết lập giới hạn dừng lỗ, thêm nhiều chứng thực chỉ số kỹ thuật

Hướng tối ưu hóa chiến lược

  1. Thêm một loạt các chỉ số kỹ thuật
  2. Tham gia hệ thống lọc tỷ lệ dao động
  3. Tối ưu hóa lựa chọn thời gian ra sân
  4. Thêm chức năng điều chỉnh tham số thích ứng
  5. Tăng cường mô-đun kiểm soát rủi ro

Tóm tắt

Chiến lược này thực hiện một hệ thống giao dịch qua đêm đáng tin cậy thông qua kiểm soát thời gian chính xác và lọc các chỉ số kỹ thuật. Thiết kế chiến lược đã xem xét đầy đủ các nhu cầu chiến đấu thực tế, bao gồm các yếu tố như thích ứng đa thị trường, kiểm soát rủi ro và tự động hóa giao dịch, có giá trị thực tế mạnh mẽ. Bằng cách tối ưu hóa và hoàn thiện liên tục, chiến lược này có thể đạt được lợi nhuận ổn định trong giao dịch thực tế.

Mã nguồn chiến lược
/*backtest
start: 2019-12-23 08:00:00
end: 2024-11-11 00:00:00
period: 1d
basePeriod: 1d
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/
// © PresentTrading

// This strategy, titled "Overnight Market Entry Strategy with EMA Filter," is designed for entering long positions shortly before 
// the market closes and exiting shortly after the market opens. The strategy allows for selecting between different global market sessions (US, Asia, Europe) and 
// uses an optional EMA (Exponential Moving Average) filter to validate entry signals. The core logic is to enter trades based on conditions set for a specified period before 
// the market close and to exit trades either after a specified period following the market open or just before the weekend close. 
// Additionally, 3commas bot integration is included to automate the execution of trades. The strategy dynamically adjusts to market open and close times, ensuring trades are properly timed based on the selected market. 
// It also includes a force-close mechanism on Fridays to prevent holding positions over the weekend.

//@version=5
strategy("Overnight Positioning with EMA Confirmation - Strategy [presentTrading]", overlay=true, precision=3, commission_value=0.02, commission_type=strategy.commission.percent, slippage=1, currency=currency.USD, default_qty_type=strategy.percent_of_equity, default_qty_value=10, initial_capital=10000)

// Input parameters
entryMinutesBeforeClose = input.int(20, title="Minutes Before Close to Enter", minval=1)
exitMinutesAfterOpen = input.int(20, title="Minutes After Open to Exit", minval=1)
emaLength = input.int(100, title="EMA Length", minval=1)
emaTimeframe = input.timeframe("240", title="EMA Timeframe")
useEMA = input.bool(true, title="Use EMA Filter")

// Market Selection Input
marketSelection = input.string("US", title="Select Market", options=["US", "Asia", "Europe"])

// Timezone for each market
marketTimezone = marketSelection == "US" ? "America/New_York" :
                 marketSelection == "Asia" ? "Asia/Tokyo" :
                 "Europe/London"  // Default to London for Europe

// Market Open and Close Times for each market
var int marketOpenHour = na
var int marketOpenMinute = na
var int marketCloseHour = na
var int marketCloseMinute = na

if marketSelection == "US"
    marketOpenHour := 9
    marketOpenMinute := 30
    marketCloseHour := 16
    marketCloseMinute := 0
else if marketSelection == "Asia"
    marketOpenHour := 9
    marketOpenMinute := 0
    marketCloseHour := 15
    marketCloseMinute := 0
else if marketSelection == "Europe"
    marketOpenHour := 8
    marketOpenMinute := 0
    marketCloseHour := 16
    marketCloseMinute := 30

// 3commas Bot Settings
emailToken = input.string('', title='Email Token', group='3commas Bot Settings')
long_bot_id = input.string('', title='Long Bot ID', group='3commas Bot Settings')
usePairAdjust = input.bool(false, title='Use this pair in PERP', group='3commas Bot Settings')
selectedExchange = input.string("Binance", title="Select Exchange", group='3commas Bot Settings', options=["Binance", "OKX", "Gate.io", "Bitget"])

// Determine the trading pair based on settings
var pairString = ""
if usePairAdjust
    pairString := str.tostring(syminfo.currency) + "_" + str.tostring(syminfo.basecurrency) + (selectedExchange == "OKX" ? "-SWAP" : "") 
else
    pairString := str.tostring(syminfo.currency) + "_" + str.tostring(syminfo.basecurrency)

// Function to check if it's a trading day (excluding weekends)
isTradingDay(t) =>
    dayOfWeek = dayofweek(t, marketTimezone)
    dayOfWeek >= dayofweek.monday and dayOfWeek <= dayofweek.friday

// Function to get the timestamp for market open and close times
getMarketTimes(t) =>
    y = year(t, marketTimezone)
    m = month(t, marketTimezone)
    d = dayofmonth(t, marketTimezone)
    marketOpenTime = timestamp(marketTimezone, y, m, d, marketOpenHour, marketOpenMinute, 0)
    marketCloseTime = timestamp(marketTimezone, y, m, d, marketCloseHour, marketCloseMinute, 0)
    [marketOpenTime, marketCloseTime]

// Get the current time in the market's timezone
currentTime = time

// Calculate market times
[marketOpenTime, marketCloseTime] = getMarketTimes(currentTime)

// Calculate entry and exit times
entryTime = marketCloseTime - entryMinutesBeforeClose * 60 * 1000
exitTime = marketOpenTime + exitMinutesAfterOpen * 60 * 1000

// Get EMA data from the specified timeframe
emaValue = request.security(syminfo.tickerid, emaTimeframe, ta.ema(close, emaLength))

// Entry condition with optional EMA filter
longCondition = close > emaValue or not useEMA

// Functions to create JSON strings
getEnterJson() =>
    '{"message_type": "bot", "bot_id": "' + long_bot_id + '", "email_token": "' + emailToken + '", "delay_seconds": 0, "pair": "' + pairString + '"}'

getExitJson() =>
    '{"action": "close_at_market_price", "message_type": "bot", "bot_id": "' + long_bot_id + '", "email_token": "' + emailToken + '", "delay_seconds": 0, "pair": "' + pairString + '"}'

// Entry Signal
entrySignal = isTradingDay(currentTime) and currentTime >= entryTime and currentTime < marketCloseTime and dayofweek(currentTime, marketTimezone) != dayofweek.friday

// Exit Signal
exitSignal = isTradingDay(currentTime) and currentTime >= exitTime and currentTime < marketCloseTime

// Entry Logic
if strategy.position_size == 0 and longCondition
    strategy.entry("Long", strategy.long, alert_message=getEnterJson())

// Exit Logic
if  strategy.position_size > 0
    strategy.close("Long", alert_message=getExitJson())

// Force Close Logic on Friday before market close
isFriday = dayofweek(currentTime, marketTimezone) == dayofweek.friday
if  strategy.position_size > 0  // Close 5 minutes before market close on Friday
    strategy.close("Long", comment="Force close on Friday before market close", alert_message=getExitJson())

// Plotting entry and exit points
plotshape( strategy.position_size == 0 and longCondition, title="Entry", text="Entry", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape( strategy.position_size > 0, title="Exit", text="Exit", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)

// Plot EMA for reference
plot(useEMA ? emaValue : na, title="EMA", color=color.blue)