环境信息:
1. Java 8
2. JanusGraph 0.54 (使用Oracle Berkeley DB Java Edition作为底层存储)
3. Windows 10
一、股票数据源
注册 Tushare https://tushare.pro/register?reg=332190 获取数据。
二、导入JanusGraph
1. 启动JanusGraph
打开cmd, 进入janusGraph根目录,输入
bin\gremlin-server.bat conf\gremlin-server\gremlin-server-berkeleyje.yaml
2. 定义模型
打开另一个cmd, 进入janusGraph根目录,输入
bin\gremlin.bat
gremlin> :remote connect tinkerpop.server conf/remote.yaml
==>Configured localhost/127.0.0.1:8182
gremlin> :remote console
==>All scripts will now be sent to Gremlin Server - [localhost/127.0.0.1:8182] - type ':remote console' to return to local mode
复制黏贴以下代码建立模型。日K线即为图中的顶点,K线图是链表结构,组成有向无环图。
mgmt = graph.openManagement()
tsCode = mgmt.makePropertyKey('tsCode').dataType(String.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
tradeDate = mgmt.makePropertyKey('tradeDate').dataType(Date.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
open = mgmt.makePropertyKey('open').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
high = mgmt.makePropertyKey('high').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
low = mgmt.makePropertyKey('low').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
close = mgmt.makePropertyKey('close').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
preClose = mgmt.makePropertyKey('preClose').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
ma5 = mgmt.makePropertyKey('ma5').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
ma10 = mgmt.makePropertyKey('ma10').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
ma20 = mgmt.makePropertyKey('ma20').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
ma30 = mgmt.makePropertyKey('ma30').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
ma90 = mgmt.makePropertyKey('ma90').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
ma120 = mgmt.makePropertyKey('ma120').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
ma250 = mgmt.makePropertyKey('ma250').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
change = mgmt.makePropertyKey('change').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
pctChange = mgmt.makePropertyKey('pctChange').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
volume = mgmt.makePropertyKey('volume').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
amount = mgmt.makePropertyKey('amount').dataType(Double.class).cardinality(org.janusgraph.core.Cardinality.SINGLE).make()
tradeRecord = mgmt.makeVertexLabel('tradeRecord').make()
mgmt.addProperties(tradeRecord, tsCode, tradeDate, open, high, low, close, preClose, ma5, ma10, ma20, ma30, ma90, ma120, ma250, change, pctChange, volume, amount)
mgmt.commit()
3. 导入数据
导入数据各有各法,实现每支股票的K线 k0 -> k1 -> k2 -> ... -> kn
GraphTraversalSource g = traversal().withRemote("conf/remote-graph.properties");
// 前一个日k线
Vertex kp = null;
// 当前日k线
Vertex kc;
for (...) {
String tsCode = ...;
Date tradeDate = ...;
Double open = ...;
Double high = ...;
Double low = ...;
Double close = ...;
Double ma5 = ...;
Double ma10 = ...;
Double ma20 = ...;
Double ma30 = ...;
Double ma90 = ...;
Double ma120 = ...;
Double ma250 = ...;
Double change = ...;
Double pctChange = ...;
Double volume = ...;
Double amount = ...;
kc = g.addV("tradeRecord").property("tsCode", tsCode)
.property("tradeDate", tradeDate)
.property("open", open)
.property("high", high)
.property("low", low)
.property("close", close)
.property("ma5", ma5)
.property("ma10", ma10)
.property("ma20", ma20)
.property("ma30", ma30)
.property("ma90", ma90)
.property("ma120", ma120)
.property("ma250", ma250)
.property("change", change)
.property("pctChange", pctChange)
.property("volume", volume)
.property("amount", amount)
.next();
if (null != kp) {
g.addE("links").from(kp).to(kc).next();
}
kp = kc;
}
三、找出涨停板后未来两个交易日走势
try (GraphTraversalSource g = traversal().withRemote("conf/remote-graph.properties")) {
GraphTraversal<Vertex, Map<String, Object>> t = g.with(Tokens.ARGS_EVAL_TIMEOUT, 3000000L).V()
.and(has("tsCode", "000001.SZ"), has("pctChange", P.gte(9.99)))
.as("x") // 涨停板
.out().as("y") // 涨停板后第一个K线
.out().as("z") // 涨停板后第二个K线
.project("d1", "d2", "d2pctChange", "d3", "d3pctChange")
.by(select("x").by("tradeDate")) // 涨停板日期
.by(select("y").by("tradeDate")) // 涨停板后第一个K线日期
.by(select("y").by("pctChange")) // 涨停板后第一个K线涨跌幅
.by(select("z").by("tradeDate")) // 涨停板后第二个K线日期
.by(select("z").by("pctChange")) // 涨停板后第二个K线涨跌幅
.select("d1", "d2", "d2pctChange", "d3", "d3pctChange")
.order().by("d1");
while (t.hasNext()) {
Map<String, Object> e = t.next();
String r = String.format("%s,%s,%s,%s,%s \n",
sdf.format(e.get("d1")), sdf.format(e.get("d2")), e.get("d2pctChange"), sdf.format(e.get("d3")), e.get("d3pctChange"));
System.out.print(r);
}
} catch (Exception e) {
e.printStackTrace();
}
输出结果:
000001.SZ,19921001,19921130,19921201,2.35,19921202,-1.53
000001.SZ,19940228,19940429,19940503,12.57,19940504,-8.06
000001.SZ,19940304,19940503,19940504,-8.06,19940505,3.63
000001.SZ,19940602,19940801,19940802,2.08,19940803,10.2
000001.SZ,19940604,19940803,19940804,1.76,19940805,4.19
000001.SZ,19940707,19940905,19940906,5.16,19940907,-8.98
000001.SZ,19941016,19941215,19941216,-1.59,19941219,-4.41
000001.SZ,19950319,19950518,19950519,2.99,19950522,1.58
000001.SZ,19960226,19960426,19960429,13.73,19960430,-2.52
000001.SZ,19960229,19960429,19960430,-2.52,19960502,-6.45
000001.SZ,19960401,19960531,19960603,7.27,19960604,1.67
000001.SZ,19960412,19960611,19960612,0.53,19960613,0.93
000001.SZ,19961019,19961218,19961219,-1.74,19961220,-0.06
000001.SZ,19970123,19970324,19970325,3.39,19970326,1.51
000001.SZ,19970306,19970505,19970506,9.99,19970507,9.96
000001.SZ,19970307,19970506,19970507,9.96,19970508,-8.18
000001.SZ,19970308,19970507,19970508,-8.18,19970509,4.54
000001.SZ,19970321,19970520,19970521,-1.87,19970522,-8.19
000001.SZ,19970421,19970620,19970623,7.57,19970624,4.6
000001.SZ,19990327,19990526,19990527,9.99,19990531,10.03
000001.SZ,19990328,19990527,19990531,10.03,19990601,8.41
000001.SZ,19990401,19990531,19990601,8.41,19990602,0.75
000001.SZ,19990425,19990624,19990625,1.09,19990628,8.97
000001.SZ,19990521,19990720,19990721,8.75,19990722,-5.22
000001.SZ,19991216,20000214,20000215,-4.47,20000216,-3.24
000001.SZ,20010824,20011023,20011024,0.14,20011025,-2.34
000001.SZ,20020425,20020624,20020625,10.0,20020626,0.21
000001.SZ,20020426,20020625,20020626,0.21,20020627,2.29
000001.SZ,20021115,20030114,20030115,-2.1,20030116,1.52
000001.SZ,20030215,20030416,20030417,-2.25,20030418,-4.23
000001.SZ,20050206,20050407,20050408,10.05,20050411,2.21
000001.SZ,20050207,20050408,20050411,2.21,20050412,-4.32
000001.SZ,20050822,20051021,20051024,-1.42,20051025,-2.72
000001.SZ,20060225,20060426,20060427,4.55,20060428,7.21
000001.SZ,20080721,20080919,20080922,9.98,20080923,-6.22
000001.SZ,20080724,20080922,20080923,-6.22,20080924,-2.58
000001.SZ,20080814,20081013,20081014,-0.6,20081016,-5.99
000001.SZ,20081225,20090223,20090225,1.07,20090226,-8.91
000001.SZ,20090416,20090615,20090616,2.68,20090617,-1.73
000001.SZ,20090531,20090730,20090803,-1.26,20090804,-3.13
000001.SZ,20090903,20091102,20091103,0.85,20091104,1.84
000001.SZ,20130711,20130909,20130910,2.56,20130911,2.01
000001.SZ,20140929,20141128,20141201,-2.01,20141202,7.63
000001.SZ,20150209,20150410,20150413,1.1,20150414,-1.45
000001.SZ,20200507,20200706,20200707,-1.28,20200708,1.81
四、总结
纯粹个人娱乐,没有参考价值。