
Bài viết này giới thiệu về một chiến lược giao dịch định lượng dựa trên nguyên tắc chéo trung bình di chuyển. Chiến lược này được viết bằng mã Pine Script, tích hợp với API của nền tảng giao dịch Dhan, có thể thực hiện giao dịch tự động của tín hiệu chiến lược.
Cốt lõi của chiến lược này là đường trung bình di chuyển, dựa trên tính toán trung bình di chuyển đơn giản của giá đóng cửa trong một chu kỳ nhất định để đánh giá xu hướng. Khi giá vượt qua đường trung bình, nó tạo ra tín hiệu nhiều và khi đi xuống, nó tạo ra tín hiệu trống. Đồng thời, sử dụng chức năng extrem để lọc các tín hiệu lặp đi lặp lại liên tục, cải thiện chất lượng tín hiệu.
Moving Average Crossover là một phương pháp theo dõi xu hướng đơn giản, dễ sử dụng, có thể nắm bắt hiệu quả xu hướng trung và dài hạn của thị trường. Bằng cách đặt các tham số hợp lý, chiến lược có thể thu được lợi nhuận ổn định trong tình huống xu hướng. Cài đặt dừng lỗ có lợi cho việc kiểm soát rút lui, tăng tỷ lệ lợi nhuận rủi ro.
Đường trung bình di chuyển về bản chất là một chỉ số chậm trễ, khi thị trường biến đổi, tín hiệu có thể bị trì hoãn, dẫn đến thời gian giao dịch không tốt nhất hoặc tạo tín hiệu giả. Thiết lập tham số không đúng có thể ảnh hưởng đến hiệu suất chiến lược và cần được tối ưu hóa theo các đặc điểm và chu kỳ thị trường khác nhau.
Chiến lược chéo đường trung bình di chuyển là một chiến lược giao dịch định lượng đơn giản và thực tế, có thể kiếm lợi nhuận trong tình huống xu hướng thông qua theo dõi xu hướng và kiểm soát dừng lỗ. Tuy nhiên, chiến lược tự nó có một số hạn chế, cần phải được tối ưu hóa và cải thiện theo đặc điểm thị trường và sở thích rủi ro. Trong ứng dụng thực tế, cũng cần chú ý đến việc thực hiện kỷ luật nghiêm ngặt và kiểm soát rủi ro.
/*backtest
start: 2024-05-01 00:00:00
end: 2024-05-31 23:59:59
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © syam-mohan-vs @ T7 - wwww.t7wealth.com www.t7trade.com
//This is an educational code done to describe the fundemantals of pine scritpting language and integration with Indian discount broker Dhan. This strategy is not tested or recommended for live trading.
//@version=5
strategy("Pine & Dhan - Moving Average Crossover Strategy", overlay=true)
//Remove excess signals
exrem(condition1, condition2) =>
temp = false
temp := na(temp[1]) ? false : not temp[1] and condition1 ? true : temp[1] and condition2 ? false : temp[1]
ta.change(temp) == true ? true : false
// Define MA period
ma_period = input(20, title = "MA Length")
// Define target and stop loss levels
target_percentage = input.float(title="Target Profit (%)", defval=2.0)
stop_loss_percentage = input.float(title="Stop Loss (%)", defval=1.0)
// Calculate the MA
ma = ta.sma(close, ma_period)
// Entry conditions
long_entry = close >= ma
short_entry = close < ma
// Calculate target and stop loss prices
target_price = long_entry ? strategy.position_avg_price + (close * (target_percentage / 100)) : strategy.position_avg_price - (close * (target_percentage / 100))
stop_loss_price = short_entry ? strategy.position_avg_price + (close * (stop_loss_percentage/ 100)) : strategy.position_avg_price - (close * (stop_loss_percentage / 100))
long_entry := exrem(long_entry,short_entry)
short_entry := exrem(short_entry,long_entry)
// Plot the MA
plot(ma, color=color.blue, linewidth=2, title="MA")
// Plot the entry and exit signals
plotshape(long_entry, style=shape.arrowup, color=color.green, size=size.small,location = location.belowbar)
plotshape(short_entry, style=shape.arrowdown, color=color.red, size=size.small,location = location.abovebar)
//Find absolute value of positon size to exit position properly
size = math.abs(strategy.position_size)
//Replace these four JSON strings with those generated from user Dhan account
long_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"B","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY1!","instrument":"FUT","productType":"I","sort_order":"1","price":"0"}]}'
long_exit_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"S","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY1!","instrument":"FUT","productType":"M","sort_order":"1","price":"0"}]}'
short_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"S","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY1!","instrument":"FUT","productType":"M","sort_order":"1","price":"0"}]}'
short_exit_msg = '{"secret":"C0B2u","alertType":"multi_leg_order","order_legs":[{"transactionType":"B","orderType":"MKT","quantity":"1","exchange":"NSE","symbol":"NIFTY1!","instrument":"FUT","productType":"M","sort_order":"1","price":"0"}]}'
// Submit orders based on signals
if(strategy.position_size == 0)
if long_entry
strategy.order("Long", strategy.long,alert_message=long_msg)
if short_entry
strategy.order("Short", strategy.short,alert_message=short_msg)
if(strategy.position_size > 0)
if(short_entry)
strategy.order("Short", strategy.short, qty = size, alert_message=short_msg)
else
strategy.exit("Long Exit", from_entry="Long", qty = size, stop=stop_loss_price, limit= target_price, alert_message=long_exit_msg)
if(strategy.position_size < 0)
if(long_entry)
strategy.order("Long", strategy.long, qty = size, alert_message=long_msg)
else
strategy.exit("Short Exit", from_entry="Short", qty = size, stop=stop_loss_price, limit= target_price, alert_message=short_exit_msg)