### Gainlab Size Style Example Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Example of defining a size input with a title, showing the resulting code. ```Python size = S.size(20,title='尺寸') ``` -------------------------------- ### Gainlab Style Method Examples Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Examples demonstrating the use of style methods for color selection, both directly and through the 'S' shorthand. ```Python color1 = S.color('#5b8ff9', title='颜色1') color2 = style.color('rgb(255,0,0)', title='颜色2') ``` -------------------------------- ### Gainlab Full Style Example Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Example of defining a fill style input with a title, showing the resulting code. ```Python full = S.full('fill',title='填充') ``` -------------------------------- ### Gainlab Color Style Example Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Example of defining a color input with a title, showing the resulting code. ```Python color = S.color('#00F',title='颜色') ``` -------------------------------- ### Gainlab Style Method Examples Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Provides examples of using standalone style methods for color and line styling, showing how parameters are passed. ```Python color = S.color('#5b8ff9', title='颜色') lineStyle = S.line('#5b8ff9', 1, 'solid',title='主线') ``` -------------------------------- ### Gainlab Icon Style Example Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Example of defining an icon input with a title, showing the resulting code. ```Python icon = S.icon('arrowDown',title='图形') ``` -------------------------------- ### Gainlab Select Input Examples Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Examples of defining select inputs for data source and moving average calculation type. ```Python src = I.select('close', options=SOURCE, title='数据源') maType = I.select('MA', options=['MA','EMA','SMA','WMA'], title='均线计算方式') ``` -------------------------------- ### Example: sshape Style Usage Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Demonstrates creating 'sshape' style objects for buy and sell signals using different icons, colors, and sizes. The examples show how to define these styles for visual representation on a chart. ```Python buySignal = S.sshape('arrowUp','#00FF00',12,'fill',title='支撑区') sellSignal = S.sshape('arrowDown','#FF0000',12,'fill',title='压力区') # Expected return values: # buySignal = {"icon":"arrowUp","color":"#00FF00","size":12,"full":"fill","show":true} # sellSignal = {"icon":"arrowDown","color":"#FF0000","size":12,"full":"fill","show":true} ``` -------------------------------- ### Gainlab Width Style Example Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Example of defining a line width input with a title, showing the resulting code. ```Python width = S.width(2,title='线宽') ``` -------------------------------- ### Gainlab Style (Line Style) Example Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Example of defining a line style input with a title, showing the resulting code. ```Python style = S.style('solid',title='线型') ``` -------------------------------- ### Data Extraction Examples Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Examples of extracting specific data attributes like closing prices, percentage changes, and high-low averages from a list of data points. ```javascript // 获取所有k线的收盘价数组 closeArr = F.attr(dataList, 'close') // 获取所有k线的涨跌幅%数组 percentageArr = F.attr(dataList, 'percentage') // 获取所有k线的最高价和最低价的平均值 hl2Arr = F.attr(dataList, 'hl2') // 获取k线的macd值 macdArr = F.attr(F.macd(dataList, 12,26,9),'macd') ``` -------------------------------- ### Gainlab Boolean Input Example Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Example of defining a boolean input for smooth calculation with a title. ```Python smooth = I.bool(true, title='是否平滑计算') ``` -------------------------------- ### TB-Trend Indicator Setup Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab官方脚本案例文档 _副本.md This script sets up the basic parameters for the TB-Trend indicator, which uses dual trend bands based on moving averages of high and low prices. It defines input periods for fast and slow lines and styles for the fast upper band. ```Pine Script //@name=TB-Trend //@title=TB-Trend 趋势指标 //@desc = ## TB-Trend 趋势指标 //基于快线和慢线的双重趋势带指标,通过计算最高价和最低价的简单移动平均线来构建趋势通道。 //#### 应用法则: //- 价格突破快线上轨时,快线通道显示看涨信号 //- 价格跌破快线下轨时,快线通道显示看跌信号 //- 慢线通道提供主要趋势方向确认 //- 快慢线配合使用,可以识别趋势的强度和持续性 //@position=main //@version=1 period1 = I.int(20, title="快线", min=1) period2 = I.int(180, title="慢线", min=1) fastUpper = S.line('rgb(0, 195, 255)',1, 'solid', title="快线上轨") ``` -------------------------------- ### Example: scircle Style Usage Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Provides examples of creating 'scircle' style objects with different parameter combinations, including fill color only, fill color with border, and border only. It also shows the resulting style objects. ```Python scircle1 = S.scircle('rgba(255,0,0,0.1)', 1, title='圆形1') scircle2 = S.scircle('rgba(255,0,0,0.1)', border='#F00',1, title='圆形2') scircle3 = S.scircle(border='#F00', 1, title='圆形3') # Expected return values: # scircle1 = {"color":"rgba(255,0,0,0.1)","border":null,"size":1,"style":"solid","show":true} # scircle2 = {"color":"rgba(255,0,0,0.1)","border":"#F00","size":1,"style":"solid","show":true} # scircle3 = {"color":null,"border":"#F00","size":1,"style":"solid","show":true} ``` -------------------------------- ### Gainlab Charting: draw.hline() Examples Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Provides examples for drawing horizontal lines using `draw.hline()`, including basic lines, multiple lines, and lines with specified start and end X-coordinates. ```javascript // Basic horizontal line lineStyle = S.line('#5B8FF9', 1, 'solid', title="水平线样式") D.hline('center', lineStyle) // Multiple horizontal lines D.hline(['30%', '80%'], lineStyle) // Specify start and end positions D.hline(100, lineStyle, 'index:-10', 'index:end') // Using Y-axis corresponding values D.hline(50, lineStyle, 'left', 'right') ``` -------------------------------- ### Gainlab Charting: draw.vline() Examples Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Provides examples for drawing vertical lines using `draw.vline()`, including basic lines, multiple lines, and lines with specified start and end Y-coordinates. ```javascript // Basic vertical line lineStyle = S.line('#F5222D', 1, 'solid', title="垂直线样式") D.vline('center', lineStyle) // Multiple vertical lines D.vline(['index:-20', 'index:-10', 'index:-5'], lineStyle) // Specify start and end positions D.vline('index:end', lineStyle, 0, '100px') // Using pixel coordinates D.vline('30%', lineStyle, 'top', 'bottom') ``` -------------------------------- ### Gainlab Float Input Example Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Demonstrates how to define a float input with a title, minimum, and maximum value. ```Python ratio = I.float(0.5, title='系数', min=0, max=5, ) ``` -------------------------------- ### Gainlab Charting: draw.label() Example with Text Data Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Example demonstrating how to use `draw.label()` to display '涨' (rise) or '跌' (fall) text based on price comparison, using predefined label styles and an optional background style. ```javascript labelStyle1 = S.label('rgb(0, 255, 255)',12,'left','lowDown',title='看多') labelStyle1.y = -10 labelStyle1.x = 5 labelStyle2 = S.label('rgb(255, 0, 0)',12,'left','highUp',title='看空') labelStyle2.y = 10 labelStyle2.x = -5 labelBg = S.labelbg('rgba(0, 0, 0,0.6)','fill',title='标签背景') labelBg.padding = 5 labelBg.radius = 2 textData = dataList.map(item => { if(item.close > item.open){ return {text:'涨'} }else{ return {text:'跌'} } }) D.label(textData, ({value}) => { return value.text === '涨' ? labelStyle1 : labelStyle2 }, labelBg) ``` -------------------------------- ### QQE and Bollinger Bands Calculation Execution Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab官方脚本案例文档 _副本.md Executes the QQE and Bollinger Bands calculations using the previously defined configuration parameters and functions. It generates the necessary data series for plotting these indicators. ```JavaScript const primaryQQE = calculateQQE(dataList, rsiLengthPrimary, rsiSmoothingPrimary, qqeFactorPrimary) const secondaryQQE = calculateQQE(dataList, rsiLengthSecondary, rsiSmoothingSecondary, qqeFactorSecondary) const bollingerBasis = [] const bollingerDeviation = [] const bollingerUpper = [] const bollingerLower = [] dataList.forEach((kData,i)=>{ if (i < bollingerLength - 1) { bollingerBasis.push(0) bollingerDeviation.push(0) bollingerUpper.push(0) bollingerLower.push(0) }else{ let sum = 0 for (let j = i - bollingerLength + 1; j <= i; j++) { sum += (primaryQQE.qqeTrendLine[j] - 50) } const basis = sum / bollingerLength bollingerBasis.push(basis) let variance = 0 for (let j = i - bollingerLength + 1; j <= i; j++) { variance += Math.pow((primaryQQE.qqeTrendLine[j] - 50) - basis, 2) } ``` -------------------------------- ### GainLab Script MA Example Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md A basic example of a GainLab Script to calculate and draw a Moving Average (MA) on a chart. It demonstrates defining parameters, selecting data sources, and applying styles. ```JavaScript // @name = MA // @position = main len = I.int(10, title='周期') style = S.line('#5b8ff9', 1, 'solid') ma = F.ma(dataList, len, 'close') D.line(ma, style) ``` -------------------------------- ### Bar Chart with Keyword Baseline Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Illustrates using keyword strings ('center', 'bottom') as baselines for the bar chart, simplifying the setup for common baseline positions. ```javascript // 使用关键字基线 D.bar(data, 'center', barStyle) // 以Y轴中心为基线 D.bar(data, 'bottom', barStyle) // 以Y轴底部为基线 ``` -------------------------------- ### QQE and Bollinger Bands Configuration Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab官方脚本案例文档 _副本.md Defines configuration variables for QQE and Bollinger Bands indicators, including factors, lengths, smoothing, and styling for lines and signals. These parameters are used to customize the indicator's behavior and appearance. ```Python qqeFactorSecondary = I.float(1.61, title='次QQE因子', min=0.1, max=10, step=0.01, tip='次要QQE系统的因子') threshold = I.float(3.0, title='信号阈值', min=0.1, max=10, step=0.1, tip='次要系统的信号阈值') bollingerLength = I.int(50, title='布林带周期', min=10, max=200, tip='布林带计算周期') bollingerMultiplier = I.float(0.35, title='布林带倍数', min=0.1, max=2, step=0.05, tip='布林带宽度倍数') primaryLineStyle = S.line('#FFFFFF', 2, solid, title='主QQE线') secondaryLineStyle = S.line('#FF9900', 2, solid,false, title='次QQE线') bollingerUpperStyle = S.line('#888888',1,'dashed',false,title='布林带上轨') bollingerLowerStyle = S.line('#888888',1,'dashed',false,title='布林带下轨') zeroLineStyle = S.line('#ffffff', 1, dotted, title='零轴线') upSignalStyle = S.bar('#00c3ff', fill, title='看涨信号') downSignalStyle = S.bar('#ff0062', fill, title='看跌信号') neutralStyle = S.bar('#707070', fill, title='中性区域') ``` -------------------------------- ### Gainlab Charting: SAR Example with Dynamic Styling Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Demonstrates how to use the `F.sar` function to calculate SAR values and apply dynamic styles to chart shapes based on the SAR value relative to the midpoint of high and low prices. ```javascript upStyle = S.shape('circle','#339933',6,title='多方') downStyle = S.shape('circle','#FF0033',6,title='空方') sar = F.sar(dataList,startAf,step,maxAf) D.shape(sar,({value,index}) => { return (value < (dataList[index].high + dataList[index].low) / 2) ? downStyle : upStyle }) ``` -------------------------------- ### Example: RSI and MA Line Drawing Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Demonstrates drawing an EMA line using the D.line method. It shows how to define parameters like period and data source, create a style object using S.line, calculate the EMA using F.ema, and then plot the line. ```Python period = I.int(5, title="周期", min=1) source = I.select('close', title="数据源", tip='用来计算的数据', options=SOURCE) emaStyle = S.line('#F00',1, 'solid', title="EMA样式") ema = F.ema(dataList, period, source) D.line(ema,emaStyle) ``` -------------------------------- ### GainLab Script Moving Average (MA) with Full Options Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md A comprehensive example of a Moving Average script, including metadata, user-configurable parameters for period and data source, style customization, calculation, drawing, and outputting tools. ```JavaScript //@name=MA //@title=MovingAverage 移动平均线 //@desc = ## MovingAverage 移动平均线 //作为衡量主力成本重要的参照指标,用以观察价格变动趋势和起到测试压力和支撑的作用。 //当作为趋势判断的时候,周期越长、越有效。 //另一方面,需要注意的是,当均线形成多头排列后,短期均线才是作为持仓的依据。 //@position=main //@version=1 period = I.int(5, title="周期", min=1) source = I.select('close', title="数据源", tip='用来计算的数据', options=SOURCE) maStyle = S.line('#F00',1, 'solid', title="MA样式") ma = F.ma(dataList, period, source) D.line(ma,maStyle) setPrecision('price') O.tools('MA'+period,ma,maStyle) ``` -------------------------------- ### Gainlab Charting: draw.label() Example with Emoji Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Example showing how to use `draw.label()` to display emoji symbols ('✔️' or '❌') based on price comparison, applying specific label styles. ```javascript labelStyle1 = S.label('rgb(0, 255, 255)',12,'left','lowDown',title='看多') labelStyle1.y = -10 labelStyle1.x = 5 labelStyle1.text = '✔️' labelStyle2 = S.label('rgb(255, 0, 0)',12,'left','highUp',title='看空') labelStyle2.y = 10 labelStyle2.x = -5 labelStyle2.text = '❌' D.label(dataList, ({value}) => { return value.open > value.close ? labelStyle1 : labelStyle2 }) ``` -------------------------------- ### Example: Drawing Multiple Lines and Shapes Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Illustrates drawing various chart elements including RSI, MA lines, horizontal lines (buy/sell/zero), and a rectangular area. It showcases the use of different drawing methods (D.line, D.hline, D.srect) and style definitions (S.line, S.area). ```Python rsiPeriod = I.int(14, title="RSI周期", min=1) maPperiod = I.int(10, title="MA周期", min=1) buyLine = I.int(70, title="超买线") sellLine = I.int(30, title="超卖线") rsiStyle = S.line('#FF6D00',title='RSI') maStyle = S.line('#F00',title='MA') buyLineStyle = S.line('#888','dotted',title='超买线') sellLineStyle = S.line('#888','dotted',title='超卖线') zero = S.line('#888','dashed',title='中轴线') full = S.area('rgba(129, 29, 160, 0.1)',title='填充') rsiData = F.rsi(dataList, rsiPeriod) maData = F.ma(rsiData, maPperiod) D.line(rsiData,rsiStyle) D.line(maData,maStyle) D.hline(50, zero) D.hline(buyLine, buyLineStyle) D.hline(sellLine, sellLineStyle) D.srect({x1:'left',y1:buyLine,x2:'right',y2:sellLine},full) ``` -------------------------------- ### Bar Chart with Dynamic Callback Styling Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Explains how to use a callback function to dynamically style bars. The example changes bar color based on the data value relative to zero and adjusts size and visibility. ```javascript // 根据数据值动态调整柱状图颜色 D.bar(data, 0, ({value, index}) => { return { color: value > 0 ? '#00FF00' : '#FF0000', full: 'fill', size: Math.abs(value) / 10, show: value !== 0 } }) ``` -------------------------------- ### Plotting Lines and Areas with Custom Styles Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab官方脚本案例文档 _副本.md This snippet demonstrates how to plot lines and areas with specified colors, styles, and titles. It utilizes functions like S.line and S.area, likely from a charting library, to visualize different data series such as fast and slow upper/lower轨 (tracks) and fill areas. ```Python fastLower = S.line('hsl(194, 100%, 50%)',1, 'solid', title="快线下轨") slowUpper = S.line('rgb(255, 107, 53)',1, 'solid', title="慢线上轨") slowLower = S.line('#FF6B35',1, 'solid', title="慢线下轨") fastFill = S.area('rgba(0, 195, 255, 0.15)', title="快线通道") slowFill = S.area('rgba(255, 107, 53, 0.15)', title="慢线通道") fastHighMA = F.ma(dataList, period1, 'high') fastLowMA = F.ma(dataList, period1, 'low') slowHighMA = F.ma(dataList, period2, 'high') slowLowMA = F.ma(dataList, period2, 'low') D.area(fastHighMA,fastLowMA,fastFill) D.area(slowHighMA,slowLowMA,slowFill) D.line(fastHighMA,fastUpper) D.line(fastLowMA,fastLower) D.line(slowHighMA,slowUpper) D.line(slowLowMA,slowLower) O.tools('快线上轨',fastHighMA,fastUpper) O.tools('快线下轨',fastLowMA,fastLower) O.tools('慢线上轨',slowHighMA,slowUpper) O.tools('慢线下轨',slowLowMA,slowLower) setPrecision('price') ``` -------------------------------- ### Volume and Moving Average Indicator Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab官方脚本案例文档 _副本.md This script plots volume bars with conditional coloring based on price change and overlays multiple moving average lines. It also includes tools for volume and MA display and sets the precision and minimum value for the volume. ```Pine Script buyStyle = S.bar('#2DC08E', 'stroke', title="多柱") sellStyle = S.bar('#F92855', 'fill', title="空柱") volumeData = F.attr(dataList, 'volume') ma1 = F.ma(volumeData, period1) ma2 = F.ma(volumeData, period2) ma3 = F.ma(volumeData, period3) D.bar(volumeData,0, ({value,prev})=>{ return value > prev ? buyStyle : sellStyle }) D.line(ma1,maStyle1) D.line(ma2,maStyle2) D.line(ma3,maStyle3) O.tools('MA'+period1,ma1,maStyle1) O.tools('MA'+period2,ma2,maStyle2) O.tools('MA'+period3,ma3,maStyle3) O.tools('VOL',volumeData,buyStyle) setPrecision('volume') setMin(0) ``` -------------------------------- ### MACD Indicator with Main Chart Signals Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md This example shows how to calculate and display the MACD indicator in a sub-chart, along with buy and sell signals plotted on the main chart. It covers defining styles for MACD lines, bars, and signals, calculating MACD values, and drawing these elements using the `D` and `MD` functions. ```script n = I.int(12, title="快线周期", tip="计算的快线周期", min=1) k = I.int(26, title="慢线周期", tip="计算的慢线周期", min=1) m = I.int(9, title="DEA周期", tip="计算的DEA周期", min=1) // 定义可设置样式 difStyle = S.line('#FF9900', 1, 'solid', title="DIF样式") deaStyle = S.line('#0066CC', 1, 'solid', title="DEA样式") zeroStyle = S.line('#999', 1, 'solid', title="0轴样式") buyMacdUp=S.bar('#2DC08E','stroke',title='多头增加') buyMacdDown=S.bar('#2DC08E','fill',title='多头减少') sellMacdUp=S.bar('#F92855','stroke',title='多头增加') sellMacdDown=S.bar('#F92855','fill',title='多头减少') buyStyle = S.shape('arrowup', 12, '#00ff00', 'lowDown',title='买入信号') sellStyle = S.shape('arrowdown', 12, '#ff0000', 'highUp',title='卖出信号') // 计算数据 macdResult = F.macd(dataList, n, k, m) difData = F.attr(macdResult,'dif') deaData = F.attr(macdResult,'dea') macdData = F.attr(macdResult,'macd') // 绘制0轴 D.hline(0, zeroStyle) // 绘制MACD柱状图(动态颜色) D.bar(macdData, 0, ({value, prev}) => { if (value >= 0) { // 多头 return prev < value ? buyMacdUp : buyMacdDown } else { // 空头 return prev < value ? sellMacdUp : sellMacdDown } }) // 绘制DIF线 D.line(difData, difStyle) // 绘制DEA线 D.line(deaData, deaStyle) // 主图绘制买入信号 buySignals = F.throughUp(difData, deaData) MD.shape(buySignals,buyStyle) // 主图绘制卖出信号 sellSignals = F.throughDown(difData, deaData) MD.shape(sellSignals,sellStyle ) // 设置精度 setPrecision(4) // 输出工具栏提示数据 O.tools('DIF',difData,difStyle) O.tools('DEA',deaData,deaStyle) O.tools('MACD',macdData,{color: '#666666'}) ``` -------------------------------- ### Bollinger Bands and QQE Indicator Calculation (JavaScript) Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab官方脚本案例文档 _副本.md Calculates Bollinger Bands and QQE (Quantitative Qualitative Estimation) indicator values, including upper and lower bands, deviations, and signals based on RSI and QQE trend lines. It then plots these indicators using a charting library. ```javascript const stdev = Math.sqrt(variance / bollingerLength) const deviation = bollingerMultiplier * stdev bollingerDeviation.push(deviation) bollingerUpper.push(basis + deviation) bollingerLower.push(basis - deviation) } }) let primaryQQETrendLine = [] let primaryRSIArr = [] let secondaryQQETrendLine = [] let secondaryRSIArr = [] let upSignal = [] let downSignal = [] let noSignal = [] dataList.forEach((kData,i)=>{ let _primaryRSI = primaryQQE.smoothedRsi[i] - 50 let _secondaryRSI = secondaryQQE.smoothedRsi[i] - 50 let _primaryQQETrendLine = primaryQQE.qqeTrendLine[i] - 50 let _secondaryQQETrendLine = secondaryQQE.qqeTrendLine[i] - 50 let _upSignal = null let _downSignal = null let _noSignal = null if (_secondaryRSI > threshold && _primaryRSI > bollingerUpper[i]) { _upSignal = _secondaryRSI }else if (_secondaryRSI < -threshold && _primaryRSI < bollingerLower[i]) { _downSignal = _secondaryRSI }else{ _noSignal = _secondaryRSI } primaryQQETrendLine.push(_primaryQQETrendLine) primaryRSIArr.push(_primaryRSI) secondaryQQETrendLine.push(_secondaryQQETrendLine) secondaryRSIArr.push(_secondaryRSI) upSignal.push(_upSignal) downSignal.push(_downSignal) noSignal.push(_noSignal) }) D.hline(0, zeroLineStyle) D.bar(upSignal, 0, upSignalStyle) D.bar(downSignal, 0, downSignalStyle) D.bar(noSignal, 0, neutralStyle) D.line(primaryQQETrendLine,primaryLineStyle) D.line(secondaryQQETrendLine, secondaryLineStyle) D.line(bollingerUpper, bollingerUpperStyle) D.line(bollingerLower, bollingerLowerStyle) O.tools('主QQE', primaryQQETrendLine, primaryLineStyle) O.tools('次QQE', secondaryQQETrendLine, secondaryLineStyle) O.tools('布林上轨', bollingerUpper, bollingerUpperStyle) O.tools('布林下轨', bollingerLower, bollingerLowerStyle) O.tools('主RSI', primaryRSIArr, primaryLineStyle) O.tools('次RSI', secondaryRSIArr,secondaryLineStyle) // 设置精度 setPrecision(2) ``` -------------------------------- ### Calculate QQE Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab官方脚本案例文档 _副本.md Calculates the QQE (Quantitative Qualitative Estimation) indicator, which smooths RSI values and uses ATR (Average True Range) to create trend lines and bands. It helps identify trend direction and potential reversals. ```JavaScript function calculateQQE(dataList, rsiLength, smoothingFactor, qqeFactor) { const wildersLength = rsiLength * 2 - 1 const rsiValues = calculateRSI(dataList, rsiLength) const smoothedRsi = calculateEMA(rsiValues, smoothingFactor) const atrRsi = [] for (let i = 0; i < smoothedRsi.length; i++) { if (i === 0) { atrRsi.push(0) } else { atrRsi.push(Math.abs(smoothedRsi[i] - smoothedRsi[i - 1])) } } const smoothedAtrRsi = calculateEMA(atrRsi, wildersLength) const qqeTrendLine = [] const longBand = [] const shortBand = [] let trendDirection = [] for (let i = 0; i < smoothedRsi.length; i++) { const dynamicAtrRsi = smoothedAtrRsi[i] * qqeFactor const newLongBand = smoothedRsi[i] - dynamicAtrRsi const newShortBand = smoothedRsi[i] + dynamicAtrRsi if (i === 0) { longBand.push(newLongBand) shortBand.push(newShortBand) trendDirection.push(1) } else { if (smoothedRsi[i - 1] > longBand[i - 1] && smoothedRsi[i] > longBand[i - 1]) { longBand.push(Math.max(longBand[i - 1], newLongBand)) } else { longBand.push(newLongBand) } if (smoothedRsi[i - 1] < shortBand[i - 1] && smoothedRsi[i] < shortBand[i - 1]) { shortBand.push(Math.min(shortBand[i - 1], newShortBand)) } else { shortBand.push(newShortBand) } if (smoothedRsi[i] > shortBand[i - 1] && smoothedRsi[i - 1] <= shortBand[i - 1]) { trendDirection.push(1) } else if (smoothedRsi[i] < longBand[i - 1] && smoothedRsi[i - 1] >= longBand[i - 1]) { trendDirection.push(-1) } else { trendDirection.push(trendDirection[i - 1]) } } qqeTrendLine.push(trendDirection[i] === 1 ? longBand[i] : shortBand[i]) } return { qqeTrendLine, smoothedRsi } } ``` -------------------------------- ### Bollinger Bands (BOLL) Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab官方脚本案例文档 _副本.md Calculates and plots Bollinger Bands, including upper band (ub), middle band (mid), and lower band (lb). It also defines styles for the bands and the fill area between the upper and lower bands. ```Python ubStyle = S.line('rgb(255, 0, 255)', 1, 'solid', title="上轨") midStyle = S.line('rgb(30, 144, 255)', 1, 'solid', title="中轨") lbStyle = S.line('rgb(255, 0, 255)', 1, 'solid', title="下轨") fullStyle = S.area('rgba(255, 0, 255, 0.1)',false, title="填充") const boll = F.boll(dataList, n, k) const ubData = F.attr(boll,'ub') const midData = F.attr(boll,'mid') const lbData = F.attr(boll,'lb') D.area(ubData,lbData,fullStyle) D.line(ubData, ubStyle) D.line(midData, midStyle) D.line(lbData, lbStyle) O.tools('UB', ubData, ubStyle) O.tools('MID', midData, midStyle) O.tools('LB', lbData, lbStyle) ``` -------------------------------- ### Keltner Channel and Signal Calculation (JavaScript) Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab官方脚本案例文档 _副本.md Calculates Keltner Channels based on middle values, ATR, and multipliers, and identifies buy/sell signals based on Bollinger Bandwidth Ratio (BBR) crossing overbought or oversold levels. It also plots various data series using a 'D' object. ```JavaScript const buySignalData = [] const sellSignalData = [] let pintarojo = 0.0 let pintaverde = 0.0 dataList.forEach((kData,i)=>{ if (i >= Math.max(maLength, atrLength, keltnerLength)) { const _middleValue = middleValues[i] const _atrValue = atrValues[i] middleData.push(_middleValue) upperData.push(_middleValue + _atrValue * atrMultiplierMin) lowerData.push(_middleValue - _atrValue * atrMultiplierMin) upperMaxData.push(_middleValue + _atrValue * atrMultiplierMax) lowerMaxData.push(_middleValue - _atrValue * atrMultiplierMax) const dev = keltnerMultiplier * stdevValues[i] const upper = _middleValue + dev const lower = _middleValue - dev const bbr = (upper - lower) !== 0 ? (sourceValues[i] - lower) / (upper - lower) : 0 if (i > 0) { const prevBasis = middleValues[i - 1] const prevDev = keltnerMultiplier * stdevValues[i - 1] const prevUpper = prevBasis + prevDev const prevLower = prevBasis - prevDev const prevBBR = (prevUpper - prevLower) !== 0 ? (sourceValues[i - 1] - prevLower) / (prevUpper - prevLower) : 0 if (prevBBR > overbought && bbr < overbought) { pintarojo = dataList[i - 1].high } if (prevBBR < oversold && bbr > oversold) { pintaverde = dataList[i - 1].low } const prevPintarojo = resistanceData[i-1] || 0 const prevPintaverde = supportData[i-1] || 0 buySignalData.push(prevPintaverde > 0 && pintaverde !== prevPintaverde ? true : null) sellSignalData.push(prevPintarojo > 0 && pintarojo !== prevPintarojo ? true : null) resistanceData.push(pintarojo > 0 ? pintarojo : null) supportData.push(pintaverde > 0 ? pintaverde : null) }else{ resistanceData.push(null) supportData.push(null) buySignalData.push(null) sellSignalData.push(null) } }else{ middleData.push(null) upperData.push(null) lowerData.push(null) upperMaxData.push(null) lowerMaxData.push(null) resistanceData.push(null) supportData.push(null) buySignalData.push(null) sellSignalData.push(null) } }) D.area(upperMaxData,upperData,upperChannelFill) D.area(lowerMaxData,lowerData,lowerChannelFill) D.line(upperMaxData,upperMax) D.line(upperData,upper) D.line(middleData,middle) D.line(lowerData,lower) D.line(lowerMaxData,lowerMax) D.shape(resistanceData,resistance) D.shape(supportData,support) D.shape(buySignalData,buySignal) D.shape(sellSignalData,sellSignal) ``` -------------------------------- ### TD Sequential Indicator (Pine Script) Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab官方脚本案例文档 _副本.md Implements the TD Sequential indicator, developed by Tom DeMark, to identify potential market turning points. It uses a counting mechanism to generate buy (9) and sell (9) signals, with optional TD Countdown for stronger reversal confirmation. The script includes visual cues like triangles and labels for signals. ```pine-script //@name=TD //@title=Tom DeMark Sequential //@desc=TD Sequential(TD序列指标)是由著名技术分析师汤姆·德马克开发的技术分析工具,通过严格的计数规则识别市场转折点。 //#### 应用法则: //- 当买入设置完成(标记为绿色数字9)时,表示下跌趋势可能结束,是潜在的买入信号。 //- 当卖出设置完成(标记为红色数字9)时,表示上涨趋势可能结束,是潜在的卖出信号。 //- TD倒计时:如果设置完成后市场未反转,则启动倒计时阶段,寻找13个符合条件的交易日,完成后产生更强的反转信号。 //- 信号确认:第9根K线收盘价应突破第6根或第7根K线的高低点,增加信号可靠性。 //- 风险控制:买入信号的止损位设在第9根K线最低价下方,卖出信号的止损位设在第9根K线最高价上方。 //- 适用范围:TD指标适用于所有时间框架和金融品种,在日线图表中表现最佳,特别适合趋势市场中寻找反转机会。 //- 使用要点:必须严格按照规则计数,不能中断;建议结合其他技术指标确认;注意资金管理和风险控制。 //@position=main //@version=1 period = I.int(4, title="周期", min=1) buySignal = S.shape('triangleUp','#4CAF50',8,'fill','lowDown',title='多箭头') sellSignal = S.shape('triangleDown','#F44336',8,'fill','highUp',title='卖箭头') textBuyStyle = S.label('#fff', 12, 'center','lowDown', title='数字多') textBuyBgStyle = S.labelbg('#4CAF50','fill',title='数字背景多') textSellStyle = S.label('#fff', 12, 'center','highUp', title='数字空') textSellBgStyle = S.labelbg('#F44336','fill',title='数字背景空') let tdState = {type:'',count:0} const tdArr = dataList.map((item,index)=>{ let td = {} if (index >= period){ const oclose = dataList[index - period].close if (item.close > oclose) { if (tdState.type === 'up') { tdState.count++ } else { tdState.type = 'up' tdState.count = 1 } } else if (item.close < oclose) { if (tdState.type === 'down') { tdState.count++ } else { tdState.type = 'down' tdState.count = 1 } } else { tdState.type = '' tdState.count = 0 } if (tdState.type !== '') { td.type = tdState.type td.count = tdState.count } } return td }) let buyData = [] let sellData = [] tdArr.forEach(item=>{ if(item && item.count && (item.count === 9 || item.count === 13)){ if(item.type === 'up'){ sellData.push({text:item.count}) buyData.push(null) }else{ sellData.push(null) buyData.push({text:item.count}) } }else{ buyData.push(null) sellData.push(null) } }) D.shape(buyData,buySignal) D.shape(sellData,sellSignal) textBuyStyle.y = 8 textSellStyle.y = -6 textBuyBgStyle.padding = 8 textSellBgStyle.padding = 8 D.label(buyData,textBuyStyle,textBuyBgStyle) D.label(sellData,textSellStyle,textSellBgStyle) ``` -------------------------------- ### Gainlab Style Method Common Parameters Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Details the common 'title' and 'tip' parameters applicable to most style methods for customizing the appearance in the settings panel. ```APIDOC Common Parameters: * title: String, optional. Sets the display name in the settings panel. * tip: String, optional. Provides a tooltip description for the setting. ``` -------------------------------- ### Gainlab Style Method Shorthand and Usage Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Introduces the 'S' shorthand for style methods and explains how style parameters appear in the settings panel. Default values are set by initial values. ```APIDOC style = S.method(default, title, tip) Usage: Style methods define configurable visual properties that appear in the settings panel. Using a regular variable definition (e.g., `color = '#5b8ff9'`) prevents the style from being adjustable in the panel. ``` -------------------------------- ### Basic Bar Chart with Baseline Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Shows how to draw a basic bar chart using `D.bar`. It includes setting a fixed baseline (0), defining a bar style, and plotting volume data. ```javascript // 基础柱状图,基线为0 barStyle = S.bar('#5B8FF9', 'fill', 1, 'solid', title="柱状图样式") D.bar(volume, 0, barStyle) ``` -------------------------------- ### Get Current Timestamp Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Retrieves the current system time as a millisecond timestamp. ```JavaScript U.now() ``` -------------------------------- ### Styling and Drawing Labels Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Demonstrates how to create and apply styles to labels, including text content, font size, color, alignment, and background properties. It shows how to display a label with specific text and background styling on a chart. ```script labelStyle = S.slabel('#fff', 14, 'left', title='标签样式') labelStyle.text = '提示文本' backgroundStyle = S.labelbg('rgba(255, 0, 0, 0.6)', 'fill', title='标签背景') backgroundStyle.padding = 8 backgroundStyle.radius = 2 D.slabel({x:'right',y:'top',text:'提示'}, labelStyle, backgroundStyle) ``` -------------------------------- ### Safely Get Array Value by Index Source: https://github.com/gainlabasher-prog/gainlab-/blob/main/ GainLab脚本语言参考手册_副本.md Retrieves an element from an array at a specified index, returning a default value if the index is out of bounds or the array is invalid. ```JavaScript U.getValue(array, index, defaultValue=null) ```