001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program is distributed in the hope that it will be useful,
010     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     *   
017     *   Derived from code developed by Opentick Corporation, http://www.opentick.com.
018     */
019    
020    package org.otfeed.event;
021    
022    import static org.otfeed.event.IdentityUtil.equalsTo;
023    import static org.otfeed.event.IdentityUtil.safeCompare;
024    import static org.otfeed.event.IdentityUtil.safeHashCode;
025    
026    import java.io.Serializable;
027    import java.util.Date;
028    
029    /**
030     * This class provides historical OHLC values (open, high, low, and close prices) for the requested time period.
031     * Also it provides the volume value (the accumulated number of shares) in the requested time period. In cases
032     * when there were not any transactions within certain time period, all the price values, as well as the volume
033     * value, are set to zero.
034     */
035    public final class OTOHLC implements Comparable<OTOHLC>, Serializable {
036            
037            private static final long serialVersionUID = 4758194026639533702L;
038            
039            private Date timestamp;
040        private double openPrice;
041        private double highPrice;
042        private double lowPrice;
043        private double closePrice;
044        private long volume;
045    
046        /**
047         * Default constructor.
048         */
049        public OTOHLC() { }
050    
051        /**
052         * Constructor.
053         * @param timestamp Time when the event occurred.
054         * @param openPrice Open price.
055         * @param highPrice High price.
056         * @param lowPrice Low price.
057         * @param closePrice Close price.
058         * @param volume Accumulated volume for the requested time period.
059         */
060        public OTOHLC(Date timestamp, 
061                    double openPrice, 
062                    double highPrice, 
063                    double lowPrice, 
064                    double closePrice, 
065                    long volume) {
066            this.timestamp = timestamp;
067            this.openPrice = openPrice;
068            this.highPrice = highPrice;
069            this.lowPrice = lowPrice;
070            this.closePrice = closePrice;
071            this.volume = volume;
072        }
073    
074        /**
075         *
076         * @return Starting time of the time period.
077         */
078        public Date getTimestamp() {
079            return timestamp;
080        }
081    
082        /**
083         * Sets time of the event.
084         * @param timestamp Time when the event occurred.
085         */
086        public void setTimestamp(Date timestamp) {
087            this.timestamp = timestamp;
088        }
089    
090        /**
091         * 
092         * @return High price.
093         */
094        public double getOpenPrice() {
095            return openPrice;
096        }
097    
098        /**
099         * Sets open price.
100         * @param openPrice Open price.
101         */
102        public void setOpenPrice(double openPrice) {
103            this.openPrice = openPrice;
104        }
105    
106        /**
107         * 
108         * @return High price.
109         */
110        public double getHighPrice() {
111            return highPrice;
112        }
113    
114        /**
115         * Sets high price.
116         * @param highPrice High price.
117         */
118        public void setHighPrice(double highPrice) {
119            this.highPrice = highPrice;
120        }
121    
122        /**
123         *
124         * @return Low Price.
125         */
126        public double getLowPrice() {
127            return lowPrice;
128        }
129    
130        /**
131         * Sets low price.
132         * @param lowPrice  Low price.
133         */
134        public void setLowPrice(double lowPrice) {
135            this.lowPrice = lowPrice;
136        }
137    
138        /**
139         *
140         * @return Close price.
141         */
142        public double getClosePrice() {
143            return closePrice;
144        }
145    
146        /**
147         * Sets close price.
148         * @param closePrice Close price.
149         */
150        public void setClosePrice(double closePrice) {
151            this.closePrice = closePrice;
152        }
153    
154        /**
155         * 
156         * @return Accumulated volume for the requested time period.
157         */
158        public long getVolume() {
159            return volume;
160        }
161    
162        /**
163         * Sets accumulated volume for the requested time period.
164         * @param volume Accumulated volume for the requested time period.
165         */
166        public void setVolume(long volume) {
167            this.volume = volume;
168        }
169    
170        @Override
171            public String toString() {
172            return "OTOHLC: timestamp=" + timestamp + ", o=" + openPrice + ", h=" + highPrice + ", l=" + lowPrice + ", c=" + closePrice + ", volume=" + volume;
173        }
174    
175        @Override
176            public int hashCode() {
177           return safeHashCode(timestamp) 
178                    + 3 * safeHashCode(openPrice)
179                    + 5 * safeHashCode(highPrice)
180                    + 7 * safeHashCode(lowPrice)
181                    + 11 * safeHashCode(closePrice)
182                    + 13 * safeHashCode(volume);
183        }
184            
185        @Override
186            public boolean equals(Object o) {
187            return equalsTo(this, o);
188        }
189        
190            public int compareTo(OTOHLC other) {
191                    int rc;
192    
193                    if((rc = safeCompare(timestamp, other.timestamp)) != 0) {
194                            return rc;
195                    }
196                    
197                    if((rc = safeCompare(openPrice, other.openPrice)) != 0) {
198                            return rc;
199                    }
200    
201                    if((rc = safeCompare(highPrice, other.highPrice)) != 0) {
202                            return rc;
203                    }
204    
205                    if((rc = safeCompare(lowPrice, other.lowPrice)) != 0) {
206                            return rc;
207                    }
208    
209                    if((rc = safeCompare(closePrice, other.closePrice)) != 0) {
210                            return rc;
211                    }
212    
213                    if((rc = safeCompare(volume, other.volume)) != 0) {
214                            return rc;
215                    }
216                    
217                    return 0;
218            }
219    }