Working stop button

This commit is contained in:
7u83 2017-02-04 16:29:45 +01:00
parent 4a01d8f1f6
commit 98965eed43
11 changed files with 325 additions and 142 deletions

View File

@ -37,13 +37,19 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
protected Rectangle clip_bounds = new Rectangle(); protected Rectangle clip_bounds = new Rectangle();
protected Dimension gdim; protected Dimension gdim;
protected int first_bar, last_bar; private int first_bar, last_bar;
public final void initChart(){
data = new OHLCData(60000*30);
}
/** /**
* Creates new form Chart * Creates new form Chart
*/ */
public Chart() { public Chart() {
initComponents(); initComponents();
initChart();
if (Globals.se == null) { if (Globals.se == null) {
return; return;
} }
@ -52,16 +58,15 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver, Scrollab
} }
// int item_width = 10;
//int items = 350; OHLCData data;
//long ntime = 0;
OHLCData data = new OHLCData(60000*30);
OHLCDataItem current = null; OHLCDataItem current = null;
void drawCandle(Graphics2D g, OHLCData d, int x, int y) { //void drawCandle(Graphics2D g, OHLCData d, int x, int y) {
//
} // }
@Override @Override
public Dimension getPreferredScrollableViewportSize() { public Dimension getPreferredScrollableViewportSize() {

View File

@ -26,6 +26,15 @@
<Property name="text" type="java.lang.String" value="File"/> <Property name="text" type="java.lang.String" value="File"/>
</Properties> </Properties>
<SubComponents> <SubComponents>
<MenuItem class="javax.swing.JMenuItem" name="jMenuItem1">
<Properties>
<Property name="mnemonic" type="int" value="110"/>
<Property name="text" type="java.lang.String" value="New"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem1ActionPerformed"/>
</Events>
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="openMenuItem"> <MenuItem class="javax.swing.JMenuItem" name="openMenuItem">
<Properties> <Properties>
<Property name="mnemonic" type="int" value="111"/> <Property name="mnemonic" type="int" value="111"/>
@ -110,6 +119,34 @@
</MenuItem> </MenuItem>
</SubComponents> </SubComponents>
</Menu> </Menu>
<Menu class="javax.swing.JMenu" name="jMenu1">
<Properties>
<Property name="text" type="java.lang.String" value="Sim"/>
</Properties>
<SubComponents>
<MenuItem class="javax.swing.JMenuItem" name="jMenuItem4">
<Properties>
<Property name="text" type="java.lang.String" value="Start"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem4ActionPerformed"/>
</Events>
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="jMenuItem5">
<Properties>
<Property name="text" type="java.lang.String" value="Stop"/>
</Properties>
</MenuItem>
<MenuItem class="javax.swing.JMenuItem" name="jMenuItem6">
<Properties>
<Property name="text" type="java.lang.String" value="Reset"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jMenuItem6ActionPerformed"/>
</Events>
</MenuItem>
</SubComponents>
</Menu>
<Menu class="javax.swing.JMenu" name="viewMenu"> <Menu class="javax.swing.JMenu" name="viewMenu">
<Properties> <Properties>
<Property name="text" type="java.lang.String" value="View"/> <Property name="text" type="java.lang.String" value="View"/>
@ -201,9 +238,9 @@
<Group type="103" groupAlignment="0" attributes="0"> <Group type="103" groupAlignment="0" attributes="0">
<Component id="jPanel2" max="32767" attributes="0"/> <Component id="jPanel2" max="32767" attributes="0"/>
<Group type="102" attributes="0"> <Group type="102" attributes="0">
<Component id="orderBookPanel1" min="-2" pref="233" max="-2" attributes="0"/> <Component id="orderBookPanel" min="-2" pref="233" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/> <EmptySpace max="-2" attributes="0"/>
<Component id="jScrollPane1" max="32767" attributes="0"/> <Component id="jChartScrollPane" max="32767" attributes="0"/>
</Group> </Group>
</Group> </Group>
<EmptySpace max="-2" attributes="0"/> <EmptySpace max="-2" attributes="0"/>
@ -217,8 +254,8 @@
<Component id="jPanel2" min="-2" max="-2" attributes="0"/> <Component id="jPanel2" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/> <EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0"> <Group type="103" groupAlignment="0" attributes="0">
<Component id="jScrollPane1" max="32767" attributes="0"/> <Component id="jChartScrollPane" max="32767" attributes="0"/>
<Component id="orderBookPanel1" max="32767" attributes="0"/> <Component id="orderBookPanel" max="32767" attributes="0"/>
</Group> </Group>
<EmptySpace max="-2" attributes="0"/> <EmptySpace max="-2" attributes="0"/>
</Group> </Group>
@ -226,13 +263,13 @@
</DimensionLayout> </DimensionLayout>
</Layout> </Layout>
<SubComponents> <SubComponents>
<Component class="gui.OrderBookPanel" name="orderBookPanel1"> <Component class="gui.OrderBookPanel" name="orderBookPanel">
</Component> </Component>
<Container class="javax.swing.JScrollPane" name="jScrollPane1"> <Container class="javax.swing.JScrollPane" name="jChartScrollPane">
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/> <Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents> <SubComponents>
<Container class="chart.Chart" name="chart1"> <Container class="chart.Chart" name="chart">
<Layout> <Layout>
<DimensionLayout dim="0"> <DimensionLayout dim="0">
@ -297,6 +334,9 @@
<Property name="horizontalTextPosition" type="int" value="0"/> <Property name="horizontalTextPosition" type="int" value="0"/>
<Property name="verticalTextPosition" type="int" value="3"/> <Property name="verticalTextPosition" type="int" value="3"/>
</Properties> </Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton1ActionPerformed"/>
</Events>
</Component> </Component>
<Component class="javax.swing.JButton" name="jRunButton"> <Component class="javax.swing.JButton" name="jRunButton">
<Properties> <Properties>
@ -335,7 +375,7 @@
<Component class="javax.swing.JSpinner" name="jSpinner2"> <Component class="javax.swing.JSpinner" name="jSpinner2">
<Properties> <Properties>
<Property name="model" type="javax.swing.SpinnerModel" editor="org.netbeans.modules.form.editors2.SpinnerModelEditor"> <Property name="model" type="javax.swing.SpinnerModel" editor="org.netbeans.modules.form.editors2.SpinnerModelEditor">
<SpinnerModel initial="0.0" maximum="10000.0" minimum="0.0" numberType="java.lang.Double" stepSize="0.1" type="number"/> <SpinnerModel initial="1.0" minimum="0.0" numberType="java.lang.Double" stepSize="100.0" type="number"/>
</Property> </Property>
</Properties> </Properties>
<Events> <Events>

View File

@ -98,31 +98,12 @@ public class NewMDIApplication extends javax.swing.JFrame {
Globals.se.fairValue=moneyTotal/sharesTotal; Globals.se.fairValue=moneyTotal/sharesTotal;
Globals.se.fairValue=1.0;
for (int i=0; i<Globals.se.traders.size(); i++){ for (int i=0; i<Globals.se.traders.size(); i++){
Globals.se.traders.get(i).start(); Globals.se.traders.get(i).start();
} }
// System.exit(0);
/*
AutoTraderConfig cfg1 = new RandomTraderConfig();
AutoTrader rt1 = cfg1.createTrader(Globals.se, null, 100000, 100000);
Globals.se.traders.add(rt1);
rt1.setName("Alice");
rt1.start();
AutoTraderConfig cfg2 = new RandomTraderConfig();
for (int i = 0; i < 700; i++) {
AutoTrader randt = cfg2.createTrader(Globals.se, null, 100, 100);
Globals.se.traders.add(randt);
randt.setName("Bob");
randt.start();
}
*/
} }
@ -137,9 +118,9 @@ public class NewMDIApplication extends javax.swing.JFrame {
jScrollPane2 = new javax.swing.JScrollPane(); jScrollPane2 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea(); jTextArea1 = new javax.swing.JTextArea();
orderBookPanel1 = new gui.OrderBookPanel(); orderBookPanel = new gui.OrderBookPanel();
jScrollPane1 = new javax.swing.JScrollPane(); jChartScrollPane = new javax.swing.JScrollPane();
chart1 = new chart.Chart(); chart = new chart.Chart();
jPanel2 = new javax.swing.JPanel(); jPanel2 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton(); jButton1 = new javax.swing.JButton();
jRunButton = new javax.swing.JButton(); jRunButton = new javax.swing.JButton();
@ -148,6 +129,7 @@ public class NewMDIApplication extends javax.swing.JFrame {
clock1 = new gui.Clock(); clock1 = new gui.Clock();
menuBar = new javax.swing.JMenuBar(); menuBar = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu(); fileMenu = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
openMenuItem = new javax.swing.JMenuItem(); openMenuItem = new javax.swing.JMenuItem();
saveMenuItem = new javax.swing.JMenuItem(); saveMenuItem = new javax.swing.JMenuItem();
saveAsMenuItem = new javax.swing.JMenuItem(); saveAsMenuItem = new javax.swing.JMenuItem();
@ -159,6 +141,10 @@ public class NewMDIApplication extends javax.swing.JFrame {
deleteMenuItem = new javax.swing.JMenuItem(); deleteMenuItem = new javax.swing.JMenuItem();
jSeparator2 = new javax.swing.JPopupMenu.Separator(); jSeparator2 = new javax.swing.JPopupMenu.Separator();
editPreferences = new javax.swing.JMenuItem(); editPreferences = new javax.swing.JMenuItem();
jMenu1 = new javax.swing.JMenu();
jMenuItem4 = new javax.swing.JMenuItem();
jMenuItem5 = new javax.swing.JMenuItem();
jMenuItem6 = new javax.swing.JMenuItem();
viewMenu = new javax.swing.JMenu(); viewMenu = new javax.swing.JMenu();
jMenuItem2 = new javax.swing.JMenuItem(); jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem(); jMenuItem3 = new javax.swing.JMenuItem();
@ -176,24 +162,29 @@ public class NewMDIApplication extends javax.swing.JFrame {
setMinimumSize(new java.awt.Dimension(640, 480)); setMinimumSize(new java.awt.Dimension(640, 480));
setPreferredSize(new java.awt.Dimension(800, 561)); setPreferredSize(new java.awt.Dimension(800, 561));
javax.swing.GroupLayout chart1Layout = new javax.swing.GroupLayout(chart1); javax.swing.GroupLayout chartLayout = new javax.swing.GroupLayout(chart);
chart1.setLayout(chart1Layout); chart.setLayout(chartLayout);
chart1Layout.setHorizontalGroup( chartLayout.setHorizontalGroup(
chart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) chartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE) .addGap(0, 300, Short.MAX_VALUE)
); );
chart1Layout.setVerticalGroup( chartLayout.setVerticalGroup(
chart1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) chartLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE) .addGap(0, 300, Short.MAX_VALUE)
); );
jScrollPane1.setViewportView(chart1); jChartScrollPane.setViewportView(chart);
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toolbarButtonGraphics/general/Stop24.gif"))); // NOI18N jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toolbarButtonGraphics/general/Stop24.gif"))); // NOI18N
jButton1.setText("Stop"); jButton1.setText("Stop");
jButton1.setFocusable(false); jButton1.setFocusable(false);
jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jRunButton.setFont(jRunButton.getFont()); jRunButton.setFont(jRunButton.getFont());
jRunButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/run.gif"))); // NOI18N jRunButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/run.gif"))); // NOI18N
@ -219,7 +210,7 @@ public class NewMDIApplication extends javax.swing.JFrame {
} }
}); });
jSpinner2.setModel(new javax.swing.SpinnerNumberModel(0.0d, 0.0d, 10000.0d, 0.1d)); jSpinner2.setModel(new javax.swing.SpinnerNumberModel(1.0d, 0.0d, null, 100.0d));
jSpinner2.addChangeListener(new javax.swing.event.ChangeListener() { jSpinner2.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) { public void stateChanged(javax.swing.event.ChangeEvent evt) {
jSpinner2StateChanged(evt); jSpinner2StateChanged(evt);
@ -260,6 +251,15 @@ public class NewMDIApplication extends javax.swing.JFrame {
fileMenu.setMnemonic('f'); fileMenu.setMnemonic('f');
fileMenu.setText("File"); fileMenu.setText("File");
jMenuItem1.setMnemonic('n');
jMenuItem1.setText("New");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
fileMenu.add(jMenuItem1);
openMenuItem.setMnemonic('o'); openMenuItem.setMnemonic('o');
openMenuItem.setText("Open"); openMenuItem.setText("Open");
openMenuItem.addActionListener(new java.awt.event.ActionListener() { openMenuItem.addActionListener(new java.awt.event.ActionListener() {
@ -337,6 +337,29 @@ public class NewMDIApplication extends javax.swing.JFrame {
menuBar.add(editMenu); menuBar.add(editMenu);
jMenu1.setText("Sim");
jMenuItem4.setText("Start");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem4ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem4);
jMenuItem5.setText("Stop");
jMenu1.add(jMenuItem5);
jMenuItem6.setText("Reset");
jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem6ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem6);
menuBar.add(jMenu1);
viewMenu.setText("View"); viewMenu.setText("View");
jMenuItem2.setText("Traders"); jMenuItem2.setText("Traders");
@ -394,9 +417,9 @@ public class NewMDIApplication extends javax.swing.JFrame {
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup() .addGroup(layout.createSequentialGroup()
.addComponent(orderBookPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(orderBookPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1))) .addComponent(jChartScrollPane)))
.addContainerGap()) .addContainerGap())
); );
layout.setVerticalGroup( layout.setVerticalGroup(
@ -406,8 +429,8 @@ public class NewMDIApplication extends javax.swing.JFrame {
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1) .addComponent(jChartScrollPane)
.addComponent(orderBookPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(orderBookPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap()) .addContainerGap())
); );
@ -424,9 +447,32 @@ public class NewMDIApplication extends javax.swing.JFrame {
}//GEN-LAST:event_aboutMenuItemActionPerformed }//GEN-LAST:event_aboutMenuItemActionPerformed
private void jRunButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRunButtonActionPerformed
void startSim(){
resetSim();
this.startTraders(); this.startTraders();
Globals.se.timer.start(); Globals.se.timer.start();
}
void stopSim(){
Globals.se.timer.terminate();
}
void resetSim(){
Globals.se.terminate();
Globals.se.reset();
chart.initChart();
chart.invalidate();
chart.repaint();
this.orderBookPanel.invalidate();
this.orderBookPanel.repaint();
}
private void jRunButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRunButtonActionPerformed
startSim();
}//GEN-LAST:event_jRunButtonActionPerformed }//GEN-LAST:event_jRunButtonActionPerformed
private void editPreferencesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editPreferencesActionPerformed private void editPreferencesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editPreferencesActionPerformed
@ -527,6 +573,25 @@ public class NewMDIApplication extends javax.swing.JFrame {
Globals.se.timer.setMultiply(val); Globals.se.timer.setMultiply(val);
}//GEN-LAST:event_jSpinner2StateChanged }//GEN-LAST:event_jSpinner2StateChanged
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jMenuItem1ActionPerformed
private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
startSim();
}//GEN-LAST:event_jMenuItem4ActionPerformed
private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
resetSim();
//this.chart.
//this.initComponents();
}//GEN-LAST:event_jMenuItem6ActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
stopSim();
}//GEN-LAST:event_jButton1ActionPerformed
/** /**
* @param args the command line arguments * @param args the command line arguments
* @throws java.lang.IllegalAccessException * @throws java.lang.IllegalAccessException
@ -583,7 +648,7 @@ public class NewMDIApplication extends javax.swing.JFrame {
// Variables declaration - do not modify//GEN-BEGIN:variables // Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenuItem aboutMenuItem; private javax.swing.JMenuItem aboutMenuItem;
private chart.Chart chart1; private chart.Chart chart;
private gui.Clock clock1; private gui.Clock clock1;
private javax.swing.JMenuItem contentMenuItem; private javax.swing.JMenuItem contentMenuItem;
private javax.swing.JMenuItem deleteMenuItem; private javax.swing.JMenuItem deleteMenuItem;
@ -595,11 +660,16 @@ public class NewMDIApplication extends javax.swing.JFrame {
private javax.swing.JMenu helpMenu; private javax.swing.JMenu helpMenu;
private javax.swing.JButton jButton1; private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2; private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jChartScrollPane;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2; private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3; private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JMenuItem jMenuItem4;
private javax.swing.JMenuItem jMenuItem5;
private javax.swing.JMenuItem jMenuItem6;
private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel2;
private javax.swing.JButton jRunButton; private javax.swing.JButton jRunButton;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JPopupMenu.Separator jSeparator1; private javax.swing.JPopupMenu.Separator jSeparator1;
private javax.swing.JPopupMenu.Separator jSeparator2; private javax.swing.JPopupMenu.Separator jSeparator2;
@ -607,7 +677,7 @@ public class NewMDIApplication extends javax.swing.JFrame {
private javax.swing.JTextArea jTextArea1; private javax.swing.JTextArea jTextArea1;
private javax.swing.JMenuBar menuBar; private javax.swing.JMenuBar menuBar;
private javax.swing.JMenuItem openMenuItem; private javax.swing.JMenuItem openMenuItem;
private gui.OrderBookPanel orderBookPanel1; private gui.OrderBookPanel orderBookPanel;
private javax.swing.JMenuItem pasteMenuItem; private javax.swing.JMenuItem pasteMenuItem;
private javax.swing.JMenuItem saveAsMenuItem; private javax.swing.JMenuItem saveAsMenuItem;
private javax.swing.JMenuItem saveMenuItem; private javax.swing.JMenuItem saveMenuItem;

View File

@ -79,6 +79,8 @@ public class TraderListPanel extends javax.swing.JPanel
} }
final ArrayList<TraderListItem> getTraderList() { final ArrayList<TraderListItem> getTraderList() {
if (se.traders==null)
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;

View File

@ -56,12 +56,12 @@ public class Exchange {
IDGenerator account_id = new IDGenerator(); IDGenerator account_id = new IDGenerator();
//public static Timer timer = new Timer(); //public static Timer timer = new Timer();
public Scheduler timer = new Scheduler(); public Scheduler timer; // = new Scheduler();
public ArrayList<AutoTrader> traders = new ArrayList(); public ArrayList<AutoTrader> traders;
/** /**
* Implements a trading account * Implements a trading account
@ -113,7 +113,7 @@ public class Exchange {
} }
// private final ConcurrentHashMap<Double, Account> accounts = new ConcurrentHashMap<>(); // private final ConcurrentHashMap<Double, Account> accounts = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Double, Account> accounts = new ConcurrentHashMap<>(); private ConcurrentHashMap<Double, Account> accounts;
public double createAccount(double money, double shares) { public double createAccount(double money, double shares) {
@ -167,7 +167,7 @@ public class Exchange {
} }
HashMap<OrderType, SortedSet<Order>> order_books = new HashMap(); HashMap<OrderType, SortedSet<Order>> order_books;
IDGenerator order_id = new IDGenerator(); IDGenerator order_id = new IDGenerator();
@ -224,21 +224,38 @@ public class Exchange {
/** /**
* Histrory of quotes * Histrory of quotes
*/ */
public TreeSet<Quote> quoteHistory = new TreeSet<>(); public TreeSet<Quote> quoteHistory; // = new TreeSet<>();
final void initExchange(){
timer = new Scheduler(); // timer = new Scheduler();
random = new Random(12);
quoteHistory = new TreeSet();
accounts = new ConcurrentHashMap<>();
traders = new ArrayList();
num_trades=0;
// Create order books
order_books= new HashMap();
for (OrderType type : OrderType.values()) {
order_books.put(type, new TreeSet(new OrderComparator(type)));
}
}
/** /**
* Constructor * Constructor
*/ */
public Exchange() { public Exchange() {
this.random = new java.util.Random(12); qrlist = (new CopyOnWriteArrayList<>());
initExchange();
this.qrlist = (new CopyOnWriteArrayList<>());
// Create order books
for (OrderType type : OrderType.values()) {
//SortedSet b = new TreeSet(new OrderComparator(type));
order_books.put(type, new TreeSet(new OrderComparator(type)));
}
} }
@ -262,10 +279,26 @@ public class Exchange {
} }
void start() {
/**
* Start the exchange
*/
public void start() {
timer.start(); timer.start();
} }
public void reset(){
initExchange();
}
public void terminate() {
timer.terminate();
}
/*
class BidBook extends TreeSet { class BidBook extends TreeSet {
TreeSet t = new TreeSet(); TreeSet t = new TreeSet();
@ -275,6 +308,7 @@ public class Exchange {
return true; return true;
} }
} }
*/
public SortedSet<Quote> getQuoteHistory(long start) { public SortedSet<Quote> getQuoteHistory(long start) {
@ -366,7 +400,7 @@ public class Exchange {
} }
// Here we store the list of quote receivers // Here we store the list of quote receivers
private final List<QuoteReceiver> qrlist; private List<QuoteReceiver> qrlist;
/** /**
* *
@ -455,7 +489,7 @@ public class Exchange {
return ret; return ret;
} }
final Random random; Random random;
public int randNextInt() { public int randNextInt() {
System.out.printf("Next int: %d\n", random.nextInt()); System.out.printf("Next int: %d\n", random.nextInt());
@ -463,7 +497,7 @@ public class Exchange {
} }
public int randNextInt(int bounds) { public int randNextInt(int bounds) {
System.out.printf("Next doub: %f\n", random.nextDouble());
return random.nextInt(bounds); return random.nextInt(bounds);
} }
@ -493,6 +527,8 @@ public class Exchange {
} }
/** /**
* *
*/ */

View File

@ -25,7 +25,7 @@
*/ */
package sesim; package sesim;
import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date; //import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Comparator; import java.util.Comparator;
@ -45,7 +45,11 @@ public class Scheduler extends Thread {
private double multiplier = 0.0; private double multiplier = 0.0;
public void setMultiply(double val) { public void setMultiply(double val) {
this.multiplier = val; this.multiplier = val;
synchronized(this){
this.notify();
}
} }
public double getMultiply() { public double getMultiply() {
@ -53,27 +57,36 @@ public class Scheduler extends Thread {
} }
private final SortedMap<Long, SortedSet<TimerTask>> event_queue = new TreeMap<>(); private final SortedMap<Long, SortedSet<TimerTask>> event_queue = new TreeMap<>();
private boolean halt = false;
public interface TimerTask { public interface TimerTask {
long timerTask(); long timerTask();
} }
private boolean terminate = false;
/** /**
* \ * Terminate the scheduler
*
*/ */
public void halt() { public void terminate() {
halt = true; terminate = true;
synchronized (event_queue) { synchronized (event_queue) {
event_queue.notifyAll(); event_queue.notifyAll();
} }
}
@Override
public void start() {
if (this.isAlive()) {
return;
}
this.initScheduler();
super.start();
} }
/**
*
*/
private class ObjectComparator implements Comparator<Object> { private class ObjectComparator implements Comparator<Object> {
@Override @Override
@ -148,7 +161,6 @@ public class Scheduler extends Thread {
private boolean addEvent(TimerTask e, long evtime) { private boolean addEvent(TimerTask e, long evtime) {
// long evtime = time + currentTimeMillis(); // long evtime = time + currentTimeMillis();
SortedSet<TimerTask> s = event_queue.get(evtime); SortedSet<TimerTask> s = event_queue.get(evtime);
if (s == null) { if (s == null) {
s = new TreeSet<>(new ObjectComparator()); s = new TreeSet<>(new ObjectComparator());
@ -167,7 +179,6 @@ public class Scheduler extends Thread {
long ct = currentTimeMillis1(); long ct = currentTimeMillis1();
//System.out.printf("Current CMP %d %d\n", ct, t); //System.out.printf("Current CMP %d %d\n", ct, t);
if (t <= ct) { if (t <= ct) {
this.current_time_millis = t; this.current_time_millis = t;
SortedSet s = event_queue.get(t); SortedSet s = event_queue.get(t);
@ -189,10 +200,16 @@ public class Scheduler extends Thread {
} }
void initScheduler() {
current_time_millis = 0.0;
terminate = false;
}
@Override @Override
public void run() { public void run() {
// this.current_time_millis=0;
while (!halt) { while (!terminate) {
long wtime = runEvents(); long wtime = runEvents();
@ -214,6 +231,8 @@ public class Scheduler extends Thread {
} }
} }
this.event_queue.clear();
} }
} }

View File

@ -121,7 +121,7 @@ public class RandomTrader extends AutoTrader {
return (max - min) * r + min; return (max - min) * r + min;
} }
protected int getRandom(Integer[] minmax) { protected int getRandom(Long[] minmax) {
return (int) Math.round(getRandom(minmax[0], minmax[1])); return (int) Math.round(getRandom(minmax[0], minmax[1]));
} }

View File

@ -44,13 +44,13 @@ public class RandomTraderConfig extends AutoTraderBase implements AutoTraderConf
public Float[] sell_volume = {100f, 100f}; public Float[] sell_volume = {100f, 100f};
public Float[] sell_limit = {-0.1f, 0.10101f}; public Float[] sell_limit = {-0.1f, 0.10101f};
public Integer[] sell_wait = {10000, 50000}; public Long[] sell_wait = {10000L, 50000L};
public Integer[] wait_after_sell = {1000, 30000}; public Long[] wait_after_sell = {1000L, 30000L};
public Float[] buy_volume = {100f, 100f}; public Float[] buy_volume = {100f, 100f};
public Float[] buy_limit = {-0.1f, 0.10101f}; public Float[] buy_limit = {-0.1f, 0.10101f};
public Integer[] buy_wait = {10000, 50000}; public Long[] buy_wait = {10000L, 50000L};
public Integer[] wait_after_buy = {10, 30}; 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, double money, double shares) {
@ -122,6 +122,17 @@ public class RandomTraderConfig extends AutoTraderBase implements AutoTraderConf
} }
private Long[] to_long(JSONArray a) {
Long[] ret = new Long[a.length()];
for (int i = 0; i < a.length(); i++) {
ret[i] = a.getLong(i);
}
return ret;
}
private Number[] to_arn(JSONArray a) { private Number[] to_arn(JSONArray a) {
Number[] ret = new Number[a.length()]; Number[] ret = new Number[a.length()];
// Float x[] = new Float[2]; // Float x[] = new Float[2];
@ -148,11 +159,11 @@ public class RandomTraderConfig extends AutoTraderBase implements AutoTraderConf
buy_volume = to_float(cfg.getJSONArray(BUY_VOLUME)); buy_volume = to_float(cfg.getJSONArray(BUY_VOLUME));
sell_limit = to_float(cfg.getJSONArray(SELL_LIMIT)); sell_limit = to_float(cfg.getJSONArray(SELL_LIMIT));
buy_limit = to_float(cfg.getJSONArray(BUY_LIMIT)); buy_limit = to_float(cfg.getJSONArray(BUY_LIMIT));
sell_wait = to_integer(cfg.getJSONArray(SELL_WAIT)); sell_wait = to_long(cfg.getJSONArray(SELL_WAIT));
buy_wait = to_integer(cfg.getJSONArray(BUY_WAIT)); buy_wait = to_long(cfg.getJSONArray(BUY_WAIT));
wait_after_sell = to_integer(cfg.getJSONArray(WAIT_AFTER_SELL)); wait_after_sell = to_long(cfg.getJSONArray(WAIT_AFTER_SELL));
wait_after_buy = to_integer(cfg.getJSONArray(WAIT_AFTER_BUY)); wait_after_buy = to_long(cfg.getJSONArray(WAIT_AFTER_BUY));
} }

View File

@ -220,28 +220,28 @@
<Component class="javax.swing.JSpinner" name="buyWaitMin"> <Component class="javax.swing.JSpinner" name="buyWaitMin">
<Properties> <Properties>
<Property name="model" type="javax.swing.SpinnerModel" editor="org.netbeans.modules.form.editors2.SpinnerModelEditor"> <Property name="model" type="javax.swing.SpinnerModel" editor="org.netbeans.modules.form.editors2.SpinnerModelEditor">
<SpinnerModel initial="0" minimum="0" numberType="java.lang.Integer" stepSize="1000" type="number"/> <SpinnerModel initial="0" minimum="0" numberType="java.lang.Long" stepSize="1000" type="number"/>
</Property> </Property>
</Properties> </Properties>
</Component> </Component>
<Component class="javax.swing.JSpinner" name="sellWaitMin"> <Component class="javax.swing.JSpinner" name="sellWaitMin">
<Properties> <Properties>
<Property name="model" type="javax.swing.SpinnerModel" editor="org.netbeans.modules.form.editors2.SpinnerModelEditor"> <Property name="model" type="javax.swing.SpinnerModel" editor="org.netbeans.modules.form.editors2.SpinnerModelEditor">
<SpinnerModel initial="0" minimum="0" numberType="java.lang.Integer" stepSize="1000" type="number"/> <SpinnerModel initial="0" minimum="0" numberType="java.lang.Long" stepSize="1000" type="number"/>
</Property> </Property>
</Properties> </Properties>
</Component> </Component>
<Component class="javax.swing.JSpinner" name="sellWaitMax"> <Component class="javax.swing.JSpinner" name="sellWaitMax">
<Properties> <Properties>
<Property name="model" type="javax.swing.SpinnerModel" editor="org.netbeans.modules.form.editors2.SpinnerModelEditor"> <Property name="model" type="javax.swing.SpinnerModel" editor="org.netbeans.modules.form.editors2.SpinnerModelEditor">
<SpinnerModel initial="0" minimum="0" numberType="java.lang.Integer" stepSize="1000" type="number"/> <SpinnerModel initial="0" minimum="0" numberType="java.lang.Long" stepSize="1000" type="number"/>
</Property> </Property>
</Properties> </Properties>
</Component> </Component>
<Component class="javax.swing.JSpinner" name="buyWaitMax"> <Component class="javax.swing.JSpinner" name="buyWaitMax">
<Properties> <Properties>
<Property name="model" type="javax.swing.SpinnerModel" editor="org.netbeans.modules.form.editors2.SpinnerModelEditor"> <Property name="model" type="javax.swing.SpinnerModel" editor="org.netbeans.modules.form.editors2.SpinnerModelEditor">
<SpinnerModel initial="0" minimum="0" numberType="java.lang.Integer" stepSize="1000" type="number"/> <SpinnerModel initial="0" minimum="0" numberType="java.lang.Long" stepSize="1000" type="number"/>
</Property> </Property>
</Properties> </Properties>
</Component> </Component>

View File

@ -79,15 +79,15 @@ public class RandomTraderGui extends AutoTraderGui {
cfg.buy_limit[1] = (Float) this.buyLimitMax.getValue(); cfg.buy_limit[1] = (Float) this.buyLimitMax.getValue();
cfg.sell_limit[0] = (Float) this.sellLimitMin.getValue(); cfg.sell_limit[0] = (Float) this.sellLimitMin.getValue();
cfg.sell_limit[1] = (Float) this.sellLimitMax.getValue(); cfg.sell_limit[1] = (Float) this.sellLimitMax.getValue();
cfg.buy_wait[0] = (Integer) this.buyWaitMin.getValue(); cfg.buy_wait[0] = (Long) this.buyWaitMin.getValue();
cfg.buy_wait[1] = (Integer) this.buyWaitMax.getValue(); cfg.buy_wait[1] = (Long) this.buyWaitMax.getValue();
cfg.sell_wait[0] = (Integer) this.sellWaitMin.getValue(); cfg.sell_wait[0] = (Long) this.sellWaitMin.getValue();
cfg.sell_wait[1] = (Integer) this.sellWaitMax.getValue(); cfg.sell_wait[1] = (Long) this.sellWaitMax.getValue();
cfg.wait_after_buy[0] = (Integer) this.waitAfterBuyMin.getValue(); cfg.wait_after_buy[0] = (Long) this.waitAfterBuyMin.getValue();
cfg.wait_after_buy[1] = (Integer) this.waitAfterBuyMax.getValue(); cfg.wait_after_buy[1] = (Long) this.waitAfterBuyMax.getValue();
cfg.wait_after_sell[0] = (Integer) this.waitAfterSellMin.getValue(); cfg.wait_after_sell[0] = (Long) this.waitAfterSellMin.getValue();
cfg.wait_after_sell[1] = (Integer) this.waitAfterSellMax.getValue(); cfg.wait_after_sell[1] = (Long) this.waitAfterSellMax.getValue();
// cfg.buy_volume[0] = Float.parseFloat(this.buyVolMin.getText()); // cfg.buy_volume[0] = Float.parseFloat(this.buyVolMin.getText());
@ -153,13 +153,13 @@ public class RandomTraderGui extends AutoTraderGui {
setBorder(null); setBorder(null);
buyWaitMin.setModel(new javax.swing.SpinnerNumberModel(0, 0, null, 1000)); buyWaitMin.setModel(new javax.swing.SpinnerNumberModel(0L, 0L, null, 1000L));
sellWaitMin.setModel(new javax.swing.SpinnerNumberModel(0, 0, null, 1000)); sellWaitMin.setModel(new javax.swing.SpinnerNumberModel(0L, 0L, null, 1000L));
sellWaitMax.setModel(new javax.swing.SpinnerNumberModel(0, 0, null, 1000)); sellWaitMax.setModel(new javax.swing.SpinnerNumberModel(0L, 0L, null, 1000L));
buyWaitMax.setModel(new javax.swing.SpinnerNumberModel(0, 0, null, 1000)); buyWaitMax.setModel(new javax.swing.SpinnerNumberModel(0L, 0L, null, 1000L));
buyLimitMin.setModel(new javax.swing.SpinnerNumberModel(0.0f, -100.0f, null, 1.0f)); buyLimitMin.setModel(new javax.swing.SpinnerNumberModel(0.0f, -100.0f, null, 1.0f));

View File

@ -47,13 +47,13 @@ public class SwitchingTraderConfig extends RandomTraderConfig implements AutoTra
sell_volume = new Float[]{17f, 100f}; sell_volume = new Float[]{17f, 100f};
sell_limit = new Float[]{-30f, 1f}; sell_limit = new Float[]{-30f, 1f};
sell_wait = new Integer[]{1, 5}; sell_wait = new Long[]{1L, 5L};
wait_after_sell = new Integer[]{1, 5}; wait_after_sell = new Long[]{1L, 5L};
buy_volume = new Float[]{18f, 100f}; buy_volume = new Float[]{18f, 100f};
buy_limit = new Float[]{-1f, 30f}; buy_limit = new Float[]{-1f, 30f};
buy_wait = new Integer[]{1, 5}; buy_wait = new Long[]{1L, 5L};
wait_after_buy = new Integer[]{1, 5}; wait_after_buy = new Long[]{1L, 5L};
} }
@Override @Override