程序猿的量化交易之路(26)--Cointrader之Listing挂牌实体(13)

转载须注明出处:http://blog.csdn.net/minimicall?

viewmode=contentshttp://cloudtrade.top

Listing:挂牌。

比方某仅仅股票在某证券交易所挂牌交易。也就是上市交易。

老规矩,通过源代码学习:

package org.cryptocoinpartners.schema;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Cacheable;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
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;

/**
 * Represents the possibility to trade one Asset for another
 */
@SuppressWarnings("UnusedDeclaration")
@Entity //在数据库创建Listing这个表
@Cacheable//可缓存
//命名查询
@NamedQueries({ @NamedQuery(name = "Listing.findByQuoteBase", query = "select a from Listing a where base=?1 and quote=?2 and prompt IS NULL"),
        @NamedQuery(name = "Listing.findByQuoteBasePrompt", query = "select a from Listing a where base=?1 and quote=?2 and prompt=?3") })
@Table(indexes = { @Index(columnList = "base"), @Index(columnList = "quote"), @Index(columnList = "prompt") })
//@Table(name = "listing", uniqueConstraints = { @UniqueConstraint(columnNames = { "base", "quote", "prompt" }),
//@UniqueConstraint(columnNames = { "base", "quote" }) })
public class Listing extends EntityBase {
<pre name="code" class="java">    protected Asset base;
    protected Asset quote;
    private Prompt prompt;

    @ManyToOne(optional = false)
    //@Column(unique = true)
    public Asset getBase() {
        return base;
    }

    @PostPersist
    private void postPersist() {
        //  PersistUtil.clear();
        //  PersistUtil.refresh(this);
        //PersistUtil.merge(this);
        // PersistUtil.close();
        //PersistUtil.evict(this);

    }

    @ManyToOne(optional = false)
    //@Column(unique = true)
    public Asset getQuote() {
        return quote;
    }

    @ManyToOne(optional = true)
    public Prompt getPrompt() {
        return prompt;
    }

    /** will create the listing if it doesn't exist */
    public static Listing forPair(Asset base, Asset quote) {

        try {
            Listing listing = PersistUtil.namedQueryZeroOne(Listing.class, "Listing.findByQuoteBase", base, quote);
            if (listing == null) {
                listing = new Listing(base, quote);
                PersistUtil.insert(listing);
            }
            return listing;
        } catch (NoResultException e) {
            final Listing listing = new Listing(base, quote);
            PersistUtil.insert(listing);
            return listing;
        }
    }

    public static Listing forPair(Asset base, Asset quote, Prompt prompt) {
        try {

            Listing listing = PersistUtil.namedQueryZeroOne(Listing.class, "Listing.findByQuoteBasePrompt", base, quote, prompt);
            if (listing == null) {
                listing = new Listing(base, quote, prompt);
                PersistUtil.insert(listing);
            }
            return listing;
        } catch (NoResultException e) {
            final Listing listing = new Listing(base, quote, prompt);
            PersistUtil.insert(listing);
            return listing;
        }
    }

    @Override
    public String toString() {
        return getSymbol();
    }

    @Transient
    public String getSymbol() {
        if (prompt != null)
            return base.getSymbol() + '.' + quote.getSymbol() + '.' + prompt;
        return base.getSymbol() + '.' + quote.getSymbol();
    }

    @Transient
    protected double getMultiplier() {
        if (prompt != null)
            return prompt.getMultiplier();
        return getContractSize() * getTickSize();
    }

    @Transient
    protected double getTickValue() {
        if (prompt != null)
            return prompt.getTickValue();
        return 1;
    }

    @Transient
    protected double getContractSize() {
        if (prompt != null)
            return prompt.getContractSize();
        return 1;
    }

    @Transient
    protected double getTickSize() {
        if (prompt != null)
            return prompt.getTickSize();
        return getPriceBasis();
    }

    @Transient
    protected Amount getMultiplierAsAmount() {

        return new DiscreteAmount((long) getMultiplier(), getVolumeBasis());
    }

    @Transient
    protected double getVolumeBasis() {
        double volumeBasis = 0;
        if (prompt != null)
            volumeBasis = prompt.getVolumeBasis();
        return volumeBasis == 0 ? getBase().getBasis() : volumeBasis;

    }

    @Transient
    public FeeMethod getMarginMethod() {
        FeeMethod marginMethod = null;
        if (prompt != null)
            marginMethod = prompt.getMarginMethod();
        return marginMethod == null ? null : marginMethod;

    }

    @Transient
    public FeeMethod getMarginFeeMethod() {
        FeeMethod marginFeeMethod = null;
        if (prompt != null)
            marginFeeMethod = prompt.getMarginFeeMethod();
        return marginFeeMethod == null ? null : marginFeeMethod;

    }

    @Transient
    protected double getPriceBasis() {
        double priceBasis = 0;
        if (prompt != null)
            priceBasis = prompt.getPriceBasis();
        return priceBasis == 0 ? getQuote().getBasis() : priceBasis;

    }

    @Transient
    protected Asset getTradedCurrency() {
        if (prompt != null && prompt.getTradedCurrency() != null)
            return prompt.getTradedCurrency();
        return getQuote();
    }

    @Transient
    public FeeMethod getFeeMethod() {
        if (prompt != null && prompt.getFeeMethod() != null)
            return prompt.getFeeMethod();
        return null;
    }

