Android原生股票图-分时图讲解(一)
Android原生股票图-分时图讲解(二)
Android原生股票图-K线图讲解和绘制(一)
包含(MA ,BOLL ,RSI,KDJ ,MACD)指标。
public class DataHelper {
/**
* 计算RSI
*
* @param datas
*/
static void calculateRSI(List<KLineEntity> datas) {
float rsi1 = 0;
float rsi2 = 0;
float rsi3 = 0;
float rsi1ABSEma = 0;
float rsi2ABSEma = 0;
float rsi3ABSEma = 0;
float rsi1MaxEma = 0;
float rsi2MaxEma = 0;
float rsi3MaxEma = 0;
for (int i = 0; i < datas.size(); i++) {
KLineEntity point = datas.get(i);
final float closePrice = point.getClosePrice();
if (i == 0) {
rsi1 = 0;
rsi2 = 0;
rsi3 = 0;
rsi1ABSEma = 0;
rsi2ABSEma = 0;
rsi3ABSEma = 0;
rsi1MaxEma = 0;
rsi2MaxEma = 0;
rsi3MaxEma = 0;
} else {
float Rmax = Math.max(0, closePrice - datas.get(i - 1).getClosePrice()); //收盘价
float RAbs = Math.abs(closePrice - datas.get(i - 1).getClosePrice()); //收盘价
rsi1MaxEma = (Rmax + (6f - 1) * rsi1MaxEma) / 6f;
rsi1ABSEma = (RAbs + (6f - 1) * rsi1ABSEma) / 6f;
rsi2MaxEma = (Rmax + (12f - 1) * rsi2MaxEma) / 12f;
rsi2ABSEma = (RAbs + (12f - 1) * rsi2ABSEma) / 12f;
rsi3MaxEma = (Rmax + (24f - 1) * rsi3MaxEma) / 24f;
rsi3ABSEma = (RAbs + (24f - 1) * rsi3ABSEma) / 24f;
rsi1 = (rsi1MaxEma / rsi1ABSEma) * 100;
rsi2 = (rsi2MaxEma / rsi2ABSEma) * 100;
rsi3 = (rsi3MaxEma / rsi3ABSEma) * 100;
}
point.rsi1 = rsi1;
point.rsi2 = rsi2;
point.rsi3 = rsi3;
}
}
/**
* 计算kdj
*
* @param datas
*/
static void calculateKDJ(List<KLineEntity> datas) {
float k = 0;
float d = 0;
for (int i = 0; i < datas.size(); i++) {
KLineEntity point = datas.get(i);
final float closePrice = point.getClosePrice(); //收盘价
int startIndex = i - 8;
if (startIndex < 0) {
startIndex = 0;
}
float max9 = Float.MIN_VALUE;
float min9 = Float.MAX_VALUE;
for (int index = startIndex; index <= i; index++) {
max9 = Math.max(max9, datas.get(index).getHighPrice());//最高价
min9 = Math.min(min9, datas.get(index).getLowPrice());//最低价
}
float rsv = 100f * (closePrice - min9) / (max9 - min9);
if (i == 0) {
k = rsv;
d = rsv;
} else {
k = (rsv + 2f * k) / 3f;
d = (k + 2f * d) / 3f;
}
point.k = k;
point.d = d;
point.j = 3f * k - 2 * d;
}
}
/**
* 计算macd
*
* @param datas
*/
static void calculateMACD(List<KLineEntity> datas) {
float ema12 = 0;
float ema26 = 0;
float dif = 0;
float dea = 0;
float macd = 0;
for (int i = 0; i < datas.size(); i++) {
KLineEntity point = datas.get(i);
final float closePrice = point.getClosePrice(); //收盘价
if (i == 0) {
ema12 = closePrice;
ema26 = closePrice;
} else {
// EMA(12) = 前一日EMA(12) X 11/13 + 今日收盘价 X 2/13
// EMA(26) = 前一日EMA(26) X 25/27 + 今日收盘价 X 2/27
ema12 = ema12 * 11f / 13f + closePrice * 2f / 13f;
ema26 = ema26 * 25f / 27f + closePrice * 2f / 27f;
}
// DIF = EMA(12) - EMA(26) 。
// 今日DEA = (前一日DEA X 8/10 + 今日DIF X 2/10)
// 用(DIF-DEA)*2即为MACD柱状图。
dif = ema12 - ema26;
dea = dea * 8f / 10f + dif * 2f / 10f;
macd = (dif - dea) * 2f;
point.dif = dif;
point.dea = dea;
point.macd = macd;
}
}
/**
* 计算 BOLL 需要在计算ma之后进行
*
* @param datas
*/
static void calculateBOLL(List<KLineEntity> datas) {
for (int i = 0; i < datas.size(); i++) {
KLineEntity point = datas.get(i);
final float closePrice = point.getClosePrice(); //收盘价
if (i == 0) {
point.mb = closePrice;
point.up = Float.NaN;
point.dn = Float.NaN;
} else {
int n = 20;
if (i < 20) {
n = i + 1;
}
float md = 0;
for (int j = i - n + 1; j <= i; j++) {
float c = datas.get(j).getClosePrice(); //收盘价
float m = point.getMA20Price(); //二十(月,日,时,分,5分等)均价
float value = c - m;
md += value * value;
}
md = md / (n - 1);
md = (float) Math.sqrt(md);
point.mb = point.getMA20Price();
point.up = point.mb + 2f * md;
point.dn = point.mb - 2f * md;
}
}
}
/**
* 计算ma
*
* @param datas
*/
static void calculateMA(List<KLineEntity> datas) {
float ma5 = 0;
float ma10 = 0;
float ma20 = 0;
float ma40 = 0;
float ma60 = 0;
for (int i = 0; i < datas.size(); i++) {
KLineEntity point = datas.get(i);
final float closePrice = point.getClosePrice(); //收盘价
ma5 += closePrice;
ma10 += closePrice;
ma20 += closePrice;
ma40 += closePrice;
ma60 += closePrice;
if (i >= 5) {
ma5 -= datas.get(i - 5).getClosePrice();
point.MA5Price = ma5 / 5f;
} else {
point.MA5Price = ma5 / (i + 1f);
}
if (i >= 10) {
ma10 -= datas.get(i - 10).getClosePrice();
point.MA10Price = ma10 / 10f;
} else {
point.MA10Price = ma10 / (i + 1f);
}
if (i >= 20) {
ma20 -= datas.get(i - 20).getClosePrice();
point.MA20Price = ma20 / 20f;
} else {
point.MA20Price = ma20 / (i + 1f);
}
if (i >= 40) {
ma40 -= datas.get(i - 40).getClosePrice();
point.MA40Price = ma40 / 40f;
} else {
point.MA40Price = ma40 / (i + 1f);
}
if (i >= 60) {
ma60 -= datas.get(i - 60).getClosePrice();
point.MA60Price = ma60 / 60f;
} else {
point.MA60Price = ma60 / (i + 1f);
}
}
}
/**
* 计算MA BOLL RSI KDJ MACD
*
* @param datas
*/
static void calculate(List<KLineEntity> datas) {
calculateMA(datas);
calculateMACD(datas);
calculateBOLL(datas);
calculateRSI(datas);
calculateKDJ(datas);
calculateVolumeMA(datas);
}
//成交量MA 平滑线
private static void calculateVolumeMA(List<KLineEntity> entries) {
float volumeMa5 = 0;
float volumeMa10 = 0;
for (int i = 0; i < entries.size(); i++) {
KLineEntity entry = entries.get(i);
volumeMa5 += entry.getVolume(); //成交量
volumeMa10 += entry.getVolume();
if (i >= 5) {
volumeMa5 -= entries.get(i - 5).getVolume();
entry.MA5Volume = (volumeMa5 / 5f);
} else {
entry.MA5Volume = (volumeMa5 / (i + 1f));
}
if (i >= 10) {
volumeMa10 -= entries.get(i - 10).getVolume();
entry.MA10Volume = (volumeMa10 / 5f);
} else {
entry.MA10Volume = (volumeMa10 / (i + 1f));
}
}
}
}
参数和接口
- *k线实体接口
public interface IKLine extends ICandle, IMACD, IKDJ, IRSI,IVolume,IBOLL {
}
- 布林线指标接口
public interface IBOLL {
/**
* 上轨线
*/
float getUp();
/**
* 中轨线
*/
float getMb();
/**
* 下轨线
*/
float getDn();
}
- 成交量接口
public interface IVolume {
/**
* 开盘价
*/
float getOpenPrice();
/**
* 收盘价
*/
float getClosePrice();
/**
* 成交量
*/
float getVolume();
/**
* 持仓量
*/
float getInterest();
/**
* 五(月,日,时,分,5分等)均量
*/
float getMA5Volume();
/**
* 十(月,日,时,分,5分等)均量
*/
float getMA10Volume();
}
- RSI指标接口
public interface IRSI {
/**
* RSI1值
*/
float getRsi1();
/**
* RSI2值
*/
float getRsi2();
/**
* RSI3值
*/
float getRsi3();
}
- KDJ指标(随机指标)接口
public interface IKDJ {
/**
* K值
*/
float getK();
/**
* D值
*/
float getD();
/**
* J值
*/
float getJ();
}
- MACD指标(指数平滑移动平均线)接口
public interface IMACD {
/**
* DEA值
*/
float getDea();
/**
* DIF值
*/
float getDif();
/**
* MACD值
*/
float getMacd();
}
- 蜡烛图实体接口
public interface ICandle {
/**
* 开盘价
*/
float getOpenPrice();
/**
* 最高价
*/
float getHighPrice();
/**
* 最低价
*/
float getLowPrice();
/**
* 收盘价
*/
float getClosePrice();
/**
* 五(月,日,时,分,5分等)均价
*/
float getMA5Price();
/**
* 十(月,日,时,分,5分等)均价
*/
float getMA10Price();
/**
* 二十(月,日,时,分,5分等)均价
*/
float getMA20Price();
}
- KLineEntity
public class KLineEntity implements IKLine {
public String getDatetime() {
return Date;
}
@Override
public float getOpenPrice() {
return Open;
}
@Override
public float getHighPrice() {
return High;
}
@Override
public float getLowPrice() {
return Low;
}
@Override
public float getClosePrice() {
return Close;
}
@Override
public float getMA5Price() {
return MA5Price;
}
@Override
public float getMA10Price() {
return MA10Price;
}
@Override
public float getMA20Price() {
return MA20Price;
}
@Override
public float getDea() {
return dea;
}
@Override
public float getDif() {
return dif;
}
@Override
public float getMacd() {
return macd;
}
@Override
public float getK() {
return k;
}
@Override
public float getD() {
return d;
}
@Override
public float getJ() {
return j;
}
@Override
public float getRsi1() {
return rsi1;
}
@Override
public float getRsi2() {
return rsi2;
}
@Override
public float getRsi3() {
return rsi3;
}
@Override
public float getUp() {
return up;
}
@Override
public float getMb() {
return mb;
}
@Override
public float getDn() {
return dn;
}
@Override
public float getVolume() {
return Volume;
}
@Override
public float getMA5Volume() {
return MA5Volume;
}
@Override
public float getMA10Volume() {
return MA10Volume;
}
public String Date;
public float Open; //开盘价
public float Close; //收盘价
public float High; //最高价
public float Low; //最低价
public float Volume; //成交量
//ma
public float MA5Price;
public float MA10Price;
public float MA20Price;
//MACD
public float dea; //DEA值
public float dif; //DIF值
public float macd; //MACD值
//KDJ
public float k;
public float d;
public float j;
//RSI
public float rsi1; //RSI1值
public float rsi2; //RSI2值
public float rsi3; //RSI3值
//BOLL
public float up; //上轨线
public float mb; //中轨线
public float dn; //下轨线
public float MA5Volume;
public float MA10Volume;
}