some tes, some improvements

This commit is contained in:
7u83 2017-03-13 07:21:13 +01:00
parent 7c89af5a42
commit 5cf177a346
5 changed files with 100 additions and 101 deletions

View File

@ -305,11 +305,11 @@
<Component id="jButton2" min="-2" pref="67" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="stopButton" min="-2" pref="63" max="-2" attributes="0"/>
<EmptySpace pref="389" max="32767" attributes="0"/>
<EmptySpace pref="375" max="32767" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="clock" alignment="1" min="-2" max="-2" attributes="0"/>
<Group type="102" alignment="1" attributes="0">
<Component id="jComboBox1" min="-2" max="-2" attributes="0"/>
<Component id="jLabel1" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="accelSpinner" min="-2" pref="100" max="-2" attributes="0"/>
</Group>
@ -326,18 +326,12 @@
<Group type="102" attributes="0">
<EmptySpace min="-2" pref="3" max="-2" attributes="0"/>
<Component id="clock" min="-2" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="accelSpinner" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
<Group type="102" alignment="1" attributes="0">
<EmptySpace max="32767" attributes="0"/>
<Component id="jComboBox1" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="accelSpinner" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="jLabel1" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="0" pref="5" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
@ -394,35 +388,23 @@
<Component class="javax.swing.JSpinner" name="accelSpinner">
<Properties>
<Property name="model" type="javax.swing.SpinnerModel" editor="org.netbeans.modules.form.editors2.SpinnerModelEditor">
<SpinnerModel initial="1.0" minimum="0.0" numberType="java.lang.Double" stepSize="100.0" type="number"/>
<SpinnerModel initial="1.0" minimum="0.0" numberType="java.lang.Double" stepSize="1.0" type="number"/>
</Property>
</Properties>
<Events>
<EventHandler event="stateChanged" listener="javax.swing.event.ChangeListener" parameters="javax.swing.event.ChangeEvent" handler="accelSpinnerStateChanged"/>
<EventHandler event="propertyChange" listener="java.beans.PropertyChangeListener" parameters="java.beans.PropertyChangeEvent" handler="accelSpinnerPropertyChange"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_SerializeTo" type="java.lang.String" value="NewMDIApplication_accelSpinner"/>
</AuxValues>
</Component>
<Component class="gui.Clock" name="clock">
</Component>
<Component class="javax.swing.JComboBox" name="jComboBox1">
<Component class="javax.swing.JLabel" name="jLabel1">
<Properties>
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
<StringArray count="7">
<StringItem index="0" value="1x"/>
<StringItem index="1" value="2x"/>
<StringItem index="2" value="4x"/>
<StringItem index="3" value="10x"/>
<StringItem index="4" value="100x"/>
<StringItem index="5" value="1000x"/>
<StringItem index="6" value="max."/>
</StringArray>
</Property>
<Property name="text" type="java.lang.String" value="Acceleration:"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jComboBox1ActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="&lt;String&gt;"/>
</AuxValues>
</Component>
</SubComponents>
</Container>

View File

@ -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<String> jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;

View File

@ -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 {

View File

@ -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();
}

View File

@ -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);
}
}
}