### Example: Circle Styles Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab脚本语言参考手册_副本.md Provides examples of creating different circle styles using S.scircle. These examples demonstrate setting fill colors, border colors, and titles for the circles. The output shows the resulting style objects. ```APIDOC 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') ``` -------------------------------- ### GainLab Script Input Parameter Examples Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab脚本语言参考手册_副本.md Illustrates how to define user-configurable input parameters in GainLab Script using the `input` method (or its shorthand `I`). Shows examples for integer inputs with specified titles, minimum, and maximum values. ```JavaScript period1 = I.int(10, title='周期1', min=1, max=500) period2 = input.int(20, title='周期2', min=1, max=500) ``` -------------------------------- ### Data Extraction Examples Source: https://github.com/gainlabasher-prog/gainlabscript/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. Also shows how to calculate the MACD indicator. ```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') ``` -------------------------------- ### draw.vline() Examples Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab脚本语言参考手册_副本.md Provides examples of drawing vertical lines: a basic line, multiple lines using an array of X-coordinates, 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 Script BBI Indicator Example Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab脚本语言参考手册_副本.md An example of a BBI (Bull and Bear Index) indicator script in GainLab Script. It demonstrates defining multiple integer inputs for periods, configuring the BBI line style, calculating the BBI by averaging multiple moving averages, and drawing the result. ```JavaScript //@name=BBI //@title=BBI 多空指数 //@desc=BBI(Bull and Bear Index)多空指数 //是一种将不同周期移动平均线加权平均之后的综合指标,属于均线型指标,一般选用3、6、12、24等4个参数。 //它是针对普通移动平均线MA指标的一种改进,既有短期移动平均线的灵敏,又有明显的中期趋势特征。 //@position=main //@version=1 // 输入参数 N1 = I.int(3, title="N1", tip="第一个周期参数") N2 = I.int(6, title="N2", tip="第二个周期参数") N3 = I.int(12, title="N3", tip="第三个周期参数") N4 = I.int(24, title="N4", tip="第四个周期参数") // 样式设置 bbiStyle = S.line('#9933FF', 1, 'solid', title="BBI样式") // 计算BBI // BBI = (MA(CLOSE, N1) + MA(CLOSE, N2) + MA(CLOSE, N3) + MA(CLOSE, N4)) / 4 // 计算四个周期的移动平均线 ma1 = F.ma(dataList, N1, 'close') ma2 = F.ma(dataList, N2, 'close') ma3 = F.ma(dataList, N3, 'close') ma4 = F.ma(dataList, N4, 'close') // 计算BBI = (MA1 + MA2 + MA3 + MA4) / 4 bbi = ma1.map((value, index) => { if (value !== null && ma2[index] !== null && ma3[index] !== null && ma4[index] !== null) { return (value + ma2[index] + ma3[index] + ma4[index]) / 4 } return null }) // 绘制BBI线 D.line(bbi, bbiStyle) // 添加工具提示 O.tools('BBI', bbi, bbiStyle) // 设置精度 setPrecision('price') ``` -------------------------------- ### TB-Trend Indicator Setup Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab官方脚本案例文档 _副本.md Initializes the TB-Trend indicator, which uses fast and slow moving averages to create trend bands. It defines input parameters for the periods of these moving averages and sets the style for the fast upper trend line. ```Gainlabscript //@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="快线上轨") ``` -------------------------------- ### QQE and Bollinger Band Configuration Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab官方脚本案例文档 _副本.md Defines configuration variables for QQE and Bollinger Bands indicators, including lengths, smoothing factors, multipliers, and styling for lines and signals. ```JavaScript 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='中性区域') ``` -------------------------------- ### draw.hline() Examples Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab脚本语言参考手册_副本.md Provides examples of drawing horizontal lines: a basic line, multiple lines using an array of Y-coordinates, 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') ``` -------------------------------- ### draw.label() Example with Styles and Background Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab脚本语言参考手册_副本.md Illustrates creating and applying custom label styles and background styles. It defines styles for 'bullish' and 'bearish' labels and a background, then applies them using a data mapping function. ```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) ``` -------------------------------- ### Example: Shape Styles Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab脚本语言参考手册_副本.md Demonstrates creating shape styles using S.sshape for buy and sell signals. It sets the icon, color, size, fill type, and title for each shape, resulting in distinct style objects for visual representation on the chart. ```APIDOC buySignal = S.sshape('arrowUp','#00FF00',12,'fill',title='支撑区') sellSignal = S.sshape('arrowDown','#FF0000',12,'fill',title='压力区') ``` -------------------------------- ### Style Callback Function Example Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab脚本语言参考手册_副本.md Demonstrates using a style callback function with the D.line method. The callback receives the current value, index, and previous value, allowing dynamic style application based on data conditions. In this example, the line style changes if the current value is greater than or equal to the previous value. ```APIDOC D.line(ma, ({ value, index, prev }) => { return value >= prev ? style1 : style2 }) ``` -------------------------------- ### SAR Example with Conditional Styling Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab脚本语言参考手册_副本.md Demonstrates how to apply different styles based on a condition, using the SAR indicator. It defines styles for 'bullish' and 'bearish' signals and applies them conditionally. ```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 }) ``` -------------------------------- ### Bollinger Bands Indicator Setup Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab官方脚本案例文档 _副本.md Sets up the Bollinger Bands (BOLL) indicator, which uses standard deviation to create price envelopes. It draws three lines: a middle band (moving average), an upper band, and a lower band. The width of the bands reflects price volatility, with widening bands indicating increased volatility and narrowing bands indicating consolidation. The indicator is used to identify overbought and oversold conditions. ```Pine Script //@name=BOLL //@title=Bollinger Bands 布林线 //@desc=BOLL(Bollinger Bands)通过计算价格的"标准差",再求价格的"信赖区间",是一个路径型指标 // 该指标在图形上画出三条线,上下两条线可以分别看成是价格的压力线和支撑线,中间为价格平均线。 // 价格波动在上限和下限的区间之内,价格涨跌幅度加大时,带状区会变宽,涨跌幅度狭小盘整时,带状区会变窄。价超越上限时,代表超买,价格超越下限时,代表超卖。 //#### 应用法则: //- 当布林线的带状区呈水平方向移动时,可以视为处于"常态范围"。 //- 此时,价格向上穿越"上限"时,将形成短期回档,为短线的卖出信号;价格向下穿越"下限"时,将形成短期反弹,为短线的买进时机。 //- 当带状区朝右上方和右下方移动时,则属于脱离常态。 //- 当价格连续穿越"上限",暗示价格将朝上涨方向前进;当价格连续穿越"下限",暗示价格将朝下跌方向前进。 //@position=main //@version=1 setPrecision('price') n = I.int(20, title="N", tip="中轨线周期") k = I.int(2, title="K", tip="标准差倍数") ``` -------------------------------- ### draw.label() Example with Emoji Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab脚本语言参考手册_副本.md Shows how to use emoji as label text. It defines two styles with emoji and applies them based on whether the closing price is greater than the opening price. ```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 }) ``` -------------------------------- ### Volume Analysis with Moving Averages Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab官方脚本案例文档 _副本.md Analyzes trading volume using moving averages and plots them with customizable styles. It also applies volume-based bar coloring and sets precision and minimum values for the volume data. ```Gainlabscript 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) ``` -------------------------------- ### Outputting Data to Debug Panel Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab脚本语言参考手册_副本.md Provides examples of using O.print(), O.warn(), and O.error() to output messages to the script's debug panel. This is useful for debugging and monitoring script execution. ```script ma = F.ma(dataList, 20) O.print('第一个ma的值:' + ma[0]) O.print('脚本已加载') O.warn('警告信息') O.error('错误信息') ``` -------------------------------- ### GainLab Script MA Example Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab脚本语言参考手册_副本.md Example of a Moving Average (MA) indicator script in GainLab Script. It defines input parameters for period and data source, sets the line style, calculates the MA using the formula library, and draws the line on the chart. ```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) ``` -------------------------------- ### Gradient Color Example Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab脚本语言参考手册_副本.md Shows how to apply gradient colors to drawing methods by passing an array of colors to the 'color' property of the style object. The system automatically creates a gradient based on the provided color array. ```APIDOC style = { color: ['#FF0000', '#00FF00', '#0000FF'] } ``` -------------------------------- ### Bollinger Bands (BOLL) Implementation Source: https://github.com/gainlabasher-prog/gainlabscript/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 lines and areas for visualization. ```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) ``` -------------------------------- ### FVG Logic Example Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab官方脚本案例文档 _副本.md A conditional logic snippet that returns different values based on the 'type' and 'filled' properties of a 'value' object. This appears to be part of a larger financial charting script, likely determining which Fair Value Gap (FVG) representation to use based on market conditions. ```javascript if(value.type=== 'buy' ){ return value.filled ? bullishFilledFVG : bullishFVG }else{ return value.filled ? bearishFilledFVG : bearishFVG } }) fvgLabel.y = fvgLabel.size fvgLabel.x = 5 filledFVGLabel.y = fvgLabel.size + filledFVGLabel.size filledFVGLabel.x = filledFVGLabel.size D.label(labelList,fvgLabel,fvgLabelBg) D.shape(filledList,filledFVGLabel) ``` ``` -------------------------------- ### Financial Data Plotting with Lines and Areas Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab官方脚本案例文档 _副本.md This snippet demonstrates how to plot financial data using lines and areas. It defines styles for lines and fills, calculates moving averages for high and low prices, and then plots these elements using a plotting library. ```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') ``` -------------------------------- ### Calculate QQE Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab官方脚本案例文档 _副本.md Calculates the QQE (Quantitative Qualitative Estimation) indicator, which smooths RSI and uses ATR to determine trend lines and direction. It returns the QQE trend line and smoothed RSI values. ```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 } } ``` -------------------------------- ### Calculate RSI Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab官方脚本案例文档 _副本.md Calculates the Relative Strength Index (RSI) for a given list of data points over a specified period. Handles initial values by setting them to 50. ```JavaScript function calculateRSI(dataList, period) { const rsiValues = [] for (let i = 0; i < dataList.length; i++) { if (i < period) { rsiValues.push(50) continue } let gains = 0 let losses = 0 for (let j = i - period + 1; j <= i; j++) { const change = dataList[j].close - dataList[j - 1].close if (change > 0) { gains += change } else { losses += Math.abs(change) } } const avgGain = gains / period const avgLoss = losses / period if (avgLoss === 0) { rsiValues.push(100) } else { const rs = avgGain / avgLoss const rsi = 100 - (100 / (1 + rs)) rsiValues.push(rsi) } } return rsiValues } ``` -------------------------------- ### SAR (Stop and Reverse) Indicator Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab官方脚本案例文档 _副本.md Implements the SAR (Stop and Reverse) indicator, which identifies potential turning points in price. It uses customizable parameters for acceleration factor (startAf, step, maxAf) and defines styles for bullish and bearish signals. ```PineScript //@name=SAR //@title=SAR 停损点转向指标 //@desc=## SAR 停损点转向指标 //SAR(Stop and Reverse)是应买进或抛售价位的转向点,此种技术分析工具与移动平均线原理颇为相似,属于价格与时间并重的分析工具。 //由于组成该线的点以弧形的方式移动,故称抛物转向。 //### 应用法则: //- 价格曲线在SAR曲线之上时,为多头市场。 //- 价格曲线在SAR曲线之下时,为空头市场。 //- 价格曲线由上向下跌破SAR曲线时,为卖出信号并应同时放空。 //- 价格曲线由下向上穿破SAR曲线时,为买入信号并应同时补空。 //- 专门使用SAR操作时,不必参考其他指标,放弃自己人性的研判,采取突破买进与跌破卖出的手法。 //- SAR不求买在最低价, 卖在最高价,却注重一段时期内的绝对获利率。 //- 注意,SAR 在盘整时失效。 //@position=main //@version=1 startAf = I.int(2,title='开始') step = I.int(2,title='增量') maxAf = I.int(20,title='极限') 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 }) O.tools('SAR',sar,upStyle) setPrecision('price') ``` -------------------------------- ### GainLab Script MA Indicator with Full Metadata Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab脚本语言参考手册_副本.md A comprehensive Moving Average (MA) indicator script example for GainLab Script. It includes detailed metadata (@name, @title, @desc, @position, @version), input definitions for period and data source, style configuration, MA calculation, drawing, and outputting tooltips. ```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) ``` -------------------------------- ### VOLUME (VOL) Indicator with Moving Averages Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab官方脚本案例文档 _副本.md Calculates and plots Volume (VOL) along with three moving average lines (MA1, MA2, MA3) for trend analysis. It defines styles for each moving average line. ```PineScript //@name=VOL //@title=VOLUME 成交量 //@desc = ## VOLUME 成交量 //成交量是指在某一时段内具体的交易数,将一定时期内的成交量相加后平均,在成交量的柱条图中形成较为平滑的曲线,即均量线。 //均量线反映的仅是市场交投的主要趋向,对未来价格变动的大势起着辅助指标的作用。 //#### 应用法则: //- 价格随成交量的递增而上涨,为市场行情的正常特性,此种量增价涨的关系,表示价格将继续上升。 //- 价格下跌,向下跌破价格形态、趋势线、移动平均线,同时出现大成交量是价格将深幅下跌的信号,强调趋势的反转。 //- 价格随着缓慢递增的成交量而逐渐上涨,渐渐的走势突然成为垂直上升的爆发行情,成交量急剧增加,价格爆涨,紧接着,成交量大幅萎缩,价格急剧下跌,表示涨势已到末期,有转势可能。 //- 温和放量。成交量在前期持续低迷之后,出现连续温和放量形态,一般可以证明有实力资金在介入。但这并不意味着投资者就可以马上介入,在底部出现温和放量之后,价格会随量上升,量缩时价格会适量调整。当持续一段时间后,价格的上涨会逐步加快。 //- 突放巨量。这其中可能存在多种情况,如果价格经历了较长时间的上涨过程后放巨量,通常表明多空分歧加大,有实力资金开始派发,后市继续上涨将面临一定困难。 //- 而经历了深幅下跌后的巨量一般多为空方力量的最后一次集中释放,后市继续深跌的可能性很小。 //- 如果市场整体下跌,而某个币种逆势放量,在市场一片喊空声之时放量上攻,往往持续时间不长,随后反而加速下跌。 //- 成交量也有形态,当成交量构筑圆弧底,而价格也形成圆弧底时,往往表明后市将出现较大上涨机会。 //@position=vice //@version=1 period1 = I.int(5, title="平均线周期1", min=1) period2 = I.int(10, title="平均线周期2", min=1) period3 = I.int(20, title="平均线周期3", min=1) maStyle1 = S.line('#F00',1, 'solid', title="MA1样式") maStyle2 = S.line('#FF0',1, 'solid', title="MA2样式") maStyle3 = S.line('#0FF',1, 'solid',false, title="MA3样式") ``` -------------------------------- ### Example: RSI and MA Plotting Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab脚本语言参考手册_副本.md Illustrates plotting RSI and its Moving Average (MA) using Gainlab Script. It defines parameters for RSI period and source, MA period, and styles for the lines. It then calculates RSI and MA using F.rsi and F.ma, and plots them using D.line. Horizontal lines for overbought, oversold, and a midpoint are also drawn using D.hline. ```APIDOC 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) ``` -------------------------------- ### ZigZag Indicator Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab官方脚本案例文档 _副本.md Implements the ZigZag indicator to identify significant turning points in price. It calculates zigzag lines based on depth, deviation, and backstep parameters, and visualizes uptrends and downtrends with distinct line styles. ```Gainlabscript //@name=ZigZag //@title=ZigZag指标 //@desc=ZigZag 指标用于识别价格重要转折点。 //Depth: 寻找极值的深度 //Deviation: 最小偏差点数 //Backstep: 回退步数。 //@position=main //@version=1 // 设置精度 setPrecision('price') // 输入参数 depth = I.int(12, title="Depth", tip="寻找极值的深度") deviation = I.int(5, title="Deviation", tip="最小偏差点数") backstep = I.int(2, title="Backstep", tip="回退步数") buyStyle = S.line('#00FF00', 2, 'solid', true, title="多头样式") sellStyle = S.line('#FF0000', 2, 'solid', true, title="空头样式") buyStyle.continuous = true sellStyle.continuous = true function calculateZigZag(dataList, depth, deviation, backstep) { const len = dataList.length const zigzagBuffer = new Array(len).fill(0) const highBuffer = new Array(len).fill(0) const lowBuffer = new Array(len).fill(0) for (let i = depth; i < len - depth; i++) { const currentHigh = dataList[i].high let isHighPoint = true for (let j = i - depth; j <= i + depth; j++) { if (j !== i && dataList[j].high >= currentHigh) { isHighPoint = false break } } if (isHighPoint) highBuffer[i] = currentHigh const currentLow = dataList[i].low let isLowPoint = true for (let j = i - depth; j <= i + depth; j++) { if (j !== i && dataList[j].low <= currentLow) { isLowPoint = false break } } if (isLowPoint) lowBuffer[i] = currentLow } let whatlookfor = 0, back = 0, pos = 0, val = 0, res = 0, extreme = 0 let foundPoints = 0 for (let i = 0; i < len; i++) { if (whatlookfor === 0) { if (highBuffer[i] > 0) { val = highBuffer[i] if (val === dataList[i].high) { zigzagBuffer[i] = dataList[i].high whatlookfor = -1 pos = i extreme = val foundPoints++ } } if (lowBuffer[i] > 0) { val = lowBuffer[i] if (val === dataList[i].low) { zigzagBuffer[i] = dataList[i].low whatlookfor = 1 pos = i extreme = val foundPoints++ } } } else if (whatlookfor === 1) { if (highBuffer[i] > 0) { val = highBuffer[i] if (val > extreme && val === dataList[i].high) { zigzagBuffer[i] = dataList[i].high whatlookfor = -1 pos = i extreme = val foundPoints++ } } if (lowBuffer[i] > 0) { val = lowBuffer[i] if (val < extreme && val === dataList[i].low) { back = pos pos = i extreme = val res = val if (i - back >= backstep) { zigzagBuffer[i] = dataList[i].low whatlookfor = 1 foundPoints++ } else { } } } } else if (whatlookfor === -1) { if (lowBuffer[i] > 0) { val = lowBuffer[i] if (val < extreme && val === dataList[i].low) { zigzagBuffer[i] = dataList[i].low whatlookfor = 1 pos = i extreme = val foundPoints++ } } if (highBuffer[i] > 0) { val = highBuffer[i] if (val > extreme && val === dataList[i].high) { back = pos pos = i extreme = val res = val if (i - back >= backstep) { zigzagBuffer[i] = dataList[i].high whatlookfor = -1 foundPoints++ } else { } } } } } const result = zigzagBuffer.map(val => val !== 0 ? val : null) return result } zigzagData = calculateZigZag(dataList, depth, deviation, backstep) D.line(zigzagData, ({ value, prevValid }) => { if (prevValid !== null) { if (value > prevValid) { return buyStyle } else if (value < prevValid) { return sellStyle } } return buyStyle }) ``` -------------------------------- ### Calculate Bollinger Bands Source: https://github.com/gainlabasher-prog/gainlabscript/blob/main/ GainLab官方脚本案例文档 _副本.md Calculates the basis (simple moving average), deviation, upper band, and lower band for Bollinger Bands using the QQE trend line data. It requires the Bollinger Band length and multiplier. ```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) } ```