Work on new Trading API

This commit is contained in:
7u83 2018-12-23 03:04:37 +01:00
parent ba03d4d292
commit 5023b2ad7a
7 changed files with 86 additions and 12 deletions

View File

@ -363,7 +363,7 @@ public class MasterChart extends javax.swing.JPanel implements QuoteReceiver {
}//GEN-LAST:event_chartMouseMoved
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);
}//GEN-LAST:event_chartMousePressed

View File

@ -38,6 +38,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import java.util.Set;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
@ -45,6 +46,7 @@ import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import opensesim.gui.AssetPairEditor.NewJDialog;
import static opensesim.gui.Globals.getWorld;
import opensesim.gui.exchangeeditor.ExchangeListDialog;
import org.json.JSONArray;
@ -54,9 +56,13 @@ import opensesim.old_sesim.AutoTraderInterface;
import opensesim.old_sesim.Exchange;
import opensesim.old_sesim.Scheduler;
import opensesim.old_sesim.Scheduler.TimerTaskDef;
import opensesim.world.AbstractAsset;
import opensesim.world.AssetPair;
import opensesim.world.GodWorld;
import opensesim.world.Order;
import opensesim.world.Trader;
import opensesim.world.TradingAPI;
import opensesim.world.World;
import opensesim.world.scheduler.EventListener;
@ -601,13 +607,33 @@ public class SeSimApplication extends javax.swing.JFrame {
void startSim() {
GodWorld godworld = new GodWorld(Globals.getWorld());
// GodWorld godworld = new GodWorld(Globals.getWorld());
JSONObject cfg = new JSONObject("{"
+ "strategy: opensesim.trader.SimpleTrader"
+ "}");
Trader t = godworld.createTrader(cfg);
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();

View File

@ -25,8 +25,12 @@
*/
package opensesim.trader;
import opensesim.world.AbstractAsset;
import opensesim.world.AbstractTrader;
import opensesim.world.AssetPair;
import opensesim.world.Exchange;
import opensesim.world.Order;
import opensesim.world.TradingAPI;
import opensesim.world.World;
import opensesim.world.scheduler.Event;
import opensesim.world.scheduler.EventListener;
@ -40,6 +44,8 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
Exchange ex = null;
TradingAPI api;
@Override
public String getStrategyTypeName() {
return "Very Simple Trader";
@ -47,6 +53,10 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
public SimpleTrader(World world, JSONObject cfg) {
super(world, cfg);
if (cfg == null) {
return;
}
}
public SimpleTrader() {
@ -87,6 +97,15 @@ public class SimpleTrader extends AbstractTrader implements EventListener {
setStatus("Stopped.");
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));
setStatus(String.format("Initial delay: Sleeping for %d seconds.", delay));

View File

@ -29,6 +29,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import opensesim.world.RealWorld;
@ -42,7 +43,7 @@ import org.json.JSONObject;
*/
public class Exchange implements Configurable, GetJson {
private World world;
private GodWorld world;
private String name;
private String symbol;
@ -52,13 +53,13 @@ public class Exchange implements Configurable, GetJson {
private final HashMap<AssetPair, TradingAPI> asset_pairs = new HashMap<>();
Exchange(World world, String symbol) {
Exchange(GodWorld world, String symbol) {
this.world = world;
this.symbol = symbol;
}
Exchange(World world, JSONObject cfg) {
Exchange(GodWorld world, JSONObject cfg) {
this.world = world;
this.name = cfg.optString("name", "Sesim");
this.symbol = cfg.optString("symbol");
@ -112,8 +113,10 @@ public class Exchange implements Configurable, GetJson {
class TradingEnv implements TradingAPI {
protected HashMap<Order.Type, SortedSet<Order>> order_books;
AssetPair pair;
TradingEnv() {
TradingEnv(AssetPair p) {
pair = p;
reset();
}
@ -129,16 +132,41 @@ public class Exchange implements Configurable, GetJson {
// 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
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) {
TradingEnv e = new TradingEnv();
TradingEnv e = new TradingEnv(p);
asset_pairs.put(p, e);
return e;
}

View File

@ -82,7 +82,7 @@ public class GodWorld implements GetJson, World {
HashSet<AssetPair> assetPairs = new HashSet<>();
private Scheduler scheduler = new Scheduler();
private Scheduler scheduler = new Scheduler();
/**
* Create a World object.
@ -205,7 +205,7 @@ public class GodWorld implements GetJson, World {
private Exchange default_exchange = null;
public void createExchange(JSONObject cfg) {
Exchange ex = new Exchange(this.getWorld(), cfg);
Exchange ex = new Exchange(this, cfg);
exchanges.put(ex.getSymbol(), ex);
if (default_exchange == null) {
default_exchange = ex;
@ -224,7 +224,6 @@ public class GodWorld implements GetJson, World {
// --------------------------------------------------------------------
// Assets
// --------------------------------------------------------------------
@Override
public Collection<AbstractAsset> getAssetCollection() {
return assets.getCollection(); //Collections.unmodifiableCollection(assetsById);

View File

@ -25,7 +25,6 @@
*/
package opensesim.world;
import opensesim.util.idgenerator.IDGenerator;
import opensesim.util.idgenerator.Id;
/**

View File

@ -25,6 +25,8 @@
*/
package opensesim.world;
import java.util.Set;
/**
*
* @author tube
@ -33,4 +35,5 @@ public interface TradingAPI {
public Order createOrder(Account account, Order.Type type, double volume, double limit);
public Set getOrderBook(Order.Type type);
}