Work on new Trading API
This commit is contained in:
parent
ba03d4d292
commit
5023b2ad7a
@ -363,7 +363,7 @@ public class MasterChart extends javax.swing.JPanel implements QuoteReceiver {
|
|||||||
}//GEN-LAST:event_chartMouseMoved
|
}//GEN-LAST:event_chartMouseMoved
|
||||||
|
|
||||||
private void chartMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_chartMousePressed
|
private void chartMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_chartMousePressed
|
||||||
System.out.printf("Mauspress\n");
|
// System.out.printf("Mauspress\n");
|
||||||
this.formMousePressed(evt);
|
this.formMousePressed(evt);
|
||||||
}//GEN-LAST:event_chartMousePressed
|
}//GEN-LAST:event_chartMousePressed
|
||||||
|
|
||||||
|
@ -38,6 +38,7 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
import java.util.Set;
|
||||||
import javax.swing.JDialog;
|
import javax.swing.JDialog;
|
||||||
import javax.swing.JFileChooser;
|
import javax.swing.JFileChooser;
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
@ -45,6 +46,7 @@ import javax.swing.JOptionPane;
|
|||||||
import javax.swing.filechooser.FileFilter;
|
import javax.swing.filechooser.FileFilter;
|
||||||
import javax.swing.filechooser.FileNameExtensionFilter;
|
import javax.swing.filechooser.FileNameExtensionFilter;
|
||||||
import opensesim.gui.AssetPairEditor.NewJDialog;
|
import opensesim.gui.AssetPairEditor.NewJDialog;
|
||||||
|
import static opensesim.gui.Globals.getWorld;
|
||||||
|
|
||||||
import opensesim.gui.exchangeeditor.ExchangeListDialog;
|
import opensesim.gui.exchangeeditor.ExchangeListDialog;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
@ -54,9 +56,13 @@ import opensesim.old_sesim.AutoTraderInterface;
|
|||||||
import opensesim.old_sesim.Exchange;
|
import opensesim.old_sesim.Exchange;
|
||||||
import opensesim.old_sesim.Scheduler;
|
import opensesim.old_sesim.Scheduler;
|
||||||
import opensesim.old_sesim.Scheduler.TimerTaskDef;
|
import opensesim.old_sesim.Scheduler.TimerTaskDef;
|
||||||
|
import opensesim.world.AbstractAsset;
|
||||||
|
import opensesim.world.AssetPair;
|
||||||
|
|
||||||
import opensesim.world.GodWorld;
|
import opensesim.world.GodWorld;
|
||||||
|
import opensesim.world.Order;
|
||||||
import opensesim.world.Trader;
|
import opensesim.world.Trader;
|
||||||
|
import opensesim.world.TradingAPI;
|
||||||
import opensesim.world.World;
|
import opensesim.world.World;
|
||||||
|
|
||||||
import opensesim.world.scheduler.EventListener;
|
import opensesim.world.scheduler.EventListener;
|
||||||
@ -601,7 +607,7 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
|
|
||||||
void startSim() {
|
void startSim() {
|
||||||
|
|
||||||
GodWorld godworld = new GodWorld(Globals.getWorld());
|
// GodWorld godworld = new GodWorld(Globals.getWorld());
|
||||||
|
|
||||||
JSONObject cfg = new JSONObject("{"
|
JSONObject cfg = new JSONObject("{"
|
||||||
+ "strategy: opensesim.trader.SimpleTrader"
|
+ "strategy: opensesim.trader.SimpleTrader"
|
||||||
@ -609,6 +615,26 @@ public class SeSimApplication extends javax.swing.JFrame {
|
|||||||
Trader t = godworld.createTrader(cfg);
|
Trader t = godworld.createTrader(cfg);
|
||||||
t.start();
|
t.start();
|
||||||
|
|
||||||
|
TradingAPI api;
|
||||||
|
AbstractAsset c,a;
|
||||||
|
c=godworld.getAssetBySymbol("EUR");
|
||||||
|
a=godworld.getAssetBySymbol("AAPL");
|
||||||
|
AssetPair p = new AssetPair(c,a);
|
||||||
|
|
||||||
|
opensesim.world.Exchange ex = godworld.getDefaultExchange();
|
||||||
|
api = ex.getAPI(p);
|
||||||
|
|
||||||
|
Set<Order> ob;
|
||||||
|
|
||||||
|
ob = api.getOrderBook(Order.Type.BUY);
|
||||||
|
|
||||||
|
for (Order o: ob){
|
||||||
|
System.out.printf("Volume: %d\n",o.getVolume());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
opensesim.world.scheduler.Scheduler s = godworld.getScheduler();
|
opensesim.world.scheduler.Scheduler s = godworld.getScheduler();
|
||||||
|
|
||||||
|
|
||||||
|
@ -25,8 +25,12 @@
|
|||||||
*/
|
*/
|
||||||
package opensesim.trader;
|
package opensesim.trader;
|
||||||
|
|
||||||
|
import opensesim.world.AbstractAsset;
|
||||||
import opensesim.world.AbstractTrader;
|
import opensesim.world.AbstractTrader;
|
||||||
|
import opensesim.world.AssetPair;
|
||||||
import opensesim.world.Exchange;
|
import opensesim.world.Exchange;
|
||||||
|
import opensesim.world.Order;
|
||||||
|
import opensesim.world.TradingAPI;
|
||||||
import opensesim.world.World;
|
import opensesim.world.World;
|
||||||
import opensesim.world.scheduler.Event;
|
import opensesim.world.scheduler.Event;
|
||||||
import opensesim.world.scheduler.EventListener;
|
import opensesim.world.scheduler.EventListener;
|
||||||
@ -40,6 +44,8 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
|
|||||||
|
|
||||||
Exchange ex = null;
|
Exchange ex = null;
|
||||||
|
|
||||||
|
TradingAPI api;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getStrategyTypeName() {
|
public String getStrategyTypeName() {
|
||||||
return "Very Simple Trader";
|
return "Very Simple Trader";
|
||||||
@ -47,6 +53,10 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
|
|||||||
|
|
||||||
public SimpleTrader(World world, JSONObject cfg) {
|
public SimpleTrader(World world, JSONObject cfg) {
|
||||||
super(world, cfg);
|
super(world, cfg);
|
||||||
|
if (cfg == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleTrader() {
|
public SimpleTrader() {
|
||||||
@ -87,6 +97,15 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
|
|||||||
setStatus("Stopped.");
|
setStatus("Stopped.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
AbstractAsset c,a;
|
||||||
|
c=getWorld().getAssetBySymbol("EUR");
|
||||||
|
a=getWorld().getAssetBySymbol("AAPL");
|
||||||
|
AssetPair p = new AssetPair(c,a);
|
||||||
|
|
||||||
|
ex = getWorld().getDefaultExchange();
|
||||||
|
api = ex.getAPI(p);
|
||||||
|
Order o = api.createOrder(account, Order.Type.BUY, 100, 200);
|
||||||
|
|
||||||
|
|
||||||
long delay = (long) (1000.0f * getWorld().randNextFloat(3.0f, 12.7f));
|
long delay = (long) (1000.0f * getWorld().randNextFloat(3.0f, 12.7f));
|
||||||
setStatus(String.format("Initial delay: Sleeping for %d seconds.", delay));
|
setStatus(String.format("Initial delay: Sleeping for %d seconds.", delay));
|
||||||
|
@ -29,6 +29,7 @@ import java.util.Collection;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.SortedSet;
|
import java.util.SortedSet;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
import opensesim.world.RealWorld;
|
import opensesim.world.RealWorld;
|
||||||
@ -42,7 +43,7 @@ import org.json.JSONObject;
|
|||||||
*/
|
*/
|
||||||
public class Exchange implements Configurable, GetJson {
|
public class Exchange implements Configurable, GetJson {
|
||||||
|
|
||||||
private World world;
|
private GodWorld world;
|
||||||
private String name;
|
private String name;
|
||||||
private String symbol;
|
private String symbol;
|
||||||
|
|
||||||
@ -52,13 +53,13 @@ public class Exchange implements Configurable, GetJson {
|
|||||||
|
|
||||||
private final HashMap<AssetPair, TradingAPI> asset_pairs = new HashMap<>();
|
private final HashMap<AssetPair, TradingAPI> asset_pairs = new HashMap<>();
|
||||||
|
|
||||||
Exchange(World world, String symbol) {
|
Exchange(GodWorld world, String symbol) {
|
||||||
|
|
||||||
this.world = world;
|
this.world = world;
|
||||||
this.symbol = symbol;
|
this.symbol = symbol;
|
||||||
}
|
}
|
||||||
|
|
||||||
Exchange(World world, JSONObject cfg) {
|
Exchange(GodWorld world, JSONObject cfg) {
|
||||||
this.world = world;
|
this.world = world;
|
||||||
this.name = cfg.optString("name", "Sesim");
|
this.name = cfg.optString("name", "Sesim");
|
||||||
this.symbol = cfg.optString("symbol");
|
this.symbol = cfg.optString("symbol");
|
||||||
@ -112,8 +113,10 @@ public class Exchange implements Configurable, GetJson {
|
|||||||
class TradingEnv implements TradingAPI {
|
class TradingEnv implements TradingAPI {
|
||||||
|
|
||||||
protected HashMap<Order.Type, SortedSet<Order>> order_books;
|
protected HashMap<Order.Type, SortedSet<Order>> order_books;
|
||||||
|
AssetPair pair;
|
||||||
|
|
||||||
TradingEnv() {
|
TradingEnv(AssetPair p) {
|
||||||
|
pair = p;
|
||||||
reset();
|
reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,16 +132,41 @@ public class Exchange implements Configurable, GetJson {
|
|||||||
// ohlc_data = new HashMap();
|
// ohlc_data = new HashMap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void addOrderToBook(Order o) {
|
||||||
|
order_books.get(o.type).add(o);
|
||||||
|
switch (o.type) {
|
||||||
|
case BUY:
|
||||||
|
case BUYLIMIT:
|
||||||
|
break;
|
||||||
|
case SELL:
|
||||||
|
case SELLLIMIT:
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Order createOrder(Account account, Order.Type type, double volume, double limit) {
|
public Order createOrder(Account account, Order.Type type, double volume, double limit) {
|
||||||
|
|
||||||
return null;
|
Order o = new opensesim.world.Order(world, account, pair, type, volume, limit);
|
||||||
|
synchronized (this){
|
||||||
|
order_books.get(o.type).add(o);
|
||||||
}
|
}
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set getOrderBook(Order.Type type) {
|
||||||
|
return Collections.unmodifiableSet(order_books.get(type));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private TradingAPI add(AssetPair p) {
|
private TradingAPI add(AssetPair p) {
|
||||||
TradingEnv e = new TradingEnv();
|
TradingEnv e = new TradingEnv(p);
|
||||||
asset_pairs.put(p, e);
|
asset_pairs.put(p, e);
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
@ -205,7 +205,7 @@ public class GodWorld implements GetJson, World {
|
|||||||
private Exchange default_exchange = null;
|
private Exchange default_exchange = null;
|
||||||
|
|
||||||
public void createExchange(JSONObject cfg) {
|
public void createExchange(JSONObject cfg) {
|
||||||
Exchange ex = new Exchange(this.getWorld(), cfg);
|
Exchange ex = new Exchange(this, cfg);
|
||||||
exchanges.put(ex.getSymbol(), ex);
|
exchanges.put(ex.getSymbol(), ex);
|
||||||
if (default_exchange == null) {
|
if (default_exchange == null) {
|
||||||
default_exchange = ex;
|
default_exchange = ex;
|
||||||
@ -224,7 +224,6 @@ public class GodWorld implements GetJson, World {
|
|||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
// Assets
|
// Assets
|
||||||
// --------------------------------------------------------------------
|
// --------------------------------------------------------------------
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<AbstractAsset> getAssetCollection() {
|
public Collection<AbstractAsset> getAssetCollection() {
|
||||||
return assets.getCollection(); //Collections.unmodifiableCollection(assetsById);
|
return assets.getCollection(); //Collections.unmodifiableCollection(assetsById);
|
||||||
|
@ -25,7 +25,6 @@
|
|||||||
*/
|
*/
|
||||||
package opensesim.world;
|
package opensesim.world;
|
||||||
|
|
||||||
import opensesim.util.idgenerator.IDGenerator;
|
|
||||||
import opensesim.util.idgenerator.Id;
|
import opensesim.util.idgenerator.Id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,6 +25,8 @@
|
|||||||
*/
|
*/
|
||||||
package opensesim.world;
|
package opensesim.world;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author tube
|
* @author tube
|
||||||
@ -33,4 +35,5 @@ public interface TradingAPI {
|
|||||||
|
|
||||||
public Order createOrder(Account account, Order.Type type, double volume, double limit);
|
public Order createOrder(Account account, Order.Type type, double volume, double limit);
|
||||||
|
|
||||||
|
public Set getOrderBook(Order.Type type);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user