Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.06 KB | None | 0 0
  1. package com.ust.sharedmsgs.ticker;
  2.  
  3. import com.activfinancial.contentplatform.contentgatewayapi.FieldListValidator;
  4. import com.activfinancial.contentplatform.contentgatewayapi.consts.FieldStatus;
  5. import com.activfinancial.middleware.activbase.MiddlewareException;
  6. import com.activfinancial.middleware.fieldtypes.Boolean;
  7. import com.activfinancial.middleware.fieldtypes.*;
  8.  
  9. import java.math.BigDecimal;
  10. import java.sql.Timestamp;
  11.  
  12. /**
  13.  * @author danushka
  14.  * 3/21/2020
  15.  */
  16. public class Quote {
  17.     private long quote_id = 0;
  18.     private Timestamp timestamp = new Timestamp(System.currentTimeMillis());
  19.     private String symbol;
  20.     private BigDecimal price;
  21.     private BigDecimal percentage_change;
  22.     private long trade_time;
  23.     private BigDecimal bid;
  24.     private Integer bid_size;
  25.     private long bid_time;
  26.     private BigDecimal ask;
  27.     private Integer ask_size;
  28.     private long ask_time;
  29.     private BigDecimal limit_heigh;
  30.     private BigDecimal limit_low;
  31.     private boolean is_halted;
  32.     private boolean regular;
  33.     private long cum_vol;
  34.     private BigDecimal cum_val;
  35.     private int event_type;
  36.     private long state;
  37.     private BigDecimal open;
  38.     private String bid_exchange;
  39.     private String ask_exchange;
  40.     private long bid_id = 0;
  41.     private long ask_id = 0;
  42.  
  43.     public Quote() {
  44.         this.symbol = "";
  45.         this.price = new BigDecimal(0);
  46.         this.percentage_change = new BigDecimal(0);
  47.         this.trade_time = 0;
  48.         this.bid = new BigDecimal(0);
  49.         this.bid_size = 0;
  50.         this.bid_time = 0;
  51.         this.bid_exchange = "";
  52.         this.ask = new BigDecimal(0);
  53.         this.ask_size = 0;
  54.         this.ask_time = 0;
  55.         this.ask_exchange = "";
  56.         this.limit_heigh = new BigDecimal(0);
  57.         this.limit_low = new BigDecimal(0);
  58.         this.is_halted = true;
  59.         this.regular = true;
  60.         this.cum_vol = 0;
  61.         this.cum_val = new BigDecimal(0);
  62.         this.event_type = 0;
  63.         this.state = 0;
  64.         this.open = new BigDecimal(0);
  65.     }
  66.  
  67.     private Quote(String symbol, BigDecimal price, BigDecimal percentage_change,
  68.                   long trade_time, BigDecimal bid, Integer bid_size, long bid_time, String bid_exchange, BigDecimal ask,
  69.                   Integer ask_size, long ask_time, String ask_exchange, BigDecimal limit_heigh, BigDecimal limit_low, boolean is_halted,
  70.                   boolean regular, long cum_vol, BigDecimal cum_val, int event_type, long state, BigDecimal open) {
  71.         this.symbol = symbol;
  72.         this.price = price;
  73.         this.percentage_change = percentage_change;
  74.         this.trade_time = trade_time;
  75.         this.bid = bid;
  76.         this.bid_size = bid_size;
  77.         this.bid_time = bid_time;
  78.         this.bid_exchange = bid_exchange;
  79.         this.ask = ask;
  80.         this.ask_size = ask_size;
  81.         this.ask_time = ask_time;
  82.         this.ask_exchange = ask_exchange;
  83.         this.limit_heigh = limit_heigh;
  84.         this.limit_low = limit_low;
  85.         this.is_halted = is_halted;
  86.         this.regular = regular;
  87.         this.cum_vol = cum_vol;
  88.         this.cum_val = cum_val;
  89.         this.event_type = event_type;
  90.         this.state = state;
  91.         this.open = open;
  92.     }
  93.  
  94.     public long getQuote_id() {
  95.         return quote_id;
  96.     }
  97.  
  98.     public void setQuote_id(long quote_id) {
  99.         this.quote_id = quote_id;
  100.     }
  101.  
  102.     public Timestamp getTimestamp() {
  103.         return timestamp;
  104.     }
  105.  
  106.     public void setTimestamp(Timestamp timestamp) {
  107.         this.timestamp = timestamp;
  108.     }
  109.  
  110.     public String getSymbol() {
  111.         return symbol;
  112.     }
  113.  
  114.     public void setSymbol(String symbol) {
  115.         this.symbol = symbol;
  116.     }
  117.  
  118.     public BigDecimal getPrice() {
  119.         return price;
  120.     }
  121.  
  122.     public void setPrice(BigDecimal price) {
  123.         this.price = price;
  124.     }
  125.  
  126.     public BigDecimal getPercentage_change() {
  127.         return percentage_change;
  128.     }
  129.  
  130.     public void setPercentage_change(BigDecimal percentage_change) {
  131.         this.percentage_change = percentage_change;
  132.     }
  133.  
  134.     public long getTrade_time() {
  135.         return trade_time;
  136.     }
  137.  
  138.     public void setTrade_time(long trade_time) {
  139.         this.trade_time = trade_time;
  140.     }
  141.  
  142.     public BigDecimal getBid() {
  143.         return bid;
  144.     }
  145.  
  146.     public void setBid(BigDecimal bid) {
  147.         this.bid = bid;
  148.     }
  149.  
  150.     public Integer getBid_size() {
  151.         return bid_size;
  152.     }
  153.  
  154.     public void setBid_size(Integer bid_size) {
  155.         this.bid_size = bid_size;
  156.     }
  157.  
  158.     public long getBid_time() {
  159.         return bid_time;
  160.     }
  161.  
  162.     public void setBid_time(long bid_time) {
  163.         this.bid_time = bid_time;
  164.     }
  165.  
  166.     public BigDecimal getAsk() {
  167.         return ask;
  168.     }
  169.  
  170.     public void setAsk(BigDecimal ask) {
  171.         this.ask = ask;
  172.     }
  173.  
  174.     public Integer getAsk_size() {
  175.         return ask_size;
  176.     }
  177.  
  178.     public void setAsk_size(Integer ask_size) {
  179.         this.ask_size = ask_size;
  180.     }
  181.  
  182.     public long getAsk_time() {
  183.         return ask_time;
  184.     }
  185.  
  186.     public void setAsk_time(long ask_time) {
  187.         this.ask_time = ask_time;
  188.     }
  189.  
  190.     public BigDecimal getLimit_heigh() {
  191.         return limit_heigh;
  192.     }
  193.  
  194.     public void setLimit_heigh(BigDecimal limit_heigh) {
  195.         this.limit_heigh = limit_heigh;
  196.     }
  197.  
  198.     public BigDecimal getLimit_low() {
  199.         return limit_low;
  200.     }
  201.  
  202.     public void setLimit_low(BigDecimal limit_low) {
  203.         this.limit_low = limit_low;
  204.     }
  205.  
  206.     public boolean isIs_halted() {
  207.         return is_halted;
  208.     }
  209.  
  210.     public void setIs_halted(boolean is_halted) {
  211.         this.is_halted = is_halted;
  212.     }
  213.  
  214.     public boolean isRegular() {
  215.         return regular;
  216.     }
  217.  
  218.     public void setRegular(boolean regular) {
  219.         this.regular = regular;
  220.     }
  221.  
  222.     public long getCum_vol() {
  223.         return cum_vol;
  224.     }
  225.  
  226.     public void setCum_vol(long cum_vol) {
  227.         this.cum_vol = cum_vol;
  228.     }
  229.  
  230.     public BigDecimal getCum_val() {
  231.         return cum_val;
  232.     }
  233.  
  234.     public void setCum_val(BigDecimal cum_val) {
  235.         this.cum_val = cum_val;
  236.     }
  237.  
  238.     public int getEvent_type() {
  239.         return event_type;
  240.     }
  241.  
  242.     public void setEvent_type(int event_type) {
  243.         this.event_type = event_type;
  244.     }
  245.  
  246.     public long getState() {
  247.         return state;
  248.     }
  249.  
  250.     public void setState(long state) {
  251.         this.state = state;
  252.     }
  253.  
  254.     public BigDecimal getOpen() {
  255.         return open;
  256.     }
  257.  
  258.     public void setOpen(BigDecimal open) {
  259.         this.open = open;
  260.     }
  261.  
  262.     public String getBid_exchange() {
  263.         return bid_exchange;
  264.     }
  265.  
  266.     public void setBid_exchange(String bid_exchange) {
  267.         this.bid_exchange = bid_exchange;
  268.     }
  269.  
  270.     public String getAsk_exchange() {
  271.         return ask_exchange;
  272.     }
  273.  
  274.     public void setAsk_exchange(String ask_exchange) {
  275.         this.ask_exchange = ask_exchange;
  276.     }
  277.  
  278.     public long getBid_id() {
  279.         return bid_id;
  280.     }
  281.  
  282.     public void setBid_id(long bid_id) {
  283.         this.bid_id = bid_id;
  284.     }
  285.  
  286.     public long getAsk_id() {
  287.         return ask_id;
  288.     }
  289.  
  290.     public void setAsk_id(long ask_id) {
  291.         this.ask_id = ask_id;
  292.     }
  293.  
  294.  
  295.     public TickerAlgohouseQuoteMsg generateTickerAlgohouseQuoteMsg() {
  296.         return new TickerAlgohouseQuoteMsg(
  297.                 quote_id,
  298.                 this.getMaximumTimestamp(),
  299.                 symbol.replace(".", ""),
  300.                 price,
  301.                 percentage_change,
  302.                 trade_time,
  303.                 bid,
  304.                 bid_size,
  305.                 bid_time,
  306.                 ask,
  307.                 ask_size,
  308.                 ask_time,
  309.                 limit_heigh,
  310.                 limit_low,
  311.                 is_halted,
  312.                 false,      //todo need to be generated
  313.                 cum_vol,
  314.                 new BigDecimal(0),
  315.                 new BigDecimal(0),
  316.                 new BigDecimal(0),
  317.                 new BigDecimal(0),
  318.                 event_type,
  319.                 state
  320.         );
  321.     }
  322.  
  323.     public TickerMakerQuoteMsg generateTickerMakerQuoteMsg() {
  324.         return new TickerMakerQuoteMsg(
  325.                 quote_id,
  326.                 this.getMaximumTimestamp(),
  327.                 symbol.replace(".", ""),
  328.                 price,
  329.                 percentage_change,
  330.                 trade_time,
  331.                 bid,
  332.                 bid_size,
  333.                 bid_time,
  334.                 this.getBidId(),
  335.                 ask,
  336.                 ask_size,
  337.                 ask_time,
  338.                 this.getAskId(),
  339.                 limit_heigh,
  340.                 limit_low,
  341.                 is_halted,
  342.                 true,  //todo need to be generated
  343.                 cum_vol,
  344.                 cum_val,
  345.                 event_type,
  346.                 open
  347.         );
  348.     }
  349.  
  350.     private long getMaximumTimestamp() {
  351.         return Math.max(this.bid_time, Math.max(this.ask_time, this.trade_time));
  352.     }
  353.  
  354.     public Object convert(String type) {
  355.         if (type.equals("ALGOV2")) {
  356.             return this.generateTickerAlgohouseQuoteMsg();
  357.         } else if (type.equals("MAKERV2")) {
  358.             return this.generateTickerMakerQuoteMsg();
  359.         } else {
  360.             throw new NullPointerException("The subscription type is required");
  361.         }
  362.     }
  363.  
  364.     public long getAskId() {
  365.         return 0;
  366.     }
  367.  
  368.     public long getBidId() {
  369.         return 0;
  370.     }
  371.  
  372.     public Quote updateFields(FieldListValidator fieldListValidator) {
  373.         String symbolId = (String) this.getValue(TickerField.SYMBOL, String.class, fieldListValidator);
  374.         BigDecimal price = (BigDecimal) this.getValue(TickerField.PRICE, BigDecimal.class, fieldListValidator);
  375.         BigDecimal percentage_change = (BigDecimal) this.getValue(TickerField.PERCENTAGE_CHANGE, BigDecimal.class, fieldListValidator);
  376.         long trade_time = (Long) this.getValue(TickerField.TRADE_TIME, Long.class, fieldListValidator);
  377.         BigDecimal bid = (BigDecimal) this.getValue(TickerField.BID, BigDecimal.class, fieldListValidator);
  378.         Integer bid_size = Math.toIntExact((Long) this.getValue(TickerField.BID_SIZE, Long.class, fieldListValidator));
  379.         long bid_time = (Long) this.getValue(TickerField.BID_TIME, Long.class, fieldListValidator);
  380.         String bid_exchange = (String) this.getValue(TickerField.BID_EXCHANGE, String.class, fieldListValidator);
  381.         BigDecimal ask = (BigDecimal) this.getValue(TickerField.ASK, BigDecimal.class, fieldListValidator);
  382.         Integer ask_size = Math.toIntExact((Long) this.getValue(TickerField.ASK_SIZE, Long.class, fieldListValidator));
  383.         long ask_time = (Long) this.getValue(TickerField.ASK_TIME, Long.class, fieldListValidator);
  384.         String ask_exchange = (String) this.getValue(TickerField.ASK_EXCHNAGE, String.class, fieldListValidator);
  385.         BigDecimal limit_heigh = (BigDecimal) this.getValue(TickerField.LIMIT_HIGH, BigDecimal.class, fieldListValidator);
  386.         BigDecimal limit_low = (BigDecimal) this.getValue(TickerField.LIMIT_LOW, BigDecimal.class, fieldListValidator);
  387.         boolean is_halted = false;
  388.         boolean regular = true;
  389.         long cum_vol = (Long) this.getValue(TickerField.CUM_VOL, Long.class, fieldListValidator);
  390.         BigDecimal cum_val = (BigDecimal) this.getValue(TickerField.CUM_VAL, BigDecimal.class, fieldListValidator);
  391.         int event_type = (Integer) this.getValue(TickerField.EVENT_TYPE, Integer.class, fieldListValidator);
  392.         long state = (Long) this.getValue(TickerField.STATE, Long.class, fieldListValidator);
  393.         BigDecimal open = (BigDecimal) this.getValue(TickerField.OPEN, BigDecimal.class, fieldListValidator);
  394.  
  395.         this.setTimestamp(new Timestamp(System.currentTimeMillis()));
  396.         this.setSymbol(symbolId);
  397.         this.setPrice(price);
  398.         this.setPercentage_change(percentage_change);
  399.         this.setTrade_time(trade_time);
  400.         this.setBid(bid);
  401.         this.setBid_size(bid_size);
  402.         this.setBid_time(bid_time);
  403.         this.setBid_exchange(bid_exchange);
  404.         this.setAsk(ask);
  405.         this.setAsk_size(ask_size);
  406.         this.setAsk_time(ask_time);
  407.         this.setAsk_exchange(ask_exchange);
  408.         this.setLimit_heigh(limit_heigh);
  409.         this.setLimit_low(limit_low);
  410.         this.setIs_halted(is_halted);
  411.         this.setRegular(regular);
  412.         this.setCum_vol(cum_vol);
  413.         this.setCum_val(cum_val);
  414.         this.setEvent_type(event_type);
  415.         this.setState(state);
  416.         this.setOpen(open);
  417.  
  418.         return this;
  419.     }
  420.  
  421.     private Object getValue(TickerField fieldId, Class<?> type, FieldListValidator fieldListValidator) {
  422.         FieldListValidator.Field field;
  423.         if ((field = fieldListValidator.getField(fieldId.getCode())) != null) {
  424.             try {
  425.                 if (FieldStatus.FIELD_STATUS_DEFINED == field.fieldStatus) {
  426.                     switch (field.fieldType.getType()) {
  427.                         case 1:
  428.                             return (((Time) field.fieldType).getCalendar().getTimeInMillis());
  429.                         case 4:
  430.                             return ((UInt) field.fieldType).getValue();
  431.                         case 5:
  432.                             return BigDecimal.valueOf(((Rational) field.fieldType).getDouble());
  433.                         case 6:
  434.                             return BigDecimal.valueOf(((TRational) (field.fieldType)).getRational().getDouble());
  435.                         case 13:
  436.                             return ((Boolean) field.fieldType).getValue();
  437.                         default:
  438.                             return field.fieldType.toString();
  439.                     }
  440.                 }
  441.             } catch (MiddlewareException e) {
  442.                 e.printStackTrace();
  443.             }
  444.         }
  445.         return this.getDefaultTypeValue(type);
  446.     }
  447.  
  448.     public Object getDefaultTypeValue(Class<?> type) {
  449.         if (type == String.class) return "";
  450.         if (type == Integer.class) return 0;
  451.         if (type == BigDecimal.class) return new BigDecimal(0);
  452.         if (type == Long.class) return 0L;
  453.         else return "";
  454.     }
  455. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement