道量化交易程序猿(25)--Cointrader之MarketData市场数据实体(12)
转载注明出处:http://blog.csdn.net/minimicall。http://cloudtrade.top/
前面一节我们说到了远端事件。当中。市场数据就属于远端事件。市场数据有什么?我们通过代码来回答这个问题:
package org.cryptocoinpartners.schema; import javax.annotation.Nullable;
import javax.persistence.Entity;
import javax.persistence.ManyToOne; import org.joda.time.Instant; /**
* @author Tim Olson
*/
@Entity //实体,在数据库中会创建表格
public abstract class MarketData extends RemoteEvent { protected MarketData(Instant time, @Nullable String remoteKey, Market market) {
this(time, Instant.now(), remoteKey, market);
} protected MarketData(Instant time, Instant timeReceived, String remoteKey, Market market) {
super(time, timeReceived, remoteKey);
this.market = market;
} @ManyToOne(optional = false)
public Market getMarket() {
return market;
} // JPA
protected MarketData() {
super();
} protected void setMarket(Market market) {
this.market = market;
} private Market market;//市场
}
市场数据里面仅仅有一个成员。市场。
也即是说市场数据是某一时刻的市场。
我们接下来看看市场又有什么数据,Market。
我们通过凝视代码来学习。
package org.cryptocoinpartners.schema; import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import javax.persistence.Basic;
import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.NoResultException;
import javax.persistence.PostPersist;
import javax.persistence.Table;
import javax.persistence.Transient; import org.cryptocoinpartners.enumeration.FeeMethod;
import org.cryptocoinpartners.util.PersistUtil;
import org.cryptocoinpartners.util.RemainderHandler; /**
* Represents the possibility to trade one Asset for another at a specific Exchange.
*
* @author Tim Olson
*/
@Entity //表格
@Cacheable // 缓存
@NamedQuery(name = "Market.findByMarket", query = "select m from Market m where exchange=?1 and listing=?2") // 命名查询
@Table(indexes = { @Index(columnList = "exchange"), @Index(columnList = "listing"), @Index(columnList = "active") }) //编织索引
public class Market extends EntityBase { public static Collection<Market> findAll() {//查询全部市场数据
return PersistUtil.queryList(Market.class, "select m from Market m");
} /** adds the Market to the database if it does not already exist */
public static Market findOrCreate(Exchange exchange, Listing listing) {
return findOrCreate(exchange, listing, listing.getPriceBasis(), listing.getVolumeBasis());
} @PostPersist
private void postPersist() { //PersistUtil.detach(this); }
//查询或创建市场数据
public static Market findOrCreate(Exchange exchange, Listing listing, double quoteBasis, double volumeBasis) {
// final String queryStr = "select m from Market m where exchange=?1 and listing=? 2";
try {
return PersistUtil.namedQueryOne(Market.class, "Market.findByMarket", exchange, listing);
} catch (NoResultException e) {
final Market ml = new Market(exchange, listing, quoteBasis, volumeBasis);
PersistUtil.insert(ml);
return ml;
}
} /**
@return active Markets for the given exchange
*/
public static Collection<Market> find(Exchange exchange) {
return PersistUtil.queryList(Market.class, "select s from Market s where exchange=? 1 and active=?2", exchange, true);
} /**
@return active Markets for the given listing
*/
public static Collection<Market> find(Listing listing) {
return PersistUtil.queryList(Market.class, "select s from Market s where listing=?1 and active=?2", listing, true);
} @ManyToOne(optional = false)
public Exchange getExchange() {
return exchange;
} @ManyToOne(optional = false)
public Listing getListing() {
return listing;
} @Basic(optional = false)
public double getPriceBasis() { return listing.getPriceBasis() == 0 ? priceBasis : listing.getPriceBasis(); } @Transient
public int getScale() { int length = (int) (Math.log10(getPriceBasis()));
return length;
} @Basic(optional = false)
public double getVolumeBasis() {
return listing.getVolumeBasis() == 0 ? volumeBasis : listing.getVolumeBasis(); } /** @return true iff the Listing is currently traded at the Exchange. The Market could have been retired. */
public boolean isActive() {
return active;
} @Transient
public Asset getBase() {
return listing.getBase();
} @Transient
public Asset getQuote() {
return listing.getQuote();
} @Transient
public int getMargin() {
return listing.getMargin() == 0 ? exchange.getMargin() : listing.getMargin(); } @Transient
public double getFeeRate() {
return listing.getFeeRate() == 0 ? exchange.getFeeRate() : listing.getFeeRate(); } @Transient
public FeeMethod getMarginFeeMethod() {
return listing.getMarginFeeMethod() == null ? exchange.getMarginFeeMethod() : listing.getMarginFeeMethod(); } @Transient
public FeeMethod getFeeMethod() {
return listing.getFeeMethod() == null ? exchange.getFeeMethod() : listing.getFeeMethod(); } @Transient
public double getMultiplier() {
return listing.getMultiplier(); } @Transient
public double getTickValue() {
return listing.getTickValue(); } @Transient
public double getContractSize() {
return listing.getContractSize(); } @Transient
public double getTickSize() {
return listing.getTickSize(); } @Transient
public Asset getTradedCurrency() {
return listing.getTradedCurrency(); } @Transient
public String getSymbol() {
return exchange.toString() + ':' + listing.toString();
} @Override
public String toString() {
return getSymbol();
} public static Market forSymbol(String marketSymbol) { for (Market market : findAll()) {
if (market.getSymbol().equalsIgnoreCase(marketSymbol))
return market;
}
return null;
} public static List<String> allSymbols() {
List<String> result = new ArrayList<>();
List<Market> markets = PersistUtil.queryList(Market.class, "select m from Market m");
for (Market market : markets)
result.add((market.getSymbol()));
return result;
} public static class MarketAmountBuilder { public DiscreteAmount fromPriceCount(long count) {
return priceBuilder.fromCount(count);
} public DiscreteAmount fromVolumeCount(long count) {
return volumeBuilder.fromCount(count);
} public DiscreteAmount fromPrice(BigDecimal amount, RemainderHandler remainderHandler) {
return priceBuilder.fromValue(amount, remainderHandler);
} public DiscreteAmount fromVolume(BigDecimal amount, RemainderHandler remainderHandler) {
return volumeBuilder.fromValue(amount, remainderHandler);
} public MarketAmountBuilder(double priceBasis, double volumeBasis) {
this.priceBuilder = DiscreteAmount.withBasis(priceBasis);
this.volumeBuilder = DiscreteAmount.withBasis(volumeBasis);
} private final DiscreteAmount.DiscreteAmountBuilder priceBuilder;
private final DiscreteAmount.DiscreteAmountBuilder volumeBuilder;
} public MarketAmountBuilder buildAmount() {
if (marketAmountBuilder == null)
marketAmountBuilder = new MarketAmountBuilder(getPriceBasis(), getVolumeBasis());
return marketAmountBuilder;
} // JPA
protected Market() {
} protected void setExchange(Exchange exchange) {
this.exchange = exchange;
} protected void setListing(Listing listing) {
this.listing = listing;
} protected void setActive(boolean active) {
this.active = active;
} protected void setPriceBasis(double quoteBasis) {
this.priceBasis = quoteBasis;
} protected void setVolumeBasis(double volumeBasis) {
this.volumeBasis = volumeBasis;
} private Market(Exchange exchange, Listing listing, double priceBasis, double volumeBasis) {
this.exchange = exchange;
this.listing = listing;
this.priceBasis = priceBasis;
this.volumeBasis = volumeBasis;
this.active = true;
} private Exchange exchange;//交易所
private Listing listing;//挂牌清单
private double priceBasis;//基准价格
private double volumeBasis;//基准量
private boolean active;//是否活跃
private MarketAmountBuilder marketAmountBuilder;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
道量化交易程序猿(25)--Cointrader之MarketData市场数据实体(12)的更多相关文章
- 黑马程序猿——25,打印流,合并流,对象序列化,管道流,RandomAccessFile
------<ahref="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培 ...
- 《零起点,python大数据与量化交易》
<零起点,python大数据与量化交易>,这应该是国内第一部,关于python量化交易的书籍. 有出版社约稿,写本量化交易与大数据的书籍,因为好几年没写书了,再加上近期"前海智库 ...
- 屌丝程序猿赚钱之道 之APP
假设你已经通过APP赚到了钱,那么本文对你而言没有意义.倒是希望你可以给我们诸多建议. 通过制作APP或者说手机应用赚钱,相信是非常多程序猿希望做的事情.也确实有一些人通过APP赚到了钱. 对于程序猿 ...
- 屌丝程序猿赚钱之道之taobao 2
续上篇,之前写的案例,都是比較0基础的. 案例4: 代写情书.软文.论文等等. 这是我一个同学的真实故事. 我隔壁寝室的小王平时没事就爱谢谢博客.逛逛论坛.大二的时候接触了威客网,開始在网上 ...
- Java程序猿修炼之道 之 Logging(3/3) - 怎么分析Log
1. 说明 作为一个程序猿我们常常要做一件事情:获取某个Log文件,从当中找出自己想要的信息. 本文总结了我在工作中使用了哪些工具来分析Log文件获取我想要的信息,我近期几年的工作环境都是server ...
- 程序猿接私活经验总结,来自csdn论坛语录
下面为网上摘录,以做笔记: 但是到网上看看,似乎接私活也有非常多不easy,技术问题本身是个因素,还有非常多有技术的人接私活时被骗,或者是合作到最后以失败告终,所以想请有经验的大侠们出来指点一下,接私 ...
- 程序猿最浪漫的表白,肯定会得到你的她——Jason niu 原文来自GitHub,本人已部分修改
程序猿最浪漫的表白,肯定会得到你的她——Jason niu 原文来自GitHub,主页本人已部分修改,感谢程序猿大神wuxia2001和hackerzhou的开源,感谢这两位大神! 视频结果展示 ...
- 程序猿职业生涯中的 Norris 常数
我的朋友Clift Norris发现了一个基本常数.我称之为Norris常数,一个未经培训的程序猿在他或她遇到瓶颈之前能写出的平均代码量.Clift预计这个值是1500行. 超过这个数以后,代码会变得 ...
- 漫谈程序猿系列:无BUG不生活
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZm9ydW9r/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...
随机推荐
- struts2--笔记(一)
1.什么是struts2? 框架是一些已经写好的代码,一般情况下于产品是无关的,可以提高效率. 2.javaEE的三层结构:表现层.业务层.持久层组成,struts是变现层的一个框架结构,分成结构的方 ...
- 如何启用“锁定内存页”选项 (Windows)
默认情况下,禁用 Windows 策略"锁定内存页"选项.必须启用此权限才能配置地址窗口化扩展插件 (AWE).此策略将确定哪些帐户可以使用进程将数据保留在物理内存中,从而阻止系统 ...
- radare, the reverse engineering framework
History The radare project [http://radare.org/] started in February of 2006 aiming to provide a free ...
- 【redis,1】java操作redis: 将string、list、map、自己定义的对象保存到redis中
一.操作string .list .map 对象 1.引入jar: jedis-2.1.0.jar 2.代码 /** * @param args */ public s ...
- Vmware P2V 清除被隱藏網卡佔用的的IP
修改註冊表也可以讓非正常卸載的網卡釋放捆綁的固定的IP地址,註冊表中定位於中:HKEY_LOCAL_MACHINE \SYSTEM\ CurrentControlSet\Services\Tcpip\ ...
- gdb 调试多线程 神贴
gdb 调试多线程如果目标进程已经core dump了,那么 gdb -c core xxx xxx是对应的程序文件.如果目标进程还在运行,通常此时用于调试线程死锁的情况.有两种方法一是 gdb ...
- 具体解释https是怎样确保安全的
Https 介绍 什么是Https HTTPS(全称:Hypertext Transfer Protocol over Secure Socket Layer).是以安全为目标的HTTP通道,简单讲是 ...
- 手把手生成决策树(dicision tree)
手把手生成决策树(dicision tree) 标签: Python 机器学习 主要參考资料: Peter HARRINGTON.机器学习实战[M].李锐,李鹏,曲亚东,王斌译.北京:人民邮电出版社, ...
- C++实践參考——二进制文件浏览器
[项目-二进制文件浏览器] (1)做一个相似BinaryViewer的查看二进制文件的程序.输入文件名称后,能够以16进制和ASCII对比的方式列出该文件的内容.能够參考下图: 提示:循环中,一次读入 ...
- Android多线程研究(9)——线程锁Lock
在前面我们在解决线程同步问题的时候使用了synchronized关键字,今天我们来看看Java 5.0以后提供的线程锁Lock. Lock接口的实现类提供了比使用synchronized关键字更加灵活 ...