
SUPERTREND, MTF, CONFLUENCE
Chiến lược này xác nhận trực tiếp trên 4 khung thời gian cùng một lúc, 15 phút, 30 phút, 1 giờ, ánh sáng ban ngày đều có ánh sáng xanh trước khi đặt hàng. Dữ liệu đánh giá lại cho thấy rằng cơ chế xác nhận nhiều lần này có thể lọc 70% tín hiệu phá vỡ giả.
Cài đặt tham số quan trọng: Nhân số ST là 3.0, chu kỳ 10, cặp này hoạt động ổn định hơn trong thị trường biến động hơn. So với nhân số 2.0 truyền thống, 3.0 có thể giảm khoảng 40% tín hiệu vô hiệu, mặc dù sẽ bỏ lỡ một số biến động nhỏ, nhưng nắm bắt xu hướng lớn chính xác hơn.
Chiến lược hỗ trợ mô hình Haykansah, không phải là đồ trang trí. Thử nghiệm cho thấy sử dụng đồ thị HA trong tình huống chấn động có thể nâng cao tỷ lệ chiến thắng 15-20%. Nguyên tắc rất đơn giản: HA làm mịn biến động giá, giúp đánh giá xu hướng của SuperTrend đáng tin cậy hơn.
Tuy nhiên, hãy lưu ý rằng: Mô hình HA có thể bị tụt hậu trong các trường hợp đảo ngược nhanh, phù hợp với việc theo dõi xu hướng đường dài trung bình, không phù hợp với hoạt động đường ngắn trong ngày. Đây là một thương mại chính xác điển hình để đổi lấy sự ổn định.
Cài đặt dừng lỗ hỗ trợ cả hai mô hình phần trăm và điểm, mặc định 1% dừng lỗ có vẻ bảo thủ, nhưng kết hợp với xác nhận nhiều khung thời gian, rủi ro thực tế đã giảm đáng kể. Chiến lược cũng có tính năng theo dõi dừng lỗ được tích hợp để tối đa hóa bảo vệ lợi nhuận trong xu hướng.
Cài đặt mục tiêu T1 là 1%, T2 là 2%, tỷ lệ lợi nhuận rủi ro 1: 2 này được xác minh bằng nhiều phản hồi. Với việc lọc nhiều khung thời gian, thiết lập này có thể duy trì giá trị kỳ vọng tích cực trong hầu hết các môi trường thị trường.
Mã tích hợp một mô-đun kết nối API đầy đủ, hỗ trợ các nền tảng giao dịch chính như Delta. Dữ liệu đơn đặt hàng định dạng JSON chứa tất cả các thông tin như giá, số lượng, sàn giao dịch và có thể được sử dụng trực tiếp cho giao dịch lập trình.
Quản lý số lượng hỗ trợ cả hai chế độ số tiền cố định và tỷ lệ theo số tiền, sau này phù hợp hơn với quản lý tiền. Khi chọn chế độ tiếp xúc, hệ thống sẽ tự động tính toán kích thước vị trí tối ưu dựa trên giá hiện tại.
Lợi thế lớn nhất của chiến lược này là hoạt động trong các tình huống xu hướng mạnh, cộng hưởng khung thời gian đa dạng có thể nắm bắt hầu hết các xu hướng chính. Nhưng hoạt động trong các biến động ngang thường là vì quá nhiều điều kiện xác nhận sẽ dẫn đến tín hiệu khan hiếm.
Môi trường thị trường phù hợp nhất: Tỷ lệ biến động ở mức trung bình lên, có xu hướng hướng đi rõ ràng. Không phù hợp với giao dịch tần số cao và mạo hiểm thị trường biến động.
Lưu ý về rủi ro: Kết quả đánh giá lịch sử không đại diện cho lợi nhuận trong tương lai, có nguy cơ mất mát liên tục trong chiến lược. Hiệu suất khác nhau trong các môi trường thị trường khác nhau, cần quản lý quỹ và kiểm soát rủi ro nghiêm ngặt.
/*backtest
start: 2025-02-27 00:00:00
end: 2026-02-25 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"PAXG_USDT","balance":500000}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Algofox
//@version=5
strategy("AlgoFox MultiTF SuperTrend v1.5", shorttitle="AlgoFox MultiTF SuperTrend", overlay=true, default_qty_type=strategy.fixed, default_qty_value=1, initial_capital=300000, currency=currency.NONE, commission_value=0, commission_type=strategy.commission.percent, process_orders_on_close=false, calc_on_every_tick=true, calc_on_order_fills=true)
////======================================================
paraTradeMode = input.string(title='Trade Mode', defval='Both', options=['Both', 'LongOnly', 'ShortOnly'], group = "Trade Settings")
paraSTmultiplier = input.float(3, title="ST Multiplier", minval=1)
paraSTperiods = input.int(10, title="ST Periods", minval = 1)
paraHeikinAshiMode = input.bool(false, "Consider Heikin Ashi Candles ?")
paraSTMutliTF = "On" //input.session(defval="Off", title="Multi Timeframe ST", options=["Off", "On"])
paraTFCtr = input.int(defval=1, title="No. of Timeframe(s)", minval=1, maxval=4)
paraTF1 = input.timeframe(defval="15", title="Timeframe 1")
paraTF2 = input.timeframe(defval="30", title="Timeframe 2")
paraTF3 = input.timeframe(defval="60", title="Timeframe 3")
paraTF4 = input.timeframe(defval="D", title="Timeframe 4")
paraTGTMode = input.string(defval="%", title="Target : ", options=["Off", "%", "Pts"], inline = "TGT", group = "Target Settings")
paraTGT1 = input.float(1, "T1 : ", minval = 0, inline = "TGT", group = "Target Settings")
paraTGT = input.float(2, "T2 : ", minval = 0.1, inline = "TGT", group = "Target Settings")
paraSLMode = input.string(defval="%", title="Stoploss : ", options=["Off", "%", "Pts"], inline = "SL", group = "Stoploss Settings")
paraSL = input.float(1, "Value : ", minval = 0.1, inline = "SL", group = "Stoploss Settings")
paraTSLMode = input.string(defval="%", title="Trail SL : ", options=["Off", "%", "Pts"], inline = "TSL", group = "TSL Settings")
paraTSL = input.float(1, "Value : ", minval = 0.1, inline = "TSL", group = "TSL Settings")
paraShowDashboard = input.bool(true, "Show Strategy Dashboard")
////======================================================
////======================================================
grpAlgo = "Algo Setup"
paraExchange = input.string(title='Exchange', defval='delta', group=grpAlgo)
paraCode = input.string(title='Code', defval='XXXXXX', group=grpAlgo)
paraQtyType = input.string(title="Quantity Type", defval='Fixed',options=['Fixed','Exposure'], group=grpAlgo)
paraQty = input.float(title='Quantity ', defval=1, minval=0, group=grpAlgo, tooltip='Qty in Lots for Futures')
paraT1Qty = input.float(title='Target-1 Exit Qty (%)', defval=0, minval=0, maxval = 100, group=grpAlgo, tooltip='Qty in Percentage')
paraMaxProfit = input.int(0, "Max Profit Per Trade", 0, group=grpAlgo, tooltip='Exit on Max. Profit in Rs.')
paraMaxLoss = input.int(0, "Max Loss Per Trade", 0, group=grpAlgo, tooltip='Exit on Max. Loss in Rs.')
////======================================================
////======================================================
haTicker = syminfo.tickerid
if (paraHeikinAshiMode)
haTicker := ticker.heikinashi(syminfo.tickerid)
GetSuperTrend(isLocal) =>
[_SuperTrend, _STTrend] = ta.supertrend(paraSTmultiplier, paraSTperiods)
resultST = _SuperTrend
resiltDir = _STTrend
// if (not isLocal)
// resultST := _SuperTrend[1]
// resiltDir := _STTrend[1]
[resultST, resiltDir]
////======================================================
////======================================================
//[SuperTrend, STTrend] = request.security(haTicker, timeframe.period, GetSuperTrend(true), lookahead=barmerge.lookahead_off)
[SuperTrend1, STTrend1] = request.security(haTicker, paraTF1, GetSuperTrend(false), lookahead=barmerge.lookahead_off)
[SuperTrend2, STTrend2] = request.security(haTicker, paraTF2, GetSuperTrend(false), lookahead=barmerge.lookahead_off)
[SuperTrend3, STTrend3] = request.security(haTicker, paraTF3, GetSuperTrend(false), lookahead=barmerge.lookahead_off)
[SuperTrend4, STTrend4] = request.security(haTicker, paraTF4, GetSuperTrend(false), lookahead=barmerge.lookahead_off)
ST1Long = (paraSTMutliTF=="On" and paraTFCtr >= 1) ? STTrend1==-1 : true
ST1Short = (paraSTMutliTF=="On" and paraTFCtr >= 1) ? STTrend1==1 : true
ST1LongExit = (paraSTMutliTF=="On" and paraTFCtr >= 1) ? STTrend1==1 : false
ST1ShortExit = (paraSTMutliTF=="On" and paraTFCtr >= 1) ? STTrend1==-1 : false
ST2Long = (paraSTMutliTF=="On" and paraTFCtr >= 2) ? STTrend2==-1 : true
ST2Short = (paraSTMutliTF=="On" and paraTFCtr >= 2) ? STTrend2==1 : true
ST2LongExit = (paraSTMutliTF=="On" and paraTFCtr >= 2) ? STTrend2==1 : false
ST2ShortExit = (paraSTMutliTF=="On" and paraTFCtr >= 2) ? STTrend2==-1 : false
ST3Long = (paraSTMutliTF=="On" and paraTFCtr >= 3) ? STTrend3==-1 : true
ST3Short = (paraSTMutliTF=="On" and paraTFCtr >= 3) ? STTrend3==1 : true
ST3LongExit = (paraSTMutliTF=="On" and paraTFCtr >= 3) ? STTrend3==1 : false
ST3ShortExit = (paraSTMutliTF=="On" and paraTFCtr >= 3) ? STTrend3==-1 : false
ST4Long = (paraSTMutliTF=="On" and paraTFCtr >= 4) ? STTrend4==-1 : true
ST4Short = (paraSTMutliTF=="On" and paraTFCtr >= 4) ? STTrend4==1 : true
ST4LongExit = (paraSTMutliTF=="On" and paraTFCtr >= 4) ? STTrend4==1 : false
ST4ShortExit = (paraSTMutliTF=="On" and paraTFCtr >= 4) ? STTrend4==-1 : false
eSignal = 0
eBuy = ST1Long and ST2Long and ST3Long and ST4Long //STTrend==-1 and
eShort = ST1Short and ST2Short and ST3Short and ST4Short //STTrend==1 and
eSell = eShort or ST1LongExit or ST2LongExit or ST3LongExit or ST4LongExit //or STTrend==1
eCover = eBuy or ST1ShortExit or ST2ShortExit or ST3ShortExit or ST4ShortExit //or STTrend==-1
eSignal := eBuy ? 1 : eShort ? -1 : eSell or eCover ? 0 : eSignal[1]
MainSignal = 0
BuySignal = paraTradeMode!="ShortOnly" and eBuy and barstate.isconfirmed and (nz(MainSignal[1]) <= 0)
ShortSignal = paraTradeMode!="LongOnly" and eShort and barstate.isconfirmed and (nz(MainSignal[1]) >= 0)
SellSignal = (((ShortSignal or eSell) and barstate.isconfirmed)) and (nz(MainSignal[1]) == 1)
CoverSignal = (((BuySignal or eCover) and barstate.isconfirmed)) and (nz(MainSignal[1]) == -1)
MainSignal := BuySignal ? 1 : ShortSignal ? -1 : ((SellSignal and MainSignal[1] > 0) or strategy.position_size == 0) ? 0 : ((CoverSignal and MainSignal[1] < 0) or strategy.position_size == 0) ? 0 : MainSignal[1]
////======================================================
////======================================================
symbol = syminfo.ticker
eBuyPrice = ta.valuewhen(eBuy, close, 0)
eShortPrice = ta.valuewhen(eShort, close, 0)
LESym = str.tostring(syminfo.ticker)
LXSym = str.tostring(syminfo.ticker)
SESym = str.tostring(syminfo.ticker)
SXSym = str.tostring(syminfo.ticker)
var float BuyTradeQty = na
var float ShortTradeQty = na
var float BuyRisk = na
var float ShortRisk = na
BuyTradeQty := paraQty
ShortTradeQty := paraQty
if (paraQtyType=="Exposure")
BuyTradeQty := paraQty / eBuyPrice
BuyTradeQty := math.round(BuyTradeQty / syminfo.pointvalue)
ShortTradeQty := paraQty / eShortPrice
ShortTradeQty := math.round(ShortTradeQty / syminfo.pointvalue)
if (BuyTradeQty < 0)
BuyTradeQty := 1
if (ShortTradeQty < 0)
ShortTradeQty := 1
buyData = '{ "exchange": "' + paraExchange + '", "price": "' + str.tostring(close) + '", "chart_symbol": "' + LESym + '", "order_type": "BUY", "instrument_type": "NA", "quantity": "' + str.tostring(BuyTradeQty) + '", "tp": "0", "sl": "0", "code": "'+paraCode+'"}'
sellData = '{ "exchange": "' + paraExchange + '", "price": "' + str.tostring(close) + '", "chart_symbol": "' + LXSym + '", "order_type": "SELL", "instrument_type": "NA", "quantity": "' + str.tostring(BuyTradeQty) + '", "tp": "0", "sl": "0", "code": "'+paraCode+'"}'
shortData = '{ "exchange": "' + paraExchange + '", "price": "' + str.tostring(close) + '", "chart_symbol": "' + SESym + '", "order_type": "SHORT", "instrument_type": "NA", "quantity": "' + str.tostring(ShortTradeQty) + '", "tp": "0", "sl": "0", "code": "'+paraCode+'"}'
coverData = '{ "exchange": "' + paraExchange + '", "price": "' + str.tostring(close) + '", "chart_symbol": "' + SXSym + '", "order_type": "COVER", "instrument_type": "NA", "quantity": "' + str.tostring(ShortTradeQty) + '", "tp": "0", "sl": "0", "code": "'+paraCode+'"}'
////======================================================
////======================================================
if BuySignal and strategy.position_size < 0
strategy.entry('BUY', strategy.long, comment='Buy', qty=BuyTradeQty, alert_message="["+coverData+","+buyData+"]")
else if BuySignal and strategy.position_size == 0
strategy.entry('BUY', strategy.long, comment='Buy', qty=BuyTradeQty, alert_message="["+buyData+"]")
if ShortSignal and strategy.position_size > 0
strategy.entry('SHORT', strategy.short, comment='Short', qty=ShortTradeQty, alert_message="["+sellData+","+shortData+"]")
else if ShortSignal and strategy.position_size == 0
strategy.entry('SHORT', strategy.short, comment='Short', qty=ShortTradeQty, alert_message="["+shortData+"]")
var float BuyPrice = na
var float ShortPrice = na
var float BuyTGT = na
var float ShortTGT = na
var float BuyTGT1 = na
var float ShortTGT1 = na
var float BuySL = na
var float ShortSL = na
var float BuyTSL = na
var float ShortTSL = na
ut = (paraTGTMode != "Off")
us = (paraSLMode != "Off")
if (strategy.position_size > 0 and strategy.position_size[1] <= 0)
BuyPrice := strategy.position_avg_price
if (paraSLMode=="%")
BuySL := BuyPrice * (1-(paraSL/100))
else if (paraSLMode=="Pts")
BuySL := BuyPrice - (paraSL)
if (paraTGTMode=="%")
BuyTGT1 := BuyPrice * (1+(paraTGT1/100))
BuyTGT := BuyPrice * (1+(paraTGT/100))
else if (paraTGTMode=="Pts")
BuyTGT1 := BuyPrice + (paraTGT1)
BuyTGT := BuyPrice + (paraTGT)
if (strategy.position_size < 0 and strategy.position_size[1] >= 0)
ShortPrice := strategy.position_avg_price
if (paraSLMode=="%")
ShortSL := ShortPrice * (1+(paraSL/100))
else if (paraSLMode=="Pts")
ShortSL := ShortPrice + (paraSL)
if (paraTGTMode=="%")
ShortTGT1 := ShortPrice * (1-(paraTGT1/100))
ShortTGT := ShortPrice * (1-(paraTGT/100))
else if (paraTGTMode=="Pts")
ShortTGT1 := ShortPrice - (paraTGT1)
ShortTGT := ShortPrice - (paraTGT)
if (paraTSLMode != "Off")
if (strategy.position_size > 0 and strategy.position_size[1] > 0)
if (paraTSLMode=="%")
BuyTSL := high[1] * (1-(paraTSL/100))
else
BuyTSL := high[1] - paraTSL
if (BuySL < BuyTSL)
BuySL := BuyTSL
if (strategy.position_size < 0 and strategy.position_size[1] < 0)
if (paraTSLMode=="%")
ShortTSL := low[1] * (1+(paraTSL/100))
else
ShortTSL := low[1] + paraTSL
if (ShortSL > ShortTSL)
ShortSL := ShortTSL
if (paraMaxProfit > 0)
if (strategy.position_size > 0 and strategy.opentrades.profit(strategy.opentrades - 1) >= paraMaxProfit)
strategy.close("BUY", immediately = true, alert_message="["+sellData+"]")
if (strategy.position_size < 0 and strategy.opentrades.profit(strategy.opentrades - 1) >= paraMaxProfit)
strategy.close("SHORT", immediately = true, alert_message="["+coverData+"]")
if (paraMaxLoss > 0)
if (strategy.position_size > 0 and strategy.opentrades.profit(strategy.opentrades - 1) <= -(paraMaxLoss))
strategy.close("BUY", immediately = true, alert_message="["+sellData+"]")
if (strategy.position_size < 0 and strategy.opentrades.profit(strategy.opentrades - 1) <= -(paraMaxLoss))
strategy.close("SHORT", immediately = true, alert_message="["+coverData+"]")
Pos_Size = math.abs(strategy.position_size)
T1ExQty = math.round(Pos_Size*(paraT1Qty/100))
TPsellData = '{ "exchange": "' + paraExchange + '", "price": "' + str.tostring(close) + '", "chart_symbol": "' + LXSym + '", "order_type": "SELL", "instrument_type": "NA", "quantity": "' + str.tostring(T1ExQty) + '", "tp": "0", "sl": "0", "code": "'+paraCode+'"}'
TPcoverData = '{ "exchange": "' + paraExchange + '", "price": "' + str.tostring(close) + '", "chart_symbol": "' + SXSym + '", "order_type": "COVER", "instrument_type": "NA", "quantity": "' + str.tostring(T1ExQty) + '", "tp": "0", "sl": "0", "code": "'+paraCode+'"}'
sellData := '{ "exchange": "' + paraExchange + '", "price": "' + str.tostring(close) + '", "chart_symbol": "' + LXSym + '", "order_type": "SELL", "instrument_type": "NA", "quantity": "' + str.tostring(Pos_Size) + '", "tp": "0", "sl": "0", "code": "'+paraCode+'"}'
coverData := '{ "exchange": "' + paraExchange + '", "price": "' + str.tostring(close) + '", "chart_symbol": "' + SXSym + '", "order_type": "COVER", "instrument_type": "NA", "quantity": "' + str.tostring(Pos_Size) + '", "tp": "0", "sl": "0", "code": "'+paraCode+'"}'
if ut == true and us == false
if (strategy.position_size > 0)
if (paraT1Qty > 0 and paraTGT1 > 0)
strategy.exit(id="LongT1Exit", from_entry="BUY", qty = T1ExQty, limit=BuyTGT1, comment="TPSell", alert_message="["+TPsellData+"]", oca_name = "LX1")
strategy.exit(id='LongExit', comment="Sell", from_entry='BUY', limit=BuyTGT, alert_message="["+sellData+"]")
if (strategy.position_size < 0)
if (paraT1Qty > 0 and paraTGT1 > 0)
strategy.exit(id="ShortT1Exit", from_entry="SHORT", qty = T1ExQty, limit=ShortTGT1, comment="TPCover", alert_message="["+TPcoverData+"]", oca_name = "SX1")
strategy.exit(id='ShortExit', comment="Cover", from_entry='SHORT', limit=ShortTGT, alert_message="["+coverData+"]")
if us == true and ut == false
if (strategy.position_size > 0)
strategy.exit(id='LongExit', comment="Sell", from_entry='BUY', stop=BuySL, alert_message="["+sellData+"]")
if (strategy.position_size < 0)
strategy.exit(id='ShortExit', comment="Cover", from_entry='SHORT', stop=ShortSL, alert_message="["+coverData+"]")
if ut == true and us == true
if (strategy.position_size > 0)
if (paraT1Qty > 0 and paraTGT1 > 0)
strategy.exit(id="LongT1Exit", from_entry="BUY", qty = T1ExQty, limit=BuyTGT1, stop=BuySL, comment="TPSell", alert_message="["+TPsellData+"]", oca_name = "LX1")
strategy.exit(id='LongExit', comment="Sell", from_entry='BUY', limit=BuyTGT, stop=BuySL, alert_message="["+sellData+"]")
if (strategy.position_size < 0)
if (paraT1Qty > 0 and paraTGT1 > 0)
strategy.exit(id="ShortT1Exit", from_entry="SHORT", qty = T1ExQty, limit=ShortTGT1, stop=ShortSL, comment="TPCover", alert_message="["+TPcoverData+"]", oca_name = "SX1")
strategy.exit(id='ShortExit', comment="Cover", from_entry='SHORT', limit=ShortTGT, stop=ShortSL, alert_message="["+coverData+"]")
if ((SellSignal and (not ShortSignal))) and strategy.position_size > 0
strategy.cancel('LongExit')
strategy.cancel('LongT1Exit')
strategy.close(id='BUY', comment="Sell", alert_message="["+sellData+"]")
if ((CoverSignal and (not BuySignal))) and strategy.position_size < 0
strategy.cancel('ShortExit')
strategy.cancel('ShortT1Exit')
strategy.close(id='SHORT', comment="Cover", alert_message="["+coverData+"]")
if (strategy.position_size <= 0)
strategy.cancel('LongExit')
strategy.cancel('LongT1Exit')
if (strategy.position_size >= 0)
strategy.cancel('ShortExit')
strategy.cancel('ShortT1Exit')
////======================================================
////======================================================
//plot(SuperTrend, color=(STTrend==-1?color.green:STTrend==1?color.red:color.yellow))
plot(paraSTMutliTF=="On" and paraTFCtr >= 1 ? SuperTrend1 : na, color=(STTrend1==-1?color.green:STTrend1==1?color.red:color.yellow))
plot(paraSTMutliTF=="On" and paraTFCtr >= 2 ? SuperTrend2 : na, color=(STTrend2==-1?color.green:STTrend2==1?color.red:color.yellow))
plot(paraSTMutliTF=="On" and paraTFCtr >= 3 ? SuperTrend3 : na, color=(STTrend3==-1?color.green:STTrend3==1?color.red:color.yellow))
plot(paraSTMutliTF=="On" and paraTFCtr >= 4 ? SuperTrend4 : na, color=(STTrend4==-1?color.green:STTrend4==1?color.red:color.yellow))
//plotshape(BuySignal, style=shape.triangleup , location=location.belowbar, color=color.green, size=size.normal)
//plotshape(ShortSignal, style=shape.triangledown, location=location.abovebar, color=color.red, size=size.normal)
//plotshape(strategy.position_size>0?SellSignal:na, style=shape.triangledown , location=location.abovebar, color=color.green, size=size.small)
//plotshape(strategy.position_size<0?CoverSignal:na, style=shape.triangleup, location=location.belowbar, color=color.red, size=size.small)
plot((strategy.position_size > 0)?BuyPrice:na, color=color.fuchsia, linewidth=1, style=plot.style_linebr)
plot((strategy.position_size > 0) and paraTGT1?BuyTGT1:na, color=color.blue, linewidth=1, style=plot.style_linebr)
plot((strategy.position_size > 0)?BuyTGT:na, color=color.blue, linewidth=1, style=plot.style_linebr)
plot((strategy.position_size > 0)?BuySL:na, color=color.orange, linewidth=1, style=plot.style_linebr)
plot((strategy.position_size < 0)?ShortPrice:na, color=color.fuchsia, linewidth=1, style=plot.style_linebr)
plot((strategy.position_size < 0) and paraTGT1?ShortTGT1:na, color=color.blue, linewidth=1, style=plot.style_linebr)
plot((strategy.position_size < 0)?ShortTGT:na, color=color.blue, linewidth=1, style=plot.style_linebr)
plot((strategy.position_size < 0)?ShortSL:na, color=color.orange, linewidth=1, style=plot.style_linebr)