Stop loss orders implemented

This commit is contained in:
7u83 2017-02-10 08:12:38 +01:00
parent f57d1a4cb0
commit 542389b35f
5 changed files with 106 additions and 28 deletions

View File

@ -135,7 +135,7 @@ public class TraderListPanel extends javax.swing.JPanel {
}
System.out.print("TimerTaskUpdater\n");
//System.out.print("TimerTaskUpdater\n");
ArrayList<TraderListItem> newlist = getTraderList();
SwingUtilities.invokeLater(new Updater(this.model, newlist));

View File

@ -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<Order> sl = order_books.get(OrderType.STOPLOSS);
SortedSet<Order> 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<Order> bid = order_books.get(OrderType.BUYLIMIT);
SortedSet<Order> ask = order_books.get(OrderType.SELLLIMIT);
SortedSet<Order> ul_buy = order_books.get(OrderType.BUY);
SortedSet<Order> 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();

View File

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

View File

@ -21,13 +21,15 @@
<Group type="103" groupAlignment="1" attributes="0">
<Component id="ordersList1" max="32767" attributes="0"/>
<Group type="102" alignment="0" attributes="0">
<Component id="buyButton" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="sellButton" min="-2" pref="191" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Group type="103" groupAlignment="1" attributes="0">
<Group type="102" alignment="0" attributes="0">
<Component id="buyButton" min="-2" pref="96" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="sellButton" min="-2" pref="89" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="stopLossButton" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<Component id="jLabel2" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="44" max="-2" attributes="0"/>
<Component id="volumeSpinner" min="-2" pref="108" max="-2" attributes="0"/>
@ -71,11 +73,19 @@
<Component id="jLabel2" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="buyButton" pref="72" max="32767" attributes="0"/>
<Component id="sellButton" max="32767" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="buyButton" pref="72" max="32767" attributes="0"/>
<Component id="sellButton" max="32767" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="1" attributes="0">
<Component id="stopLossButton" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="20" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
@ -133,5 +143,13 @@
</Component>
<Component class="gui.OrdersList" name="ordersList1">
</Component>
<Component class="javax.swing.JButton" name="stopLossButton">
<Properties>
<Property name="text" type="java.lang.String" value="StopLoss"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="stopLossButtonActionPerformed"/>
</Events>
</Component>
</SubComponents>
</Form>

View File

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