From 5cf177a346080878fb7f306d201e409cddf02ac9 Mon Sep 17 00:00:00 2001
From: 7u83 <7u83@maiol.ru>
Date: Mon, 13 Mar 2017 07:21:13 +0100
Subject: [PATCH] some tes, some improvements
---
src/main/java/gui/NewMDIApplication.form | 46 ++++++------------
src/main/java/gui/NewMDIApplication.java | 46 +++++++++---------
src/main/java/sesim/Scheduler.java | 4 +-
src/main/java/traders/RandomTraderA.java | 60 ++++++++++++++++--------
src/test/java/sesim/Test.java | 45 ++++++++----------
5 files changed, 100 insertions(+), 101 deletions(-)
diff --git a/src/main/java/gui/NewMDIApplication.form b/src/main/java/gui/NewMDIApplication.form
index e1850d5..0510c05 100644
--- a/src/main/java/gui/NewMDIApplication.form
+++ b/src/main/java/gui/NewMDIApplication.form
@@ -305,11 +305,11 @@
-
+
-
+
@@ -326,18 +326,12 @@
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
@@ -394,35 +388,23 @@
-
+
+
+
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
diff --git a/src/main/java/gui/NewMDIApplication.java b/src/main/java/gui/NewMDIApplication.java
index 3526be5..eb1c042 100644
--- a/src/main/java/gui/NewMDIApplication.java
+++ b/src/main/java/gui/NewMDIApplication.java
@@ -128,8 +128,10 @@ public class NewMDIApplication extends javax.swing.JFrame {
Globals.se.fairValue = moneyTotal / sharesTotal;
- Globals.se.fairValue = 1.0;
-
+ // Globals.se.fairValue = 1.0;
+ System.out.printf("Failr Value is %f\n", Globals.se.fairValue);
+
+
for (int i = 0; i < Globals.se.traders.size(); i++) {
Globals.se.traders.get(i).start();
}
@@ -156,7 +158,7 @@ public class NewMDIApplication extends javax.swing.JFrame {
jButton2 = new javax.swing.JButton();
accelSpinner = new javax.swing.JSpinner();
clock = new gui.Clock();
- jComboBox1 = new javax.swing.JComboBox<>();
+ jLabel1 = new javax.swing.JLabel();
jSplitPane3 = new javax.swing.JSplitPane();
jSplitPane4 = new javax.swing.JSplitPane();
orderBooksHorizontal1 = new gui.orderbook.OrderBooksHorizontal();
@@ -243,20 +245,20 @@ public class NewMDIApplication extends javax.swing.JFrame {
}
});
- accelSpinner.setModel(new javax.swing.SpinnerNumberModel(1.0d, 0.0d, null, 100.0d));
+ accelSpinner.setModel(new javax.swing.SpinnerNumberModel(1.0d, 0.0d, null, 1.0d));
accelSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
accelSpinnerStateChanged(evt);
}
});
-
- jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "1x", "2x", "4x", "10x", "100x", "1000x", "max." }));
- jComboBox1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jComboBox1ActionPerformed(evt);
+ accelSpinner.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
+ public void propertyChange(java.beans.PropertyChangeEvent evt) {
+ accelSpinnerPropertyChange(evt);
}
});
+ jLabel1.setText("Acceleration:");
+
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
@@ -267,11 +269,11 @@ public class NewMDIApplication extends javax.swing.JFrame {
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(stopButton, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 389, Short.MAX_VALUE)
+ .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 375, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(clock, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
- .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(accelSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
@@ -284,15 +286,11 @@ public class NewMDIApplication extends javax.swing.JFrame {
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(clock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(jPanel2Layout.createSequentialGroup()
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(accelSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(0, 0, Short.MAX_VALUE))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addContainerGap())))
+ .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+ .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+ .addComponent(accelSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+ .addComponent(jLabel1))
+ .addGap(0, 5, Short.MAX_VALUE))
);
getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_START);
@@ -728,9 +726,9 @@ public class NewMDIApplication extends javax.swing.JFrame {
}
}//GEN-LAST:event_viewTraderListCheckBoxActionPerformed
- private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
- // TODO add your handling code here:
- }//GEN-LAST:event_jComboBox1ActionPerformed
+ private void accelSpinnerPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_accelSpinnerPropertyChange
+ System.out.printf("Accel Spinner PRop Change\n");
+ }//GEN-LAST:event_accelSpinnerPropertyChange
/**
* @param args the command line arguments
@@ -774,7 +772,7 @@ public class NewMDIApplication extends javax.swing.JFrame {
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jChartScrollPane;
private javax.swing.JCheckBoxMenuItem jCheckBoxMenuItem1;
- private javax.swing.JComboBox jComboBox1;
+ private javax.swing.JLabel jLabel1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
diff --git a/src/main/java/sesim/Scheduler.java b/src/main/java/sesim/Scheduler.java
index 6be5328..07706ca 100644
--- a/src/main/java/sesim/Scheduler.java
+++ b/src/main/java/sesim/Scheduler.java
@@ -26,6 +26,7 @@
package sesim;
//import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
+import gui.Globals;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Comparator;
@@ -364,6 +365,7 @@ public class Scheduler extends Thread {
@Override
public long timerTask() {
+ // System.out.printf("Current best brice %f\n", Globals.se.getBestPrice());
return 1000;
}
@@ -402,7 +404,7 @@ public class Scheduler extends Thread {
synchronized (this) {
try {
- System.out.printf("My WTIME %d\n", wtime);
+// System.out.printf("My WTIME %d\n", wtime);
if (wtime != -1 && !pause) {
wait(wtime);
} else {
diff --git a/src/main/java/traders/RandomTraderA.java b/src/main/java/traders/RandomTraderA.java
index 1693228..ecb4f73 100644
--- a/src/main/java/traders/RandomTraderA.java
+++ b/src/main/java/traders/RandomTraderA.java
@@ -73,7 +73,8 @@ public class RandomTraderA extends AutoTraderBase implements AccountListener {
a.setListener(this);
long delay = (long) (getRandom(initial_delay[0], initial_delay[1]) * 1000);
- timerTask = se.timer.startTimerTask(this, delay);
+ setStatus("Inital delay: %d\n",delay);
+ timerTask = se.timer.startTimerTask(this, delay);
}
@Override
@@ -82,7 +83,7 @@ public class RandomTraderA extends AutoTraderBase implements AccountListener {
sesim.Exchange.Account a = se.getAccount(account_id);
// System.out.printf("Have Account %d\n", Thread.currentThread().getId());
long rc = this.doTrade();
-// System.out.printf("Exit TimerTask for %d / %d\n", System.identityHashCode(this), Thread.currentThread().getId());
+ setStatus("Sleeping for %d ms", rc);
return rc;
}
@@ -114,6 +115,11 @@ public class RandomTraderA extends AutoTraderBase implements AccountListener {
return jo;
}
+ void setStatus(String format, Object... arguments) {
+ // String s = String.format(format, arguments);
+ // System.out.printf("%s: %s\n", this.getName(), s);
+ }
+
private Float[] to_float(JSONArray a) {
Float[] ret = new Float[a.length()];
for (int i = 0; i < a.length(); i++) {
@@ -184,21 +190,23 @@ public class RandomTraderA extends AutoTraderBase implements AccountListener {
}
sesim.Scheduler.TimerTaskDef timerTask;
-
+
@Override
public void accountUpdated(Account a, Exchange.Order o) {
// System.out.printf("Order what %s %d\n", o.getOrderStatus().toString(), Thread.currentThread().getId());
- if (o.getOrderStatus() == OrderStatus.CLOSED ) {
+ if (o.getOrderStatus() == OrderStatus.CLOSED) {
// System.out.printf("Enteter canel timer %d\n", Thread.currentThread().getId());
- // se.timer.cancelTimerTask(this);
+ // se.timer.cancelTimerTask(this);
//System.out.printf("back from canel timer %d\n", System.identityHashCode(this));
//System.exit(0);
-
Long w = waitAfterOrder();
+
+ setStatus("Order closed, %s", o.getType().toString());
+
// System.out.printf("We have now to wait for %d\n", w);
//timerTask = se.timer.startTimerTask(this, w);
- se.timer.XXXrescheduleTimerTask(timerTask, w);
+ // se.timer.XXXrescheduleTimerTask(timerTask, w);
}
// System.out.printf("Updatetd Account\n", "");
@@ -247,54 +255,68 @@ public class RandomTraderA extends AutoTraderBase implements AccountListener {
return 0;
}
-
-
- long waitAfterOrder(){
- if (mode == Action.BUY) {
+
+ long waitAfterOrder() {
+ if (mode == Action.BUY) {
mode = Action.RANDOM;
- return getRandom(wait_after_buy);
+ long r = getRandom(wait_after_buy);
+ setStatus("Wait after buy: %d ms", r);
+ return r;
}
if (mode == Action.SELL) {
mode = Action.RANDOM;
- return getRandom(wait_after_sell);
+ long r = getRandom(wait_after_sell);
+ setStatus("Wait after sell: %d ms", r);
+ return r;
}
// System.out.printf("Return action 0\n");
return 0;
}
-
long doTrade() {
-// System.out.printf("Do Trader %d\n", Thread.currentThread().getId());
- cancelOrders();
-// System.out.printf("Orders Canceled %d\n", Thread.currentThread().getId());
+ long co = cancelOrders();
+ setStatus("Orders cancled: %d",co);
+ if (co>0)
+ mode = Action.RANDOM;
+
Action a = getAction();
// System.out.printf("Action is %s\n", a.toString());
-
if (mode == Action.RANDOM) {
-
+
+ setStatus("Mode is %s, next action is %s",mode.toString(),a.toString());
+
// System.out.printf("Action: %s\n", a.toString());
Integer rc = doTrade1(a);
if (rc != null) {
+ setStatus("Action %s success full, ret %d",a.toString(),rc);
return rc;
}
+
rc = doTrade1(Action.BUY);
if (rc != null) {
+ setStatus("BuyAction %s successfull, ret %d",a.toString(),rc);
+
return rc;
}
rc = doTrade1(Action.SELL);
if (rc != null) {
+ setStatus("SellAction %s successfull, ret %d",a.toString(),rc);
return rc;
}
+
+ setStatus("No trade possible, returning 5000");
// System.out.printf("All ha s failed\n");
return 5000;
}
+
+ setStatus("Current mode is %s",mode.toString());
return waitAfterOrder();
}
diff --git a/src/test/java/sesim/Test.java b/src/test/java/sesim/Test.java
index 4138791..6fb7c57 100644
--- a/src/test/java/sesim/Test.java
+++ b/src/test/java/sesim/Test.java
@@ -38,6 +38,7 @@ import java.util.ArrayList;
import java.util.SortedMap;
import java.util.TreeMap;
import java.lang.ClassLoader.*;
+import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import sesim.Scheduler.TimerTaskRunner;
@@ -111,27 +112,28 @@ public class Test {
static class Runner extends Thread {
}
- static Scheduler s = new Scheduler();
-
- static class MyTask implements TimerTaskRunner{
+ static Scheduler s = new Scheduler();
+
+ static class MyTask implements TimerTaskRunner {
+
+ long ctr = 0;
- long ctr=0;
@Override
public long timerTask() {
ctr++;
- double r=1;
- for (int i=0; i<100000; i++){
- r=r+i*r;
- r=r+1.0;
+ double r = 1;
+ for (int i = 0; i < 100000; i++) {
+ r = r + i * r;
+ r = r + 1.0;
}
- synchronized (this){
+ synchronized (this) {
try {
wait(500);
} catch (InterruptedException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
- System.out.printf("TimerTask %d %d %f\n",ctr,s.currentTimeMillis(),r);
+ System.out.printf("TimerTask %d %d %f\n", ctr, s.currentTimeMillis(), r);
return 1000;
}
@@ -140,28 +142,21 @@ public class Test {
public long getID() {
return 0;
}
-
+
}
-
-
+
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException, MalformedURLException, InstantiationException, IllegalAccessException, IOException {
- Clock clock = new Clock();
-
-
- s.start();
+ Random r;
+ r = new Random(10);
- s.setAcceleration(1);
-
- MyTask t = new MyTask();
-
- s.setAcceleration(1.0);
- s.startTimerTask(t, 0);
-
- s.join();
+ for (int i = 0; i < 100; i++) {
+ int e = r.nextInt(50);
+ System.out.printf("Zufallszahl: %d\n", e);
+ }
}
}