
Chiến lược này sử dụng đường trung bình di chuyển đơn giản và một số tính toán toán học để xác định điểm mua/bán. Chúng tôi sử dụng đường SMA 100 ngày làm đường chuẩn. Nếu giá đóng cửa thấp hơn đường, chúng tôi chọn điểm mở cửa dựa trên mức độ thấp hơn đường, giá trị này (<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Chiến lược này sử dụng ba đường SMA: đường nhanh ((14 ngày mặc định), đường chậm ((100 ngày mặc định) và đường tham chiếu ((30 ngày mặc định)
Khi giá đóng cửa thấp hơn đường tham chiếu và độ lệch thấp của đường chậm lớn hơn so với độ lệch thấp của cấu hình, và đường nhanh lên và đường chậm xuống, vào đa đầu. Khi đáp ứng các điều kiện này, đường nhanh và đường chậm có khả năng giao nhau, vì vậy là một điểm khởi đầu tốt hơn.
Khi giá đóng cửa cao hơn đường tham chiếu, và độ di chuyển cao của đường chậm lớn hơn độ di chuyển cao của cấu hình, và giá đóng cửa đã tăng 3 đường K liên tiếp, đã đạt được lợi nhuận, và đường nhanh cao hơn đường chậm, vị trí yên là đơn giản. Nếu giá tiếp tục tăng cao, việc theo dõi dừng lỗ sẽ được khởi động.
Mỗi lần giao dịch, các vị trí được nhập theo tỷ lệ quyền lợi nhất định, bằng cách này kiểm soát vị trí.
Các biện pháp tối ưu hóa:
Chiến lược giao dịch biến động SMA tìm kiếm thời điểm đầu vào tốt nhất bằng cách đặt biến động dựa trên các đường trung bình SMA khác nhau. Đồng thời, cơ chế thoát ra được thiết lập để theo dõi lỗ hổng để khóa lợi nhuận. Chiến lược này đơn giản, dễ hiểu và dễ thực hiện.
/*backtest
start: 2022-12-12 00:00:00
end: 2023-12-18 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// @version=4
// Author: Sonny Parlin (highschool dropout)
strategy(shorttitle="SMA+Strategy", title="SMA Offset Strategy",
overlay=true, currency=currency.USD,
initial_capital=10000)
// Inputs and variables
ss = input(14, minval=10, maxval=50, title="SMA Fast (days)")
ff = input(100, minval=55, maxval=200, title="SMA Slow (days)")
ref = input(30, minval=20, maxval=50, title="SMA Reference (days)")
lowOffset = input(0.001, "Low Offset (%)", minval=0, step=0.001)
highOffset = input(0.0164, "High Offset (%)", minval=0, step=0.0001)
orderStake = input(0.96, "Order Stake (%)", minval=0, step=0.01)
// SMA
smaFast = sma(close, ss)
smaSlow = sma(close, ff)
smaRef = sma(close, ref)
distanceLow = (close - smaSlow) / close
distanceHigh = (close - smaSlow) / close
// Set up SMA plot but don't show by default
plot(smaFast, "smaFast", color=#00ff00, display = 0)
plot(smaSlow, "smaSlow", color=#ff0000, display = 0)
plot(smaRef, "smaRef", color=#ffffff, display = 0)
// The buy stratey:
// guard that the low is under our sma low reference line by our lowOffset %,
// default is 0.001. (low < smaRef) and (distanceLow > lowOffset)
// SMA fast is on the rise and SMA slow is falling and they are very likely
// to cross. (rising(smaFast,1)) and (falling(smaSlow, 1))
enterLong = (low < smaRef) and (distanceLow > lowOffset) and (rising(smaFast,1)) and (falling(smaSlow, 1))
// The sell Strategy:
// Guard that close is higher than our sma high reference line by our
// highOffset %, default is 0.0164. (close > smaRef) and (distanceHigh > highOffset)
// Guard that close has risen by 3 candles in a row (rising(close,3))
// Guard that we currently have profit (strategy.openprofit > 0)
// Guard that SMA fast is higher than smaSlow (smaFast > smaSlow)
// If it keeps going up past our close position the trailing stoploss will kick in!
enterShort = (close > smaRef) and (distanceHigh > highOffset) and (rising(close,3)) and (strategy.openprofit > 0) and (smaFast > smaSlow)
// Order size is based on total equity
// Example 1:
// startingEquity = 2000
// close = 47434.93
// orderStake = 0.45
// (2,000 × orderStake) / close = orderSize = 0.0189733599 = approx $900
// Example 2:
// startingEquity = 2000
// close = 1.272
// orderStake = 0.45
// (startingEquity × orderStake) / close = orderSize = 707.5471698113 = approx $900
orderSize = (strategy.equity * orderStake) / close
// Trailing Stoploss
// I'm using 1.35 as my default value, play with this for different results.
longTrailPerc = input(title="Trailing Stoploss (%)",
type=input.float, minval=0.0, step=0.1, defval=1.35) * 0.01
longStopPrice = 0.0
longStopPrice := if (strategy.position_size > 0)
stopValue = close * (1 - longTrailPerc)
max(stopValue, longStopPrice[1])
else
0
if (enterLong)
strategy.entry("Open Long Position", strategy.long, orderSize, when=strategy.position_size <= 0)
if (enterShort)
strategy.exit(id="Close Long Position", stop=longStopPrice)
//plot(strategy.equity)