#pine-script #tradingview-api #pine-script-v4
Вопрос:
Приведенные ниже коды относятся к версии v2. Мне нужно обновить версию до v5, но я многого не знаю, и я в замешательстве. Вы не могли бы мне помочь?
//Settings needlong = input(true, "long") needshort = input(true, "short") needstops = input(false, "stops") stoppercent = input(5, defval = 5, minval = 1, maxval = 50, title = "Stop, %") usefastsma = input(true, "Use fast MA Filter") fastlen = input(5, defval = 5, minval = 1, maxval = 50, title = "fast MA Period") slowlen = input(21, defval = 20, minval = 2, maxval = 200, title = "slow MA Period") bars = input(2, defval = 2, minval = 0, maxval = 3, title = "Bars Q") needbg = input(false, defval = false, title = "Need trend Background?") needex = input(true, defval = true, title = "Need extreme? (crypto/fiat only!!!)") fromyear = input(1900, defval = 1900, minval = 1900, maxval = 2100, title = "From Year") toyear = input(2100, defval = 2100, minval = 1900, maxval = 2100, title = "To Year") frommonth = input(01, defval = 01, minval = 01, maxval = 12, title = "From Month") tomonth = input(12, defval = 12, minval = 01, maxval = 12, title = "To Month") fromday = input(01, defval = 01, minval = 01, maxval = 31, title = "From day") today = input(31, defval = 31, minval = 01, maxval = 31, title = "To day") src = close //PriceChannel 1 lasthigh = highest(src, slowlen) lastlow = lowest(src, slowlen) center = (lasthigh lastlow) / 2 //PriceChannel 2 lasthigh2 = highest(src, fastlen) lastlow2 = lowest(src, fastlen) center2 = (lasthigh2 lastlow2) / 2 //Trend trend = low gt; center and low[1] gt; center[1] ? 1 : high lt; center and high[1] lt; center[1] ? -1 : trend[1] //Bars bar = close gt; open ? 1 : close lt; open ? -1 : 0 redbars = bars == 0 ? 1 : bars == 1 and bar == -1 ? 1 : bars == 2 and bar == -1 and bar[1] == -1 ? 1 : bars == 3 and bar == -1 and bar[1] == -1 and bar[2] == -1 ? 1 : 0 greenbars = bars == 0 ? 1 : bars == 1 and bar == 1 ? 1 : bars == 2 and bar == 1 and bar[1] == 1 ? 1 : bars == 3 and bar == 1 and bar[1] == 1 and bar[2] == 1 ? 1 : 0 //Fast RSI fastup = rma(max(change(close), 0), 2) fastdown = rma(-min(change(close), 0), 2) fastrsi = fastdown == 0 ? 100 : fastup == 0 ? 0 : 100 - (100 / (1 fastup / fastdown)) //CryptoBottom mac = sma(close, 10) len = abs(close - mac) sma = sma(len, 100) max = max(open, close) min = min(open, close) //Signals up1 = trend == 1 and (low lt; center2 or usefastsma == false) and redbars == 1 dn1 = trend == -1 and (high gt; center2 or usefastsma == false) and greenbars == 1 up2 = high lt; center and high lt; center2 and bar == -1 and needex dn2 = low gt; center and low gt; center2 and bar == 1 and needex up3 = close lt; open and len gt; sma * 3 and min lt; min[1] and fastrsi lt; 10 ? 1 : 0 //Lines plot(center2, color = red, linewidth = 3, transp = 0, title = "Fast MA") plot(center, color = blue, linewidth = 3, transp = 0, title = "Slow MA") //Background col = needbg == false ? na : trend == 1 ? lime : red bgcolor(col, transp = 80) //Trading stoplong = up1 == 1 and needstops == true ? close - (close / 100 * stoppercent) : stoplong[1] stopshort = dn1 == 1 and needstops == true ? close (close / 100 * stoppercent) : stopshort[1] if up1 or up2 or up3 strategy.entry("Long", strategy.long, needlong == false ? 0 : na, when=(time gt; timestamp(fromyear, frommonth, fromday, 00, 00) and time lt; timestamp(toyear, tomonth, today, 23, 59))) strategy.exit("Stop Long", "Long", stop = stoplong) if dn1 strategy.entry("Short", strategy.short, needshort == false ? 0 : na, when=(time gt; timestamp(fromyear, frommonth, fromday, 00, 00) and time lt; timestamp(toyear, tomonth, today, 23, 59))) strategy.exit("Stop Short", "Short", stop = stopshort) if time gt; timestamp(toyear, tomonth, today, 23, 59) strategy.close_all()
Ответ №1:
Основными изменениями являются:
- Использование новой
input.int()
функции для ввода пользователемinteger
типов - Использование новых пространств
ta
имен иmath
- Использование новой
color.new()
функции для создания цвета с прозрачностью
Вот v5:
//Settings needlong = input(true, "long") needshort = input(true, "short") needstops = input(false, "stops") stoppercent = input.int(5, minval = 1, maxval = 50, title = "Stop, %") usefastsma = input(true, "Use fast MA Filter") fastlen = input.int(5, minval = 1, maxval = 50, title = "fast MA Period") slowlen = input.int(21, minval = 2, maxval = 200, title = "slow MA Period") bars = input.int(2, minval = 0, maxval = 3, title = "Bars Q") needbg = input(false, title = "Need trend Background?") needex = input(true, title = "Need extreme? (crypto/fiat only!!!)") fromyear = input.int(1900, minval = 1900, maxval = 2100, title = "From Year") toyear = input.int(2100, minval = 1900, maxval = 2100, title = "To Year") frommonth = input.int(01, minval = 01, maxval = 12, title = "From Month") tomonth = input.int(12, minval = 01, maxval = 12, title = "To Month") fromday = input.int(01, minval = 01, maxval = 31, title = "From day") today = input.int(31, minval = 01, maxval = 31, title = "To day") src = close //PriceChannel 1 lasthigh = ta.highest(src, slowlen) lastlow = ta.lowest(src, slowlen) center = (lasthigh lastlow) / 2 //PriceChannel 2 lasthigh2 = ta.highest(src, fastlen) lastlow2 = ta.lowest(src, fastlen) center2 = (lasthigh2 lastlow2) / 2 //Trend trend = false trend := low gt; center and low[1] gt; center[1] ? 1 : high lt; center and high[1] lt; center[1] ? -1 : trend[1] //Bars bar = close gt; open ? 1 : close lt; open ? -1 : 0 redbars = bars == 0 ? 1 : bars == 1 and bar == -1 ? 1 : bars == 2 and bar == -1 and bar[1] == -1 ? 1 : bars == 3 and bar == -1 and bar[1] == -1 and bar[2] == -1 ? 1 : 0 greenbars = bars == 0 ? 1 : bars == 1 and bar == 1 ? 1 : bars == 2 and bar == 1 and bar[1] == 1 ? 1 : bars == 3 and bar == 1 and bar[1] == 1 and bar[2] == 1 ? 1 : 0 //Fast RSI fastup = ta.rma(math.max(ta.change(close), 0), 2) fastdown = ta.rma(-math.min(ta.change(close), 0), 2) fastrsi = fastdown == 0 ? 100 : fastup == 0 ? 0 : 100 - (100 / (1 fastup / fastdown)) //CryptoBottom mac = ta.sma(close, 10) len = math.abs(close - mac) sma = ta.sma(len, 100) max = math.max(open, close) min = math.min(open, close) //Signals up1 = trend == 1 and (low lt; center2 or usefastsma == false) and redbars == 1 dn1 = trend == -1 and (high gt; center2 or usefastsma == false) and greenbars == 1 up2 = high lt; center and high lt; center2 and bar == -1 and needex dn2 = low gt; center and low gt; center2 and bar == 1 and needex up3 = close lt; open and len gt; sma * 3 and min lt; min[1] and fastrsi lt; 10 ? 1 : 0 //Lines c_red = color.new(color.red, 0) c_blue = color.new(color.blue, 0) c_lime = color.new(color.lime, 0) plot(center2, color = c_red, linewidth = 3, title = "Fast MA") plot(center, color = c_blue, linewidth = 3, title = "Slow MA") //Background col = needbg == false ? na : trend == 1 ? c_lime : c_red bgcolor(col) //Trading stoplong = 0.0 stoplong := up1 == 1 and needstops == true ? close - (close / 100 * stoppercent) : stoplong[1] stopshort = 0.0 stopshort := dn1 == 1 and needstops == true ? close (close / 100 * stoppercent) : stopshort[1] if up1 or up2 or up3 strategy.entry("Long", strategy.long, needlong == false ? 0 : na, when=(time gt; timestamp(fromyear, frommonth, fromday, 00, 00) and time lt; timestamp(toyear, tomonth, today, 23, 59))) strategy.exit("Stop Long", "Long", stop = stoplong) if dn1 strategy.entry("Short", strategy.short, needshort == false ? 0 : na, when=(time gt; timestamp(fromyear, frommonth, fromday, 00, 00) and time lt; timestamp(toyear, tomonth, today, 23, 59))) strategy.exit("Stop Short", "Short", stop = stopshort) if time gt; timestamp(toyear, tomonth, today, 23, 59) strategy.close_all()
Комментарии:
1. Большое тебе спасибо, мой друг. Ты великолепен. @витрувий