博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
程序猿的量化交易之路(26)--Cointrader之Listing挂牌实体(13)
阅读量:7109 次
发布时间:2019-06-28

本文共 8863 字,大约阅读时间需要 29 分钟。

转载须注明出处:

viewmode=contents,

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 {
    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
allSymbols() { List
result = new ArrayList<>(); List
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;
你可能感兴趣的文章
tomcat+nginx+shiro+jfinal 实现负载均衡,session共享
查看>>
深入Java虚拟机之虚拟机体系结构
查看>>
Bitcoin的解决的一个核心问题是什么
查看>>
java NIO2(file io)
查看>>
【读书笔记】06 | 白话容器基础(二):隔离与限制
查看>>
Django 学习笔记(二)
查看>>
Linux系统的grep以及正则表达式浅析!
查看>>
Orange的扩展插件Widgets开发(七)-GUI Control
查看>>
用python实现银行转账功能
查看>>
python学习笔记---列表
查看>>
Decorator 装饰器小记
查看>>
Centos6.5搭建Wiki
查看>>
linux 清空文件内容的方法
查看>>
2018“硅谷技划”随笔(三):人工智能火热背后的真正温度
查看>>
综合技术 --ORM
查看>>
我的友情链接
查看>>
[C++]STL萃取学习
查看>>
httpClient学习
查看>>
函数指针和指针函数的区别
查看>>
贪吃蛇小游戏
查看>>