程序员的量化交易之路(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;
}





相关文章
|
7天前
|
人工智能 JSON API
全网刷屏的 Jev 模型正式开放!一手实战测评 + 保姆级教程
全网爆火的 Jev 模型是什么?有什么用?怎么使用?怎么接入 AI 编程工具?效果真的好么?傻子可懂的 Jev 保姆级实战教程 + 项目实战测评来啦
6950 9
|
5天前
|
人工智能 测试技术 API
最近全网爆火的 Jev 到底是什么?适合干什么、怎么用,一篇讲透!
Jev是TypeSafe AI推出的“系统一模型”,不生成文本,专做毫秒级结构化决策:Choice(多选)、Score(打分)、Noul(是非概率)。响应快193倍、成本低444倍,适合工单路由、内容审核、测试定级等高频判断场景。
1400 4
最近全网爆火的 Jev 到底是什么?适合干什么、怎么用,一篇讲透!
|
6天前
|
人工智能 并行计算 PyTorch
秋叶 ComfyUI 2026 整合包 v3.2 完整部署教程:Python 3.13 + Torch 2.13 全栈升级
秋叶aaaki ComfyUI 2026年8月整合包v3.2正式发布!全面升级Python 3.13.11、PyTorch 2.13.0+cu130及ComfyUI v0.30.2,原生支持MiniMax H3、Wan 2.2、Qwen-Image-2.1等2026主流音视频/图像模型,解压即用,无需环境配置。
869 5
|
19天前
|
人工智能 自然语言处理 安全
阿里云千问办公 QwenWork详细介绍:产品核心能力、典型场景、价格及常见问题解答
千问办公是阿里云推出的一站式AI办公平台,主打"不止于对话,更注重交付",依托通义千问旗舰大模型,用户一句话即可完成数据分析、PPT生成、视频剪辑等复杂任务,直接输出可用成果。产品深度打通钉钉生态与企业OA,覆盖桌面端、网页端,提供企业标准版198元/人/月等多档订阅方案,新用户注册即赠2000积分,适配工程师、HR、财务等多职业办公场景,成为能动手干活的"全能AI同事"。
3440 10
|
14天前
|
缓存 IDE Java
【保姆级】Android Studio下载、安装和汉化教程(2026最新)
Android Studio 是 Google 官方推出的免费 Android 应用开发集成环境,基于 IntelliJ IDEA,内置模拟器、调试器、性能分析及 Compose 界面工具,功能全面,文档丰富,是安卓开发首选工具。(239字)
1515 1
|
18天前
|
IDE 开发工具
Qoder 上线 Sonus 模型,Computer Use 能力全面增强
Qoder国际版上线全新内置大模型Sonus(/ˈsoʊnəs/),全球领先,专精超长任务执行与电脑操作(Computer Use)。配合Qoder桌面端0.2.3版本,可自主完成编程、金融建模、科研及表格制作等复杂工作。现全面支持Qoder全系产品,效率提升3.2倍。
1898 9
Qoder 上线 Sonus 模型,Computer Use 能力全面增强

热门文章

最新文章