
该策略是一个基于简单移动平均线(SMA)的BankNifty期货交易策略。策略的主要思路是利用SMA作为趋势指标,当价格上穿SMA时做多,当价格下穿SMA时做空。同时,该策略还设置了止损和止盈条件,以控制风险和锁定利润。
该策略的核心是使用SMA作为趋势指标。具体来说,策略首先计算指定周期(默认为200)的SMA,然后根据价格与SMA的相对位置来判断趋势方向。当价格上穿SMA时,认为上升趋势已经形成,此时做多;当价格下穿SMA时,认为下降趋势已经形成,此时做空。此外,该策略还设置了止损和止盈条件,以控制风险和锁定利润。止损条件包括:价格突破SMA一定范围(由”Stop Loss Buffer”参数设置)、价格突破开仓价一定范围(由”Stop Loss”参数设置)以及交易时间到达15:00。止盈条件为价格突破开仓价一定范围(由”Target Price”参数设置)。
该策略是一个基于SMA的简单交易策略,适用于BankNifty期货。它的优点在于原理简单,适应性强,同时设有风控措施。但在实际应用中,还需要注意参数优化、震荡市场、趋势逆转和盘中波动等潜在风险。未来可以考虑从参数优化、结合其他指标、动态止损和限制交易时间等方面对策略进行优化和改进。
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Bhasker_S
//@version=5
strategy("Strategy BankNifty SMA", overlay=true, margin_long=100, margin_short=100)
src = input(close, title="Source")
timeFrame = input.timeframe(defval='5', title = "Select Chart Timeframe")
typeMA = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA"])
len = input.int(200, minval=1, title="Length", step = 10)
alertPrecision = input.float(0, "Alert Precision", minval = 0, maxval = 50, step=1)
slTimeFrame = input.timeframe(defval='1', title = "Select Stoploss Candle Timeframe")
slBuffer = input.float(0, "Stop Loss Buffer", minval = 0, maxval = 50, step = 1)
targetSlab = input.float(150, "Target Price", minval = 1, maxval = 2000, step = 10)
Stoploss = input.float(20, "Stop Loss", minval = 1, maxval = 2000, step = 5)
offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)
//out = ta.sma(src, len)
ma(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
tfSource = request.security(syminfo.tickerid, timeFrame, src, barmerge.gaps_on, barmerge.lookahead_off)
mySMA = ma(tfSource, len, typeMA)
plot(mySMA, color=color.rgb(243, 33, 89), title="MA", offset=offset, linewidth = 2)
slClose = request.security(syminfo.tickerid, slTimeFrame, src, barmerge.gaps_on, barmerge.lookahead_off)
highTravel = low > mySMA
lowTravel = high < mySMA
touchabove = (((high[1] + alertPrecision) > mySMA[1]) and (low[1] < mySMA[1])) //and (high[2] < mySMA[2])
touchbelow = (((low[1] - alertPrecision) < mySMA[1]) and (high[1] > mySMA[1])) //and (low[2] > mySMA[2])
crossabove = math.min(open, close) > mySMA
crossbelow = math.max(open, close) < mySMA
upalert = (touchabove or touchbelow) and crossabove
downalert = (touchabove or touchbelow) and crossbelow
h=hour(time('1'),"Asia/Kolkata")
m=minute(time('1'),"Asia/Kolkata")
startTime=h*100+m
if upalert and strategy.position_size == 0
strategy.entry("buy", strategy.long, 15)
if downalert and strategy.position_size == 0
strategy.entry("sell", strategy.short, 15)
longexit = (slClose < (mySMA - slBuffer)) or (slClose < (strategy.opentrades.entry_price(strategy.opentrades - 1) - Stoploss)) or (slClose > (strategy.opentrades.entry_price(strategy.opentrades - 1) + targetSlab)) or (hour(time) == 15)
shortexit = (slClose > (mySMA + slBuffer)) or (slClose > (strategy.opentrades.entry_price(strategy.opentrades - 1) + Stoploss)) or (slClose < (strategy.opentrades.entry_price(strategy.opentrades - 1) - targetSlab)) or (hour(time) == 15)
if longexit
strategy.close("buy")
if shortexit
strategy.close("sell")