Some changes ...

This commit is contained in:
7u83 2016-12-31 21:18:28 +01:00
parent 4f30bcbc30
commit 456be84413
10 changed files with 201 additions and 38 deletions

View File

@ -5,6 +5,7 @@
<group>
<file>file:/home/tube/NetBeansProjects/SeSim/additional/README.txt</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/SeSim/Trader.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/SeSim/Locker.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/SeSim/Order.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/Gui/ControlPanel.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/SeSim/Quote.java</file>
@ -18,10 +19,10 @@
<file>file:/home/tube/NetBeansProjects/SeSim/src/Gui/NewPanel.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/Gui/Chart.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/Traders/RandomTraderConfig.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/SeSim/TraderRunner.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/Gui/AskBook.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/SeSim/AutoTrader.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/Gui/BidBook.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/Traders/SwitchingTrader.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/build.xml</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/Gui/OrderBookPanel.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/Traders/RandomTrader.java</file>
@ -31,6 +32,7 @@
<file>file:/home/tube/NetBeansProjects/SeSim/README.md</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/Gui/CandlestickDemo.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/SeSim/Logger.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/Traders/SwitchingTraderConfig.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/src/SeSim/SellOrder.java</file>
<file>file:/home/tube/NetBeansProjects/SeSim/LICENSE</file>
</group>

View File

@ -37,6 +37,8 @@ import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Iterator;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
@ -52,6 +54,7 @@ import org.jfree.data.xy.XYDataset;
import SeSim.Exchange.*;
import SeSim.Quote;
import java.util.SortedSet;
import java.util.TreeSet;
/**
*
@ -65,7 +68,8 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
public Chart() {
initComponents();
String stockSymbol = "Schliemanz Koch AG";
// String stockSymbol = "Schliemanz Koch AG";
String stockSymbol = "MSFT";
DateAxis domainAxis = new DateAxis("Date");
NumberAxis rangeAxis = new NumberAxis("Price");
@ -111,9 +115,90 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
return result;
}
protected OHLCDataItem getOhlcData(long first, long last, TreeSet quotes ){
Quote e=new Quote();
e.time=first;
e.id=0;
SortedSet<Quote> l = quotes.tailSet(e);
double open=0;
double high=0;
double low=0;
double close=0;
double volume;
Iterator <Quote>it = l.iterator();
Quote q;
q = it.next();
open=q.price;
high=q.price;
low=q.price;
volume = q.volume;
while (it.hasNext() && q.time<last){
q = it.next();
if (q.price>high)
high = q.price;
if (q.price<low)
low=q.price;
volume += q.volume;
}
close=q.price;
Date date = new Date(first);
return new OHLCDataItem(
date, open, high, low, close, volume
);
}
protected OHLCDataItem[] getData() {
List<OHLCDataItem> data = new ArrayList<>();
TreeSet <Quote>s = MainWin.se.getQuoteHistory(60);
Iterator <Quote>i = s.iterator();
this.getOhlcData(0, System.currentTimeMillis(), s);
// OHLCDataItem item = new OHLCDataItem();
long t=0;
// if ()
// Quote q = i.next();
// OHLCDataItem item = new OHLCDataItem(
// date, open, high, low, close, volume);
/* double open =
double high =
double low =
double close =
double volume = Double.parseDouble(st.nextToken());
double adjClose = Double.parseDouble(st.nextToken());
*/
// data.add(item);
//return data.toArray(new <OHLCDataItem> rdata[]);
return data.toArray(new OHLCDataItem[data.size()]);
@ -121,7 +206,7 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
//This method uses yahoo finance to get the OHLC data
protected OHLCDataItem[] getData_old() {
String stockSymbol = "Schliemanz Koch AG";
String stockSymbol = "MSFT";
List<OHLCDataItem> dataItems = new ArrayList<OHLCDataItem>();
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";
@ -154,6 +239,8 @@ public class Chart extends javax.swing.JPanel implements QuoteReceiver {
//Convert the list into an array
OHLCDataItem[] data = dataItems.toArray(new OHLCDataItem[dataItems.size()]);
System.out.print("Return oghls old data items\n");
return data;
}

View File

@ -158,8 +158,8 @@ public class MainWin extends javax.swing.JFrame {
RandomTraderConfig rcfg = new RandomTraderConfig();
at.add(1000, rcfg, se, 1000, 10000);
SwitchingTraderConfig scfg = new SwitchingTraderConfig();
at.add(1, scfg, se, 1000000, 0);
//SwitchingTraderConfig scfg = new SwitchingTraderConfig();
//at.add(1, scfg, se, 1000000, 0);
// at.add(10, rcfg, se, 1000000, 0);

View File

@ -42,6 +42,22 @@ public abstract class AutoTrader extends Trader implements Runnable {
sleep(seconds*1000);
} catch (InterruptedException e) {
}
}
public void start(){
System.out.print("Starting AutoTrader\n");
class Runner extends Thread{
AutoTrader trader;
@Override
public void run(){
trader.run();
}
}
Runner r = new Runner();
r.trader=this;
r.start();
}
}

