copy ta-lib-0.4.0.jar to \groovy-2.5.x\lib\
stock_atr.groovy
import java.util.Arrays;
import java.text.DecimalFormat;
import com.tictactec.ta.lib.*;
/* copy from Core.java
public int atrLookback( int optInTimePeriod )
{
}
public RetCode atr( int startIdx,
int endIdx,
double inHigh[],
double inLow[],
double inClose[],
int optInTimePeriod,
MInteger outBegIdx,
MInteger outNBElement,
double outReal[] )
*/
public class Stock_atr {
public static void main(String[] args){
def filename =null;
int SIZE =0;
if (args.size()==2){
filename = args[0];
SIZE = Integer.parseInt(args[1])-1; // 数据的行数,剔除头行 headline
} else {
println " usage: groovy stock_atr filename int ";
return;
}
// 申明输入参数
double[] inHigh = new double[SIZE];
double[] inLow = new double[SIZE];
double[] inClose = new double[SIZE];
// 初始值
Arrays.fill(inHigh, 0.0);
Arrays.fill(inLow, 0.0);
Arrays.fill(inClose,0.0);
def alist =[];
def dates =[];
int i= -2;
boolean yes = false;
int n =0;
new File(filename).eachLine{ line ->
i++; // println i;
alist = line.split(',');
n = alist.size();
if(i== -1 && line.trim().equals(",date,open,close,high,low,volume,code")){
yes=true;
} else if( yes == true && n>7 ){
dates.add(alist[1]);
inClose[i]= Double.parseDouble(alist[3]);
inHigh[i] = Double.parseDouble(alist[4]);
inLow[i] = Double.parseDouble(alist[5]);
}
}
if( i != SIZE-1 ){
println(" i != SIZE-1 : "+i);
return;
}
int optInTimePeriod =14;
// 申明输出参数
MInteger outBegIdx = new MInteger();
MInteger outNBElement = new MInteger();
double[] outReal = new double[SIZE];
// 输出格式化
DecimalFormat df = new DecimalFormat("0.0000");
if( yes == true){
Core talib = new Core();
int lookback = talib.atrLookback(optInTimePeriod);
RetCode retCode = talib.atr(0,SIZE-1,inHigh,inLow,inClose, optInTimePeriod,outBegIdx,outNBElement,outReal);
if( retCode == RetCode.Success){
println("outBegIdx=${outBegIdx.value}, outNBElement=${outNBElement.value}");
for( i=0; i< outNBElement.value; i++){
println("${i},${dates[i+14]},${df.format(outReal[i])}");
}
} else {
println(" retCode: "+retCode);
throw new RuntimeException("TALib was unable to process data");
}
} else {
println " headline is error ";
}
return;
}
}
运行 cmd
wc -l 600030.csv
737 rows
groovy stock_atr 600030.csv 737
719,2020-01-21,0.6381
720,2020-01-22,0.6647
721,2020-01-23,0.7301