produces now exactly the same chart for the same random seed

This commit is contained in:
7u83 2017-02-05 01:19:09 +01:00
parent 7a6f56883d
commit c877d42e41
16 changed files with 90 additions and 47 deletions

View File

@ -50,9 +50,8 @@ public class Clock extends javax.swing.JPanel {
public void run() { public void run() {
long t = Globals.se.timer.currentTimeMillis(); long t = Globals.se.timer.currentTimeMillis();
System.out.printf("The Clock: %d\n",t);
Statistics s = Globals.se.getStatistics(); Statistics s = Globals.se.getStatistics();
System.out.printf("Num trades: %d, %d\n",s.trades,s.orders);
jLabel1.setText(Scheduler.formatTimeMillis(t)); jLabel1.setText(Scheduler.formatTimeMillis(t));
} }

View File

@ -302,12 +302,12 @@ public final class EditStrategies extends javax.swing.JDialog {
JSONObject jo = ac.getConfig(); JSONObject jo = ac.getConfig();
if (jo != null) { if (jo != null) {
System.out.printf("%s\n", jo.toString(2)); System.out.printf("%s\n", jo.toString(2));
jo = new JSONObject(jo.toString()); // jo = new JSONObject(jo.toString());
} }
System.out.print("Crete auto trader afeter select\n"); // System.out.print("Crete auto trader afeter select\n");
AutoTrader at = ac.createTrader(Globals.se, jo, 100, 100); // AutoTrader at = ac.createTrader(Globals.se, jo, 100, 100);
this.repaint(); this.repaint();
}//GEN-LAST:event_jComboBoxStrategySelectorActionPerformed }//GEN-LAST:event_jComboBoxStrategySelectorActionPerformed

View File

@ -246,7 +246,8 @@ public class MainWin extends javax.swing.JFrame {
// rcfg1.sell_limit[0]=-1; // rcfg1.sell_limit[0]=-1;
// rcfg1.sell_limit[1]=1; // rcfg1.sell_limit[1]=1;
AutoTrader rt1 = rcfg1.createTrader(Globals.se, null, 1000000, 1000000); /*
AutoTrader rt1 = rcfg1.createTrader(Globals.se,0,"", null, 1000000, 1000000);
Globals.se.traders.add(rt1); Globals.se.traders.add(rt1);
rt1.setName("Alice"); rt1.setName("Alice");
rt1.start(); rt1.start();
@ -258,7 +259,7 @@ public class MainWin extends javax.swing.JFrame {
// SwitchingTraderConfig cfg = new SwitchingTraderConfig(); // SwitchingTraderConfig cfg = new SwitchingTraderConfig();
RandomTraderConfig cfg= new RandomTraderConfig(); RandomTraderConfig cfg= new RandomTraderConfig();
*
for (int i=0; i<30; i++){ for (int i=0; i<30; i++){
AutoTrader randt = cfg.createTrader(Globals.se,null, 100000, 100000); AutoTrader randt = cfg.createTrader(Globals.se,null, 100000, 100000);
@ -267,7 +268,7 @@ public class MainWin extends javax.swing.JFrame {
randt.start(); randt.start();
} }
*/

View File

@ -121,6 +121,7 @@
</Menu> </Menu>
<Menu class="javax.swing.JMenu" name="simMenu"> <Menu class="javax.swing.JMenu" name="simMenu">
<Properties> <Properties>
<Property name="mnemonic" type="int" value="115"/>
<Property name="text" type="java.lang.String" value="Sim"/> <Property name="text" type="java.lang.String" value="Sim"/>
</Properties> </Properties>
<SubComponents> <SubComponents>
@ -157,6 +158,7 @@
</Menu> </Menu>
<Menu class="javax.swing.JMenu" name="viewMenu"> <Menu class="javax.swing.JMenu" name="viewMenu">
<Properties> <Properties>
<Property name="mnemonic" type="int" value="118"/>
<Property name="text" type="java.lang.String" value="View"/> <Property name="text" type="java.lang.String" value="View"/>
</Properties> </Properties>
<SubComponents> <SubComponents>

View File

@ -65,7 +65,7 @@ public class NewMDIApplication extends javax.swing.JFrame {
Double moneyTotal=0.0; Double moneyTotal=0.0;
Double sharesTotal=0.0; Double sharesTotal=0.0;
long id=0;
for (int i=0; i<tlist.length();i++){ for (int i=0; i<tlist.length();i++){
JSONObject t=tlist.getJSONObject(i); JSONObject t=tlist.getJSONObject(i);
String strategy_name = t.getString("Strategy"); String strategy_name = t.getString("Strategy");
@ -84,9 +84,9 @@ public class NewMDIApplication extends javax.swing.JFrame {
for (int i1=0;i1<count;i1++){ for (int i1=0;i1<count;i1++){
AutoTrader trader = ac.createTrader(Globals.se, strategy, money, shares); AutoTrader trader = ac.createTrader(Globals.se, strategy, id++, t.getString("Name")+i1,money, shares);
Globals.se.traders.add(trader); Globals.se.traders.add(trader);
trader.setName(t.getString("Name")+i1); // trader.setName(t.getString("Name")+i1);
moneyTotal+=money; moneyTotal+=money;
sharesTotal+=shares; sharesTotal+=shares;
@ -338,6 +338,7 @@ public class NewMDIApplication extends javax.swing.JFrame {
menuBar.add(editMenu); menuBar.add(editMenu);
simMenu.setMnemonic('s');
simMenu.setText("Sim"); simMenu.setText("Sim");
simMenuStart.setText("Start"); simMenuStart.setText("Start");
@ -369,6 +370,7 @@ public class NewMDIApplication extends javax.swing.JFrame {
menuBar.add(simMenu); menuBar.add(simMenu);
viewMenu.setMnemonic('v');
viewMenu.setText("View"); viewMenu.setText("View");
jMenuItem2.setText("Traders"); jMenuItem2.setText("Traders");

View File

@ -29,6 +29,8 @@ import java.awt.Color;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Formatter; import java.util.Formatter;
import java.util.Iterator; import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory; import javax.swing.BorderFactory;
import javax.swing.ListModel; import javax.swing.ListModel;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
@ -42,12 +44,13 @@ import sesim.Scheduler;
* *
* @author 7u83 <7u83@mail.ru> * @author 7u83 <7u83@mail.ru>
*/ */
public class TraderListPanel extends javax.swing.JPanel public class TraderListPanel extends javax.swing.JPanel {
implements Scheduler.TimerTask {
Exchange se; Exchange se;
TraderListModel model; TraderListModel model;
TimerTask updater;
/** /**
* Creates new form TraderListPanel * Creates new form TraderListPanel
*/ */
@ -73,14 +76,27 @@ public class TraderListPanel extends javax.swing.JPanel
if (Globals.se != null) { if (Globals.se != null) {
this.se = Globals.se; this.se = Globals.se;
this.list = this.getTraderList(); this.list = this.getTraderList();
se.timer.startTimerEvent(this, 1000); // se.timer.startTimerEvent(this, 1000);
Timer timer = new Timer();
updater = new TimerTask() {
@Override
public void run() {
timerTask();
}
};
timer.schedule(updater, 0, 1000);
} }
} }
final ArrayList<TraderListItem> getTraderList() { final ArrayList<TraderListItem> getTraderList() {
if (se.traders==null) if (se.traders == null) {
return new ArrayList<>(); return new ArrayList<>();
}
sesim.Quote q = se.getLastQuoete(); sesim.Quote q = se.getLastQuoete();
double price = q == null ? 0 : q.price; double price = q == null ? 0 : q.price;
@ -101,7 +117,6 @@ public class TraderListPanel extends javax.swing.JPanel
return tlist; return tlist;
} }
@Override
public long timerTask() { public long timerTask() {
class Updater implements Runnable { class Updater implements Runnable {
@ -149,7 +164,6 @@ public class TraderListPanel extends javax.swing.JPanel
public void update(ArrayList newlist) { public void update(ArrayList newlist) {
list = newlist; //getOrderBook(); list = newlist; //getOrderBook();
this.fireTableDataChanged(); this.fireTableDataChanged();
} }
@ -183,9 +197,6 @@ public class TraderListPanel extends javax.swing.JPanel
return 5; return 5;
} }
@Override @Override
public Object getValueAt(int r, int c) { public Object getValueAt(int r, int c) {
TraderListItem ti; TraderListItem ti;

View File

@ -37,11 +37,12 @@ public abstract class AutoTrader implements Scheduler.TimerTask {
protected String name; protected String name;
public AutoTrader(Exchange se, double money, double shares, AutoTraderConfig config) { public AutoTrader(Exchange se, long id, String name, double money, double shares, AutoTraderConfig config) {
account_id = se.createAccount(money, shares); account_id = se.createAccount(money, shares);
this.se = se; this.se = se;
this.config = config; this.config = config;
this.name = ""; this.name = "";
this.id=id;
} }
@ -53,6 +54,12 @@ public abstract class AutoTrader implements Scheduler.TimerTask {
return name; return name;
} }
@Override
public long getID(){
return id;
}
private final long id;
public Exchange.Account getAccount(){ public Exchange.Account getAccount(){
return se.getAccount(account_id); return se.getAccount(account_id);
} }

View File

@ -33,11 +33,17 @@ import org.json.JSONObject;
* @author 7u83 * @author 7u83
*/ */
public abstract interface AutoTraderConfig { public abstract interface AutoTraderConfig {
public abstract AutoTrader createTrader(Exchange se, JSONObject cfg,double money, double shares);
public abstract AutoTrader createTrader(Exchange se, JSONObject cfg, long id, String name, double money, double shares);
public abstract String getDisplayName(); public abstract String getDisplayName();
public abstract AutoTraderGui getGui(); public abstract AutoTraderGui getGui();
public abstract JSONObject getConfig(); public abstract JSONObject getConfig();
public abstract void putConfig(JSONObject cfg); public abstract void putConfig(JSONObject cfg);
public abstract boolean getDevelStatus(); public abstract boolean getDevelStatus();
} }

View File

@ -226,6 +226,7 @@ public class Exchange {
final void initExchange() { final void initExchange() {
timer = new Scheduler(); // timer = new Scheduler(); timer = new Scheduler(); // timer = new Scheduler();
random = new Random(12); random = new Random(12);
quoteHistory = new TreeSet(); quoteHistory = new TreeSet();
accounts = new ConcurrentHashMap<>(); accounts = new ConcurrentHashMap<>();
@ -478,12 +479,14 @@ public class Exchange {
Random random; Random random;
public int randNextInt() { public int randNextInt() {
System.out.printf("Next int: %d\n", random.nextInt());
return random.nextInt(); return random.nextInt();
} }
public int randNextInt(int bounds) { public int randNextInt(int bounds) {
return random.nextInt(bounds); return random.nextInt(bounds);
} }
public double randNextDouble() { public double randNextDouble() {

View File

@ -139,7 +139,7 @@ public class OHLCData { //extends ArrayList <OHLCDataItem> {
current_frame_end = current_frame_start + frame_size; current_frame_end = current_frame_start + frame_size;
System.out.printf("TA %d TE %d\n",this.current_frame_start,this.current_frame_end); //System.out.printf("TA %d TE %d\n",this.current_frame_start,this.current_frame_end);
data.add(new OHLCDataItem(this.current_frame_start, price, volume)); data.add(new OHLCDataItem(this.current_frame_start, price, volume));
this.updateMinMax(price); this.updateMinMax(price);

View File

@ -61,6 +61,7 @@ public class Scheduler extends Thread {
public interface TimerTask { public interface TimerTask {
long timerTask(); long timerTask();
long getID();
} }
private boolean terminate = false; private boolean terminate = false;
@ -91,7 +92,9 @@ public class Scheduler extends Thread {
@Override @Override
public int compare(Object o1, Object o2) { public int compare(Object o1, Object o2) {
return System.identityHashCode(o1) - System.identityHashCode(o2);
return (((TimerTask)o1).getID() - ((TimerTask)o2).getID())<0 ? -1:1;
//return System.identityHashCode(o1) - System.identityHashCode(o2);
} }
} }
@ -104,11 +107,11 @@ public class Scheduler extends Thread {
*/ */
public long currentTimeMillis1() { public long currentTimeMillis1() {
long diff = System.currentTimeMillis() - last_time_millis+1; long diff = System.currentTimeMillis() - last_time_millis;
diff=12199999L; // diff=12199999L;
last_time_millis += diff; last_time_millis += diff;
this.current_time_millis += diff * this.multiplier; this.current_time_millis += diff * this.multiplier;
//System.out.printf("Current TM: %f\n", this.current_time_millis);
return (long) this.current_time_millis; return (long) this.current_time_millis;
} }

View File

@ -51,8 +51,8 @@ public class RandomTrader extends AutoTrader {
} }
public RandomTrader(Exchange se, double money, double shares, RandomTraderConfig config) { public RandomTrader(Exchange se, long id, String name, double money, double shares, RandomTraderConfig config) {
super(se, money, shares, config); super(se, id, name,money, shares, config);
if (this.config == null) { if (this.config == null) {
this.config = new RandomTraderConfig(); this.config = new RandomTraderConfig();
} }
@ -165,10 +165,13 @@ public class RandomTrader extends AutoTrader {
OrderType type = OrderType.BID; OrderType type = OrderType.BID;
if (ad == null || myconfig == null) { if (ad == null || myconfig == null) {
// System.out.print(ad + "\n"); //System.out.printf("%s: myconf = 0 \n", this.getName());
return 0; return 0;
} }
//System.out.printf("%s: calling rand for money\n", this.getName());
// how much money we ant to envest? // how much money we ant to envest?
double money = getRandomAmmount(ad.money, myconfig.buy_volume); double money = getRandomAmmount(ad.money, myconfig.buy_volume);
@ -189,6 +192,8 @@ public class RandomTrader extends AutoTrader {
return 0; return 0;
} }
//System.out.printf("%s: create order %s %f\n", this.getName(),type.toString(),limit);
se.createOrder(account_id, type, volume, limit); se.createOrder(account_id, type, volume, limit);
return getRandom(myconfig.buy_wait); return getRandom(myconfig.buy_wait);
@ -201,6 +206,7 @@ public class RandomTrader extends AutoTrader {
OrderType type = OrderType.ASK; OrderType type = OrderType.ASK;
//System.out.printf("%s: calling rand for volume\n", this.getName());
// how much money we ant to envest? // how much money we ant to envest?
double volume = (long) getRandomAmmount(ad.shares, myconfig.sell_volume); double volume = (long) getRandomAmmount(ad.shares, myconfig.sell_volume);
@ -219,6 +225,8 @@ public class RandomTrader extends AutoTrader {
// System.out.print("Volume is:"+volume+"\n"); // System.out.print("Volume is:"+volume+"\n");
// System.out.print("My Ammount is: "+volume+" My limit si:"+limit+ "\n"); // System.out.print("My Ammount is: "+volume+" My limit si:"+limit+ "\n");
//System.out.printf("%s: create order %s %f\n", this.getName(),type.toString(),limit);
se.createOrder(account_id, type, volume, limit); se.createOrder(account_id, type, volume, limit);
return getRandom(myconfig.sell_wait); return getRandom(myconfig.sell_wait);
@ -235,6 +243,7 @@ public class RandomTrader extends AutoTrader {
return doSell(); return doSell();
} }
// System.out.printf("%s: do nothing\n",this.getName());
return 0; return 0;
} }

View File

@ -53,11 +53,11 @@ public class RandomTraderConfig extends AutoTraderBase implements AutoTraderConf
public Long[] wait_after_buy = {10L, 30L}; public Long[] wait_after_buy = {10L, 30L};
@Override @Override
public AutoTrader createTrader(Exchange se, JSONObject cfg, double money, double shares) { public AutoTrader createTrader(Exchange se, JSONObject cfg, long id, String name, double money, double shares) {
if (cfg != null) { if (cfg != null) {
this.putConfig(cfg); this.putConfig(cfg);
} }
return new traders.RandomTrader(se, money, shares, this); return new traders.RandomTrader(se, id, name, money, shares, this);
} }
@Override @Override

View File

@ -39,7 +39,7 @@ import sesim.Exchange;
public class SuperTraderConfig implements AutoTraderConfig{ public class SuperTraderConfig implements AutoTraderConfig{
@Override @Override
public AutoTrader createTrader(Exchange se, JSONObject cfg, double money, double shares) { public AutoTrader createTrader(Exchange se, JSONObject cfg, long id, String name,double money, double shares) {
return null; return null;
} }

View File

@ -36,8 +36,8 @@ public class SwitchingTrader extends RandomTrader {
private int mode; private int mode;
public SwitchingTrader(Exchange se, double money, double shares, RandomTraderConfig config) { public SwitchingTrader(Exchange se, long id, String name, double money, double shares, RandomTraderConfig config) {
super(se, money, shares, config); super(se, id, name, money, shares, config);
} }
Action action = Action.RANDOM; Action action = Action.RANDOM;

View File

@ -37,9 +37,9 @@ import sesim.Exchange;
public class SwitchingTraderConfig extends RandomTraderConfig implements AutoTraderConfig { public class SwitchingTraderConfig extends RandomTraderConfig implements AutoTraderConfig {
@Override @Override
public AutoTrader createTrader(Exchange se, JSONObject cfg, double money, double shares) { public AutoTrader createTrader(Exchange se, JSONObject cfg, long id, String name, double money, double shares) {
return new traders.SwitchingTrader(se, money, shares, this); return new traders.SwitchingTrader(se, id, name, money, shares, this);
} }