OrderBookPanel with callback.
This commit is contained in:
parent
99fea69520
commit
974c1cb81e
@ -41,7 +41,7 @@ import org.json.JSONObject;
|
||||
*/
|
||||
public class AssetEditorDialog extends EscDialog {
|
||||
|
||||
GodWorld worldadm;
|
||||
GodWorld godworld;
|
||||
|
||||
|
||||
/**
|
||||
@ -124,10 +124,10 @@ public class AssetEditorDialog extends EscDialog {
|
||||
|
||||
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
|
||||
|
||||
if (assetEditorPanel.save(worldadm)){
|
||||
if (assetEditorPanel.save(godworld)){
|
||||
dispose();
|
||||
}
|
||||
|
||||
godworld.notifyUpdateListeners();
|
||||
return;
|
||||
|
||||
|
||||
@ -198,7 +198,7 @@ public class AssetEditorDialog extends EscDialog {
|
||||
static public boolean runDialog(Window parent, GodWorld worldadm, JSONObject o, JSONObject old) {
|
||||
|
||||
AssetEditorDialog d = new AssetEditorDialog(parent);
|
||||
d.worldadm = worldadm;
|
||||
d.godworld = worldadm;
|
||||
|
||||
|
||||
if (o!=null)
|
||||
|
@ -65,6 +65,7 @@ import opensesim.world.Order;
|
||||
import opensesim.world.Trader;
|
||||
import opensesim.world.TradingAPI;
|
||||
import opensesim.world.World;
|
||||
import opensesim.world.scheduler.Event;
|
||||
import opensesim.world.scheduler.FiringEvent;
|
||||
|
||||
import opensesim.world.scheduler.EventListener;
|
||||
@ -90,6 +91,7 @@ public class SeSimApplication extends javax.swing.JFrame {
|
||||
JSONObject cfg;
|
||||
cfg = new JSONObject(Globals.prefs.get("world", "{}"));
|
||||
godworld = new GodWorld(cfg);
|
||||
godworld.addAssetPair("AAPL", "EUR");
|
||||
|
||||
// Get default screen and place our window
|
||||
// to the center of this screen
|
||||
@ -597,36 +599,31 @@ public class SeSimApplication extends javax.swing.JFrame {
|
||||
}
|
||||
|
||||
void startSim() {
|
||||
|
||||
godworld.addAssetPair("AAPL", "EUR");
|
||||
|
||||
|
||||
JSONObject cfg = new JSONObject("{"
|
||||
+ "strategy: opensesim.trader.SimpleTrader"
|
||||
+ "}");
|
||||
Trader t = godworld.createTrader(cfg);
|
||||
t.start();
|
||||
|
||||
|
||||
updateGodWorld(godworld);
|
||||
|
||||
AssetPair p = godworld.getDefaultAssetPair();
|
||||
|
||||
|
||||
opensesim.world.Exchange ex = godworld.getDefaultExchange();
|
||||
TradingAPI api = ex.getAPI(p);
|
||||
|
||||
TradingAPI api = ex.getAPI(p);
|
||||
|
||||
Set<Order> ob;
|
||||
|
||||
|
||||
ob = api.getOrderBook(Order.Type.BUY);
|
||||
|
||||
for (Order o: ob){
|
||||
|
||||
for (Order o : ob) {
|
||||
double v = o.getVolume();
|
||||
System.out.printf("Volume: %f\n",o.getVolume());
|
||||
System.out.printf("Volume: %f\n", o.getVolume());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
opensesim.world.scheduler.Scheduler s = godworld.getScheduler();
|
||||
|
||||
|
||||
class MyListener implements EventListener {
|
||||
|
||||
World world;
|
||||
@ -635,11 +632,9 @@ public class SeSimApplication extends javax.swing.JFrame {
|
||||
this.world = world;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public long receive(opensesim.world.scheduler.Event task) {
|
||||
|
||||
|
||||
System.out.printf("Received an Event %d\n", Thread.currentThread().getId());
|
||||
// e.count++;
|
||||
world.schedule(this, 1000);
|
||||
@ -648,10 +643,9 @@ public class SeSimApplication extends javax.swing.JFrame {
|
||||
|
||||
}
|
||||
|
||||
// MyListener listener = new MyListener(godworld.getWorld());
|
||||
// MyListener listener = new MyListener(godworld.getWorld());
|
||||
// MyEvent arg = new MyEvent();
|
||||
|
||||
// s.startTimerTask(listener, 1000);
|
||||
// s.startTimerTask(listener, 1000);
|
||||
|
||||
/*
|
||||
|
||||
@ -909,29 +903,35 @@ public class SeSimApplication extends javax.swing.JFrame {
|
||||
stopSim();
|
||||
}//GEN-LAST:event_stopButtonActionPerformed
|
||||
|
||||
public class GodWorldEvent extends FiringEvent{
|
||||
public GodWorld goworld;
|
||||
public GodWorldEvent(EventListener listener) {
|
||||
super(listener);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
HashSet <EventListener> gwlisteners = new HashSet<>();
|
||||
|
||||
void updateGodWorld(GodWorld godworld){
|
||||
GodWorldEvent e = new GodWorldEvent(null);
|
||||
for (EventListener el : gwlisteners){
|
||||
public class GodWorldEvent extends Event {
|
||||
|
||||
public GodWorld goworld;
|
||||
|
||||
public GodWorldEvent(GodWorld world) {
|
||||
this.goworld = world;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
HashSet<EventListener> gwlisteners = new HashSet<>();
|
||||
|
||||
void updateGodWorld(GodWorld godworld) {
|
||||
GodWorldEvent e = new GodWorldEvent(null);
|
||||
for (EventListener el : gwlisteners) {
|
||||
el.receive(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void jCheckBoxMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxMenuItem1ActionPerformed
|
||||
// JDialog jd = new opensesim.gui.orderbook.OrderBookDialog(this, false);
|
||||
// jd.setVisible(rootPaneCheckingEnabled);
|
||||
EventListener e = opensesim.gui.orderbook.OrderBookDialog.runDialog(this, godworld);
|
||||
gwlisteners.add(e);
|
||||
|
||||
//EventListener e = opensesim.gui.orderbook.OrderBookDialog.runDialog(this, godworld);
|
||||
//gwlisteners.add(e);
|
||||
|
||||
AssetPair ap = godworld.getDefaultAssetPair();
|
||||
opensesim.world.Exchange ex = godworld.getDefaultExchange();
|
||||
opensesim.gui.orderbook.OrderBookDialog.runDialog(this, godworld, ex, ap);
|
||||
|
||||
|
||||
}//GEN-LAST:event_jCheckBoxMenuItem1ActionPerformed
|
||||
|
||||
|
@ -26,9 +26,11 @@
|
||||
package opensesim.gui.orderbook;
|
||||
|
||||
import java.awt.Frame;
|
||||
|
||||
import opensesim.world.AssetPair;
|
||||
import opensesim.world.Exchange;
|
||||
import opensesim.world.GodWorld;
|
||||
import opensesim.world.scheduler.Event;
|
||||
import opensesim.world.scheduler.FiringEvent;
|
||||
import opensesim.world.scheduler.EventListener;
|
||||
|
||||
/**
|
||||
@ -48,10 +50,24 @@ public class OrderBookDialog extends javax.swing.JDialog implements EventListene
|
||||
|
||||
GodWorld godworld;
|
||||
|
||||
void init(){
|
||||
this.setTitle(asset_pair.getSymbol()+" on "+ex.getSymbol());
|
||||
this.orderBookPanel1.init(godworld, ex, asset_pair);
|
||||
}
|
||||
|
||||
static public EventListener runDialog(Frame parent, GodWorld godworld){
|
||||
AssetPair asset_pair;
|
||||
Exchange ex;
|
||||
|
||||
|
||||
static public EventListener runDialog(Frame parent, GodWorld godworld, Exchange ex, AssetPair pair){
|
||||
OrderBookDialog d = new OrderBookDialog(parent,false);
|
||||
godworld.addUpdateListener(d);
|
||||
|
||||
d.godworld=godworld;
|
||||
d.asset_pair = pair;
|
||||
d.ex=ex;
|
||||
d.init();
|
||||
|
||||
d.setVisible(true);
|
||||
// d.dispose();
|
||||
|
||||
@ -139,7 +155,15 @@ public class OrderBookDialog extends javax.swing.JDialog implements EventListene
|
||||
|
||||
@Override
|
||||
public long receive(Event task) {
|
||||
this.orderBookPanel1.setGodWorld(godworld);
|
||||
|
||||
if (task.getClass() != GodWorld.UpdateEvent.class){
|
||||
return 0;
|
||||
}
|
||||
|
||||
// opensesim.gui.SeSimApplication.GodWorldEvent gwe = (opensesim.gui.SeSimApplication.GodWorldEvent) task;
|
||||
|
||||
System.out.printf("Godworld updated\n");
|
||||
// this.orderBookPanel1.setGodWorld(gwe.goworld);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
@ -38,6 +38,7 @@ import javax.swing.table.DefaultTableCellRenderer;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import javax.swing.table.TableColumn;
|
||||
import javax.swing.table.TableColumnModel;
|
||||
import opensesim.gui.SeSimApplication.GodWorldEvent;
|
||||
import opensesim.world.AssetPair;
|
||||
import opensesim.world.Exchange;
|
||||
import opensesim.world.GodWorld;
|
||||
@ -61,6 +62,9 @@ public class OrderBookPanel extends javax.swing.JPanel implements EventListener
|
||||
|
||||
@Override
|
||||
public long receive(Event task) {
|
||||
|
||||
|
||||
|
||||
System.out.printf("There is an o event \n");
|
||||
|
||||
synchronized (this) {
|
||||
@ -138,14 +142,14 @@ public class OrderBookPanel extends javax.swing.JPanel implements EventListener
|
||||
Globals.se.addBookReceiver(type, this);
|
||||
}
|
||||
*/
|
||||
public OrderBookPanel() {
|
||||
/* public OrderBookPanel() {
|
||||
initComponents();
|
||||
}
|
||||
|
||||
*/
|
||||
GodWorld godworld;
|
||||
|
||||
public void setGodWorld(GodWorld godworld) {
|
||||
// is our world alread the godworld to set?
|
||||
// is our world already the godworld to set?
|
||||
if (this.godworld == godworld) {
|
||||
return;
|
||||
}
|
||||
@ -157,11 +161,17 @@ public class OrderBookPanel extends javax.swing.JPanel implements EventListener
|
||||
api.addOrderBookListener(this);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void init(GodWorld godworld,Exchange ex, AssetPair pair){
|
||||
api = ex.getAPI(pair);
|
||||
api.addOrderBookListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new form OrderBookNew
|
||||
*/
|
||||
public OrderBookPanel(GodWorld godworld) {
|
||||
public OrderBookPanel() {
|
||||
|
||||
initComponents();
|
||||
|
||||
@ -169,12 +179,12 @@ public class OrderBookPanel extends javax.swing.JPanel implements EventListener
|
||||
// return;
|
||||
}
|
||||
|
||||
this.godworld = godworld;
|
||||
// this.godworld = godworld;
|
||||
|
||||
Exchange ex = godworld.getDefaultExchange();
|
||||
AssetPair ap = godworld.getDefaultAssetPair();
|
||||
api = ex.getAPI(ap);
|
||||
api.addOrderBookListener(this);
|
||||
// Exchange ex = godworld.getDefaultExchange();
|
||||
// AssetPair ap = godworld.getDefaultAssetPair();
|
||||
// api = ex.getAPI(ap);
|
||||
// api.addOrderBookListener(this);
|
||||
|
||||
model = (DefaultTableModel) this.list.getModel();
|
||||
trader_column = list.getColumnModel().getColumn(0);
|
||||
@ -207,7 +217,7 @@ public class OrderBookPanel extends javax.swing.JPanel implements EventListener
|
||||
model.setRowCount(ob.size());
|
||||
int row = 0;
|
||||
for (Order ob1 : ob) {
|
||||
model.setValueAt(ob1.getAccount().getOwner().getName(), row, 0);
|
||||
// model.setValueAt(ob1.getAccount().getOwner().getName(), row, 0);
|
||||
model.setValueAt(ob1.getLimit(), row, 1);
|
||||
model.setValueAt(ob1.getVolume(), row, 2);
|
||||
row++;
|
||||
|
@ -118,6 +118,7 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
|
||||
|
||||
long last_time = 0;
|
||||
|
||||
double limit = 100;
|
||||
@Override
|
||||
public long receive(Event task) {
|
||||
// System.out.printf("Here we are !!! %f\n", getWorld().randNextFloat(12f, 27f));
|
||||
@ -127,6 +128,14 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
|
||||
|
||||
System.out.printf("Here we are: %d - [%d]\n", Thread.currentThread().getId(), diff);
|
||||
getWorld().schedule(this, 1000);
|
||||
|
||||
AssetPair p = getWorld().getDefaultAssetPair();
|
||||
|
||||
ex = getWorld().getDefaultExchange();
|
||||
api = ex.getAPI(p);
|
||||
Order o = api.createOrder(account, Order.Type.BUY, 100, limit);
|
||||
limit += 12;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -36,7 +36,7 @@ public class AssetPair {
|
||||
private final AbstractAsset currency;
|
||||
|
||||
public static String buildSymbol(String asset,String currency){
|
||||
return currency+"/"+asset;
|
||||
return asset+"/"+currency;
|
||||
}
|
||||
|
||||
public AbstractAsset getAsset() {
|
||||
|
@ -38,6 +38,7 @@ import opensesim.sesim.interfaces.GetJson;
|
||||
|
||||
import opensesim.util.SeSimException;
|
||||
import opensesim.util.idgenerator.IDGenerator;
|
||||
import opensesim.world.scheduler.Event;
|
||||
import opensesim.world.scheduler.EventListener;
|
||||
|
||||
import opensesim.world.scheduler.Scheduler;
|
||||
@ -262,8 +263,8 @@ public class GodWorld implements GetJson, World {
|
||||
}
|
||||
}
|
||||
|
||||
public AssetPair addAssetPair(String currency, String asset) {
|
||||
AssetPair pair = new AssetPair(assets.get(currency), assets.get(asset));
|
||||
public AssetPair addAssetPair(String asset, String currency) {
|
||||
AssetPair pair = new AssetPair(assets.get(asset), assets.get(currency));
|
||||
add(pair);
|
||||
return pair;
|
||||
}
|
||||
@ -418,4 +419,23 @@ public class GodWorld implements GetJson, World {
|
||||
return (max - min) * r + min;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
// Update listeners
|
||||
// --------------------------------------------------------------------
|
||||
private final HashSet<EventListener> update_listeners = new HashSet<>();
|
||||
|
||||
public class UpdateEvent extends Event {
|
||||
}
|
||||
|
||||
public void addUpdateListener(EventListener u) {
|
||||
update_listeners.add(u);
|
||||
}
|
||||
|
||||
public void notifyUpdateListeners() {
|
||||
Event e = new UpdateEvent();
|
||||
for (EventListener l : update_listeners) {
|
||||
l.receive(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user