Threaded order execution now
This commit is contained in:
@ -54,7 +54,7 @@ public class CreateOrderDialog extends javax.swing.JDialog {
|
||||
this(parent, modal);
|
||||
this.account = account;
|
||||
typeComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[]{"Buy Lim", "Sell Lim", "Buy", "Sell"}));
|
||||
typeList = new OrderType[]{OrderType.BUYLIMIT, OrderType.SELLLIMIT, OrderType.BUY,OrderType.SELL};
|
||||
typeList = new OrderType[]{OrderType.BUYLIMIT, OrderType.SELLLIMIT, OrderType.BUY, OrderType.SELL};
|
||||
for (int i = 0; i < typeList.length; i++) {
|
||||
if (typeList[i] == type) {
|
||||
this.typeComboBox.setSelectedIndex(i);
|
||||
@ -74,17 +74,16 @@ public class CreateOrderDialog extends javax.swing.JDialog {
|
||||
OrderType t = getOrderType();
|
||||
Quote q = Globals.se.getBestPrice_0();
|
||||
Double price = q == null ? 0.0 : q.price;
|
||||
|
||||
|
||||
if (t == OrderType.BUYLIMIT) {
|
||||
this.limitSpinner.setValue(Globals.se.roundMoney(price));
|
||||
this.volumeSpinner.setValue(Globals.se.roundShares(account.getMoney()/price));
|
||||
this.volumeSpinner.setValue(Globals.se.roundShares(account.getMoney() / price));
|
||||
}
|
||||
if (t == OrderType.SELLLIMIT) {
|
||||
this.limitSpinner.setValue(Globals.se.roundMoney(price));
|
||||
this.volumeSpinner.setValue(Globals.se.roundShares(account.getShares()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -180,6 +179,7 @@ public class CreateOrderDialog extends javax.swing.JDialog {
|
||||
dispose();
|
||||
}//GEN-LAST:event_jButton2ActionPerformed
|
||||
|
||||
|
||||
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
|
||||
double volume = (double) volumeSpinner.getValue();
|
||||
double limit = (double) limitSpinner.getValue();
|
||||
@ -188,8 +188,16 @@ public class CreateOrderDialog extends javax.swing.JDialog {
|
||||
System.out.printf("Account is null\n");
|
||||
}
|
||||
OrderType type = this.getOrderType();
|
||||
Globals.se.createOrder(account.getID(), type, volume, limit);
|
||||
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
Globals.se.createOrder(account.getID(), type, volume, limit);
|
||||
}
|
||||
}.start();
|
||||
|
||||
dispose();
|
||||
|
||||
}//GEN-LAST:event_jButton1ActionPerformed
|
||||
|
||||
/**
|
||||
|
@ -62,7 +62,7 @@ public class ManTrader extends AutoTraderBase implements AccountListener {
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
se.timer.startTimerEvent(this, 0);
|
||||
se.timer.startTimerTask(this, 0);
|
||||
consoleDialog = new ManTraderConsoleDialog(Globals.frame, false, this.getAccount());
|
||||
this.consoleDialog.getBalancePanel().updateBalance(this.getAccount());
|
||||
// consoleDialog. rdersList1.account=trader.getAccount();
|
||||
|
@ -26,30 +26,24 @@
|
||||
package traders;
|
||||
|
||||
import gui.Globals;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import javax.swing.JDialog;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
//import sesim.AccountData;
|
||||
|
||||
import sesim.AutoTraderBase;
|
||||
import sesim.AutoTraderConfig;
|
||||
import sesim.AutoTraderGui;
|
||||
import sesim.Exchange;
|
||||
import sesim.Exchange.Account;
|
||||
import sesim.Exchange.Order;
|
||||
import sesim.OrderData;
|
||||
import sesim.Exchange.AccountListener;
|
||||
import sesim.Exchange.OrderStatus;
|
||||
import sesim.Quote;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 7u83 <7u83@mail.ru>
|
||||
*/
|
||||
public class RandomTraderA extends AutoTraderBase {
|
||||
public class RandomTraderA extends AutoTraderBase implements AccountListener {
|
||||
|
||||
public Float[] initial_delay = {0f, 5.0f};
|
||||
|
||||
@ -75,14 +69,19 @@ public class RandomTraderA extends AutoTraderBase {
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
Account a = se.getAccount(account_id);
|
||||
a.setListener(this);
|
||||
|
||||
long delay = (long) (getRandom(initial_delay[0], initial_delay[1]) * 1000);
|
||||
se.timer.startTimerEvent(this, delay);
|
||||
se.timer.startTimerTask(this, delay);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long timerTask() {
|
||||
System.out.printf("Enter TimerTask for %d\n", System.identityHashCode(this));
|
||||
sesim.Exchange.Account a = se.getAccount(account_id);
|
||||
long rc = this.doTrade();
|
||||
System.out.printf("Exit TimerTask for %d\n", System.identityHashCode(this));
|
||||
return rc;
|
||||
|
||||
}
|
||||
@ -165,18 +164,17 @@ public class RandomTraderA extends AutoTraderBase {
|
||||
int n = se.getNumberOfOpenOrders(account_id);
|
||||
if (n > 0) {
|
||||
Account ad = se.getAccount(account_id);
|
||||
|
||||
|
||||
Set <Long>keys = ad.getOrders().keySet();
|
||||
|
||||
Iterator<Long> it = keys.iterator();
|
||||
while (it.hasNext()) {
|
||||
// Order od = it.next();
|
||||
Set<Long> keys = ad.getOrders().keySet();
|
||||
|
||||
Iterator<Long> it = keys.iterator();
|
||||
while (it.hasNext()) {
|
||||
// Order od = it.next();
|
||||
boolean rc = se.cancelOrder(account_id, it.next());
|
||||
}
|
||||
}
|
||||
}
|
||||
return n;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -184,6 +182,25 @@ public class RandomTraderA extends AutoTraderBase {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accountUpdated(Account a, Exchange.Order o) {
|
||||
System.out.printf("Order waht %s\n", o.getOrderStatus().toString());
|
||||
if (o.getOrderStatus() == OrderStatus.CLOSED && false) {
|
||||
|
||||
System.out.printf("Enteter canel timer\n");
|
||||
se.timer.cancelTimerTask(this);
|
||||
System.out.printf("back from canel timer %d\n", System.identityHashCode(this));
|
||||
System.exit(0);
|
||||
|
||||
Long w = doTrade();
|
||||
System.out.printf("We have no to wait for %d\n", w);
|
||||
se.timer.startTimerTask(this, w);
|
||||
|
||||
}
|
||||
// System.out.printf("Updatetd Account\n", "");
|
||||
|
||||
}
|
||||
|
||||
protected enum Action {
|
||||
BUY, SELL, RANDOM
|
||||
}
|
||||
@ -197,11 +214,10 @@ public class RandomTraderA extends AutoTraderBase {
|
||||
|
||||
}
|
||||
|
||||
Action mode=Action.RANDOM;
|
||||
|
||||
long doTrade() {
|
||||
cancelOrders();
|
||||
Action a = getAction();
|
||||
Action mode = Action.RANDOM;
|
||||
|
||||
Integer doTrade1(Action a) {
|
||||
|
||||
switch (a) {
|
||||
case BUY: {
|
||||
boolean rc = doBuy();
|
||||
@ -209,19 +225,18 @@ public class RandomTraderA extends AutoTraderBase {
|
||||
mode = Action.BUY;
|
||||
return getRandom(buy_wait);
|
||||
}
|
||||
return 5000;
|
||||
return null;
|
||||
}
|
||||
|
||||
case SELL:
|
||||
{
|
||||
case SELL: {
|
||||
boolean rc = doSell();
|
||||
if (rc){
|
||||
if (rc) {
|
||||
mode = Action.SELL;
|
||||
return getRandom(sell_wait);
|
||||
|
||||
|
||||
}
|
||||
return 5000;
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -229,6 +244,44 @@ public class RandomTraderA extends AutoTraderBase {
|
||||
|
||||
}
|
||||
|
||||
long doTrade() {
|
||||
cancelOrders();
|
||||
Action a = getAction();
|
||||
|
||||
if (mode == Action.RANDOM) {
|
||||
|
||||
System.out.printf("Action: %s\n", a.toString());
|
||||
Integer rc = doTrade1(a);
|
||||
if (rc != null) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = doTrade1(Action.BUY);
|
||||
if (rc != null) {
|
||||
return rc;
|
||||
}
|
||||
rc = doTrade1(Action.SELL);
|
||||
if (rc != null) {
|
||||
return rc;
|
||||
}
|
||||
System.out.printf("All ha s failed\n");
|
||||
return 5000;
|
||||
}
|
||||
|
||||
if (mode == Action.BUY) {
|
||||
mode = Action.RANDOM;
|
||||
return getRandom(wait_after_buy);
|
||||
}
|
||||
|
||||
if (mode == Action.SELL) {
|
||||
mode = Action.RANDOM;
|
||||
return getRandom(wait_after_sell);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a (long) random number between min an max
|
||||
*
|
||||
@ -271,7 +324,6 @@ public class RandomTraderA extends AutoTraderBase {
|
||||
public boolean doBuy() {
|
||||
|
||||
// AccountData ad = this.se.getAccountData(account_id);
|
||||
|
||||
Account ad = se.getAccount(account_id);
|
||||
|
||||
Exchange.OrderType type = Exchange.OrderType.BUYLIMIT;
|
||||
@ -282,7 +334,7 @@ public class RandomTraderA extends AutoTraderBase {
|
||||
|
||||
// how much money we ant to invest?
|
||||
double money = getRandomAmmount(ad.getMoney(), buy_volume);
|
||||
|
||||
|
||||
Quote q = se.getBestPrice_0();
|
||||
//q=se.getLastQuoete();
|
||||
double lp = q == null ? getStart() : q.price;
|
||||
@ -290,15 +342,14 @@ public class RandomTraderA extends AutoTraderBase {
|
||||
double limit;
|
||||
limit = lp + getRandomAmmount(lp, buy_limit);
|
||||
|
||||
|
||||
double volume = money / limit;
|
||||
|
||||
// System.out.printf("Volume : %f", volume);
|
||||
|
||||
// System.out.printf("Volume : %f", volume);
|
||||
limit = se.roundMoney(limit);
|
||||
volume = se.roundShares(volume);
|
||||
|
||||
|
||||
if (volume <= 0 || money <= 0) {
|
||||
System.out.printf("Buy Order wont work\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -311,41 +362,33 @@ public class RandomTraderA extends AutoTraderBase {
|
||||
public boolean doSell() {
|
||||
// RandomTraderConfig myoldconfig = (RandomTraderConfig) this.oldconfig;
|
||||
//AccountData ad = this.se.getAccountData(account_id);
|
||||
|
||||
|
||||
Account ad = se.getAccount(account_id);
|
||||
|
||||
Exchange.OrderType type = Exchange.OrderType.SELLLIMIT;
|
||||
|
||||
|
||||
// how much shares we ant to sell?
|
||||
double volume = getRandomAmmount(ad.getShares(), sell_volume);
|
||||
volume = se.roundShares(volume);
|
||||
|
||||
|
||||
// double lp = 100.0; //se.getBestLimit(type);
|
||||
Quote q = se.getBestPrice_0();
|
||||
// q=se.getLastQuoete();
|
||||
// q=se.getLastQuoete();
|
||||
double lp = q == null ? getStart() : q.price;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
double limit;
|
||||
limit = lp + getRandomAmmount(lp, sell_limit);
|
||||
se.roundMoney(limit);
|
||||
|
||||
|
||||
if (volume <= 0 || limit <=0) {
|
||||
if (volume <= 0 || limit <= 0) {
|
||||
System.out.printf("Sell wont work\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
System.out.printf("Create a Sell Order %f %f!!!!\n", volume, limit);
|
||||
se.createOrder(account_id, type, volume, limit);
|
||||
|
||||
|
||||
return true;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ public class RandomTraderB extends AutoTraderBase {
|
||||
@Override
|
||||
public void start() {
|
||||
long delay = (long) (getRandom(initial_delay[0], initial_delay[1]) * 1000);
|
||||
se.timer.startTimerEvent(this, delay);
|
||||
se.timer.startTimerTask(this, delay);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user