Archived
0

add top orders

This commit is contained in:
iMoHax
2014-08-04 17:39:15 +04:00
parent cd08c63ce9
commit 3bee31638e
12 changed files with 277 additions and 14 deletions

View File

@@ -3,10 +3,7 @@ package ru.trader.core;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.TreeSet;
import java.util.*;
public class ItemStat {
private final static Logger LOG = LoggerFactory.getLogger(ItemStat.class);
@@ -85,8 +82,8 @@ public class ItemStat {
return offers.size();
}
public Collection<Offer> getOffers() {
return Collections.unmodifiableCollection(offers);
public NavigableSet<Offer> getOffers() {
return Collections.unmodifiableNavigableSet(offers);
}
public Offer getMin() {

View File

@@ -41,4 +41,6 @@ public interface Market {
void updatePrice(Offer offer, double price);
void setChange(boolean change);
Collection<Order> getTop(int limit, double balance, long max);
}

View File

@@ -148,5 +148,40 @@ public abstract class MarketSupport implements Market {
public void setChange(boolean change) {
this.change = change;
}
@Override
public Collection<Order> getTop(int limit, double balance, long max){
LOG.debug("Get top {}", limit);
TreeSet<Order> top = new TreeSet<>();
for (Vendor vendor : getVendors()) {
LOG.debug("Check vendor {}", vendor);
for (Offer sell : vendor.getAllSellOffers()) {
long count = Math.min(max, (long) Math.floor(balance / sell.getPrice()));
LOG.debug("Sell offer {}, count = {}", sell, count);
if (count == 0) continue;
Iterator<Offer> buyers = getStatBuy(sell.getItem()).getOffers().descendingIterator();
while (buyers.hasNext()){
Offer buy = buyers.next();
Order order = new Order(sell, buy, count);
LOG.debug("Buy offer {} profit = {}", buy, order.getProfit());
if (order.getProfit() <= 0 ) break;
if (top.size() == limit){
LOG.debug("Min order {}", top.first());
if (top.first().getProfit() < order.getProfit()) {
LOG.debug("Add to top");
top.add(order);
top.pollFirst();
} else {
LOG.debug("Is low profit, skip");
break;
}
} else {
top.add(order);
}
}
}
}
return top;
}
}

View File

@@ -0,0 +1,76 @@
package ru.trader.core;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public class Order implements Comparable<Order> {
private Offer sell;
private Offer buy;
private double profit;
public Order(Offer sell, Offer buy, long count) {
this.sell = sell;
this.buy = buy;
this.profit = (buy.getPrice() - sell.getPrice()) * count;
}
public Offer getSell() {
return sell;
}
public Offer getBuy() {
return buy;
}
public double getProfit(){
return profit;
}
@Override
public int compareTo(@NotNull Order order) {
Objects.requireNonNull(order, "Not compare with null");
if (this == order) return 0;
int cmp = Double.compare(profit, order.profit);
if (cmp!=0) return cmp;
cmp = Double.compare(sell.getPrice(), order.sell.getPrice());
if (cmp!=0) return cmp;
cmp = Double.compare(order.buy.getPrice(), buy.getPrice());
if (cmp!=0) return cmp;
cmp = sell.compareTo(order.sell);
if (cmp!=0) return cmp;
return buy.compareTo(order.buy);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Order order = (Order) o;
return Double.compare(order.profit, profit) == 0 && buy.equals(order.buy) && sell.equals(order.sell);
}
@Override
public int hashCode() {
int result;
long temp;
result = sell.hashCode();
result = 31 * result + buy.hashCode();
temp = Double.doubleToLongBits(profit);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Order{");
sb.append("sell=").append(sell);
sb.append(", buy=").append(buy);
sb.append(", profit=").append(profit);
sb.append('}');
return sb.toString();
}
}

View File

@@ -33,6 +33,7 @@ public abstract class Vendor implements Comparable<Vendor> {
List<Offer> offers = getOffers()
.stream()
.filter(offer -> offer.hasType(offerType))
.sorted()
.collect(Collectors.toList());
return Collections.unmodifiableCollection(offers);
}