程序员的量化交易之路(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;
}
时间: 2024-09-20 00:23:05

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

程序员的量化交易之路(36)--Lean之数据读取SubscriptionDataReader4

转载需注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrade.top 数据读取需要定义一个读者.直接见下面代码: namespace QuantConnect.Lean.Engine.DataFeeds { /******************************************************** * CLASS DEFINITIONS ***********************

程序员的量化交易之路(1)----规划开篇

其实,一直对量化交易有一定的理解和情节.早在中大读研究生的时候实验室师兄,已经去了中国平安核心投资团队,做高频交易研究的国源师兄的影响,就开始对金融世界产生了浓厚的兴趣.看了丁磊编著的<量化投资--策略与技术>和艾琳.奥尔德里奇的<高频交易>,反复的看,但是都入不了味,现在回过头来想,一个连股都不炒的人怎么可能入味呢.对一些金融的基本概念都不懂. 2013年7月出社会工作后,在10月份确立目标.需要炒股,而且需要一个深入的理解金融的世界.所以确定去考一个证券从业考试,选了证券基础和

程序员的量化交易之路(11)--命令参数解析库JCommonder学习

转载须注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrade.top 在学习量化交易平台的过程中,接触到一个参数解析的库,JCommander.今天把它记录一下. 它的官网为:http://www.jcommander.org/ 1. 概述 Jcommander是一个非常小的框架,用于解析命令行参数. 可以通过注解来描述你的参数选项: import com.beust.jcommander.Paramete

程序员的量化交易之路(2)----Esper文档学习之技术概览(1)

转载请注明出处:http://blog.csdn.net/minimicall/ 在接下来的20个工作日中,我将坚持翻译或者略翻译Esper的官方文档. 为什么需要学习Esper,因为我们需要理解复合事件处理 Complex Event Processing (CEP).在量化交易系统中,CEP是必不可少的.它负责处理海量的实时事件. 关于CEP更多知识,大家可以翻阅网络相关资料.我这里集中在学习开源的CEP系统,Esper.. 今天开始第一篇:技术概览. 1. CEP和事件序列分析 Esper

程序员的量化交易之路(13)--Cointrader类图(1)

转载须注明出处:http://blog.csdn.net/minimicall?viewmode=contents, htpp://cloudtrader.top 今天开始正式切入到Cointrader的源码分析学习中,其主页为:https://github.com/timolson/cointrader. 它是基于Esper的一个比特币云交易托管平台.和我想做的事情比较相近.而且虽然现在没什么功能,但代码量相对少,对于学习非常好. 下面是它的一个类图.: 后面我们会根据这个类图一步步的剖析整个

程序员的量化交易之路(38)--Lean之实时事件处理接口IRealTimeHandler和RealTimeEvent6

转载需注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrade.top/ 这节开始我们要开始说明另外一个模块:实时事件处理模块. 这个模块的工作是什么呢.它就是用来设置一些在特定时间需要执行的任务.比如,每天开盘的时候,你可以做一个什么动作,比如每天收盘的时候你也可以做一个动作.当然还有更为广泛的运用. 在Lean中,是开启一个单独的线程来处理这种定时任务的. 实时事件:RealTimeEvent 实时事件处

程序员的量化交易之路(35)--Lean之DataFeed数据槽3

转载需注明出处:http://blog.csdn.net/minimicall,http://cloudtrade.top/ Lean引擎的模块划分非常的规范.其中DataFeed是数据槽,就是供应数据的模块. 1. IDataFeed 接口 模块的接口为: namespace QuantConnect.Lean.Engine.DataFeeds { /// <summary> /// Datafeed interface for creating custom datafeed source

程序员的量化交易之路(29)--Cointrader之Tick实体(16)

转载需注明出处:http://blog.csdn.net/minimicall,http://cloudtrade.top Tick:什么是Tick,在交易平台中非常常见,其实就 单笔交易时某只证券的基本数据. 我们通过代码来学习吧: package org.cryptocoinpartners.schema; import javax.annotation.Nullable; import javax.persistence.Entity; import javax.persistence.M

程序员的量化交易之路(30)--Cointrader之ConfigUtil(17)

转载须注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrade.top/ 一个完整的系统,必然会涉及到配置文件.配置文件可以是xml.属性文件等形式.大多数而言我们并不需要重写配置读取解析模块,只需要使用开源的即可,这里使用的是apapche.commons.configuration的. 我们这里要说的是Cointrader的ConfigUtil类,它涉及到配置和注解成员之间的赋值等问题. 下面通过代码学习