View File

@ -35,8 +35,9 @@ public class AutoTraderLIst {
for (int i = 0; i < n; i++) {
AutoTrader trader = config.createTrader(se, shares, money);
TraderRunner tr = new TraderRunner(trader);
tr.start();
// TraderRunner tr = new TraderRunner(trader);
trader.start();
}
}

View File

@ -27,14 +27,18 @@ public class Exchange extends Thread {
}
public SortedSet <Quote> getQuoteHistory(int seconds){
long ct = System.currentTimeMillis() - seconds * 1000;
public TreeSet <Quote> getQuoteHistory(int seconds){
Quote last = quoteHistory.last();
long ct = last.time - seconds * 1000;
Quote e = new Quote();
e.time=ct;
SortedSet<Quote> l = quoteHistory.tailSet(e);
return l;
return (TreeSet)l;
}
// Class to describe an executed order
@ -102,7 +106,7 @@ public class Exchange extends Thread {
}
// send updated quotes to all quote receivers
void UpdateQuoteReceivers(Quote q) {
private void updateQuoteReceivers(Quote q) {
Iterator<QuoteReceiver> i = qrlist.iterator();
while (i.hasNext()) {
i.next().UpdateQuote(q);
@ -197,13 +201,13 @@ public class Exchange extends Thread {
}
public void TransferMoney(Account src, Account dst, double money) {
public void transferMoney(Account src, Account dst, double money) {
src.money -= money;
dst.money += money;
}
public void CancelOrder(Order o) {
public void cancelOrder(Order o) {
Lock();
TreeSet<Order> book = this.selectOrderBook(o.type);
book.remove(o);
@ -229,6 +233,8 @@ public class Exchange extends Thread {
src.money += price * volume;
}
long nextQuoteId=0;
public void OrderMatching() {
while (true) {
@ -300,11 +306,14 @@ public class Exchange extends Thread {
q.price = price;
q.time = System.currentTimeMillis();
q.ask=a.limit;
q.bid=b.limit;
q.id = nextQuoteId++;
this.UpdateQuoteReceivers(q);
this.updateQuoteReceivers(q);
this.updateBookReceivers(OrderType.bid);
this.updateBookReceivers(OrderType.ask);

51
src/SeSim/Locker.java Normal file
View File

@ -0,0 +1,51 @@
/*
* 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;
import java.util.concurrent.Semaphore;
/**
*
* @author 7u83 <7u83@mail.ru>
*/
public class Locker {
private final Semaphore avail = new Semaphore(1, true);
public boolean lock() {
try {
avail.acquire();
} catch (InterruptedException e) {
return false;
}
return true;
}
public void unlock() {
avail.release();
}
}

View File

@ -30,7 +30,7 @@ package SeSim;
* @author 7u83 <7u83@mail.ru>
*/
public class Quote implements Comparable {
public double bid;
public double bid_volume;
public double ask;
@ -39,6 +39,8 @@ public class Quote implements Comparable {
public double price;
public long volume;
public long time;
Locker lock = new Locker();
public void print() {
System.out.print("Quote ("
@ -51,10 +53,23 @@ public class Quote implements Comparable {
);
}
public long id;
@Override
public int compareTo(Object o) {
int ret;
Quote q = (Quote)o;
return (int)(this.time-q.time);
ret = (int)(this.time-q.time);
if (ret !=0)
return ret;
return (int)(this.id-q.id);
}
/* Quote (){
lock.lock();
id=nextid++;
lock.unlock();
}*/
}

View File

@ -1,18 +0,0 @@
package SeSim;
public class TraderRunner extends Thread {
protected long sleeptime = 1000;
AutoTrader trader;
public TraderRunner(AutoTrader trader){
this.trader=trader;
}
public void run() {
trader.run();
}
}

View File

@ -87,7 +87,7 @@ public class RandomTrader extends AutoTrader {
if (account.pending.size() != 0) {
Order o = account.pending.get(0);
account.se.CancelOrder(o);
account.se.cancelOrder(o);
return false;
}
return true;
@ -139,7 +139,7 @@ public class RandomTrader extends AutoTrader {
// System.out.print("RT: age is: "+age+"\n");
if (age > myconfig.maxage) {
// System.out.print("MaxAge is"+myconfig.maxage+"\n");
account.se.CancelOrder(o);
account.se.cancelOrder(o);
// System.out.print("Age reached - canel return false\n");
return false;
}
@ -198,7 +198,7 @@ public class RandomTrader extends AutoTrader {
@Override
public void run() {
// System.out.print("Starting Random Trader\n");
System.out.print("Starting Random Trader\n");
while (true) {
// What next to do?