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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/116308.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • java构造函数方法声明无效_如何构造函数

    java构造函数方法声明无效_如何构造函数一、什么是构造函数java构造函数,也叫构造方法,是java中一种特殊的函数。函数名与相同,无返回值。作用:一般用来初始化成员属性和成员方法的,即new对象产生后,就调用了对象了属性和方法。在现实生活中,很多事物一出现,就天生具有某些属性和行为。比如人一出生,就有年龄、身高、体重、就会哭;汽车一出产,就有颜色、有外观、可以运行等。这些,我们就可以将这些天然的属性和行为定义在构造函数中,…

  • ueditor使用注意事项

    ueditor使用注意事项

  • js中数组截取方法

    js中数组截取方法slice()vararray=[1,5,3,9,8];varcut=array.slice(1,4);console.log(cut);打印出的结果是[5,3,9]值得注意的是,slice()不会操作原有数组,所以打印array的话,是不会变的vararray=[1,5,3,9,8];varcut=array.slice(1,4);console.log(cut);console.log(array);打印结果是[5,3,9][1,5,

  • c++const用法_const头文件

    c++const用法_const头文件C++——const

  • 憨批的语义分割3——unet模型详解以及训练自己的unet模型(划分斑马线)[通俗易懂]

    憨批的语义分割3——unet模型详解以及训练自己的unet模型(划分斑马线)[通俗易懂]憨批的语义分割3——unet模型详解以及训练自己的unet模型(划分斑马线)学习前言什么是unet模型训练的是什么1、训练文件详解2、LOSS函数的组成训练代码1、文件存放方式2、训练文件3、预测文件训练结果学习前言在这一个BLOG里,我会跟大家讲一下什么是unet模型,以及如何训练自己的unet模型,其训练与上一篇的segnet模型差距不大,但是结构上有一定的差距。什么是unet模型u…

  • c语言ascii码对照表_c语言注册码

    c语言ascii码对照表_c语言注册码C语言:ASCII码对照表

    2022年10月22日

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号