groovy:ta-lib 计算 stock ATR

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值