From 4f30bcbc301902fa934c0ac9164c402c4856aa8f Mon Sep 17 00:00:00 2001 From: 7u83 <7u83@maiol.ru> Date: Fri, 30 Dec 2016 13:53:35 +0100 Subject: [PATCH] SwitchingTrader + Chart --- nbproject/private/private.xml | 2 + src/Gui/BidBook.java | 2 +- src/Gui/Chart.java | 75 +++-- src/Gui/MainWin.form | 2 +- src/Gui/MainWin.java | 6 +- src/Gui/OrderBookPanel.form | 431 ++++++++++++++++++++++++- src/Gui/OrderBookPanel.java | 31 +- src/Gui/QuotePanel.form | 326 +++++++++++++++++++ src/Gui/QuotePanel.java | 142 ++++++++ src/SeSim/Exchange.java | 47 ++- src/SeSim/Quote.java | 60 ++++ src/Traders/RandomTrader.java | 101 +++--- src/Traders/RandomTraderConfig.java | 6 +- src/Traders/SwitchingTrader.java | 107 ++++++ src/Traders/SwitchingTraderConfig.java | 57 ++++ 15 files changed, 1256 insertions(+), 139 deletions(-) create mode 100644 src/Gui/QuotePanel.form create mode 100644 src/Gui/QuotePanel.java create mode 100644 src/SeSim/Quote.java create mode 100644 src/Traders/SwitchingTrader.java create mode 100644 src/Traders/SwitchingTraderConfig.java diff --git a/nbproject/private/private.xml b/nbproject/private/private.xml index 6f34ff3..de08f81 100644 --- a/nbproject/private/private.xml +++ b/nbproject/private/private.xml @@ -7,11 +7,13 @@ file:/home/tube/NetBeansProjects/SeSim/src/SeSim/Trader.java file:/home/tube/NetBeansProjects/SeSim/src/SeSim/Order.java file:/home/tube/NetBeansProjects/SeSim/src/Gui/ControlPanel.java + file:/home/tube/NetBeansProjects/SeSim/src/SeSim/Quote.java file:/home/tube/NetBeansProjects/SeSim/src/Gui/MainWin.java file:/home/tube/NetBeansProjects/SeSim/src/Gui/OrderBook.java file:/home/tube/NetBeansProjects/SeSim/manifest.mf file:/home/tube/NetBeansProjects/SeSim/src/SeSim/TraderConfig.java file:/home/tube/NetBeansProjects/SeSim/src/SeSim/Account.java + file:/home/tube/NetBeansProjects/SeSim/src/Gui/QuotePanel.java file:/home/tube/NetBeansProjects/SeSim/src/SeSim/Exchange.java file:/home/tube/NetBeansProjects/SeSim/src/Gui/NewPanel.java file:/home/tube/NetBeansProjects/SeSim/src/Gui/Chart.java diff --git a/src/Gui/BidBook.java b/src/Gui/BidBook.java index d3791ad..f4628e2 100644 --- a/src/Gui/BidBook.java +++ b/src/Gui/BidBook.java @@ -40,7 +40,7 @@ public class BidBook extends OrderBook { return MainWin.se.getOrderBook(OrderType.bid, 40); } - BidBook() { + public BidBook() { if (MainWin.se == null) { return; } diff --git a/src/Gui/Chart.java b/src/Gui/Chart.java index 69decad..c086b5b 100644 --- a/src/Gui/Chart.java +++ b/src/Gui/Chart.java @@ -50,26 +50,27 @@ import org.jfree.data.xy.OHLCDataItem; import org.jfree.data.xy.XYDataset; import SeSim.Exchange.*; +import SeSim.Quote; +import java.util.SortedSet; /** * * @author 7u83 <7u83@mail.ru> */ -public class Chart extends javax.swing.JPanel implements QuoteReceiver{ +public class Chart extends javax.swing.JPanel implements QuoteReceiver { /** * Creates new form Chart */ public Chart() { initComponents(); - - String stockSymbol = "MSFT"; - - DateAxis domainAxis = new DateAxis("Date"); - NumberAxis rangeAxis = new NumberAxis("Price"); + String stockSymbol = "Schliemanz Koch AG"; + + DateAxis domainAxis = new DateAxis("Date"); + NumberAxis rangeAxis = new NumberAxis("Price"); CandlestickRenderer renderer = new CandlestickRenderer(); - XYDataset dataset = getDataSet(stockSymbol); + XYDataset dataset = getDataSet(stockSymbol); XYPlot mainPlot = new XYPlot(dataset, domainAxis, rangeAxis, renderer); @@ -77,43 +78,53 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver{ renderer.setSeriesPaint(0, Color.BLACK); renderer.setDrawVolume(false); rangeAxis.setAutoRangeIncludesZero(false); - domainAxis.setTimeline( SegmentedTimeline.newMondayThroughFridayTimeline() ); + domainAxis.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline()); //Now create the chart and chart panel JFreeChart chart = new JFreeChart(stockSymbol, null, mainPlot, false); ChartPanel chartPanel = new ChartPanel(chart); chartPanel.setPreferredSize(new Dimension(500, 270)); - add(chartPanel); + add(chartPanel); System.out.print("Hallo Welt\n"); - - if (MainWin.se == null) + + if (MainWin.se == null) { return; - + } + MainWin.se.addQuoteReceiver(this); } - - - protected AbstractXYDataset getDataSet(String stockSymbol) { + + protected AbstractXYDataset getDataSet(String stockSymbol) { //This is the dataset we are going to create DefaultOHLCDataset result = null; //This is the data needed for the dataset OHLCDataItem[] data; //This is where we go get the data, replace with your own data source - data = getData(stockSymbol); + data = getData(); //Create a dataset, an Open, High, Low, Close dataset result = new DefaultOHLCDataset(stockSymbol, data); return result; } + + protected OHLCDataItem[] getData() { + List data = new ArrayList<>(); + + //return data.toArray(new rdata[]); + return data.toArray(new OHLCDataItem[data.size()]); + + } + //This method uses yahoo finance to get the OHLC data - protected OHLCDataItem[] getData(String stockSymbol) { + protected OHLCDataItem[] getData_old() { + String stockSymbol = "Schliemanz Koch AG"; List dataItems = new ArrayList(); try { - String strUrl= "http://ichart.finance.yahoo.com/table.csv?s="+stockSymbol+"&a=0&b=1&c=2008&d=3&e=30&f=2008&ignore=.csv"; + String strUrl = "http://ichart.finance.yahoo.com/table.csv?s=" + stockSymbol + "&a=0&b=1&c=2008&d=3&e=30&f=2008&ignore=.csv"; URL url = new URL(strUrl); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); DateFormat df = new SimpleDateFormat("y-M-d"); @@ -123,20 +134,19 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver{ while ((inputLine = in.readLine()) != null) { StringTokenizer st = new StringTokenizer(inputLine, ","); - Date date = df.parse( st.nextToken() ); - double open = Double.parseDouble( st.nextToken() ); - double high = Double.parseDouble( st.nextToken() ); - double low = Double.parseDouble( st.nextToken() ); - double close = Double.parseDouble( st.nextToken() ); - double volume = Double.parseDouble( st.nextToken() ); - double adjClose = Double.parseDouble( st.nextToken() ); + Date date = df.parse(st.nextToken()); + double open = Double.parseDouble(st.nextToken()); + double high = Double.parseDouble(st.nextToken()); + double low = Double.parseDouble(st.nextToken()); + double close = Double.parseDouble(st.nextToken()); + double volume = Double.parseDouble(st.nextToken()); + double adjClose = Double.parseDouble(st.nextToken()); OHLCDataItem item = new OHLCDataItem(date, open, high, low, close, volume); dataItems.add(item); } in.close(); - } - catch (Exception e) { + } catch (Exception e) { e.printStackTrace(); } //Data from Yahoo is from newest to oldest. Reverse so it is oldest to newest @@ -172,7 +182,14 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver{ @Override public void UpdateQuote(Quote q) { - q.print(); - + return; + //q.print(); + /* SortedSet s = MainWin.se.getQuoteHistory(60); + System.out.print( + "SortedSet size:" + + s.size() + + "\n" + ); +*/ } } diff --git a/src/Gui/MainWin.form b/src/Gui/MainWin.form index abddac6..63e4cde 100644 --- a/src/Gui/MainWin.form +++ b/src/Gui/MainWin.form @@ -102,4 +102,4 @@ - \ No newline at end of file + diff --git a/src/Gui/MainWin.java b/src/Gui/MainWin.java index 5a6e373..e94da22 100644 --- a/src/Gui/MainWin.java +++ b/src/Gui/MainWin.java @@ -156,7 +156,11 @@ public class MainWin extends javax.swing.JFrame { AutoTraderLIst at = new AutoTraderLIst(); RandomTraderConfig rcfg = new RandomTraderConfig(); - at.add(500, rcfg, se, 1000, 10000); + at.add(1000, rcfg, se, 1000, 10000); + + SwitchingTraderConfig scfg = new SwitchingTraderConfig(); + at.add(1, scfg, se, 1000000, 0); + // at.add(10, rcfg, se, 1000000, 0); diff --git a/src/Gui/OrderBookPanel.form b/src/Gui/OrderBookPanel.form index 78c7b2e..cb1664a 100644 --- a/src/Gui/OrderBookPanel.form +++ b/src/Gui/OrderBookPanel.form @@ -16,10 +16,8 @@ - + - - @@ -29,20 +27,7 @@ - - - - - - - - - - - - - - + @@ -58,5 +43,417 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Gui/OrderBookPanel.java b/src/Gui/OrderBookPanel.java index 6aed583..0029c48 100644 --- a/src/Gui/OrderBookPanel.java +++ b/src/Gui/OrderBookPanel.java @@ -64,29 +64,24 @@ public class OrderBookPanel extends javax.swing.JPanel { java.awt.GridBagConstraints gridBagConstraints; askBook1 = new Gui.AskBook(); - jLabel4 = new javax.swing.JLabel(); bidBook1 = new Gui.BidBook(); + quotePanel2 = new Gui.QuotePanel(); setPreferredSize(new java.awt.Dimension(220, 262)); - setLayout(new java.awt.GridBagLayout()); + java.awt.GridBagLayout layout = new java.awt.GridBagLayout(); + layout.columnWidths = new int[] {0}; + layout.rowHeights = new int[] {0, 5, 0, 5, 0}; + setLayout(layout); askBook1.setPreferredSize(new java.awt.Dimension(200, 200)); gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 0; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; add(askBook1, gridBagConstraints); - jLabel4.setText("123.00"); - jLabel4.setPreferredSize(new java.awt.Dimension(150, 25)); - gridBagConstraints = new java.awt.GridBagConstraints(); - gridBagConstraints.gridx = 0; - gridBagConstraints.gridy = 1; - gridBagConstraints.gridheight = 3; - gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; - gridBagConstraints.weighty = 1.0; - add(jLabel4, gridBagConstraints); - bidBook1.setPreferredSize(new java.awt.Dimension(200, 200)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; @@ -95,12 +90,22 @@ public class OrderBookPanel extends javax.swing.JPanel { gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; add(bidBook1, gridBagConstraints); + + quotePanel2.setOpaque(false); + quotePanel2.setPreferredSize(new java.awt.Dimension(587, 200)); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 2; + gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; + gridBagConstraints.weightx = 0.5; + gridBagConstraints.weighty = 0.5; + add(quotePanel2, gridBagConstraints); }// //GEN-END:initComponents // Variables declaration - do not modify//GEN-BEGIN:variables private Gui.AskBook askBook1; private Gui.BidBook bidBook1; - private javax.swing.JLabel jLabel4; + private Gui.QuotePanel quotePanel2; // End of variables declaration//GEN-END:variables } diff --git a/src/Gui/QuotePanel.form b/src/Gui/QuotePanel.form new file mode 100644 index 0000000..a017eba --- /dev/null +++ b/src/Gui/QuotePanel.form @@ -0,0 +1,326 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Gui/QuotePanel.java b/src/Gui/QuotePanel.java new file mode 100644 index 0000000..ee2b892 --- /dev/null +++ b/src/Gui/QuotePanel.java @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2016, 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 Gui; + +import SeSim.Quote; +import java.awt.Color; +import javax.swing.SwingUtilities; +import java.util.*; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ +public class QuotePanel extends javax.swing.JPanel implements SeSim.Exchange.QuoteReceiver{ + + /** + * Creates new form QuotePanel + */ + public QuotePanel() { + initComponents(); + if (MainWin.se==null) + return; + MainWin.se.addQuoteReceiver(this); + } + + /** + * 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() { + java.awt.GridBagConstraints gridBagConstraints; + + jLabel2 = new javax.swing.JLabel(); + lastPrice = new javax.swing.JLabel(); + jLabel3 = new javax.swing.JLabel(); + + setBorder(null); + java.awt.GridBagLayout layout = new java.awt.GridBagLayout(); + layout.columnWidths = new int[] {0, 5, 0, 5, 0}; + layout.rowHeights = new int[] {0}; + setLayout(layout); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 0; + gridBagConstraints.gridy = 0; + add(jLabel2, gridBagConstraints); + + lastPrice.setFont(lastPrice.getFont().deriveFont(lastPrice.getFont().getStyle() | java.awt.Font.BOLD, lastPrice.getFont().getSize()+4)); + lastPrice.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); + lastPrice.setText("0.00"); + lastPrice.setOpaque(true); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 2; + gridBagConstraints.gridy = 0; + gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; + gridBagConstraints.weightx = 0.9; + gridBagConstraints.weighty = 0.9; + add(lastPrice, gridBagConstraints); + + jLabel3.setPreferredSize(new java.awt.Dimension(30, 18)); + gridBagConstraints = new java.awt.GridBagConstraints(); + gridBagConstraints.gridx = 4; + gridBagConstraints.gridy = 0; + add(jLabel3, gridBagConstraints); + }// //GEN-END:initComponents + + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JLabel jLabel2; + private javax.swing.JLabel jLabel3; + private javax.swing.JLabel lastPrice; + // End of variables declaration//GEN-END:variables + + @Override + public void UpdateQuote(Quote q) { + class Updater implements Runnable { + QuotePanel qp; + String text=""; + Color color = Color.BLUE; + + + @Override + public void run() { + qp.lastPrice.setText(text); + qp.lastPrice.setForeground(color); + } + + + } + Updater u= new Updater(); + u.qp=this; + + if (q.price==q.bid){ + u.color=new Color(172,0,0); + } + if (q.price==q.ask){ + u.color=new Color(0,120,0); //.; //new Color(30,0,0); + } + + + + + u.text = String.format("%.2f\n(%d)", q.price,q.volume); + + SwingUtilities.invokeLater(u); + +// SortedSet s = MainWin.se.getQuoteHistory(5); + + // System.out.print( + // "SortedSet size:" + // +s.size() + // +"\n" + // ); + + //this.lastPrice.setText(lp); + } +} diff --git a/src/SeSim/Exchange.java b/src/SeSim/Exchange.java index 76a79af..575f29e 100644 --- a/src/SeSim/Exchange.java +++ b/src/SeSim/Exchange.java @@ -15,7 +15,7 @@ public class Exchange extends Thread { /** * Histrory of quotes */ - public ArrayList quoteHistory; + public TreeSet quoteHistory = new TreeSet<>(); /** * Constructor @@ -24,35 +24,20 @@ public class Exchange extends Thread { this.ask = new TreeSet<>(); this.bid = new TreeSet<>(); this.qrlist = new ArrayList<>(); - this.quoteHistory = new ArrayList<>(); + + } + + public SortedSet getQuoteHistory(int seconds){ + long ct = System.currentTimeMillis() - seconds * 1000; + Quote e = new Quote(); + e.time=ct; + SortedSet l = quoteHistory.tailSet(e); + return l; + } // Class to describe an executed order - public class Quote { - - double bid; - double bid_volume; - double ask; - double ask_volume; - - public double price; - public long volume; - public long time; - - public void print(){ - System.out.print("Quite (" - +time - +") :" - +price - +" / " - +volume - +"\n" - ); - - - - } - } + // QuoteReceiver has to be implemented by objects that wants // to receive quote updates @@ -97,7 +82,7 @@ public class Exchange extends Thread { i.next().UpdateOrderBook(); } try { - sleep(0); + sleep(10); } catch (InterruptedException e) { System.out.println("I was Interrupted"); } @@ -314,6 +299,10 @@ public class Exchange extends Thread { q.volume = volume; q.price = price; q.time = System.currentTimeMillis(); + + q.ask=a.limit; + q.bid=b.limit; + this.UpdateQuoteReceivers(q); this.updateBookReceivers(OrderType.bid); @@ -328,7 +317,7 @@ public class Exchange extends Thread { ); */ - //quoteHistory.add(q); + quoteHistory.add(q); continue; } diff --git a/src/SeSim/Quote.java b/src/SeSim/Quote.java new file mode 100644 index 0000000..33eb5e9 --- /dev/null +++ b/src/SeSim/Quote.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2016, 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 SeSim; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ +public class Quote implements Comparable { + + public double bid; + public double bid_volume; + public double ask; + public double ask_volume; + + public double price; + public long volume; + public long time; + + public void print() { + System.out.print("Quote (" + + time + + ") :" + + price + + " / " + + volume + + "\n" + ); + + } + + @Override + public int compareTo(Object o) { + Quote q = (Quote)o; + return (int)(this.time-q.time); + } +} diff --git a/src/Traders/RandomTrader.java b/src/Traders/RandomTrader.java index d9a9361..e8b1585 100644 --- a/src/Traders/RandomTrader.java +++ b/src/Traders/RandomTrader.java @@ -32,6 +32,10 @@ import SeSim.AutoTrader; import SeSim.TraderConfig; public class RandomTrader extends AutoTrader { + + protected enum Action { + sell,buy + } // config for this trader final private RandomTraderConfig myconfig; @@ -58,9 +62,9 @@ public class RandomTrader extends AutoTrader { double r = rand.nextDouble(); return (max - min) * r + min; } - - protected int getRandom(int[] minmax){ - return (int)Math.round(getRandom(minmax[0],minmax[1])); + + protected int getRandom(int[] minmax) { + return (int) Math.round(getRandom(minmax[0], minmax[1])); } /** @@ -74,42 +78,37 @@ public class RandomTrader extends AutoTrader { double max = val * minmax[1] / 100.0; return getRandom(min, max); } - - - public boolean waitForOrder(long seconds){ - - for (int i=0; (i + * 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; + +import SeSim.Account; +import SeSim.TraderConfig; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ +public class SwitchingTrader extends RandomTrader{ + + + private Action mode; + + public SwitchingTrader(Account account, TraderConfig config) { + + super(account, config); + System.out.print("SWTrader Created\n"); + + if (account.shares>0) + mode=Action.sell; + else + mode=Action.buy; + printstartus(); + + } + + + private void printstartus(){ + + System.out.print("SWTrader:"); + switch (mode){ + case buy: + System.out.print("buy" + +account.shares + +" " + +account.money + ); + break; + case sell: + System.out.print("sell" + +account.shares + +" " + +account.money + ); + break; + + + } + System.out.print("\n"); + + } + + @Override + protected Action getAction(){ + + + + + if ( (account.shares>0) && (mode==Action.sell)){ + printstartus(); + return mode; + } + if ( (account.shares<=0 && mode==Action.sell)){ + mode=Action.buy; + printstartus(); + return mode; + } + if (account.money>100.0 && mode==Action.buy){ + printstartus(); + return mode; + } + if (account.money<=100.0 && mode==Action.buy){ + mode=Action.sell; + printstartus(); + return mode; + } + printstartus(); + return mode; + } + +} diff --git a/src/Traders/SwitchingTraderConfig.java b/src/Traders/SwitchingTraderConfig.java new file mode 100644 index 0000000..f5e18fb --- /dev/null +++ b/src/Traders/SwitchingTraderConfig.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2016, 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; + +import SeSim.Account; +import SeSim.AutoTrader; +import SeSim.Exchange; + +/** + * + * @author 7u83 <7u83@mail.ru> + */ +public class SwitchingTraderConfig extends RandomTraderConfig { + + @Override + public AutoTrader createTrader(Exchange se, long shares, double money) { + Account a = new Account(se, shares, money); + System.out.print("Returning a new sw trader\n"); + return new SwitchingTrader(a, this); + } + + public SwitchingTraderConfig() { + + sell_volume = new float[]{100, 100}; + sell_limit = new float[]{-15, 1}; + sell_order_wait = new int[]{5, 10}; + wait_after_sell = new int[]{2, 10}; + + buy_volume = new float[]{100, 100}; + buy_limit = new float[]{-5, 115}; + buy_order_wait = new int[]{15, 33}; + wait_after_buy = new int[]{20, 33}; + } +}