这是一个基于BankNifty 5分钟K线的超级趋势指标交易策略。该策略主要利用超级趋势指标识别趋势,结合交易时段和风险管理规则进行交易。
该策略首先定义了交易时段和日期范围等输入变量。交易时段设置为印度交易时段,从上午9:15到下午3:10。
然后计算超级趋势指标及其方向。超级趋势指标可以识别趋势的方向。
在每个交易时段开始时,策略要等待3根K线形成,然后才会考虑进场。这是为了过滤假突破。
多头信号是超级趋势指标方向变化由下向上时;空头信号是超级趋势指标方向变化由上向下时。
进场后会设置止损,固定止损点数和追踪止损百分比都可以通过输入变量调整。
在交易时段结束时,策略会平掉所有未平仓的头寸。
这是一个利用指标识别趋势的简单交易策略。它具有如下优势:
该策略也存在一些风险:
可以通过优化超级趋势指标的参数或增加其他指标判断来减少这些风险。
该策略还可以从以下几个方面进行优化:
本策略是一个基于BankNifty 5分钟线的超级趋势指标交易策略。它利用超级趋势指标判断趋势方向,结合交易时段和风险管理规则进行交易。相比复杂的量化策略,该策略规则简单清晰,容易理解和实施。作为一个示例策略,它为日后进行优化和改进提供了基础和方向。通过不断地完善和提高,希望该策略可以成为一个可靠稳定盈利的量化交易策略。
/*backtest
start: 2023-11-28 00:00:00
end: 2023-12-28 00:00:00
period: 1h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("BankNifty 5min Supertrend Based Strategy, 09:15 Entry with Date Range and Risk Management")
// Session and date range input variables
session = input("0915-1510", "Session", group="Indian Session Time")
start_date = input(title="Start Date", defval=timestamp("01 Jan 2022 00:00:00"), group="Backtest Specific Range")
end_date = input(title="End Date", defval=timestamp("01 Dec 2023 23:59:59"))
atrPeriod = input(50, "ATR Length", group="SuperTrend Setting")
factor = input.float(3.0, "Factor", step=0.1)
useDelay = input(true, "Use Delay?", group="Delay at Session Start")
Delay = useDelay ? input(10, title="Delay N numbers of candle", group="Delay at Session Start") : na
useDelay_stopLoss = input(true, "Use Stoploss Points?", group="Risk Management")
stopLoss = useDelay_stopLoss ? input(100, "Stop Loss Points", group="Risk Management"): na
useDelay_stopLossPerc1 = input(true, "Use Stoploss Trail?", group="Risk Management")
stopLossPerc1 =useDelay_stopLossPerc1 ? input.float(0.1, "Stop Loss Trail%", step=0.1,maxval = 1, group="Risk Management"): na
// Check if current time is within the specified session and date range
inSession = true
[supertrend, direction] = ta.supertrend(factor, atrPeriod)
// Wait for 3 candles to form at the start of every session
var candlesFormed = 0
if inSession and not inSession[1]
candlesFormed := 1
else if inSession and candlesFormed > 0
candlesFormed := candlesFormed + 1
else
candlesFormed := 0
//
// Only enter trades if 3 candles have formed at the start of the session
entryce = (ta.change(direction) < 0) or (candlesFormed >= Delay and direction < 0)
exitce = ta.change(direction) > 0
entrype = (ta.change(direction) > 0) or (candlesFormed >= Delay and direction > 0)
exitpe = ta.change(direction) < 0
var entryPrice = 0.0
if entryce and inSession
// Enter long trade
onePercent = strategy.position_avg_price *stopLossPerc1
entryPrice := close
strategy.entry("My Long Entry Id", strategy.long, comment="long" )
// Set stop loss at x% below entry price
strategy.exit("My Long Exit Id", "My Long Entry Id", stop=(entryPrice - stopLoss),trail_points=onePercent )
if entrype and inSession
onePercent1 = strategy.position_avg_price *stopLossPerc1
entryPrice := close
// Enter short trade
strategy.entry("My Short Entry Id", strategy.short, comment="short")
// Set stop loss at x% above entry price
strategy.exit("My Short Exit Id", "My Short Entry Id", stop=(entryPrice + stopLoss),trail_points=onePercent1)
// Close all trades at end of session
if not inSession and strategy.opentrades > 0
strategy.close_all()
// Plot Supertrend with changing colors
plot(supertrend, title="Supertrend", color=direction == 1 ? color.red : color.green, linewidth=2)