### GainLab Script MA Example Source: https://pro.gainlab.ai/glscriptapi/index 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 F.ma function, draws the line, and outputs the result. ```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) ``` -------------------------------- ### GLScript SAR Visualization Source: https://pro.gainlab.ai/glscriptapi/index Example of visualizing the Stop and Reverse (SAR) indicator using custom styles based on price action. It demonstrates conditional styling for upward and downward trends. ```GLScript 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 }) ``` -------------------------------- ### GainLab Script MA Indicator with Full Metadata Source: https://pro.gainlab.ai/glscriptapi/index A comprehensive example of a Moving Average (MA) indicator script in GainLab Script, including detailed metadata like title, description, position, and version. It demonstrates input parameter configuration, data source selection, line styling, MA 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 Script Metadata Definition Source: https://pro.gainlab.ai/glscriptapi/index Explanation of metadata tags used in GainLab Script, which are defined using comments starting with //@. These tags provide information such as script name, title, description, display position, and engine version. ```APIDOC // @name = MA // @title = MovingAverage // @desc = MA(MovingAverage)作为衡量主力成本重要的参照指标,是主力成本的平均值。 // 第二行描述 // ...第N行描述 // @position = main // @version = 1 ``` -------------------------------- ### APIDOC: GainLab Script Utility Functions Source: https://pro.gainlab.ai/glscriptapi/index Provides documentation for various utility functions available in GainLab Script, aliased as 'U'. These functions cover number formatting, percentage formatting, price formatting, scientific notation conversion, modulo operations, array statistics, sorting, deep cloning, merging, color conversion, date formatting, and getting the current timestamp. ```APIDOC util.formatNumber(value, precision=2, useThousandSeparator=false) - Formats a number with optional thousand separators and specified decimal precision. - Parameters: - value: The number to format. - precision: The number of decimal places (default: 2). - useThousandSeparator: Whether to use thousand separators (default: false). - Returns: Formatted string. - Example: U.formatNumber(123456.789, 2, true) // "123,456.79" U.formatNumber(12.3, 4) // "12.3000" util.formatPercent(value, precision=2) - Formats a decimal value as a percentage. - Parameters: - value: The decimal value (e.g., 0.1234). - precision: The number of decimal places (default: 2). - Returns: Formatted percentage string. - Example: U.formatPercent(0.1267, 1) // "12.7%" util.formatPrice(value, precision=2) - Formats a price to a fixed number of decimal places. - Parameters: - value: The numerical price. - precision: The number of decimal places (default: 2). - Returns: Formatted price string. - Example: U.formatPrice(1234.5, 2) // "1234.50" util.toNumber(val) - Converts a value (string or number) that may be in scientific notation to a standard number. - Parameters: - val: The input value, potentially in scientific notation. - Returns: The numerical value. - Example: U.toNumber('1e-8') // 0.00000001 U.toNumber('123.45') // 123.45 util.mod(a, b) - Calculates the modulo of two numbers. - Parameters: - a: The dividend. - b: The divisor (modulus). - Returns: The numerical result of the modulo operation. - Example: U.mod(7, 3) // 1 util.average(array) - Calculates the average of an array of numbers. - Parameters: - array: An array of numbers. - Returns: The average of the numbers in the array. Returns 0 for an empty array. - Example: U.average([1,2,3]) // 2 util.sum(array) - Calculates the sum of an array of numbers. - Parameters: - array: An array of numbers. - Returns: The sum of the numbers in the array. - Example: U.sum([1,2,3]) // 6 util.unique(array) - Removes duplicate elements from an array (shallow comparison). - Parameters: - array: An array of any type. - Returns: A new array with unique elements. - Example: U.unique([1,1,2,2,3]) // [1,2,3] util.sort(array, ascending=true) - Sorts an array of numbers. - Parameters: - array: An array of numbers. - ascending: Whether to sort in ascending order (default: true). - Returns: A new sorted array. - Example: U.sort([3,1,2]) // [1,2,3] U.sort([3,1,2], false) // [3,2,1] util.getValue(array, index, defaultValue=null) - Safely retrieves an element from an array at a specified index. - Parameters: - array: The array to access. - index: The index of the element to retrieve. - defaultValue: The value to return if the index is out of bounds (default: null). - Returns: The element at the specified index or the default value. - Example: U.getValue([10], 3, 0) // 0 util.isValidNumber(value) - Checks if a value is a finite, valid number. - Parameters: - value: Any value to check. - Returns: Boolean. - Example: U.isValidNumber(1/0) // false U.isValidNumber('123') // false U.isValidNumber(123.456) // true util.isEmpty(value) - Checks if a value is null, undefined, or an empty string. - Parameters: - value: Any value to check. - Returns: Boolean. - Example: U.isEmpty('') // true U.isEmpty(null) // true U.isEmpty('text') // false util.random(min=0, max=1) - Returns a random floating-point number within the range [min, max). - Parameters: - min: The minimum value (inclusive, default: 0). - max: The maximum value (exclusive, default: 1). - Returns: A random number. - Example: U.random(10, 20) // Random float between 10 and 20 util.randomInt(min, max) - Returns a random integer within the range [min, max] (inclusive of both endpoints). - Parameters: - min: The minimum integer value. - max: The maximum integer value. - Returns: A random integer. - Example: U.randomInt(1, 6) // Random integer between 1 and 6 util.clamp(value, min, max) - Clamps a value within a specified range [min, max]. - Parameters: - value: The number to clamp. - min: The minimum allowed value. - max: The maximum allowed value. - Returns: The clamped numerical value. - Example: U.clamp(120, 0, 100) // 100 util.lerp(start, end, t) - Performs linear interpolation between two values. - Parameters: - start: The starting value. - end: The ending value. - t: The interpolation factor, typically between 0 and 1. - Returns: The interpolated numerical value. - Example: U.lerp(0, 10, 0.3) // 3 util.toRadians(degrees) - Converts an angle from degrees to radians. - Parameters: - degrees: The angle in degrees. - Returns: The angle in radians. - Example: U.toRadians(180) // 3.1415926... util.toDegrees(radians) - Converts an angle from radians to degrees. - Parameters: - radians: The angle in radians. - Returns: The angle in degrees. - Example: U.toDegrees(Math.PI) // 180 util.distance(x1, y1, x2, y2) - Calculates the Euclidean distance between two points (x1, y1) and (x2, y2). - Parameters: - x1, y1: Coordinates of the first point. - x2, y2: Coordinates of the second point. - Returns: The distance between the two points. - Example: U.distance(0,0,3,4) // 5 U.formatDate(timestamp, format='YYYY-MM-DD') - Formats a timestamp into a string based on the provided format. - Parameters: - timestamp: The timestamp in milliseconds. - format: The format string (supports YYYY, MM, DD, HH, mm, ss, default: 'YYYY-MM-DD'). - Returns: Formatted date string. - Example: U.formatDate(1700000000000, 'YYYY-MM-DD HH:mm:ss') U.now() - Returns the current timestamp in milliseconds. - Returns: Numerical timestamp. - Example: ts = U.now() util.deepClone(obj) - Creates a deep copy of an object, array, or Date. - Parameters: - obj: The object, array, or Date to clone. - Returns: A new, deeply cloned object/array. - Example: copy = U.deepClone({a:1,b:[2,3]}) util.merge(target, source) - Performs a shallow or deep merge of properties from a source object into a target object. - Parameters: - target: The object to merge into. - source: The object to merge from. - Returns: The merged object. - Example: U.merge({a:1, b:{x:1}}, {b:{y:2}, c:3}) // {a:1, b:{x:1,y:2}, c:3} util.colorToRgb(color) - Converts a color string (e.g., hex) to an RGB object. - Parameters: - color: The color string. - Returns: An RGB object with r, g, b properties. - Example: U.colorToRgb('#ff0000') // {r:255, g:0, b:0} util.colorToRgba(color) - Converts a color string to an RGBA string format. - Parameters: - color: The color string. - alpha: The alpha value (transparency). - Returns: An RGBA string (e.g., 'rgba(r,g,b,a)'). - Example: // Assuming a function signature like U.colorToRgba(color, alpha) // U.colorToRgba('#ff0000', 0.5) // 'rgba(255,0,0,0.5)' ``` -------------------------------- ### Conditional Bar Chart Styling Source: https://pro.gainlab.ai/glscriptapi/index Illustrates how to apply dynamic styles to bar charts using callback functions. This example changes bar color and size based on data values and demonstrates using array baselines with conditional styling. ```GLScript // 根据数据值动态调整柱状图颜色 D.bar(data, 0, ({value, index}) => { return { color: value > 0 ? '#00FF00' : '#FF0000', full: 'fill', size: Math.abs(value) / 10, show: value !== 0 } }) // 结合数组基线的回调样式 baselineArray = [0, 10, 20, 30] D.bar(data, baselineArray, ({value, index}) => { return { color: value > baselineArray[index] ? '#00FF00' : '#FF0000', full: 'stroke', size: 2, style: 'dashed' } }) ``` -------------------------------- ### GLScript Emoji Label Drawing Source: https://pro.gainlab.ai/glscriptapi/index Example of drawing labels with emoji characters on a GLScript chart. It shows how to set emoji as text content for labels and apply conditional styling. ```GLScript 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 }) ``` -------------------------------- ### GainLab Script BBI Indicator Source: https://pro.gainlab.ai/glscriptapi/index Example of a BBI (Bull and Bear Index) indicator script in GainLab Script. It calculates BBI using four different moving average periods and plots the result. The script includes input parameters for the periods, style settings, and uses the F.ma function for calculations. ```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') ``` -------------------------------- ### Rectangle Drawing Source: https://pro.gainlab.ai/glscriptapi/index Describes the `D.rect()` function for drawing rectangles on the chart. It allows specifying rectangle data (start, end, indices) and applying customizable styles, including fill and stroke options. ```GLScript rectStyle = S.rect('#5B8FF9', 'fill', title="矩形样式") rectData = dataList.map((kData,index)=>{ if(kData.close < kData.open) return null return { start: kData.open, end: kData.close, startIndex: index, endIndex:index + 1 } }) D.rect(rectData, rectStyle) ``` -------------------------------- ### Formula Methods (F) Source: https://pro.gainlab.ai/glscriptapi/index Provides access to a library of common formula algorithms to simplify calculations within scripts, reducing the need for manual implementation of standard formulas. ```GLScript F.attr(list, attr) ``` -------------------------------- ### GainLab Script Input Parameter Definition (Integer) Source: https://pro.gainlab.ai/glscriptapi/index Documentation for the `input.int()` or `I.int()` method in GainLab Script, used to define integer input parameters that can be configured in the settings panel. It covers syntax, parameters (default, title, min, max, tip), and return value. ```APIDOC val = I.int(default, title, min, max, tip) Parameters: default: Initial value (required, integer) title: Display name in the panel (optional, defaults to variable name) min: Minimum value (optional) max: Maximum value (optional) tip: Tooltip description (optional) Returns: Integer value. Example: len = I.int(14, title='周期', min=1, max=500) ``` -------------------------------- ### Background and Text Styling Source: https://pro.gainlab.ai/glscriptapi/index Defines styles for background elements including color, fill, size, border style, padding, radius, and visibility. Also specifies text priority between style objects and data objects. ```GLScript 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) ``` -------------------------------- ### GainLab Script Input Parameter Usage Source: https://pro.gainlab.ai/glscriptapi/index Demonstrates how input parameters defined using `I.int()` or `input.int()` are displayed and used in the GainLab Script settings panel. It shows how user-modified values are injected back into script variables. ```JavaScript period1 = I.int(10, title='周期1', min=1, max=500) period2 = input.int(20, title='周期2', min=1, max=500) ``` -------------------------------- ### GLScript: Get Highest Value Source: https://pro.gainlab.ai/glscriptapi/index Finds the highest value in a list of numbers or objects, returning its index and value. If the list contains objects, the 'attr' parameter specifies the property to use for comparison. ```GLScript F.high(list, attr) ``` -------------------------------- ### GLScript: Get Lowest Value Source: https://pro.gainlab.ai/glscriptapi/index Finds the lowest value in a list of numbers or objects, returning its index and value. If the list contains objects, the 'attr' parameter specifies the property to use for comparison. ```GLScript F.low(list, attr) ``` -------------------------------- ### GLScript: Get Trend Direction Source: https://pro.gainlab.ai/glscriptapi/index Determines the trend direction ('up', 'down', 'sideways') based on short and long periods of source data (defaulting to 'close'). Returns an array of trend directions or null. ```GLScript F.getTrend(list, short, long, source) ``` -------------------------------- ### Conditional Line Styling Source: https://pro.gainlab.ai/glscriptapi/index Demonstrates how to apply different line styles based on data values. It shows how to set up line styles and use a callback function to dynamically choose between them. ```GLScript period = I.int(5, title="周期", min=1) source = I.select('close', title="数据源", tip='用来计算的数据', options=SOURCE) maStyle1 = S.line('#F00',2, 'solid', title="MA样式1") maStyle2 = S.line('#0FF',1, 'solid', title="MA样式2") ma = F.ma(dataList, period, source) D.line(ma,({value,index}){ return value >= dataList[index].close ? maStyle1 : maStyle2 }) ``` -------------------------------- ### Through Up Detection Source: https://pro.gainlab.ai/glscriptapi/index Detects when the first data series crosses above the second data series. It can compare a list against another list or a single value. Returns an object with crossover details or null. Includes examples for MA crossing MA and RSI crossing a threshold. ```GLScript F.throughUp(list1, list2, attr) ``` -------------------------------- ### Style Callbacks and Gradient Colors Source: https://pro.gainlab.ai/glscriptapi/index Explains how to use style callback functions for conditional styling in K-line coordinate system drawing methods. It also details how to use gradient colors by providing an array of colors to the 'color' parameter. ```GLScript D.line(ma, ({ value, index, prev }) => { return value >= prev ? style1 : style2 }) ``` -------------------------------- ### Through Down Detection Source: https://pro.gainlab.ai/glscriptapi/index Detects when the first data series crosses below the second data series. It can compare a list against another list or a single value. Returns an object with crossover details or null. Includes an example for RSI crossing a threshold downwards. ```GLScript F.throughDown(list1, list2, attr) ``` -------------------------------- ### GainLab Script Input Parameter Definition (Float) Source: https://pro.gainlab.ai/glscriptapi/index Documentation for the `input.float()` or `I.float()` method in GainLab Script, used to define floating-point input parameters that can be configured in the settings panel. It covers syntax, parameters (default, title, min, max, tip), and return value. ```APIDOC val = I.float(default, title, min, max, tip) Parameters: default: Initial value (required, numeric) title: Display name in the panel (optional) min: Minimum value (optional) max: Maximum value (optional) tip: Tooltip description (optional) Returns: Floating-point value. Example: val = I.float(0.5, title='Multiplier', min=0.1, max=10.0) ``` -------------------------------- ### Cross Detection Source: https://pro.gainlab.ai/glscriptapi/index Detects when one data series crosses another. It can compare a list against another list or a single value. Returns an object with crossover details or null. Includes examples for MA crossing close price and MACD crossing zero. ```GLScript F.cross(list1, list2 | number, attr) ``` -------------------------------- ### Output to Toolbar (O.tools) Source: https://pro.gainlab.ai/glscriptapi/index Outputs data to the script's toolbar, displayed next to the name and operation buttons. Supports array, string, or numeric data with optional styling. ```GLScript style1 = S.line('#F00',1,title='ma1样式') ma2Color = S.color('#00F',title='ma2颜色') ma1 = F.ma(dataList, 20) ma2 = F.ma(dataList, 50) setPrecision('price') O.tools('MA20', ma1, style1) O.tools('MA50', ma2, {color:ma2Color}) // 显示字符串或数值 O.tools('POC', '123.45', pocTextStyle) O.tools('Status', 'Active', statusStyle) O.tools('Price', 100.50, priceStyle) ``` -------------------------------- ### GLScript Input: Select Variable Source: https://pro.gainlab.ai/glscriptapi/index Defines a select input variable, presenting a dropdown list of options to the user. Requires a default value and a list of options. ```GLScript src = I.select('close', options=SOURCE, title='数据源') ``` ```GLScript maType = I.select('MA', options=['MA','EMA','SMA','WMA'], title='均线计算方式') ``` -------------------------------- ### GLScript: Improved Kijun-sen (kijunV2) Source: https://pro.gainlab.ai/glscriptapi/index Calculates an improved version of the Kijun-sen (Ichimoku Cloud variant) using a list of k-line data, a period, and a factor. Returns null for insufficient data or invalid values. ```GLScript F.kijunV2(list, period, factor) ``` -------------------------------- ### GLScript: Volume Ratio (VR) Source: https://pro.gainlab.ai/glscriptapi/index Calculates the Volume Ratio (VR) indicator for k-line data over a specified period. Returns null for periods with insufficient data or invalid values. ```GLScript F.vr(list, period) ``` -------------------------------- ### GLScript Input: Boolean Variable Source: https://pro.gainlab.ai/glscriptapi/index Defines a boolean input variable with a default value, title, and tooltip. Allows users to toggle settings. ```GLScript smooth = I.bool(true, title='是否平滑计算') ``` -------------------------------- ### Output to Debug Panel (O.print, O.warn, O.error) Source: https://pro.gainlab.ai/glscriptapi/index Outputs messages to the debug panel for script development and debugging. Supports printing regular messages, warnings, and errors. ```GLScript ma = F.ma(dataList, 20) O.print('第一个ma的值:' + ma[0]) O.print('脚本已加载') O.warn('警告信息') O.error('错误信息') ``` -------------------------------- ### GLScript: Market Facilitator Index / Money Flow (MF) Source: https://pro.gainlab.ai/glscriptapi/index Calculates the Market Facilitator Index (MF), similar to Money Flow. Returns an array, with the first element being null. ```GLScript F.mf(list) ``` -------------------------------- ### GLScript: On Balance Volume (OBV) Source: https://pro.gainlab.ai/glscriptapi/index Calculates the On Balance Volume (OBV) indicator for k-line data. Returns null for periods with insufficient data or invalid values. ```GLScript F.obv(list) ``` -------------------------------- ### GainLab Script: MA and RSI Calculation Source: https://pro.gainlab.ai/glscriptapi/index Demonstrates how to select Moving Average (MA) and Relative Strength Index (RSI) types, set parameters like period and length, and then call the appropriate calculation functions. The results are then plotted as lines on a chart. ```GLScript // 用户在面板选择 maType = I.select('MA', options=['MA','EMA','WMA','RMA'], title='均线类型') len = I.int(20, title='周期') // 统一调用 avg = F.call(maType, dataList, len, 'close') D.line(avg, S.line('#5b8ff9',1)) // 切换动量类 oscType = I.select('RSI', options=['RSI','CCI'], title='振荡指标') osc = F.call(oscType, dataList, 14) D.line(osc, S.line('#faad14',1)) ``` -------------------------------- ### GLScript Screen Circle Styling Source: https://pro.gainlab.ai/glscriptapi/index Defines styling for screen-relative circles. It allows customization of color and border properties through a user interface panel. ```APIDOC S.scircle(color, border, size, style, show, title, tip) Parameters: * color: Fill color. String, optional. * border: Border color. String, optional. * size: Border line width. Integer, optional. * style: Border line style. String, optional. * show: Whether to display the circle. Boolean, optional, default: true. * title: Text label for the style. String, optional. * tip: Tooltip description. String, optional. ``` -------------------------------- ### GLScript Style: Line Combination Source: https://pro.gainlab.ai/glscriptapi/index Defines a combined line style variable, including color, width, style, and visibility. Presented as a group of selectors in the settings panel. ```GLScript lineStyle = S.line('#5b8ff9', 1, 'solid',title='主线') ``` -------------------------------- ### Commodity Channel Index (CCI) Calculation Source: https://pro.gainlab.ai/glscriptapi/index Calculates the Commodity Channel Index (CCI). Requires a list of k-line data and a period. Returns null for insufficient periods or invalid values. ```GLScript F.cci(list, period) ``` -------------------------------- ### Continuous Line Drawing Source: https://pro.gainlab.ai/glscriptapi/index Explains how to draw continuous lines by setting the `continuous` property to `true`. This feature connects valid data points, and the callback provides `prevValid` for reference. ```GLScript buyStyle = S.line('#00FF00', 2, 'solid', true, title="多头样式") sellStyle = S.line('#FF0000', 2, 'solid', true, title="空头样式") buyStyle.continuous = true sellStyle.continuous = true D.line(zigzagData, ({ value, prevValid }) => { if (prevValid !== null) { if (value > prevValid) { return buyStyle } else if (value < prevValid) { return sellStyle } } return buyStyle }) ``` -------------------------------- ### GLScript: Ease of Movement (EMV) Source: https://pro.gainlab.ai/glscriptapi/index Calculates the Ease of Movement (EMV) indicator for k-line data over a specified period. Returns null for periods with insufficient data or invalid values. ```GLScript F.emv(list, period) ``` -------------------------------- ### GLScript Style: Size Variable Source: https://pro.gainlab.ai/glscriptapi/index Defines a size input variable, displayed as a number selector in the settings panel. Accepts integer values. ```GLScript size = S.size(20,title='尺寸') ``` -------------------------------- ### GLScript: Money Flow Source: https://pro.gainlab.ai/glscriptapi/index Calculates Money Flow for k-line data. Returns null for invalid values. ```GLScript F.moneyFlow(list) ``` -------------------------------- ### Draw Methods - Coordinate Systems and Usage Source: https://pro.gainlab.ai/glscriptapi/index Explains the draw methods (aliased as D) for drawing graphics on charts. It differentiates between K-line coordinate system (graphics follow K-lines) and chart coordinate system (graphics are fixed to the chart). ```GLScript 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) ``` -------------------------------- ### GLScript Line Style Source: https://pro.gainlab.ai/glscriptapi/index Defines the style for lines, including color, size, and style. It can also include a title and tooltip for display in settings panels. ```GLScript ln1 = S.line('#5b8ff9', 1, 'solid', title='线条1', tip='线条样式') ln2 = S.line('rgb(255,255,255)',title='线条2') ``` ```GLScript ln1 = {"color":"#5b8ff9","size":1,"style":"solid","show":true}; ln2 = {"color":"rgb(255,255,255)","size":1,"style":"solid","show":true}; ``` -------------------------------- ### GLScript Screen Rectangle Styling Source: https://pro.gainlab.ai/glscriptapi/index Defines styling for screen-relative rectangles. It is similar to style.rect() but includes 'border' properties for draw.srect(). It allows customization of fill color, border color, border width, and border style. ```APIDOC S.srect(color, border, size, style, show, title, tip) Parameters: * color: Fill color. String, optional. * border: Border color. String, optional, format: border='color_value'. * size: Border line width. Integer, optional, default: 1 (effective only if border is set). * style: Border line style. String, optional, default: 'solid' (effective only if border is set). * show: Whether to display the rectangle. Boolean, optional, default: true. * title: Text label for the style. String, optional. * tip: Tooltip description. String, optional. Notes: 'color' is the fill color; if not set, only the border is displayed. 'border' sets the border color; if not set, the border is not displayed. Returns: Label style object. Example: srect1 = S.srect('rgba(255,0,0,0.1)', 1, title='矩形1') srect2 = S.srect('rgba(255,0,0,0.1)', border='#F00',1, title='矩形2') srect3 = S.srect(border='#F00', 1, title='矩形3') Returned values: srect1 = {"color":"rgba(255,0,0,0.1)","border":null,"size":1,"style":"solid","show":true}; srect2 = {"color":"rgba(255,0,0,0.1)","border":"#F00","size":1,"style":"solid","show":true}; srect3 = {"color":null,"border":"#F00","size":1,"style":"solid","show":true}; ``` -------------------------------- ### GLScript: Dynamic Function Call (F.call) Source: https://pro.gainlab.ai/glscriptapi/index Dynamically calls GLScript library functions by their method name string. This allows for flexible selection of algorithms (e.g., different moving average types) and unified calling logic. Arguments are passed sequentially as they would be to the target function. ```GLScript F.call(methodName, ...args) ``` -------------------------------- ### Area Fill Drawing Source: https://pro.gainlab.ai/glscriptapi/index Explains the `D.area()` function for filling the area between two data series. It supports basic area styling and advanced styling through callback functions, providing access to current and previous values. ```GLScript // 基础区域填充 areaStyle = S.area('#5B8FF9', title="区域样式") D.area(F.ma(dataList, 20), F.ma(dataList, 60), areaStyle) // 使用回调样式与自定义样式对象 D.area(data1, data2, ({value,value2}) => { return { color: value > value2 ? 'rgba(0,255,0,0.2)' : 'rgba(255,0,0,0.2)' } }) ``` -------------------------------- ### Main Draw (MD) Method Source: https://pro.gainlab.ai/glscriptapi/index Allows drawing indicators on the main chart from a secondary chart position. Supports various drawing methods similar to the 'draw' method. Requires the script to be in a secondary chart position. ```GLScript //@position: 'vice' 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'}) ``` -------------------------------- ### draw.line - Draw Line Source: https://pro.gainlab.ai/glscriptapi/index Draws a line in the K-line coordinate system. It takes a numerical array and a style object or callback function. The style object can define color, width, style, visibility, and continuity. ```GLScript 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) ``` -------------------------------- ### GLScript Style: Color Variable Source: https://pro.gainlab.ai/glscriptapi/index Defines a color input variable, displayed as a color picker in the settings panel. Supports standard web color formats. ```GLScript color1 = S.color('#5b8ff9', title='颜色1') ``` ```GLScript color2 = style.color('rgb(255,0,0)', title='颜色2') ``` ```GLScript color = S.color('#00F',title='颜色') ``` -------------------------------- ### GLScript: Money Flow Index (MFI) Source: https://pro.gainlab.ai/glscriptapi/index Calculates the Money Flow Index (MFI) for k-line data over a specified period. Returns null for periods with insufficient data or invalid values. ```GLScript F.mfi(list, period) ``` -------------------------------- ### sshape - Draw Shape Source: https://pro.gainlab.ai/glscriptapi/index Defines style for a shape relative to the screen. It's similar to style.shape() but omits the position attribute, intended for draw.sshape(). It supports icon, color, size, fill type, visibility, title, and tooltip. ```GLScript buySignal = S.sshape('arrowUp','#00FF00',12,'fill',title='支撑区') sellSignal = S.sshape('arrowDown','#FF0000',12,'fill',title='压力区') ``` -------------------------------- ### Candlestick Chart Drawing Source: https://pro.gainlab.ai/glscriptapi/index Details the `D.candle()` function for drawing candlestick charts. Users can define styles for rising and falling candles and use callback functions to dynamically set these styles based on price action. ```GLScript upCandle = S.candle('#00FF01', 'fill', 1, title="上升") downCandle = S.candle('#FF3333', 'fill', 1, title="下降") D.candle(dataList, ({value})=>{ return value.close < value.open ? downCandle : upCandle }) ``` -------------------------------- ### GLScript Shape Style Source: https://pro.gainlab.ai/glscriptapi/index Defines the style for shapes, allowing customization of icon, color, size, position, fill type, and visibility. It also supports titles and tooltips, and allows for fine-tuning of position with x and y offsets. ```GLScript buySignal = S.shape('arrowUp','#00FF00',12,'fill','lowDown',title='买入信号') sellSignal = S.shape('arrowDown','#FF0000',12,'fill','highUp',title='卖出信号') ``` ```GLScript buySignal = {"icon":"arrowUp","color":"#00FF00","size":12,"full":"fill","position":"lowDown","show":true}; sellSignal = {"icon":"arrowDown","color":"#FF0000","size":12,"full":"fill","position":"highUp","show":true}; ```