From 542389b35f939e9b0377ed0d8c1074fd5031e287 Mon Sep 17 00:00:00 2001 From: 7u83 <7u83@maiol.ru> Date: Fri, 10 Feb 2017 08:12:38 +0100 Subject: [PATCH] Stop loss orders implemented --- src/main/java/gui/TraderListPanel.java | 2 +- src/main/java/sesim/Exchange.java | 38 +++++++++++++- .../java/traders/ManTrader/ManTrader.java | 2 +- .../traders/ManTrader/ManTraderConsole.form | 40 ++++++++++---- .../traders/ManTrader/ManTraderConsole.java | 52 ++++++++++++++----- 5 files changed, 106 insertions(+), 28 deletions(-) diff --git a/src/main/java/gui/TraderListPanel.java b/src/main/java/gui/TraderListPanel.java index fbb8d21..3972601 100644 --- a/src/main/java/gui/TraderListPanel.java +++ b/src/main/java/gui/TraderListPanel.java @@ -135,7 +135,7 @@ public class TraderListPanel extends javax.swing.JPanel { } - System.out.print("TimerTaskUpdater\n"); + //System.out.print("TimerTaskUpdater\n"); ArrayList newlist = getTraderList(); SwingUtilities.invokeLater(new Updater(this.model, newlist)); diff --git a/src/main/java/sesim/Exchange.java b/src/main/java/sesim/Exchange.java index 1705dc4..e410cf4 100644 --- a/src/main/java/sesim/Exchange.java +++ b/src/main/java/sesim/Exchange.java @@ -50,7 +50,7 @@ public class Exchange { * Definition of order types */ public enum OrderType { - BUYLIMIT, SELLLIMIT, STOPLOSS, STOPBUY + BUYLIMIT, SELLLIMIT, STOPLOSS, STOPBUY, BUY, SELL } IDGenerator account_id = new IDGenerator(); @@ -141,9 +141,13 @@ public class Exchange { double d; switch (this.type) { case BUYLIMIT: + case STOPBUY: + case BUY: d = right.limit - left.limit; break; case SELLLIMIT: + case STOPLOSS: + case SELL: d = left.limit - right.limit; break; default: @@ -522,6 +526,24 @@ public class Exchange { book.remove(book.first()); } + + void checkSLOrders(double price){ + SortedSet sl = order_books.get(OrderType.STOPLOSS); + SortedSet ask = order_books.get(OrderType.SELLLIMIT); + + if (sl.isEmpty()) + return; + + Order s = sl.first(); + if (price<=s.limit){ + sl.remove(s); + + s.type=OrderType.SELL; + addOrderToBook(s); + + System.out.printf("Stoploss hit %f %f\n", s.volume,s.limit); + } + } /** * @@ -530,11 +552,19 @@ public class Exchange { SortedSet bid = order_books.get(OrderType.BUYLIMIT); SortedSet ask = order_books.get(OrderType.SELLLIMIT); + + SortedSet ul_buy = order_books.get(OrderType.BUY); + SortedSet ul_sell = order_books.get(OrderType.SELL); + double volume_total = 0; double money_total = 0; while (!bid.isEmpty() && !ask.isEmpty()) { + + + + Order b = bid.first(); Order a = ask.first(); @@ -559,11 +589,15 @@ public class Exchange { //System.out.printf("In %f (%f) < %f (%f)\n",b.limit,b.volume,a.limit,a.volume); volume_total += volume; money_total += price * volume; + + num_trades++; removeOrderIfExecuted(a); removeOrderIfExecuted(b); + + this.checkSLOrders(price); } //System.out.print("Volume total is "+volume_total+"\n"); @@ -604,7 +638,7 @@ public class Exchange { Order o = new Order(a, type, volume, limit); if (o.volume <= 0 || o.limit <= 0) { - System.out.print("binweg\n"); + //System.out.print("binweg\n"); return -1; } tradelock.lock(); diff --git a/src/main/java/traders/ManTrader/ManTrader.java b/src/main/java/traders/ManTrader/ManTrader.java index 3bf4908..14d81e7 100644 --- a/src/main/java/traders/ManTrader/ManTrader.java +++ b/src/main/java/traders/ManTrader/ManTrader.java @@ -68,7 +68,7 @@ public class ManTrader extends AutoTraderBase implements AutoTraderConfig { @Override public long timerTask() { - System.out.printf("TimerTask\n"); + OrdersList ol = this.consoleDialog.getConsole().getOrderListPanel(); ol.updateModel(); return 1000; diff --git a/src/main/java/traders/ManTrader/ManTraderConsole.form b/src/main/java/traders/ManTrader/ManTraderConsole.form index 9f7a7b8..a09ee49 100644 --- a/src/main/java/traders/ManTrader/ManTraderConsole.form +++ b/src/main/java/traders/ManTrader/ManTraderConsole.form @@ -21,13 +21,15 @@ - - - - - - - + + + + + + + + + @@ -71,11 +73,19 @@ - - - + + + + + + + + + + + + - @@ -133,5 +143,13 @@ + + + + + + + + diff --git a/src/main/java/traders/ManTrader/ManTraderConsole.java b/src/main/java/traders/ManTrader/ManTraderConsole.java index 5865f6a..07764ee 100644 --- a/src/main/java/traders/ManTrader/ManTraderConsole.java +++ b/src/main/java/traders/ManTrader/ManTraderConsole.java @@ -68,6 +68,7 @@ public class ManTraderConsole extends javax.swing.JPanel { jLabel3 = new javax.swing.JLabel(); moneyText = new javax.swing.JLabel(); ordersList1 = new gui.OrdersList(); + stopLossButton = new javax.swing.JButton(); limitSpinner.setModel(new javax.swing.SpinnerNumberModel(0.0d, 0.0d, null, 1.0d)); @@ -95,6 +96,13 @@ public class ManTraderConsole extends javax.swing.JPanel { moneyText.setText("jLabel4"); + stopLossButton.setText("StopLoss"); + stopLossButton.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + stopLossButtonActionPerformed(evt); + } + }); + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( @@ -104,20 +112,22 @@ public class ManTraderConsole extends javax.swing.JPanel { .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(ordersList1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() - .addComponent(buyButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addComponent(sellButton, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) - .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() + .addComponent(buyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(sellButton, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(stopLossButton)) + .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addComponent(jLabel2) .addGap(44, 44, 44) .addComponent(volumeSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addGroup(layout.createSequentialGroup() + .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(limitSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)) - .addGroup(layout.createSequentialGroup() + .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(moneyText, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE))) @@ -142,10 +152,15 @@ public class ManTraderConsole extends javax.swing.JPanel { .addComponent(volumeSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) - .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) - .addComponent(buyButton, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE) - .addComponent(sellButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - .addContainerGap()) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(buyButton, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE) + .addComponent(sellButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addContainerGap()) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addComponent(stopLossButton) + .addGap(20, 20, 20)))) ); }// //GEN-END:initComponents @@ -165,12 +180,22 @@ public class ManTraderConsole extends javax.swing.JPanel { Double limit = (Double)this.limitSpinner.getValue(); Double volume = (Double)this.volumeSpinner.getValue(); - System.out.printf("Should buy: %f %f\n",volume,limit); + System.out.printf("Should sell: %f %f\n",volume,limit); long createOrder = trader.getSE().createOrder(trader.getAccount().getID(), Exchange.OrderType.SELLLIMIT, volume, limit); System.out.printf("The retval is %d",createOrder); }//GEN-LAST:event_sellButtonActionPerformed + private void stopLossButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopLossButtonActionPerformed + Double limit = (Double)this.limitSpinner.getValue(); + Double volume = (Double)this.volumeSpinner.getValue(); + + System.out.printf("Should stoploss: %f %f\n",volume,limit); + + long createOrder = trader.getSE().createOrder(trader.getAccount().getID(), Exchange.OrderType.STOPLOSS, volume, limit); + System.out.printf("The retval is %d",createOrder); + }//GEN-LAST:event_stopLossButtonActionPerformed + // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton buyButton; @@ -181,6 +206,7 @@ public class ManTraderConsole extends javax.swing.JPanel { private javax.swing.JLabel moneyText; private gui.OrdersList ordersList1; private javax.swing.JButton sellButton; + private javax.swing.JButton stopLossButton; private javax.swing.JSpinner volumeSpinner; // End of variables declaration//GEN-END:variables }