From 959fa06068985514690797dcacbdf466f904ec12 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 4 Sep 2010 19:55:25 +0100 Subject: [PATCH] arbitrage seems to work now; need to remove debugging printfs --- .../tedpearson/ypp/market/MarketUploader.java | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/com/tedpearson/ypp/market/MarketUploader.java b/src/com/tedpearson/ypp/market/MarketUploader.java index 11f085b..df87f43 100644 --- a/src/com/tedpearson/ypp/market/MarketUploader.java +++ b/src/com/tedpearson/ypp/market/MarketUploader.java @@ -39,6 +39,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis private Window window = null; private JButton findMarket = null; private JLabel resultSummary = null; + private JLabel arbitrageResult = null; private final static String PCTB_LIVE_HOST_URL = "http://pctb.crabdance.com/"; private final static String PCTB_TEST_HOST_URL = "http://pctb.ilk.org/"; @@ -252,7 +253,8 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis } frame = new JFrame("MarketUploader"); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - frame.getContentPane().setLayout(new GridLayout(2,1)); + GridLayout layout = new GridLayout(2,1); + frame.getContentPane().setLayout(layout); //frame.setPreferredSize(new Dimension(200, 60)); findMarket = new JButton("Upload Market Data"); @@ -262,6 +264,7 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis new Thread() { public void run() { resultSummary.setText(""); + arbitrageResult.setText(""); try { runPCTB(); } catch(Exception e) { @@ -285,6 +288,13 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis resultSummary = new JLabel("ready"); frame.add(resultSummary); + arbitrageResult = new JLabel(""); + + if (showArbitrage) { + layout.setRows(layout.getRows() + 1); + frame.add(arbitrageResult); + } + frame.pack(); } @@ -994,19 +1004,28 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis while (true) { int[] buy, sell; try { - buy = buys.first(); + // NB "sell" means they sell, ie we buy sell = sells.last(); + buy = buys.first(); } catch (NoSuchElementException e) { break; } + + int unitprofit = buy[0] - sell[0]; int count = buy[1] < sell[1] ? buy[1] : sell[1]; - System.out.println(" buy @"+buy[0]+" x"+buy[1]+" sell @"+sell[0]+" x"+sell[1]+" => x"+count); - profit += count * (buy[0] - sell[0]); + System.out.println(" sell @"+sell[0]+" x"+sell[1]+" buy @"+buy[0]+" x"+buy[1] + +" => x"+count+" @"+unitprofit); + + if (unitprofit <= 0) + break; + + profit += count * unitprofit; buy[1] -= count; sell[1] -= count; if (buy[1]==0) buys.remove(buy); - if (sell[1]==0) buys.remove(sell); + if (sell[1]==0) sells.remove(sell); } + System.out.println(" PROFIT "+profit); return profit; } @@ -1050,6 +1069,11 @@ public class MarketUploader implements TopLevelWindowListener, GUIInitializedLis } } arbitrage += calculateArbitrageCommodity(arb_bs); + if (arbitrage != 0) { + arbitrageResult.setText("arbitrage: "+arbitrage+" poe"); + } else { + arbitrageResult.setText("no arbitrage"); + } } } -- 2.30.2