    @Transient
    public double getFeeRate() {
        if (prompt != null && prompt.getFeeRate() != 0)
            return prompt.getFeeRate();
        return 0;
    }

    @Transient
    protected int getMargin() {
        if (prompt != null && prompt.getMargin() != 0)
            return prompt.getMargin();
        return 0;
    }

    public static List<String> allSymbols() {
        List<String> result = new ArrayList<>();
        List<Listing> listings = PersistUtil.queryList(Listing.class, "select x from Listing x");
        for (Listing listing : listings)
            result.add((listing.getSymbol()));
        return result;
    }

    // JPA
    protected Listing() {
    }

    protected void setBase(Asset base) {
        this.base = base;
    }

    protected void setQuote(Asset quote) {
        this.quote = quote;
    }

    protected void setPrompt(Prompt prompt) {
        this.prompt = prompt;
    }

    public Listing(Asset base, Asset quote) {
        this.base = base;
        this.quote = quote;
    }

    public Listing(Asset base, Asset quote, Prompt prompt) {
        this.base = base;
        this.quote = quote;
        this.prompt = prompt;
    }

    public static Listing forSymbol(String symbol) {
        symbol = symbol.toUpperCase();
        final int dot = symbol.indexOf('.');
        if (dot == -1)
            throw new IllegalArgumentException("Invalid Listing symbol: \"" + symbol + "\"");
        final String baseSymbol = symbol.substring(0, dot);
        Asset base = Asset.forSymbol(baseSymbol);
        if (base == null)
            throw new IllegalArgumentException("Invalid base symbol: \"" + baseSymbol + "\"");
        int len = symbol.substring(dot + 1, symbol.length()).indexOf('.');
        len = (len != -1) ? Math.min(symbol.length(), dot + 1 + symbol.substring(dot + 1, symbol.length()).indexOf('.')) : symbol.length();
        final String quoteSymbol = symbol.substring(dot + 1, len);
        final String promptSymbol = (symbol.length() > len) ? symbol.substring(len + 1, symbol.length()) : null;
        Asset quote = Asset.forSymbol(quoteSymbol);
        if (quote == null)
            throw new IllegalArgumentException("Invalid quote symbol: \"" + quoteSymbol + "\"");
        if (promptSymbol == null)
            return Listing.forPair(base, quote);
        Prompt prompt = Prompt.forSymbol(promptSymbol);
        return Listing.forPair(base, quote, prompt);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Listing) {
            Listing listing = (Listing) obj;

            if (!listing.getBase().equals(getBase())) {
                return false;
            }

            if (!listing.getQuote().equals(getQuote())) {
                return false;
            }
            if (listing.getPrompt() != null)
                if (this.getPrompt() != null) {
                    if (!listing.getPrompt().equals(getPrompt()))
                        return false;
                } else {
                    return false;
                }

            return true;
        }

        return false;
    }

    @Override
    public int hashCode() {
        return getPrompt() != null ? getQuote().hashCode() + getBase().hashCode() + getPrompt().hashCode() : getQuote().hashCode() + getBase().hashCode();

    }

}
    protected Asset base;
    protected Asset quote;
    private Prompt prompt;
时间: 2024-10-21 01:28:29

程序猿的量化交易之路(26)--Cointrader之Listing挂牌实体(13)的相关文章

程序猿的量化交易之路(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.

程序猿的量化交易之路(18)--Cointrader之Event实体(6)

转载需注明: 事件,是Esper的重要概念. 这里我们定义个事件类.它是Temporal实体的派生类. 不过对Temporal简单的包装.其代码例如以下: package org.cryptocoinpartners.schema; import org.joda.time.Instant; import javax.persistence.MappedSuperclass; /** * Subclasses of Event may be posted to Context * * @auth

程序猿的量化交易之路(27)--Cointrader之PriceData价格数据(14)

转载须注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrade.top/ PriceData:价格数据.价格数据是市场数据的子类. 详细代码例如以下: package org.cryptocoinpartners.schema; import java.math.BigDecimal; import javax.annotation.Nullable; import javax.persistence.Map

程序猿的量化交易之路(17)--Cointrader之Temporal实体(5)

转载须要注明:http://blog.csdn.net/minimicall,http://cloudtrader.top/ 这一小节说明一个时间实体Temporal实体,它的代码非常easy. package org.cryptocoinpartners.schema; import java.util.Date; import javax.persistence.Basic; import javax.persistence.MappedSuperclass; import javax.pe

程序猿的量化交易之路(28)--Cointrader之Offer报价实体(15)

转载须注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrade.top/ Offer:报价. bid:买方报价 ask:卖方报价 非常easy的一个类.是PriceData的子类.还记得PriceData有什么吧. 时间戳.量.价, 而PriceData又是MarketData的子类,MarketData封装了Market.Market包括市场数据.Exchange,Listing,priceBasis.vo

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

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

程序员的量化交易之路(26)--Cointrader之Listing挂牌实体(13)

转载须注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrade.top Listing:挂牌.比如某只股票在某证券交易所挂牌交易.也就是上市交易. 老规矩,通过源码学习: package org.cryptocoinpartners.schema; import java.util.ArrayList; import java.util.List; import javax.persistence.Cache

程序员的量化交易之路(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的一个比特币云交易托管平台.和我想做的事情比较相近.而且虽然现在没什么功能,但代码量相对少,对于学习非常好. 下面是它的一个类图.: 后面我们会根据这个类图一步步的剖析整个