Tradingview策略分享1-动量捕捉LRI-STG
视频描述
哈喽小伙伴们,本期带来的是LRI-STG 来源 LRI Momentum Cycles [AlgoAlpha] 感谢原作者开源的代码
本来是一个12月13日更新的指标,我们趁热打铁,做成了策略

原脚本参数

更改后的参数

默认参数的策略运行效果 时间1小时
回测效果

SOL 赢利 427%

BCH 375%

FIL 329%

ETH 194%

盈亏比设置:

盈亏比设置可以显著增加胜率,但是降低收益,适用于配合短线做高杠杆
警报设置:

策略集成了KT交易平台,有需要的小伙伴可以注册测试

KT平台复制的hook url

源码下载地址将分享在视频下方链接,您的点赞支持是我长期更新的动力,我们下期见
--------------------------
策略源码:
//@version=5
// indicator("LRI Momentum Cycles [AlgoAlpha]", shorttitle="AlgoAlpha - LRI Momentum", overlay=false)
// upgrader @ktrader2100 date 2024/12/14
strategy(title="LRI-STG", overlay=false, currency="USD", pyramiding=0,
default_qty_type = strategy.cash, calc_on_every_tick = false, initial_capital=1000)
version = '1.1'
symbol_name = syminfo.tickerid
ASSETS = strategy.initial_capital
INIT_USDT_UNIT = ASSETS
// KT INIT START...
PLR = input.float(0, 'PLR', minval=0, step=0.1, group='Trade Opts')
PROFIT = input.float(1, 'PROFIT', minval=0, step=0.1, group='Trade Opts')
showprofit = input.bool(false, "showprofit", group='Trade Opts')
//-----------------------------------------------------------------------------}
//UDT's
//-----------------------------------------------------------------------------{
type ktraderes
float profit
float profit_p
string opt
string ptime
float amount
float usdt_amount
float open_price
float pprice
string debuginfo
float stoplose
float takeprofit
type ktorder
string type
int time
string timedate
string ptime
float pt
float currency
float price
float pprice
float profit
string msg
float stoplose
float takeprofit
//-----------------------------------------------------------------------------}
//Variables
//-----------------------------------------------------------------------------{
varip orders = array.new()
//-----------------------------------------------------------------------------}
//Methods - functions
//-
getTradeResult(string ptime, float price) =>
orderlen = orders.size()
float Ebamount = 0
float Esamount = 0
float Ebcurrency = 0
float Escurrency = 0
Eshow = ''
tradeopt = 'none'
float amount = 0
float open_price = 0
float profit = 0
float profit_p = 0
if orderlen > 0
for i = 0 to orderlen -1
dfrow = orders.get(i)
if dfrow.type == 'buy'
Ebamount := Ebamount + float(dfrow.currency) / float(dfrow.price)
Ebcurrency := Ebcurrency + float(dfrow.currency)
else
Esamount := Esamount + float(dfrow.currency) / float(dfrow.price)
Escurrency := Escurrency + float(dfrow.currency)
if Ebamount > 0 and Esamount > 0
if Ebamount > Esamount
tradeopt := 'buy'
amount := Ebamount - Esamount
else
tradeopt := 'sell'
amount := Esamount - Ebamount
else
if Ebamount > 0
tradeopt := 'buy'
amount := Ebamount
else if Esamount > 0
tradeopt := 'sell'
amount := Esamount
if tradeopt != 'none'
if tradeopt == 'buy'
profit := amount * price - (Ebcurrency - Escurrency)
else
profit := (Escurrency - Ebcurrency) - (amount * price)
// if profit != 0
if tradeopt == 'buy'
profit_p := profit / (Ebcurrency - Escurrency) * 100
else if tradeopt == 'sell'
profit_p := profit / (Escurrency - Ebcurrency) * 100
debuginfo = "Escurrency: " + str.tostring(Escurrency) + "Ebcurrency: " + str.tostring(Ebcurrency)
float stoplose = 0
float takeprofit = 0
if orderlen == 1
stoplose := orders.get(0).stoplose
takeprofit := orders.get(0).takeprofit
res = ktraderes.new(debuginfo = debuginfo, profit = profit, profit_p = profit_p, pprice = price,
amount = amount, opt = tradeopt, open_price = open_price, ptime = ptime, stoplose = stoplose, takeprofit = takeprofit)
res
transTimeToTimedate(int transtime, int ghour)=>
needtranstime = transtime
if transtime == 0
needtranstime := time
gmt = ghour > 0 ? 'GMT+'+str.tostring(ghour) : 'GMT'
timedate = str.format_time(int(needtranstime), "yyyy-MM-dd HH:mm:ss", gmt)
timedate
nowstr = transTimeToTimedate(0, 8)
addOrder(optype, currency, price, addtime, timedate, msg="减仓", stoplose=0, takeprofit=0)=>
tdres = getTradeResult(nowstr, close)
canopt = true
if tdres.opt != 'none'
if tdres.opt == optype
canopt := false
if canopt == true
// label.new(bar_index + 10, 1, optype, size = size.small, color = color.rgb(116, 8, 8))
neworder = ktorder.new(type = optype, currency = currency, price = price, time = addtime, timedate = timedate, msg = msg, stoplose=stoplose, takeprofit=takeprofit)
if optype == 'buy'
strategy.order("buy", strategy.long, qty = currency / price, comment = msg)
else if optype == 'sell'
strategy.order("sell", strategy.short, qty = currency / price, comment = msg)
orders.push(neworder)
pushmsg(string tradecon, float tradeconprice, string side, float price, float currency, float stoplose = 0 , float takeprofit = 0) =>
msgstring = '{"tradecon": "'+tradecon+'", "tradeconprice": "'+str.tostring(tradeconprice)+'", "side": "'+side+'", "price": "'+str.tostring(price)+'", "currency": "'+str.tostring(currency)+'", "stoplose": "'+str.tostring(stoplose)+'", "takeprofit": "'+str.tostring(takeprofit)+'" , "symbol_name": "'+symbol_name+'"}'
alert(message = msgstring, freq = alert.freq_once_per_bar)
''
print_profit(atprice) =>
if showprofit == true
orderl = orders.size()
int i = 0
showtext = ""
showcolumns = array.from("type", "price", "currency", "stop", "take","time", "msg")
showtext := array.join(showcolumns, ",") + "n"
ktres = getTradeResult(transTimeToTimedate(0, 8), close)
while i < orders.size()
corder = orders.get(i)
jarr = array.from(corder.type, str.tostring(corder.price), str.tostring(corder.currency), str.tostring(corder.stoplose), str.tostring(corder.takeprofit), corder.timedate, corder.msg)
rowstr = jarr.join(',')
if str.length(showtext) > 1000
showtext += "n..."
// last line dump
lastlinenum = orders.size() - 1
corder := orders.get(orders.size() - 1)
jarr := array.from(corder.type, str.tostring(corder.price), str.tostring(corder.currency), str.tostring(corder.stoplose), str.tostring(corder.takeprofit), corder.timedate, corder.msg)
rowstr := jarr.join(',')
showtext += ("n" + str.tostring(lastlinenum) + '#' + rowstr)
break
showtext += ("n" + str.tostring(i) + '#' + rowstr)
i += 1
''
resshowstr = "n res:---" + ktres.opt + "n profit:" + str.tostring(ktres.profit) + "n amount:" +
str.tostring(ktres.amount) + "n pprice:" + str.tostring(ktres.pprice)+
"n profit_p: " + str.tostring(ktres.profit_p)
label.new(bar_index, atprice, showtext + resshowstr, size = size.small, color = color.white)
''
// KT INIT END...
calc_group = "Calculation Parameters"
appearance_group = "Appearance Settings"
alerts_group = "Alert Conditions"
n = input.int(22, title="Lookback Period", minval=1, group=calc_group, tooltip="Number of historical bars to analyze")
linreg_source = input.source(close, title="Linear Regression Source", group=calc_group, tooltip="Source for the linear regression calculation")
linreg_length = input.int(25, title="Linear Regression Length", minval=1, group=calc_group, tooltip="Number of bars used for linear regression")
ema_length = input.int(160, title="Ema Length", minval=1, group=calc_group, tooltip="Number of bars used for linear regression")
green = input.color(#00ffbb, title="Bullish Color", group=appearance_group, tooltip="Color when trend is bullish")
red = input.color(#ff1100, title="Bearish Color", group=appearance_group, tooltip="Color when trend is bearish")
v = ta.linreg(linreg_source, linreg_length, 0)
trend = 0
for i = 0 to n - 2
for j = i + 1 to n - 1
if v[i] != v[j]
trend += v[i] > v[j] ? 1 : -1
state = trend > trend[1] or (trend == trend[1] and trend > 0) ? 1 : -1
trendN = trend / ((n * (n - 1)) / 2)
trendColor = state == 1
? color.from_gradient(trendN, 0, 1, color.new(green, 80), color.new(green, 0))
: color.from_gradient(trendN, -1, 0, color.new(red, 0), color.new(red, 80))
p1 = plot(trendN, color=trendColor, linewidth=2, style=plot.style_linebr, title="Trend Normalized")
p2 = plot(trendN[1], display=display.none, title="Previous Trend Normalized")
baseLine = plot(0, color=color.new(color.gray, 90), title="Baseline")
fillColor = state == 1
? color.from_gradient(trendN, 0, 1, color.new(green, 80), color.new(green, 0))
: color.from_gradient(trendN, -1, 0, color.new(red, 0), color.new(red, 80))
fill(p1, p2, fillColor)
// alertcondition(state == 1 and state[1] == -1, title="Bullish Signal", message="Trend turned bullish")
// alertcondition(state == -1 and state[1] == 1, title="Bearish Signal", message="Trend turned bearish")
ema_value = ta.ema(close, ema_length)
ema_slope = ema_value - ta.ema(close[1], ema_length)
plot(ema_value, "EMA", force_overlay = true)
is_uptrend = ema_slope > 0
is_downtrend = ema_slope < 0
tradeopt = 'none'
float stoplose = 0
float takeprofit = 0
// float mvatr = ta.atr(patrlen) * patrmtl
float mvatr = close * PROFIT / 100
tdres = getTradeResult(nowstr, close)
buycon = state == 1 and state[1] == -1 and is_uptrend
sellcon = state == -1 and state[1] == 1 and is_downtrend
// plot(state , title = 'state')
// plot(buycon ? 1 : 0 , title = 'buycon')
// plot(sellcon ? 1 : 0 , title = 'sellcon')
if buycon
//label.new(bar_index, state, tdres.opt, size = size.small, color = color.rgb(158, 56, 56))
tradeopt := 'buy'
if tdres.opt == 'sell'
strategy.close_all()
orders := array.new(0)
tdres := getTradeResult(nowstr, close)
//label.new(bar_index + 5, state, tdres.opt, size = size.small, color = #b10d0d)
if tdres.opt == 'none'
if PLR > 0 and PROFIT > 0
float mvatr2 = close + (close * PROFIT / 100)
stoplose := close - mvatr / PLR
takeprofit := close + mvatr
label.new(bar_index, takeprofit, 'probuy:' + str.tostring(mvatr), size = size.small, color = #b10d0d,force_overlay = true)
else
stoplose := 0
takeprofit := 0
addOrder(optype = tradeopt, currency = INIT_USDT_UNIT, price = close, addtime=time,
timedate = nowstr, msg='开多',
stoplose = stoplose, takeprofit = takeprofit)
pushmsg(tradecon='<',
tradeconprice=close,
side='buy',
price=close,
currency=1,
stoplose = stoplose,
takeprofit = takeprofit)
if sellcon
tradeopt := 'sell'
if tdres.opt == 'buy'
strategy.close_all()
orders := array.new(0)
tdres := getTradeResult(nowstr, close)
if tdres.opt == 'none'
if PLR > 0 and PROFIT > 0
stoplose := close + mvatr / PLR
takeprofit := close - mvatr
else
stoplose := 0
takeprofit := 0
addOrder(optype = tradeopt, currency = INIT_USDT_UNIT, price = close,
addtime=time, timedate = nowstr, msg='开空',
stoplose = stoplose, takeprofit = takeprofit)
pushmsg(tradecon='>',
tradeconprice=close,
side='sell',
price=close,
currency=1,
stoplose = stoplose,
takeprofit = takeprofit)
if PLR > 0
if tdres.opt != 'none'
if tdres.profit > 0
if tdres.opt == 'buy'
if close >= tdres.takeprofit
strategy.close_all(comment = '多单止盈')
orders := array.new(0)
else if tdres.opt == 'sell'
if close <= tdres.takeprofit
strategy.close_all(comment = '空单止盈')
orders := array.new(0)
else if tdres.profit < 0
if tdres.opt == 'buy'
if close <= tdres.stoplose
strategy.close_all(comment = '多单止损')
orders := array.new(0)
else if tdres.opt == 'sell'
if close >= tdres.stoplose
strategy.close_all(comment = '空单止损')
orders := array.new(0)
if barstate.islastconfirmedhistory
print_profit(0)