
Chiến lược này là một hệ thống theo dõi xu hướng kết hợp hai đường trung bình di chuyển và MACD. Nó sử dụng đường trung bình di chuyển 50 và 200 để xác định hướng xu hướng, đồng thời sử dụng MACD để nắm bắt thời điểm nhập cảnh cụ thể. Chiến lược này sử dụng cơ chế dừng lỗ động và nâng cao chất lượng giao dịch bằng cách lọc nhiều điều kiện.
Lập luận cốt lõi của chiến lược được xây dựng dựa trên các yếu tố quan trọng sau:
Đây là một hệ thống giao dịch theo dõi xu hướng được thiết kế hợp lý, logic hoàn chỉnh. Bằng cách kết hợp các chỉ số kỹ thuật cổ điển và phương pháp quản lý rủi ro hiện đại, chiến lược này tập trung vào kiểm soát rủi ro trong khi nắm bắt xu hướng. Mặc dù có một số nơi cần tối ưu hóa, nhưng nói chung là một chiến lược giao dịch có giá trị thực tế.
/*backtest
start: 2024-11-12 00:00:00
end: 2024-12-11 08:00:00
period: 1h
basePeriod: 1h
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/
// © WolfofAlgo
//@version=5
strategy("Trend Following Scalping Strategy", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=200)
// Input Parameters
stopLossPips = input.float(5.0, "Stop Loss in Pips", minval=1.0)
takeProfitPips = input.float(10.0, "Take Profit in Pips", minval=1.0)
useFixedTakeProfit = input.bool(true, "Use Fixed Take Profit")
// Moving Average Parameters
fastMA = input.int(50, "Fast MA Period")
slowMA = input.int(200, "Slow MA Period")
// MACD Parameters
macdFastLength = input.int(12, "MACD Fast Length")
macdSlowLength = input.int(26, "MACD Slow Length")
macdSignalLength = input.int(9, "MACD Signal Length")
// Trade Filter Parameters (Adjusted to be less strict)
minBarsBetweenTrades = input.int(5, "Minimum Bars Between Trades", minval=1)
trendStrengthPeriod = input.int(10, "Trend Strength Period")
minTrendStrength = input.float(0.4, "Minimum Trend Strength", minval=0.1, maxval=1.0)
macdThreshold = input.float(0.00005, "MACD Threshold", minval=0.0)
// Variables for trade management
var int barsLastTrade = 0
barsLastTrade := nz(barsLastTrade[1]) + 1
// Calculate Moving Averages
ma50 = ta.sma(close, fastMA)
ma200 = ta.sma(close, slowMA)
// Calculate MACD
[macdLine, signalLine, _] = ta.macd(close, macdFastLength, macdSlowLength, macdSignalLength)
// Calculate trend strength (simplified)
trendDirection = ta.ema(close, trendStrengthPeriod) > ta.ema(close, trendStrengthPeriod * 2)
isUptrend = close > ma50 and ma50 > ma200
isDowntrend = close < ma50 and ma50 < ma200
// Calculate pip value
pointsPerPip = syminfo.mintick * 10
// Entry Conditions with Less Strict Filters
macdCrossUp = ta.crossover(macdLine, signalLine) and math.abs(macdLine - signalLine) > macdThreshold
macdCrossDown = ta.crossunder(macdLine, signalLine) and math.abs(macdLine - signalLine) > macdThreshold
// Long and Short Conditions
longCondition = close > ma50 and macdCrossUp and barsLastTrade >= minBarsBetweenTrades and isUptrend
shortCondition = close < ma50 and macdCrossDown and barsLastTrade >= minBarsBetweenTrades and isDowntrend
// Exit Conditions (made more lenient)
exitLongCondition = macdCrossDown or close < ma50
exitShortCondition = macdCrossUp or close > ma50
// Reset bars counter on new trade
if (longCondition or shortCondition)
barsLastTrade := 0
// Calculate stop loss and take profit levels
longStopPrice = strategy.position_avg_price - (stopLossPips * pointsPerPip)
longTakeProfitPrice = strategy.position_avg_price + (takeProfitPips * pointsPerPip)
shortStopPrice = strategy.position_avg_price + (stopLossPips * pointsPerPip)
shortTakeProfitPrice = strategy.position_avg_price - (takeProfitPips * pointsPerPip)
// Plot Moving Averages
plot(ma50, "50 MA", color=color.blue)
plot(ma200, "200 MA", color=color.red)
// Plot Entry Signals
plotshape(longCondition, "Long Signal", shape.triangleup, location.belowbar, color.green, size=size.small)
plotshape(shortCondition, "Short Signal", shape.triangledown, location.abovebar, color.red, size=size.small)
// Strategy Entry Rules
if (longCondition and strategy.position_size == 0)
strategy.entry("Long", strategy.long)
if (shortCondition and strategy.position_size == 0)
strategy.entry("Short", strategy.short)
// Strategy Exit Rules
if (strategy.position_size > 0 and exitLongCondition)
strategy.close("Long")
if (strategy.position_size < 0 and exitShortCondition)
strategy.close("Short")
// Stop Loss and Take Profit Management
if (strategy.position_size > 0)
strategy.exit("Long TP/SL", "Long", stop=longStopPrice, limit=useFixedTakeProfit ? longTakeProfitPrice : na)
if (strategy.position_size < 0)
strategy.exit("Short TP/SL", "Short", stop=shortStopPrice, limit=useFixedTakeProfit ? shortTakeProfitPrice : na)
// Performance Metrics
var float totalTrades = 0
var float winningTrades = 0
var float totalProfitPips = 0
var float totalLossPips = 0
if (strategy.closedtrades > 0)
totalTrades := strategy.closedtrades
winningTrades := strategy.wintrades
totalProfitPips := strategy.grossprofit / pointsPerPip
totalLossPips := math.abs(strategy.grossloss) / pointsPerPip
// Display Stats
var label statsLabel = na
label.delete(statsLabel[1])
// Create performance stats text
var string stats = ""
if (strategy.closedtrades > 0)
winRate = (winningTrades / math.max(totalTrades, 1)) * 100
avgWin = totalProfitPips / math.max(winningTrades, 1)
avgLoss = totalLossPips / math.max(totalTrades - winningTrades, 1)
plRatio = avgWin / math.max(avgLoss, 1)
stats := "Win Rate: " + str.tostring(winRate, "#.##") + "%\n" +
"Avg Win: " + str.tostring(avgWin, "#.##") + " pips\n" +
"Avg Loss: " + str.tostring(avgLoss, "#.##") + " pips\n" +
"P/L Ratio: " + str.tostring(plRatio, "#.##") + "\n" +
"Total Trades: " + str.tostring(totalTrades, "#")
statsLabel := label.new(x=bar_index, y=high, text=stats, style=label.style_label_down, color=color.new(color.blue, 80))