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 {
|
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
|
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
|
||||||
|
|
||||||
if (assetEditorPanel.save(worldadm)){
|
if (assetEditorPanel.save(godworld)){
|
||||||
dispose();
|
dispose();
|
||||||
}
|
}
|
||||||
|
godworld.notifyUpdateListeners();
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
||||||
@ -198,7 +198,7 @@ public class AssetEditorDialog extends EscDialog {
|
|||||||
static public boolean runDialog(Window parent, GodWorld worldadm, JSONObject o, JSONObject old) {
|
static public boolean runDialog(Window parent, GodWorld worldadm, JSONObject o, JSONObject old) {
|
||||||
|
|
||||||
AssetEditorDialog d = new AssetEditorDialog(parent);
|
AssetEditorDialog d = new AssetEditorDialog(parent);
|
||||||
d.worldadm = worldadm;
|
d.godworld = worldadm;
|
||||||
|
|
||||||
|
|
||||||
if (o!=null)
|
if (o!=null)
|
||||||
|
@ -65,6 +65,7 @@ import opensesim.world.Order;
|
|||||||
import opensesim.world.Trader;
|
import opensesim.world.Trader;
|
||||||
import opensesim.world.TradingAPI;
|
import opensesim.world.TradingAPI;
|
||||||
import opensesim.world.World;
|
import opensesim.world.World;
|
||||||
|
import opensesim.world.scheduler.Event;
|
||||||
import opensesim.world.scheduler.FiringEvent;
|
import opensesim.world.scheduler.FiringEvent;
|
||||||
|
|
||||||
import opensesim.world.scheduler.EventListener;
|
import opensesim.world.scheduler.EventListener;
|
||||||
@ -90,6 +91,7 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
JSONObject cfg;
|
JSONObject cfg;
|
||||||
cfg = new JSONObject(Globals.prefs.get("world", "{}"));
|
cfg = new JSONObject(Globals.prefs.get("world", "{}"));
|
||||||
godworld = new GodWorld(cfg);
|
godworld = new GodWorld(cfg);
|
||||||
|
godworld.addAssetPair("AAPL", "EUR");
|
||||||
|
|
||||||
// Get default screen and place our window
|
// Get default screen and place our window
|
||||||
// to the center of this screen
|
// to the center of this screen
|
||||||
@ -598,14 +600,13 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
|
|
||||||
void startSim() {
|
void startSim() {
|
||||||
|
|
||||||
godworld.addAssetPair("AAPL", "EUR");
|
|
||||||
|
|
||||||
JSONObject cfg = new JSONObject("{"
|
JSONObject cfg = new JSONObject("{"
|
||||||
+ "strategy: opensesim.trader.SimpleTrader"
|
+ "strategy: opensesim.trader.SimpleTrader"
|
||||||
+ "}");
|
+ "}");
|
||||||
Trader t = godworld.createTrader(cfg);
|
Trader t = godworld.createTrader(cfg);
|
||||||
t.start();
|
t.start();
|
||||||
|
|
||||||
|
updateGodWorld(godworld);
|
||||||
|
|
||||||
AssetPair p = godworld.getDefaultAssetPair();
|
AssetPair p = godworld.getDefaultAssetPair();
|
||||||
|
|
||||||
@ -621,12 +622,8 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
System.out.printf("Volume: %f\n", o.getVolume());
|
System.out.printf("Volume: %f\n", o.getVolume());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
opensesim.world.scheduler.Scheduler s = godworld.getScheduler();
|
opensesim.world.scheduler.Scheduler s = godworld.getScheduler();
|
||||||
|
|
||||||
|
|
||||||
class MyListener implements EventListener {
|
class MyListener implements EventListener {
|
||||||
|
|
||||||
World world;
|
World world;
|
||||||
@ -635,8 +632,6 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
this.world = world;
|
this.world = world;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long receive(opensesim.world.scheduler.Event task) {
|
public long receive(opensesim.world.scheduler.Event task) {
|
||||||
|
|
||||||
@ -650,7 +645,6 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
|
|
||||||
// MyListener listener = new MyListener(godworld.getWorld());
|
// MyListener listener = new MyListener(godworld.getWorld());
|
||||||
// MyEvent arg = new MyEvent();
|
// MyEvent arg = new MyEvent();
|
||||||
|
|
||||||
// s.startTimerTask(listener, 1000);
|
// s.startTimerTask(listener, 1000);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -909,10 +903,12 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
stopSim();
|
stopSim();
|
||||||
}//GEN-LAST:event_stopButtonActionPerformed
|
}//GEN-LAST:event_stopButtonActionPerformed
|
||||||
|
|
||||||
public class GodWorldEvent extends FiringEvent{
|
public class GodWorldEvent extends Event {
|
||||||
|
|
||||||
public GodWorld goworld;
|
public GodWorld goworld;
|
||||||
public GodWorldEvent(EventListener listener) {
|
|
||||||
super(listener);
|
public GodWorldEvent(GodWorld world) {
|
||||||
|
this.goworld = world;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -922,15 +918,19 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
void updateGodWorld(GodWorld godworld) {
|
void updateGodWorld(GodWorld godworld) {
|
||||||
GodWorldEvent e = new GodWorldEvent(null);
|
GodWorldEvent e = new GodWorldEvent(null);
|
||||||
for (EventListener el : gwlisteners) {
|
for (EventListener el : gwlisteners) {
|
||||||
|
el.receive(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void jCheckBoxMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxMenuItem1ActionPerformed
|
private void jCheckBoxMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxMenuItem1ActionPerformed
|
||||||
// JDialog jd = new opensesim.gui.orderbook.OrderBookDialog(this, false);
|
// JDialog jd = new opensesim.gui.orderbook.OrderBookDialog(this, false);
|
||||||
// jd.setVisible(rootPaneCheckingEnabled);
|
// jd.setVisible(rootPaneCheckingEnabled);
|
||||||
EventListener e = opensesim.gui.orderbook.OrderBookDialog.runDialog(this, godworld);
|
//EventListener e = opensesim.gui.orderbook.OrderBookDialog.runDialog(this, godworld);
|
||||||
gwlisteners.add(e);
|
//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
|
}//GEN-LAST:event_jCheckBoxMenuItem1ActionPerformed
|
||||||
|
@ -26,9 +26,11 @@
|
|||||||
package opensesim.gui.orderbook;
|
package opensesim.gui.orderbook;
|
||||||
|
|
||||||
import java.awt.Frame;
|
import java.awt.Frame;
|
||||||
|
|
||||||
|
import opensesim.world.AssetPair;
|
||||||
|
import opensesim.world.Exchange;
|
||||||
import opensesim.world.GodWorld;
|
import opensesim.world.GodWorld;
|
||||||
import opensesim.world.scheduler.Event;
|
import opensesim.world.scheduler.Event;
|
||||||
import opensesim.world.scheduler.FiringEvent;
|
|
||||||
import opensesim.world.scheduler.EventListener;
|
import opensesim.world.scheduler.EventListener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -48,10 +50,24 @@ public class OrderBookDialog extends javax.swing.JDialog implements EventListene
|
|||||||
|
|
||||||
GodWorld godworld;
|
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);
|
OrderBookDialog d = new OrderBookDialog(parent,false);
|
||||||
|
godworld.addUpdateListener(d);
|
||||||
|
|
||||||
d.godworld=godworld;
|
d.godworld=godworld;
|
||||||
|
d.asset_pair = pair;
|
||||||
|
d.ex=ex;
|
||||||
|
d.init();
|
||||||
|
|
||||||
d.setVisible(true);
|
d.setVisible(true);
|
||||||
// d.dispose();
|
// d.dispose();
|
||||||
|
|
||||||
@ -139,7 +155,15 @@ public class OrderBookDialog extends javax.swing.JDialog implements EventListene
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long receive(Event task) {
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,7 @@ import javax.swing.table.DefaultTableCellRenderer;
|
|||||||
import javax.swing.table.DefaultTableModel;
|
import javax.swing.table.DefaultTableModel;
|
||||||
import javax.swing.table.TableColumn;
|
import javax.swing.table.TableColumn;
|
||||||
import javax.swing.table.TableColumnModel;
|
import javax.swing.table.TableColumnModel;
|
||||||
|
import opensesim.gui.SeSimApplication.GodWorldEvent;
|
||||||
import opensesim.world.AssetPair;
|
import opensesim.world.AssetPair;
|
||||||
import opensesim.world.Exchange;
|
import opensesim.world.Exchange;
|
||||||
import opensesim.world.GodWorld;
|
import opensesim.world.GodWorld;
|
||||||
@ -61,6 +62,9 @@ public class OrderBookPanel extends javax.swing.JPanel implements EventListener
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long receive(Event task) {
|
public long receive(Event task) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
System.out.printf("There is an o event \n");
|
System.out.printf("There is an o event \n");
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
@ -138,14 +142,14 @@ public class OrderBookPanel extends javax.swing.JPanel implements EventListener
|
|||||||
Globals.se.addBookReceiver(type, this);
|
Globals.se.addBookReceiver(type, this);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
public OrderBookPanel() {
|
/* public OrderBookPanel() {
|
||||||
initComponents();
|
initComponents();
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
GodWorld godworld;
|
GodWorld godworld;
|
||||||
|
|
||||||
public void setGodWorld(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) {
|
if (this.godworld == godworld) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -158,10 +162,16 @@ public class OrderBookPanel extends javax.swing.JPanel implements EventListener
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void init(GodWorld godworld,Exchange ex, AssetPair pair){
|
||||||
|
api = ex.getAPI(pair);
|
||||||
|
api.addOrderBookListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates new form OrderBookNew
|
* Creates new form OrderBookNew
|
||||||
*/
|
*/
|
||||||
public OrderBookPanel(GodWorld godworld) {
|
public OrderBookPanel() {
|
||||||
|
|
||||||
initComponents();
|
initComponents();
|
||||||
|
|
||||||
@ -169,12 +179,12 @@ public class OrderBookPanel extends javax.swing.JPanel implements EventListener
|
|||||||
// return;
|
// return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.godworld = godworld;
|
// this.godworld = godworld;
|
||||||
|
|
||||||
Exchange ex = godworld.getDefaultExchange();
|
// Exchange ex = godworld.getDefaultExchange();
|
||||||
AssetPair ap = godworld.getDefaultAssetPair();
|
// AssetPair ap = godworld.getDefaultAssetPair();
|
||||||
api = ex.getAPI(ap);
|
// api = ex.getAPI(ap);
|
||||||
api.addOrderBookListener(this);
|
// api.addOrderBookListener(this);
|
||||||
|
|
||||||
model = (DefaultTableModel) this.list.getModel();
|
model = (DefaultTableModel) this.list.getModel();
|
||||||
trader_column = list.getColumnModel().getColumn(0);
|
trader_column = list.getColumnModel().getColumn(0);
|
||||||
@ -207,7 +217,7 @@ public class OrderBookPanel extends javax.swing.JPanel implements EventListener
|
|||||||
model.setRowCount(ob.size());
|
model.setRowCount(ob.size());
|
||||||
int row = 0;
|
int row = 0;
|
||||||
for (Order ob1 : ob) {
|
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.getLimit(), row, 1);
|
||||||
model.setValueAt(ob1.getVolume(), row, 2);
|
model.setValueAt(ob1.getVolume(), row, 2);
|
||||||
row++;
|
row++;
|
||||||
|
@ -118,6 +118,7 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
|
|||||||
|
|
||||||
long last_time = 0;
|
long last_time = 0;
|
||||||
|
|
||||||
|
double limit = 100;
|
||||||
@Override
|
@Override
|
||||||
public long receive(Event task) {
|
public long receive(Event task) {
|
||||||
// System.out.printf("Here we are !!! %f\n", getWorld().randNextFloat(12f, 27f));
|
// 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);
|
System.out.printf("Here we are: %d - [%d]\n", Thread.currentThread().getId(), diff);
|
||||||
getWorld().schedule(this, 1000);
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ public class AssetPair {
|
|||||||
private final AbstractAsset currency;
|
private final AbstractAsset currency;
|
||||||
|
|
||||||
public static String buildSymbol(String asset,String currency){
|
public static String buildSymbol(String asset,String currency){
|
||||||
return currency+"/"+asset;
|
return asset+"/"+currency;
|
||||||
}
|
}
|
||||||
|
|
||||||
public AbstractAsset getAsset() {
|
public AbstractAsset getAsset() {
|
||||||
|
@ -38,6 +38,7 @@ import opensesim.sesim.interfaces.GetJson;
|
|||||||
|
|
||||||
import opensesim.util.SeSimException;
|
import opensesim.util.SeSimException;
|
||||||
import opensesim.util.idgenerator.IDGenerator;
|
import opensesim.util.idgenerator.IDGenerator;
|
||||||
|
import opensesim.world.scheduler.Event;
|
||||||
import opensesim.world.scheduler.EventListener;
|
import opensesim.world.scheduler.EventListener;
|
||||||
|
|
||||||
import opensesim.world.scheduler.Scheduler;
|
import opensesim.world.scheduler.Scheduler;
|
||||||
@ -262,8 +263,8 @@ public class GodWorld implements GetJson, World {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public AssetPair addAssetPair(String currency, String asset) {
|
public AssetPair addAssetPair(String asset, String currency) {
|
||||||
AssetPair pair = new AssetPair(assets.get(currency), assets.get(asset));
|
AssetPair pair = new AssetPair(assets.get(asset), assets.get(currency));
|
||||||
add(pair);
|
add(pair);
|
||||||
return pair;
|
return pair;
|
||||||
}
|
}
|
||||||
@ -418,4 +419,23 @@ public class GodWorld implements GetJson, World {
|
|||||||
return (max - min) * r + min;
|
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