16 package emlab.gen.role.market;
18 import org.springframework.beans.factory.annotation.Autowired;
19 import org.springframework.data.neo4j.support.Neo4jTemplate;
20 import org.springframework.transaction.annotation.Transactional;
22 import agentspring.role.AbstractRole;
40 Neo4jTemplate
template;
47 double clearedVolume = 0d;
48 double clearedPrice = 0d;
49 double totalSupplyPrice = calculateTotalSupplyPriceForMarketForTime(market, time);
50 double totalSupply = calculateTotalSupplyForMarketForTime(market, time);
51 logger.info(
"total supply {} total price {}", totalSupply, totalSupplyPrice);
52 double totalDemandForPrice = calculateTotalDemandForMarketForTimeForPrice(market, time, totalSupplyPrice);
53 logger.info(
"total demand {} for price {}", totalDemandForPrice, totalSupplyPrice);
55 double minimumSupplyPrice = calculateMinimumSupplyPriceForMarketForTime(market, time);
56 double demandAtMinimumSupplyPrice = calculateTotalDemandForMarketForTimeForPrice(market, time, totalSupplyPrice);
58 if (demandAtMinimumSupplyPrice <= 0) {
59 clearedPrice = minimumSupplyPrice;
61 }
else if (totalDemandForPrice > totalSupply) {
63 clearedVolume = totalSupply;
64 if (market.isAuction()) {
65 clearedPrice = calculateTotalDemandForMarketForTimeForPrice(market, time, 0d);
71 double totalOfferAmount = 0d;
72 double previousPrice = 0d;
73 for (
Bid offer : reps.bidRepository.findOffersForMarketForTime(market, time)) {
74 double price = offer.getPrice();
75 double amount = offer.getAmount();
76 double demand = calculateTotalDemandForMarketForTimeForPrice(market, time, price);
77 if (demand < totalOfferAmount + amount) {
79 if (getCurrentTick() > 0) {
80 ClearingPoint cp = reps.clearingPointRepository.findClearingPointForMarketAndTime(market,
81 getCurrentTick() - 1,
false);
83 previousPrice = cp.getPrice();
85 clearedPrice = previousPrice;
86 clearedVolume = totalOfferAmount;
87 }
else if (totalOfferAmount >= demand) {
88 clearedPrice = previousPrice;
89 clearedVolume = totalOfferAmount;
92 clearedVolume = demand;
96 totalOfferAmount += amount;
97 previousPrice = price;
101 point.setAbstractMarket(market);
103 point.setPrice(Math.max(0, clearedPrice));
104 point.setVolume(clearedVolume);
105 point.setForecast(
false);
109 double previousPrice = markAcceptedBids(point,
false);
111 if (market.isAuction()) {
112 point.setPrice(Math.max(0, previousPrice));
115 markAcceptedBids(point,
true);
119 private double markAcceptedBids(
ClearingPoint point,
boolean isSupply) {
120 long time = point.getTime();
122 double clearedPrice = point.getPrice();
123 double clearedVolume = point.getVolume();
124 double totalBidVolume = 0d;
125 double previousPrice = Double.NEGATIVE_INFINITY;
126 double accpetedSamePriceVolume = 0d;
128 Iterable<Bid> bids = isSupply ? reps.bidRepository.findOffersForMarketForTimeBelowPrice(market, time, clearedPrice) : market
129 .isAuction() ? reps.bidRepository.findDemandBidsForMarketForTime(market, time) : reps.bidRepository
130 .findDemandBidsForMarketForTimeAbovePrice(market, time, clearedPrice);
132 for (
Bid bid : bids) {
133 double amount = bid.getAmount();
134 totalBidVolume += amount;
135 accpetedSamePriceVolume = bid.getPrice() == previousPrice ? accpetedSamePriceVolume + amount : amount;
136 if (totalBidVolume < clearedVolume) {
137 bid.setStatus(Bid.ACCEPTED);
138 bid.setAcceptedAmount(bid.getAmount());
140 double lastAvailableBidSize = clearedVolume - (totalBidVolume - accpetedSamePriceVolume);
141 double samePriceVolume = calculateBidsForMarketForTimeForPrice(market, time, bid.getPrice(), isSupply);
142 double adjustRatio = lastAvailableBidSize / samePriceVolume;
143 for (
Bid partBid : isSupply ? reps.bidRepository.findOffersForMarketForTimeForPrice(market, time, bid.getPrice())
144 : reps.bidRepository.findDemandBidsForMarketForTimeForPrice(market, time, bid.getPrice())) {
145 partBid.setStatus(Bid.PARTLY_ACCEPTED);
146 partBid.setAcceptedAmount(partBid.getAmount() * adjustRatio);
150 previousPrice = bid.getPrice();
152 return previousPrice;
155 private double calculateBidsForMarketForTimeForPrice(
DecarbonizationMarket market,
long time,
double price,
boolean isSupply) {
157 return isSupply ? reps.bidRepository.calculateOffersForMarketForTimeForPrice(market, time, price) : reps.bidRepository
158 .calculateDemandBidsForMarketForTimeForPrice(market, time, price);
159 }
catch (NullPointerException npe) {
164 private double calculateTotalDemandForMarketForTimeForPrice(
DecarbonizationMarket market,
long time,
double price) {
166 return market.isAuction() ? reps.bidRepository.calculateTotalDemandForMarketForTime(market, time) : reps.bidRepository
167 .calculateTotalDemandForMarketForTimeForPrice(market, time, price);
168 }
catch (NullPointerException npe) {
175 return reps.bidRepository.calculateTotalSupplyPriceForMarketForTime(market, time);
176 }
catch (NullPointerException e) {
183 return reps.bidRepository.calculateTotalSupplyForMarketForTime(market, time);
184 }
catch (NullPointerException e) {
189 private double calculateMinimumSupplyPriceForMarketForTime(
DecarbonizationMarket market,
long time) {
191 return reps.bidRepository.calculateMinimumSupplyPriceForMarketForTime(market, time);
192 }
catch (NullPointerException e) {
197 public abstract Reps getReps();