Moved Account class out of Exchange to sesim

This commit is contained in:
7u83 2017-10-09 16:28:27 +02:00
parent dfe4661d11
commit 4aacac8d76
14 changed files with 110 additions and 76 deletions

View File

@ -1,4 +1,4 @@
#Mon, 09 Oct 2017 14:13:25 +0200
#Mon, 09 Oct 2017 16:27:25 +0200
annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.processors.list=

View File

@ -34,7 +34,7 @@ import java.util.Map;
import javax.swing.table.DefaultTableModel;
import sesim.Exchange.Account;
import sesim.Account;
import sesim.Order;
import sesim.Order.OrderType;
import traders.ManTrader.CreateOrderDialog;

View File

@ -40,7 +40,7 @@ import javax.swing.table.TableRowSorter;
import sesim.AutoTraderInterface;
import sesim.Exchange;
import sesim.Exchange.Account;
import sesim.Account;
/**
*

89
src/sesim/Account.java Normal file
View File

@ -0,0 +1,89 @@
/*
* Copyright (c) 2017, tobias
* 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.ConcurrentHashMap;
/**
* Implements a trading account
*/
public class Account implements Comparable {
private Exchange.AccountListener listener = null;
protected final double id;
protected double shares;
protected double money;
protected AutoTraderInterface owner;
protected final ConcurrentHashMap<Long, Order> orders;
@Override
public int compareTo(Object a) {
Account account = (Account) a;
return this.id - account.id < 0 ? -1 : 1;
}
Account(double id, double money, double shares) {
this.id=id;
//id = (random.nextDouble() + (account_id_generator.getNext()));
orders = new ConcurrentHashMap();
this.money = money;
this.shares = shares;
}
public double getID() {
return id;
}
public double getShares() {
return shares;
}
public double getMoney() {
return money;
}
public AutoTraderInterface getOwner() {
return owner;
}
public ConcurrentHashMap<Long, Order> getOrders() {
return orders;
}
public void setListener(Exchange.AccountListener al) {
this.listener = al;
}
public void update(Order o) {
if (listener == null) {
return;
}
listener.accountUpdated(this, o);
}
}

View File

@ -72,7 +72,7 @@ public abstract class AutoTraderBase implements AutoTraderInterface, TimerTaskRu
}
private long id;
public Exchange.Account getAccount() {
public Account getAccount() {
return se.getAccount(account_id);
}

View File

@ -76,7 +76,7 @@ public interface AutoTraderInterface {
*/
public void init(Exchange se, long id, String name, double money, double shares, JSONObject cfg);
public Exchange.Account getAccount();
public Account getAccount();
public void start();

View File

@ -39,6 +39,7 @@ import org.json.JSONObject;
import sesim.Order.OrderStatus;
import sesim.Order.OrderType;
/**
* @desc Echchange class
* @author 7u83
@ -209,65 +210,6 @@ public class Exchange {
}
}
/**
* Implements a trading account
*/
public class Account implements Comparable {
private AccountListener listener = null;
private final double id;
private double shares;
private double money;
protected AutoTraderInterface owner;
private final ConcurrentHashMap<Long, Order> orders;
@Override
public int compareTo(Object a) {
Account account = (Account) a;
return this.id - account.id < 0 ? -1 : 1;
}
Account(double money, double shares) {
id = (random.nextDouble() + (account_id_generator.getNext()));
orders = new ConcurrentHashMap();
this.money = money;
this.shares = shares;
}
public double getID() {
return id;
}
public double getShares() {
return shares;
}
public double getMoney() {
return money;
}
public AutoTraderInterface getOwner() {
return owner;
}
public ConcurrentHashMap<Long, Order> getOrders() {
return orders;
}
public void setListener(AccountListener al) {
this.listener = al;
}
public void update(Order o) {
if (listener == null) {
return;
}
listener.accountUpdated(this, o);
}
}
public void createTraders(JSONArray traderdefs) {
for (int i = 0; i < traderdefs.length(); i++) {
@ -285,7 +227,9 @@ public class Exchange {
public double createAccount(double money, double shares) {
Account a = new Account(money, shares);
double id = (random.nextDouble() + (account_id_generator.getNext()));
Account a = new Account(id, money, shares);
accounts.put(a.id, a);
return a.id;
}

View File

@ -55,11 +55,11 @@ public class Order {
protected final long id;
protected final long created;
protected final Exchange.Account account;
protected final Account account;
double cost;
Order(long id, long created, Exchange.Account account, OrderType type, double volume, double limit) {
Order(long id, long created, Account account, OrderType type, double volume, double limit) {
//id = order_id_generator.getNext();
this.id = id;
this.account = account;
@ -108,7 +108,7 @@ public class Order {
return cost / e;
}
public Exchange.Account getAccount() {
public Account getAccount() {
return account;
}

View File

@ -25,7 +25,7 @@
*/
package traders.ManTrader;
import sesim.Exchange.Account;
import sesim.Account;
/**
*

View File

@ -27,7 +27,7 @@ package traders.ManTrader;
import gui.Globals;
import sesim.Exchange.Account;
import sesim.Account;
import sesim.Order.OrderType;
import sesim.Quote;

View File

@ -39,6 +39,7 @@ import sesim.Exchange;
import sesim.Exchange.AccountListener;
import sesim.Order.OrderStatus;
import sesim.Order;
import sesim.Account;
/**
*
@ -115,7 +116,7 @@ public class ManTrader extends AutoTraderBase implements AccountListener, AutoTr
}
@Override
public void accountUpdated(Exchange.Account a, Order o) {
public void accountUpdated(Account a, Order o) {
//this.consoleDialog.cons
//System.out.printf("AccountListener called\n");

View File

@ -26,7 +26,7 @@
package traders.ManTrader;
import javax.swing.JPanel;
import sesim.Exchange.Account;
import sesim.Account;
/**
*

View File

@ -34,7 +34,7 @@ import org.json.JSONObject;
import sesim.AutoTraderBase;
import sesim.AutoTraderGui;
import sesim.Exchange;
import sesim.Exchange.Account;
import sesim.Account;
import sesim.Exchange.AccountListener;
import sesim.Order.OrderType;
@ -86,7 +86,7 @@ public class RandomTraderA extends AutoTraderBase implements AccountListener {
public long timerTask() {
intask = true;
owait = null;
sesim.Exchange.Account a = se.getAccount(account_id);
sesim.Account a = se.getAccount(account_id);
long rc = this.doTrade();
setStatus("Sleeping for %d ms", rc);
intask = false;

View File

@ -35,7 +35,7 @@ import org.json.JSONObject;
import sesim.AutoTraderBase;
import sesim.AutoTraderGui;
import sesim.Exchange.Account;
import sesim.Account;
import sesim.Order.OrderType;
import sesim.Quote;
@ -75,7 +75,7 @@ public class RandomTraderB extends AutoTraderBase {
@Override
public long timerTask() {
sesim.Exchange.Account a = se.getAccount(account_id);
sesim.Account a = se.getAccount(account_id);
long rc = this.doTrade();
return rc;