程序员的量化交易之路(25)--Cointrader之MarketData市场数据实体(12)

简介:

转载需注明出处:http://blog.csdn.net/minimicallhttp://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;
}





相关文章
|
9月前
|
机器学习/深度学习 计算机视觉
RT-DETR改进策略【注意力机制篇】| 2024 蒙特卡罗注意力(MCAttn)模块,提高小目标的关注度
RT-DETR改进策略【注意力机制篇】| 2024 蒙特卡罗注意力(MCAttn)模块,提高小目标的关注度
344 1
RT-DETR改进策略【注意力机制篇】| 2024 蒙特卡罗注意力(MCAttn)模块,提高小目标的关注度
|
11月前
Node安装版本低于工程版本时打包绕过校验
在开发中,若本地Node版本低于项目配置要求,导致打包报错(如图所示),可在不变更本地环境的情况下,通过在执行`npm run build`前输入命令`set NODE_OPTIONS=--openssl-legacy-provider`来绕行此问题,确保构建顺利进行。
681 10
|
传感器 数据采集 存储
以下是一个简化的环境监测系统工程概述,并附带有Python代码示例或详解。
以下是一个简化的环境监测系统工程概述,并附带有Python代码示例或详解。
|
前端开发 Docker 容器
主机host服务器和Docker容器之间的文件互传方法汇总
Docker 成为前端工具,可实现跨设备兼容。本文介绍主机与 Docker 容器/镜像间文件传输的三种方法:1. 构建镜像时使用 `COPY` 或 `ADD` 指令;2. 启动容器时使用 `-v` 挂载卷;3. 运行时使用 `docker cp` 命令。每种方法适用于不同场景,如静态文件打包、开发时文件同步及临时文件传输。注意权限问题、容器停止后的文件传输及性能影响。
3297 1
|
人工智能 Serverless
AI助理精准匹配,为您推荐方案——如何添加一个Stable Difussion图像生成应用
介绍了一种利用AI助手快速获取并搭建Stable Diffusion图像生成应用的方法。用户只需在阿里云官网向AI助手提出需求,即可获得详细的实施方案。随后,按照AI助手提供的方案,通过函数计算部署应用,并进行测试。此过程显著提升了开发效率。
1067 2
AI助理精准匹配,为您推荐方案——如何添加一个Stable Difussion图像生成应用
|
前端开发 JavaScript 开发工具
2024年前端开发的十大必备技巧
本文概述了2024年前端开发的十大关键技能,包括现代JavaScript、CSS Grid/Flexbox布局、主流框架精通、Web性能优化、Git版本控制、调试技巧、Web可访问性、现代构建工具使用、PWA开发及持续学习,旨在助力开发者提升Web开发质量和用户体验。
|
前端开发 应用服务中间件
前端项目部署问题总结
【7月更文挑战第13天】
188 1
|
大数据 Linux Docker
mac docker 宿主机和容器间网络打通
mac docker 宿主机和容器间网络打通
269 0
|
Linux API C语言
由浅入深C系列五:使用libcurl进行基于http get/post模式的C语言交互应用开发
由浅入深C系列五:使用libcurl进行基于http get/post模式的C语言交互应用开发
|
机器学习/深度学习 人工智能 自然语言处理
从零构建医疗领域知识图谱的KBQA问答系统:其中7类实体,约3.7万实体,21万实体关系。
从零构建医疗领域知识图谱的KBQA问答系统:其中7类实体,约3.7万实体,21万实体关系。
从零构建医疗领域知识图谱的KBQA问答系统:其中7类实体,约3.7万实体,21万实体关系。