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() {
long t = Globals.se.timer.currentTimeMillis();
System.out.printf("The Clock: %d\n",t);
Statistics s = Globals.se.getStatistics();
System.out.printf("Num trades: %d, %d\n",s.trades,s.orders);
jLabel1.setText(Scheduler.formatTimeMillis(t));
}

View File

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

View File

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

View File

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

View File

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

View File

@ -29,6 +29,8 @@ import java.awt.Color;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.ListModel;
import javax.swing.SwingUtilities;
@ -42,12 +44,13 @@ import sesim.Scheduler;
*
* @author 7u83 <7u83@mail.ru>
*/
public class TraderListPanel extends javax.swing.JPanel
implements Scheduler.TimerTask {
public class TraderListPanel extends javax.swing.JPanel {
Exchange se;
TraderListModel model;
TimerTask updater;
/**
* Creates new form TraderListPanel
*/
@ -73,15 +76,28 @@ public class TraderListPanel extends javax.swing.JPanel
if (Globals.se != null) {
this.se = Globals.se;
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() {
if (se.traders==null)
if (se.traders == null) {
return new ArrayList<>();
}
sesim.Quote q = se.getLastQuoete();
double price = q == null ? 0 : q.price;
Iterator<AutoTrader> it = se.traders.iterator();
@ -94,14 +110,13 @@ public class TraderListPanel extends javax.swing.JPanel
ti.name = at.getName();
ti.shares = a.getShares();
ti.money = a.getMoney();
ti.welth = price==0 ? 0 : ti.shares * price + ti.money;
ti.welth = price == 0 ? 0 : ti.shares * price + ti.money;
tlist.add(ti);
}
return tlist;
}
@Override
public long timerTask() {
class Updater implements Runnable {
@ -120,10 +135,10 @@ public class TraderListPanel extends javax.swing.JPanel
}
}
System.out.print("TimerTaskUpdater\n");
ArrayList <TraderListItem> newlist = getTraderList();
ArrayList<TraderListItem> newlist = getTraderList();
SwingUtilities.invokeLater(new Updater(this.model, newlist));
return 2000;
@ -148,8 +163,7 @@ public class TraderListPanel extends javax.swing.JPanel
}
public void update(ArrayList newlist) {
list = newlist; //getOrderBook();
this.fireTableDataChanged();
}
@ -182,9 +196,6 @@ public class TraderListPanel extends javax.swing.JPanel
public int getColumnCount() {
return 5;
}
@Override
public Object getValueAt(int r, int c) {

View File

@ -37,11 +37,12 @@ public abstract class AutoTrader implements Scheduler.TimerTask {
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);
this.se = se;
this.config = config;
this.name = "";
this.id=id;
}
@ -53,6 +54,12 @@ public abstract class AutoTrader implements Scheduler.TimerTask {
return name;
}
@Override
public long getID(){
return id;
}
private final long id;
public Exchange.Account getAccount(){
return se.getAccount(account_id);
}

View File

@ -33,11 +33,17 @@ import org.json.JSONObject;
* @author 7u83
*/
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 AutoTraderGui getGui();
public abstract JSONObject getConfig();
public abstract void putConfig(JSONObject cfg);
public abstract boolean getDevelStatus();
}

View File

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

View File

@ -139,7 +139,7 @@ public class OHLCData { //extends ArrayList <OHLCDataItem> {
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));
this.updateMinMax(price);

View File

@ -59,8 +59,9 @@ public class Scheduler extends Thread {
private final SortedMap<Long, SortedSet<TimerTask>> event_queue = new TreeMap<>();
public interface TimerTask {
long timerTask();
long getID();
}
private boolean terminate = false;
@ -91,7 +92,9 @@ public class Scheduler extends Thread {
@Override
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() {
long diff = System.currentTimeMillis() - last_time_millis+1;
diff=12199999L;
long diff = System.currentTimeMillis() - last_time_millis;
// diff=12199999L;
last_time_millis += diff;
this.current_time_millis += diff * this.multiplier;
//System.out.printf("Current TM: %f\n", 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) {
super(se, money, shares, config);
public RandomTrader(Exchange se, long id, String name, double money, double shares, RandomTraderConfig config) {
super(se, id, name,money, shares, config);
if (this.config == null) {
this.config = new RandomTraderConfig();
}
@ -165,10 +165,13 @@ public class RandomTrader extends AutoTrader {
OrderType type = OrderType.BID;
if (ad == null || myconfig == null) {
// System.out.print(ad + "\n");
//System.out.printf("%s: myconf = 0 \n", this.getName());
return 0;
}
//System.out.printf("%s: calling rand for money\n", this.getName());
// how much money we ant to envest?
double money = getRandomAmmount(ad.money, myconfig.buy_volume);
@ -189,6 +192,8 @@ public class RandomTrader extends AutoTrader {
return 0;
}
//System.out.printf("%s: create order %s %f\n", this.getName(),type.toString(),limit);
se.createOrder(account_id, type, volume, limit);
return getRandom(myconfig.buy_wait);
@ -201,6 +206,7 @@ public class RandomTrader extends AutoTrader {
OrderType type = OrderType.ASK;
//System.out.printf("%s: calling rand for volume\n", this.getName());
// how much money we ant to envest?
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("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);
return getRandom(myconfig.sell_wait);
@ -235,6 +243,7 @@ public class RandomTrader extends AutoTrader {
return doSell();
}
// System.out.printf("%s: do nothing\n",this.getName());
return 0;
}

View File

@ -53,11 +53,11 @@ public class RandomTraderConfig extends AutoTraderBase implements AutoTraderConf
public Long[] wait_after_buy = {10L, 30L};
@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) {
this.putConfig(cfg);
}
return new traders.RandomTrader(se, money, shares, this);
return new traders.RandomTrader(se, id, name, money, shares, this);
}
@Override

View File

@ -39,7 +39,7 @@ import sesim.Exchange;
public class SuperTraderConfig implements AutoTraderConfig{
@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;
}

View File

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

View File

@ -37,9 +37,9 @@ import sesim.Exchange;
public class SwitchingTraderConfig extends RandomTraderConfig implements AutoTraderConfig {
@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);
}