Introduction
● With AICoin Script, you can manipulate the rich internal data of AICoin, calculate the indicators you want, and plot them for chart analysis. Below, we will introduce how to write AICoin Script. If you have programming experience, I believe you can quickly master it. If you are a programming novice, I believe you can also make some modifications to existing indicators after browsing. Let's start the programming journey.
Quick Start
We will explain through an example of the MACD indicator
// @version=2
// Define indicator-related parameters
N1 = 12
N2 = 26
M = 9
// Calculate the values of the indicator
dif = ema(close, N1) - ema(close, N2)
dea = ema(dif, M)
macd = (dif - dea) * 2
// Calculate the values of the alert conditions
goldencross = crossup(dif, dea)
deadcross = crossdown(dif, dea)
crossupzero = crossup(dea, 0)
crossdownzero = crossdown(dea, 0)
// Define alert conditions for use in the alert window
alertcondition(goldencross, title='MACD(12,26,9) Golden Cross')
alertcondition(deadcross, title='MACD(12,26,9) Dead Cross')
alertcondition(crossupzero, title='MACD(12,26,9) Cross Up Zero')
alertcondition(crossdownzero, title='MACD(12,26,9) Cross Down Zero')
// Plot data on the chart
plot(dif, title='DIF', color='rgba(47,210,178,1)', lineWidth=1, lineDash=[0])
plot(dea, title='DEA', color='rgba(255,180,0,1)', lineWidth=1, lineDash=[0])
plotColumn(macd, title='MACD')
Assignment Statements
Define a variable with a value of 12
N1 = 12
Assign an empty value using nan
N1 = nan
// nan is a special value that represents an empty value. You can refer to the Data Type Document for more information on built-in data types.
Similarly, we can assign the result of a function to a variable
// where close is the closing price, which is a built-in data that can be directly referenced. For more built-in data, please refer to the Market Data Document. macd is a built-in indicator. For information on MACD and more indicator functions, please refer to the Indicator Function Document.
// Single return value
ema12 = ema(close, 12)
// Multiple return values
[dif, dea, macd] = macd(close, 12, 26, 9, 'EMA', 'EMA')
Calling Calculation Functions
Use a function to calculate when dif crosses above dea
golden_cross = crossup(dif, dea)
// crossup is a function that takes two parameters and returns a boolean value. If the first parameter crosses above the second parameter, it returns true; otherwise, it returns false. You can refer to the Calculation Function Document for more information on functions.
Calling Alert Functions
Use the alert function to trigger an alert when dif crosses above dea
alertcondition(golden_cross, title='MACD(12,26,9) Golden Cross')
// alertcondition is an alert function that can push information when the alert condition is triggered. You can refer to the Alert Function Document for more information on functions.
Calling Plotting Functions
Use the plotting function to plot the line chart for dif and dea, and plot the bar chart for macd
plot(dif, title='DIF', color='rgba(47,210,178,1)', lineWidth=1, lineDash=[0])
plot(dea, title='DEA', color='rgba(255,180,0,1)', lineWidth=1, lineDash=[0])
plotColumn(macd, title='MACD')
// plot is a function for plotting line charts, and plotColumn is a function for plotting bar charts. You can refer to the Plotting Function Document for more information on functions. Some plotting functions can only be used in version=2. For details, see the Plotting Function Document.
Advanced Usage
Variable References
// @version=2
a = close[1]
// close is a built-in data representing the closing price. Using [1] indicates taking the closing price of the previous candle. Note that you can reference up to the most recent 500 data points.
Using Conditional Statements
a = nan
b = nan
if (high > close && high > open){
a := close
b := open
}
plot(a)
plot(b)
// When the conditional statement is long and involves multiple execution statements, you can use parentheses for differentiation. You can use the above method, using parentheses for differentiation. When the conditional statement is particularly long, the use of line breaks is temporarily not supported. The code block to be executed needs to be differentiated using curly braces. Additionally, when assigning a value to a variable that has already been defined, you need to use := for assignment; otherwise, the variable's value will not change.
if (high > close && high > open && a > b
&& c < d && e == f && g >= h && i <= j){
a := close
b := open
}
// Using line breaks in conditional statements will result in an error, so please try to avoid using line breaks as much as possible.
Using Loop Statements
// @version=2
a = nan
for(i=0;i<10;i++){
a := i;
}
// The use of the for loop statement is divided into three parts: the first part is the initialization statement, the second part is the condition statement, and the third part is the statement executed within the loop body. The three parts are separated by semicolons. When the condition statement is not met, the loop ends. For more information on the use of conditional expression statements, please refer to the Conditional Expression Document.
Using Function Definitions
// @version=2
funcma(x,y) {
sum = 0.0 ;
for (i = 0; i < y; i++){
sum := sum + x[i]/y ;
}
return sum ;
}
funcma10 = func_ma(close, 10)
// Function definitions use the above structure. The name and parameters of the function can be customized. func_ma is the defined function name, and the function name is used subsequently to call the function. x and y are the parameters that the function needs to obtain. Inside the function body, the return statement is used to return the value of the function. The return value of the function can be assigned to a variable using =.
Using Multi-Data Source Functions
// @version=2
// Get the close data for the corresponding trading pair
security('ethswapusdt:okcoinfutures', '5m', close)
// The security function can obtain data for the corresponding trading pair. The first parameter is the trading pair, the second parameter is the timeframe, and the third parameter is the data to be obtained. You can refer to the Multi-Data Source Function for more information on built-in data types. If the first parameter uses syminfo.tickerid, it can obtain data for the current trading pair. If the timeframe is empty, it uses the current timeframe.
version=2 Features
// Loop statements, multi-data sources, and function definitions can only be used in version=2. They are not supported in version=1. For more details, please refer to the Function Document.
免责声明:本文章仅代表作者个人观点,不代表本平台的立场和观点。本文章仅供信息分享,不构成对任何人的任何投资建议。用户与作者之间的任何争议,与本平台无关。如网页中刊载的文章或图片涉及侵权,请提供相关的权利证明和身份证明发送邮件到support@aicoin.com,本平台相关工作人员将会进行核查。