利用JanusGraph图数据库统计股票涨停板后的走势

环境信息:

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 

四、总结

纯粹个人娱乐,没有参考价值。

要使用代码统计当天涨停的股票,首先需要确定“涨停”的定义。在股票市场中,涨停通常指股票价格达到当天允许的最大涨幅限制。在中国大陆的股市,一个股票涨停板是前一个交易日收盘价的上浮10%。一旦确定了定义,你可以通过以下步骤来实现代码统计: 1. 获取当天所有股票的收盘价。 2. 计算每只股票的涨停价。 3. 比较每只股票的涨停价与当天的收盘价,如果两者相等,则表示该股票涨停。 4. 输出所有涨停的股票信息。 以下是一个使用Python语言和假设的API进行股票涨停统计的示例代码: ```python import requests # 假设有一个API可以获取股票数据,这里是一个示例URL STOCK_DATA_API = "http://example.com/api/stock_data" # 获取当天股票数据的函数 def get_todays_stock_data(): response = requests.get(f"{STOCK_DATA_API}?date=today") if response.status_code == 200: return response.json() # 假设返回的数据是JSON格式 else: raise Exception("Failed to retrieve stock data") # 计算涨停价的函数 def calculate_limit_up_price(close_price): return close_price * 1.1 # 假设涨停板是10%涨幅 # 统计涨停股票的函数 def count_limit_up_stocks(stock_data): limit_up_stocks = [] for stock in stock_data: stock_symbol = stock['symbol'] close_price = float(stock['close']) limit_up_price = calculate_limit_up_price(close_price) if round(close_price, 2) == round(limit_up_price, 2): # 比较价格是否达到涨停板 limit_up_stocks.append(stock_symbol) return limit_up_stocks # 主逻辑 try: todays_stock_data = get_todays_stock_data() limit_up_stocks = count_limit_up_stocks(todays_stock_data) print("当天涨停的股票有:", limit_up_stocks) except Exception as e: print("Error:", e) ``` 请注意,上述代码中的API URL和数据结构是假设的,实际情况下你需要使用真实的股票市场API来获取数据,并根据API提供的数据结构调整代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值