/
Orderbook.java
179 lines (156 loc) · 5.06 KB
/
Orderbook.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright 2009-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.powertac.common;
import java.util.SortedSet;
import java.util.TreeSet;
import org.joda.time.Instant;
import org.powertac.common.repo.TimeslotRepo;
import org.powertac.common.spring.SpringApplicationContext;
import org.powertac.common.state.ChainedConstructor;
import org.powertac.common.state.Domain;
import org.powertac.common.state.StateChange;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
/**
* An orderbook instance captures a snapshot of the PowerTAC wholesale market's orderbook
* (the un-cleared bids and asks remaining after the market is cleared).
* Each OrderbookEntry contained in the orderbook contains a limit price and
* total un-cleared buy / sell quantity in MWh at that price.
* Each time the market clears, one orderbook is created and sent to brokers for each
* timeslot being traded during that clearing event.
*
* @author Daniel Schnurr, John Collins
* @version 1.2 , 05/02/2011
*/
@Domain(fields = {"timeslot", "clearingPrice", "dateExecuted"})
@XStreamAlias("orderbook")
public class Orderbook
{
@XStreamAsAttribute
private long id = IdGenerator.createId();
private Instant dateExecuted;
/** the timeslot this orderbook is generated for */
@XStreamAsAttribute
private int timeslot;
/** last clearing price, expressed as a positive number - the price/mwh paid
* to sellers. Null if the market did not clear.*/
@XStreamAsAttribute
private Double clearingPrice;
/** sorted set of OrderbookOrder instances representing bids. Since bid
* prices are negative, this is an ascending sort. */
@XStreamImplicit(itemFieldName = "bid")
private SortedSet<OrderbookOrder> bids = new TreeSet<OrderbookOrder>();
/** sorted set of OrderbookOrders representing asks ascending */
@XStreamImplicit(itemFieldName = "ask")
private SortedSet<OrderbookOrder> asks = new TreeSet<OrderbookOrder>();
/**
* Constructor with default product type.
*/
public Orderbook (int timeslot, Double clearingPrice, Instant dateExecuted)
{
super();
this.timeslot = timeslot;
this.clearingPrice = clearingPrice;
this.dateExecuted = dateExecuted;
}
@ChainedConstructor
public Orderbook (Timeslot timeslot, Double clearingPrice, Instant dateExecuted)
{
this (timeslot.getSerialNumber(), clearingPrice, dateExecuted);
}
public long getId ()
{
return id;
}
/**
* Returns the positive price at which the market cleared. This is the price
* paid to sellers, the negative of the price paid by buyers. Null if no
* trades were cleared.
*/
public Double getClearingPrice ()
{
return clearingPrice;
}
/**
* The date when the market cleared.
*/
public Instant getDateExecuted ()
{
return dateExecuted;
}
/**
* The timeslot in which energy commitments represented by cleared trades
* are due.
*/
public int getTimeslotIndex ()
{
return timeslot;
}
public Timeslot getTimeslot ()
{
return getTimeslotRepo().findBySerialNumber(timeslot);
}
/**
* The set of bids (positive energy quantities) that were submitted and
* did not clear, ascending sort. Because bid prices are normally
* negative, the first element is the highest (most negative) price that
* did not clear. Null prices (market orders) sort ahead of non-null
* prices (limit orders).
*/
public SortedSet<OrderbookOrder> getBids ()
{
if (null == bids) {
// this can happen in deserialization
bids = new TreeSet<OrderbookOrder>();
}
return bids;
}
@StateChange
public Orderbook addBid (OrderbookOrder bid)
{
bids.add(bid);
return this;
}
/**
* The set of asks (negative energy quantities) that were submitted and
* did not clear, ascending sort. Null prices (market orders) sort ahead
* of non-null prices (limit orders).
*/
public SortedSet<OrderbookOrder> getAsks ()
{
if (null == asks) {
// this can happen in deserialization
asks = new TreeSet<OrderbookOrder>();
}
return asks;
}
@StateChange
public Orderbook addAsk (OrderbookOrder ask)
{
asks.add(ask);
return this;
}
// access to TimeslotRepo
private static TimeslotRepo timeslotRepo;
private static TimeslotRepo getTimeslotRepo()
{
if (null == timeslotRepo) {
timeslotRepo = (TimeslotRepo) SpringApplicationContext.getBean("timeslotRepo");
}
return timeslotRepo;
}
}