diff --git a/src/main/java/gui/OrdersList.form b/src/main/java/gui/OrdersList.form index df265fa..06de21b 100644 --- a/src/main/java/gui/OrdersList.form +++ b/src/main/java/gui/OrdersList.form @@ -8,15 +8,29 @@ - + - + + + + + + + + + + + + + + + diff --git a/src/main/java/gui/OrdersList.java b/src/main/java/gui/OrdersList.java index 2b2a3d9..d823e17 100644 --- a/src/main/java/gui/OrdersList.java +++ b/src/main/java/gui/OrdersList.java @@ -25,6 +25,7 @@ */ package gui; +import java.awt.Frame; import java.awt.MouseInfo; import java.awt.Point; import java.util.Iterator; @@ -32,8 +33,11 @@ import java.util.Map; import java.util.Set; import java.util.function.BiConsumer; import javax.swing.table.DefaultTableModel; +import sesim.Exchange; import sesim.Exchange.Account; import sesim.Exchange.Order; +import sesim.Exchange.OrderType; +import traders.ManTrader.CreateOrderDialog; /** * @@ -41,14 +45,18 @@ import sesim.Exchange.Order; */ public class OrdersList extends javax.swing.JPanel { - public Account account; + private Account account; DefaultTableModel model; public final void updateModel() { + System.out.printf("Update Model\n"); if (null == account) { + return; } + System.out.printf("Now updateing\n"); + int row = 0; Iterator> it = account.getOrders().entrySet().iterator(); @@ -78,8 +86,13 @@ public class OrdersList extends javax.swing.JPanel { initComponents(); model = (DefaultTableModel) table.getModel(); model.setRowCount(0); - updateModel(); table.setFillsViewportHeight(true); + updateModel(); + } + + public void initOrderList(Account account) { + this.account = account; + updateModel(); } /** @@ -92,16 +105,35 @@ public class OrdersList extends javax.swing.JPanel { private void initComponents() { ctxMenu = new javax.swing.JPopupMenu(); - ctxMenuCreateOrder = new javax.swing.JMenuItem(); + ctxMenuCreateBuyOrder = new javax.swing.JMenuItem(); + ctxMenuCreateSellOrder = new javax.swing.JMenuItem(); ctxMenuCancelOrder = new javax.swing.JMenuItem(); ctxMenuModifyOder = new javax.swing.JMenuItem(); jScrollPane1 = new javax.swing.JScrollPane(); table = new javax.swing.JTable(); - ctxMenuCreateOrder.setText("Create Order"); - ctxMenu.add(ctxMenuCreateOrder); + ctxMenuCreateBuyOrder.setText("Create Buy Order"); + ctxMenuCreateBuyOrder.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + ctxMenuCreateBuyOrderActionPerformed(evt); + } + }); + ctxMenu.add(ctxMenuCreateBuyOrder); + + ctxMenuCreateSellOrder.setText("Create Sell Order"); + ctxMenuCreateSellOrder.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + ctxMenuCreateSellOrderActionPerformed(evt); + } + }); + ctxMenu.add(ctxMenuCreateSellOrder); ctxMenuCancelOrder.setText("Cancel Order"); + ctxMenuCancelOrder.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + ctxMenuCancelOrderActionPerformed(evt); + } + }); ctxMenu.add(ctxMenuCancelOrder); ctxMenuModifyOder.setText("Modify Oder"); @@ -158,11 +190,10 @@ public class OrdersList extends javax.swing.JPanel { Point point = evt.getPoint(); int currentRow = table.rowAtPoint(point); - if (currentRow==-1){ - - } - else { - + if (currentRow == -1) { + + } else { + table.setRowSelectionInterval(currentRow, currentRow); } @@ -178,11 +209,38 @@ public class OrdersList extends javax.swing.JPanel { }//GEN-LAST:event_tableMousePressed + private void createOrder(OrderType t) { + CreateOrderDialog cd = new CreateOrderDialog(Globals.frame, true, account, t); + //cd.initDialog(account); + cd.setVisible(true); + + } + + + private void ctxMenuCreateSellOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ctxMenuCreateSellOrderActionPerformed + createOrder(OrderType.SELLLIMIT); + + }//GEN-LAST:event_ctxMenuCreateSellOrderActionPerformed + + private void ctxMenuCreateBuyOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ctxMenuCreateBuyOrderActionPerformed + createOrder(OrderType.BUYLIMIT); + }//GEN-LAST:event_ctxMenuCreateBuyOrderActionPerformed + + private void ctxMenuCancelOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ctxMenuCancelOrderActionPerformed + int r = table.getSelectedRow(); + Long id = (Long) model.getValueAt(r, 0); + + System.out.printf("Should cancel %d\n", id); + Globals.se.cancelOrder(account.getID(), id); + + }//GEN-LAST:event_ctxMenuCancelOrderActionPerformed + // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JPopupMenu ctxMenu; private javax.swing.JMenuItem ctxMenuCancelOrder; - private javax.swing.JMenuItem ctxMenuCreateOrder; + private javax.swing.JMenuItem ctxMenuCreateBuyOrder; + private javax.swing.JMenuItem ctxMenuCreateSellOrder; private javax.swing.JMenuItem ctxMenuModifyOder; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable table; diff --git a/src/main/java/gui/TraderListPanel.java b/src/main/java/gui/TraderListPanel.java index 495c125..64b8547 100644 --- a/src/main/java/gui/TraderListPanel.java +++ b/src/main/java/gui/TraderListPanel.java @@ -58,8 +58,7 @@ public class TraderListPanel extends javax.swing.JPanel { sesim.Quote q = Globals.se.getLastQuoete(); double price = q == null ? 0 : q.price; - System.out.printf("Price?: %f\n", price); - + int size = Globals.se.traders.size(); model.setRowCount(size); for (int i = 0; i < size; i++) { diff --git a/src/main/java/sesim/Exchange.java b/src/main/java/sesim/Exchange.java index 6d75ae4..3620dfa 100644 --- a/src/main/java/sesim/Exchange.java +++ b/src/main/java/sesim/Exchange.java @@ -227,7 +227,7 @@ public class Exchange { this.volume = roundShares(volume); this.initial_volume = this.volume; this.created = timer.currentTimeMillis(); - this.status=OrderStatus.OPEN; + this.status = OrderStatus.OPEN; } public long getID() { @@ -257,11 +257,10 @@ public class Exchange { public Account getAccount() { return account; } - - public OrderStatus getOrderStatus(){ + + public OrderStatus getOrderStatus() { return status; } - } @@ -361,52 +360,66 @@ public class Exchange { public final String CFG_SHARES_DECIMALS = "shares_decimals"; public void putConfig(JSONObject cfg) { - try{ + try { this.setMoneyDecimals(cfg.getInt(CFG_MONEY_DECIMALS)); this.setSharesDecimals(cfg.getInt(CFG_SHARES_DECIMALS)); - } - catch (Exception e){ - + } catch (Exception e) { + } } public Quote getCurrentPrice() { - /* if (!this.quoteHistory.isEmpty()){ - Quote q = this.quoteHistory.pollLast(); - System.out.printf("Quote: %f\n", q.price); - return q; - } - - return null; - */ SortedSet bid = order_books.get(OrderType.BUYLIMIT); SortedSet ask = order_books.get(OrderType.SELLLIMIT); - Quote q = null; - tradelock.lock(); - if (!bid.isEmpty() && !ask.isEmpty()) { - q = new Quote(); - q.price = (bid.first().limit + ask.first().limit) / 2.0; - + Quote lq = this.getLastQuoete(); + Order b = null, a = null; + if (!bid.isEmpty()) { + b = bid.first(); + } + if (!ask.isEmpty()) { + a = ask.first(); } tradelock.unlock(); - if (q != null) { - return q; + if (a != null && b != null) { + Quote q = new Quote(); + if (lq == null) { + q.price = (bid.first().limit + ask.first().limit) / 2.0; + return q; + } + if (lq.price < b.limit) { + q.price = b.limit; + return q; + } + if (lq.price > a.limit) { + q.price = a.limit; + return q; + } } - if (this.quoteHistory.isEmpty()) { - - return null; + if (a != null) { + Quote q = new Quote(); + if (lq == null) { + q.price = a.limit; + return q; + } + return lq; } - q = this.quoteHistory.last(); - - return q; + if (b != null) { + Quote q = new Quote(); + if (lq == null) { + q.price = b.limit; + return q; + } + return lq; + } + return null; } // Class to describe an executed order @@ -440,16 +453,15 @@ public class Exchange { public void addBookReceiver(OrderType t, BookReceiver br) { - if (br==null){ + if (br == null) { System.out.printf("Br is null\n"); - } - else{ + } else { System.out.printf("Br is not Nukk\n"); } - + ArrayList bookreceivers; bookreceivers = selectBookReceiver(t); - if (bookreceivers == null){ + if (bookreceivers == null) { System.out.printf("null in bookreceivers\n"); } bookreceivers.add(br); @@ -521,7 +533,7 @@ public class Exchange { if (this.quoteHistory.isEmpty()) { return null; } - System.out.printf("qhSize: %d\n",this.quoteHistory.size()); + return this.quoteHistory.last(); } @@ -551,6 +563,7 @@ public class Exchange { boolean rc = ob.remove(o); a.orders.remove(o.id); + a.update(o); ret = true; } @@ -587,19 +600,32 @@ public class Exchange { public double fairValue = 0; private void removeOrderIfExecuted(Order o) { + if (o.getAccount().getOwner().getName().equals("Tobias0")) { + System.out.printf("Tobias 0 test\n"); + } + if (o.volume != 0) { - o.status=OrderStatus.PARTIALLY_EXECUTED; + + if (o.getAccount().getOwner().getName().equals("Tobias0")) { + System.out.printf("Patially remove tobias\n"); + } + + o.status = OrderStatus.PARTIALLY_EXECUTED; o.account.update(o); return; } + if (o.getAccount().getOwner().getName().equals("Tobias0")) { + System.out.printf("Fully remove tobias\n"); + } + o.account.orders.remove(o.id); SortedSet book = order_books.get(o.type); book.remove(book.first()); - o.status=OrderStatus.CLOSED; + o.status = OrderStatus.CLOSED; o.account.update(o); } @@ -655,14 +681,12 @@ public class Exchange { // Transfer money and shares transferMoneyAndShares(b.account, a.account, volume * price, -volume); - // Update volume b.volume -= volume; a.volume -= volume; - // a.account.update(a); - // b.account.update(b); - + // a.account.update(a); + // b.account.update(b); //System.out.printf("In %f (%f) < %f (%f)\n",b.limit,b.volume,a.limit,a.volume); volume_total += volume; money_total += price * volume; diff --git a/src/main/java/traders/ManTrader/AccountBalance.form b/src/main/java/traders/ManTrader/AccountBalance.form new file mode 100644 index 0000000..2ad2414 --- /dev/null +++ b/src/main/java/traders/ManTrader/AccountBalance.form @@ -0,0 +1,69 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/traders/ManTrader/AccountBalance.java b/src/main/java/traders/ManTrader/AccountBalance.java new file mode 100644 index 0000000..6a3dd47 --- /dev/null +++ b/src/main/java/traders/ManTrader/AccountBalance.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2017, 7u83 <7u83@mail.ru> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package traders.ManTrader; + +import sesim.Exchange.Account; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ +public class AccountBalance extends javax.swing.JPanel { + + /** + * Creates new form AccountBalance + */ + public AccountBalance() { + initComponents(); + } + + public void updateBalance (Account account){ + this.moneyLabel.setText(Double.toString(account.getMoney())); + this.sharesLabel.setText(Double.toString(account.getShares())); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + jLabel1 = new javax.swing.JLabel(); + moneyLabel = new javax.swing.JLabel(); + sharesLabel = new javax.swing.JLabel(); + sharesLabelText = new javax.swing.JLabel(); + + jLabel1.setText("Money:"); + + moneyLabel.setText("0.0000"); + + sharesLabel.setText("jLabel3"); + + sharesLabelText.setText("Shares:"); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); + this.setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jLabel1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(moneyLabel) + .addGap(18, 18, 18) + .addComponent(sharesLabelText) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(sharesLabel) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel1) + .addComponent(moneyLabel) + .addComponent(sharesLabel) + .addComponent(sharesLabelText)) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + }// //GEN-END:initComponents + + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JLabel jLabel1; + private javax.swing.JLabel moneyLabel; + private javax.swing.JLabel sharesLabel; + private javax.swing.JLabel sharesLabelText; + // End of variables declaration//GEN-END:variables +} diff --git a/src/main/java/traders/ManTrader/CreateOrderDialog.form b/src/main/java/traders/ManTrader/CreateOrderDialog.form index e3e25f4..6dc4b94 100644 --- a/src/main/java/traders/ManTrader/CreateOrderDialog.form +++ b/src/main/java/traders/ManTrader/CreateOrderDialog.form @@ -33,15 +33,15 @@ - + - + - + @@ -53,9 +53,9 @@ - - - + + + @@ -75,21 +75,25 @@
+ + + + + + - + - + - - @@ -97,14 +101,14 @@ - + - + diff --git a/src/main/java/traders/ManTrader/CreateOrderDialog.java b/src/main/java/traders/ManTrader/CreateOrderDialog.java index c6d8875..85db366 100644 --- a/src/main/java/traders/ManTrader/CreateOrderDialog.java +++ b/src/main/java/traders/ManTrader/CreateOrderDialog.java @@ -25,6 +25,13 @@ */ package traders.ManTrader; +import gui.Globals; +import java.util.ArrayList; +import sesim.Exchange; +import sesim.Exchange.Account; +import sesim.Exchange.OrderType; +import sesim.Quote; + /** * * @author 7u83 <7u83@mail.ru> @@ -37,6 +44,47 @@ public class CreateOrderDialog extends javax.swing.JDialog { public CreateOrderDialog(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); + this.setLocationRelativeTo(this.getParent()); + + } + + OrderType typeList[]; + + public CreateOrderDialog(java.awt.Frame parent, boolean modal, Account account, OrderType type) { + this(parent, modal); + this.account = account; + typeComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{"Buy ", "Sell"})); + typeList = new OrderType[]{OrderType.BUYLIMIT, OrderType.SELLLIMIT}; + for (int i = 0; i < typeList.length; i++) { + if (typeList[i] == type) { + this.typeComboBox.setSelectedIndex(i); + } + } + initDialog(); + } + + private OrderType getOrderType() { + int i = this.typeComboBox.getSelectedIndex(); + return typeList[i]; + } + + Account account; + + public void initDialog() { + OrderType t = getOrderType(); + Quote q = Globals.se.getCurrentPrice(); + Double price = q == null ? 0.0 : q.price; + + if (t == OrderType.BUYLIMIT) { + this.limitSpinner.setValue(Globals.se.roundMoney(price)); + this.volumeSpinner.setValue(Globals.se.roundShares(account.getMoney()/price)); + } + if (t == OrderType.SELLLIMIT) { + this.limitSpinner.setValue(Globals.se.roundMoney(price)); + this.volumeSpinner.setValue(Globals.se.roundShares(account.getShares())); + } + + } /** @@ -50,9 +98,9 @@ public class CreateOrderDialog extends javax.swing.JDialog { jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); - jComboBox1 = new javax.swing.JComboBox<>(); - jSpinner1 = new javax.swing.JSpinner(); - jSpinner2 = new javax.swing.JSpinner(); + typeComboBox = new javax.swing.JComboBox<>(); + limitSpinner = new javax.swing.JSpinner(); + volumeSpinner = new javax.swing.JSpinner(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); @@ -61,15 +109,25 @@ public class CreateOrderDialog extends javax.swing.JDialog { jButton1.setMnemonic('o'); jButton1.setText("Ok"); + jButton1.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton1ActionPerformed(evt); + } + }); jButton2.setMnemonic('c'); jButton2.setText("Cancel"); + jButton2.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + jButton2ActionPerformed(evt); + } + }); - jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Buy ", "Sell", "Stop Loss", " " })); + typeComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Buy ", "Sell" })); - jSpinner1.setModel(new javax.swing.SpinnerNumberModel(0.0d, 0.0d, null, 0.1d)); + limitSpinner.setModel(new javax.swing.SpinnerNumberModel(0.0d, 0.0d, null, 0.1d)); - jSpinner2.setModel(new javax.swing.SpinnerNumberModel(0.0d, 0.0d, null, 0.1d)); + volumeSpinner.setModel(new javax.swing.SpinnerNumberModel(0.0d, 0.0d, null, 0.1d)); jLabel1.setText("Volume:"); @@ -87,15 +145,15 @@ public class CreateOrderDialog extends javax.swing.JDialog { .addGap(1, 1, 1) .addComponent(jButton2)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() - .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(typeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(limitSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addComponent(volumeSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); layout.setVerticalGroup( @@ -103,9 +161,9 @@ public class CreateOrderDialog extends javax.swing.JDialog { .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jSpinner1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(jSpinner2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(typeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(limitSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(volumeSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1) .addComponent(jLabel2)) .addGap(18, 18, 18) @@ -118,6 +176,22 @@ public class CreateOrderDialog extends javax.swing.JDialog { pack(); }// //GEN-END:initComponents + private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed + dispose(); + }//GEN-LAST:event_jButton2ActionPerformed + + private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed + double volume = (double) volumeSpinner.getValue(); + double limit = (double) limitSpinner.getValue(); + + if (account == null) { + System.out.printf("Account is null\n"); + } + OrderType type = this.getOrderType(); + Globals.se.createOrder(account.getID(), type, volume, limit); + dispose(); + }//GEN-LAST:event_jButton1ActionPerformed + /** * @param args the command line arguments */ @@ -163,10 +237,10 @@ public class CreateOrderDialog extends javax.swing.JDialog { // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; - private javax.swing.JComboBox jComboBox1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; - private javax.swing.JSpinner jSpinner1; - private javax.swing.JSpinner jSpinner2; + private javax.swing.JSpinner limitSpinner; + private javax.swing.JComboBox typeComboBox; + private javax.swing.JSpinner volumeSpinner; // End of variables declaration//GEN-END:variables } diff --git a/src/main/java/traders/ManTrader/ManTrader.java b/src/main/java/traders/ManTrader/ManTrader.java index ec8d196..12b9e85 100644 --- a/src/main/java/traders/ManTrader/ManTrader.java +++ b/src/main/java/traders/ManTrader/ManTrader.java @@ -36,12 +36,13 @@ import sesim.AutoTraderConfig; import sesim.AutoTraderGui; import sesim.Exchange; import sesim.Exchange.AccountListener; +import sesim.Exchange.OrderStatus; /** * * @author 7u83 <7u83@mail.ru> */ -public class ManTrader extends AutoTraderBase implements AccountListener{ +public class ManTrader extends AutoTraderBase implements AccountListener { public ManTrader(Exchange se, long id, String name, double money, double shares, AutoTraderConfig config) { // super(se, id, name, money, shares, null); @@ -50,10 +51,10 @@ public class ManTrader extends AutoTraderBase implements AccountListener{ public ManTrader() { super(); - + } - - public void init(Exchange se, long id, String name, double money, double shares, JSONObject cfg){ + + public void init(Exchange se, long id, String name, double money, double shares, JSONObject cfg) { super.init(se, id, name, money, shares, cfg); getAccount().setListener(this); } @@ -62,27 +63,23 @@ public class ManTrader extends AutoTraderBase implements AccountListener{ @Override public void start() { se.timer.startTimerEvent(this, 0); - consoleDialog = new ManTraderConsoleDialog(Globals.frame, false); - - // consoleDialog. rdersList1.account=trader.getAccount(); - - consoleDialog.getConsole().trader=this; - - + consoleDialog = new ManTraderConsoleDialog(Globals.frame, false, this.getAccount()); + this.consoleDialog.getBalancePanel().updateBalance(this.getAccount()); + // consoleDialog. rdersList1.account=trader.getAccount(); + +// consoleDialog.getConsole().trader=this; consoleDialog.setVisible(true); } @Override public long timerTask() { - - OrdersList ol = this.consoleDialog.getConsole().getOrderListPanel(); - ol.updateModel(); + +// OrdersList ol = this.consoleDialog.getConsole().getOrderListPanel(); +// ol.updateModel(); return 1000; } - - @Override public String getDisplayName() { return null; @@ -118,8 +115,14 @@ public class ManTrader extends AutoTraderBase implements AccountListener{ public void accountUpdated(Exchange.Account a, Exchange.Order o) { //this.consoleDialog.cons System.out.printf("AccountListener called\n"); - - this.consoleDialog.getConsole().getOrderListPanel().updateModel(); + + System.out.printf("%d %s\n", o.getID(), o.getOrderStatus().toString()); + + if (o.getOrderStatus()==OrderStatus.CLOSED){ + o.getAccount().getOrders().put(o.getID(), o); + } + this.consoleDialog.getOrderList().updateModel(); + this.consoleDialog.getBalancePanel().updateBalance(o.getAccount()); } } diff --git a/src/main/java/traders/ManTrader/ManTraderConsole.java b/src/main/java/traders/ManTrader/ManTraderConsole.java index abf1709..50dd904 100644 --- a/src/main/java/traders/ManTrader/ManTraderConsole.java +++ b/src/main/java/traders/ManTrader/ManTraderConsole.java @@ -48,6 +48,7 @@ public class ManTraderConsole extends javax.swing.JPanel { public ManTraderConsole() { initComponents(); + // this.ordersList1.account=trader.getAccount(); } @@ -177,8 +178,8 @@ public class ManTraderConsole extends javax.swing.JPanel { long createOrder = trader.getSE().createOrder(trader.getAccount().getID(), Exchange.OrderType.BUYLIMIT, volume, limit); System.out.printf("The retval is %d",createOrder); - this.ordersList.account=this.trader.getAccount(); - this.ordersList.updateModel(); + // this.ordersList.account=this.trader.getAccount(); + // this.ordersList.updateModel(); }//GEN-LAST:event_buyButtonActionPerformed diff --git a/src/main/java/traders/ManTrader/ManTraderConsoleDialog.form b/src/main/java/traders/ManTrader/ManTraderConsoleDialog.form index eefa9c9..eca98f7 100644 --- a/src/main/java/traders/ManTrader/ManTraderConsoleDialog.form +++ b/src/main/java/traders/ManTrader/ManTraderConsoleDialog.form @@ -23,20 +23,48 @@ - + + + + + + + + + + + - - + + + + + - + + + + + + + + + + + + + + + + diff --git a/src/main/java/traders/ManTrader/ManTraderConsoleDialog.java b/src/main/java/traders/ManTrader/ManTraderConsoleDialog.java index 2a587e6..e131495 100644 --- a/src/main/java/traders/ManTrader/ManTraderConsoleDialog.java +++ b/src/main/java/traders/ManTrader/ManTraderConsoleDialog.java @@ -26,6 +26,7 @@ package traders.ManTrader; import javax.swing.JPanel; +import sesim.Exchange.Account; /** * @@ -36,17 +37,26 @@ public class ManTraderConsoleDialog extends javax.swing.JDialog { /** * Creates new form ManTraderConsole */ - public ManTraderConsoleDialog(java.awt.Frame parent, boolean modal) { + public ManTraderConsoleDialog(java.awt.Frame parent, boolean modal, Account account) { super(parent, modal); initComponents(); + this.ordersList.initOrderList(account); + this.setTitle(account.getOwner().getName()+" - Trading Console"); + } + + public gui.OrdersList getOrderList(){ + return this.ordersList; + } + + public AccountBalance getBalancePanel(){ + return this.accountBalance1; } - - public ManTraderConsole getConsole(){ - return this.console; - } + // public ManTraderConsole getConsole(){ + // return this.console; + // } /** * This method is called from within the constructor to initialize the form. @@ -57,21 +67,35 @@ public class ManTraderConsoleDialog extends javax.swing.JDialog { // //GEN-BEGIN:initComponents private void initComponents() { - console = new traders.ManTrader.ManTraderConsole(); + jTabbedPane1 = new javax.swing.JTabbedPane(); + ordersList = new gui.OrdersList(); + accountBalance1 = new traders.ManTrader.AccountBalance(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); + jTabbedPane1.addTab("Open Orders", ordersList); + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addComponent(console, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE) + .addGroup(layout.createSequentialGroup() + .addComponent(accountBalance1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(0, 0, Short.MAX_VALUE))) + .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() - .addComponent(console, javax.swing.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE) - .addContainerGap()) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(accountBalance1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap(79, Short.MAX_VALUE)) ); pack(); @@ -108,7 +132,7 @@ public class ManTraderConsoleDialog extends javax.swing.JDialog { /* Create and display the dialog */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { - ManTraderConsoleDialog dialog = new ManTraderConsoleDialog(new javax.swing.JFrame(), true); + ManTraderConsoleDialog dialog = new ManTraderConsoleDialog(new javax.swing.JFrame(), true, null); dialog.addWindowListener(new java.awt.event.WindowAdapter() { @Override public void windowClosing(java.awt.event.WindowEvent e) { @@ -121,6 +145,8 @@ public class ManTraderConsoleDialog extends javax.swing.JDialog { } // Variables declaration - do not modify//GEN-BEGIN:variables - private traders.ManTrader.ManTraderConsole console; + private traders.ManTrader.AccountBalance accountBalance1; + private javax.swing.JTabbedPane jTabbedPane1; + private gui.OrdersList ordersList; // End of variables declaration//GEN-END:variables } diff --git a/src/main/java/traders/RandomTraderA.java b/src/main/java/traders/RandomTraderA.java index bb43ed9..2e2e1c9 100644 --- a/src/main/java/traders/RandomTraderA.java +++ b/src/main/java/traders/RandomTraderA.java @@ -284,6 +284,7 @@ public class RandomTraderA extends AutoTraderBase { double money = getRandomAmmount(ad.getMoney(), buy_volume); Quote q = se.getCurrentPrice(); + //q=se.getLastQuoete(); double lp = q == null ? getStart() : q.price; double limit; @@ -323,7 +324,12 @@ public class RandomTraderA extends AutoTraderBase { // double lp = 100.0; //se.getBestLimit(type); Quote q = se.getCurrentPrice(); + // q=se.getLastQuoete(); double lp = q == null ? getStart() : q.price; + + + + double limit; limit = lp + getRandomAmmount(lp, sell_limit);