Skip to content

Commit

Permalink
minor enhancements and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
guifre committed Jul 5, 2014
1 parent 5f1d31e commit 2a04730
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
Binary file modified dist/BTCWolf.jar
Binary file not shown.
8 changes: 4 additions & 4 deletions src/org/btcwolf/BTCWolf.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class BTCWolf {

private static final Logger LOGGER = Logger.getLogger(BTCWolf.class);
private static final String LOG4J_PATH = "./resources/log4j.properties";
private static final long POLLING_TIME = 6000;
private static final long POLLING_TIME = 10000;

public static void main(String[] args) {

Expand All @@ -44,16 +44,16 @@ public static void main(String[] args) {
tradingStrategyFactory.getTradingStrategy().onTickerReceived(ticker);
previousTicker = ticker;
} else {
LOGGER.debug("discarding new [" + ticker + "]");
LOGGER.debug("discarding ticker");
}
makeSomeCoffee();
}
}

private static boolean isSameTicker(Ticker previousTicker, Ticker ticker) {
return previousTicker.getBid().compareTo(ticker.getBid()) == 0 &&
previousTicker.getVolume().compareTo(ticker.getVolume()) == 0 &&
previousTicker.getLast().compareTo(ticker.getLast()) == 0 &&
//previousTicker.getVolume().compareTo(ticker.getVolume()) == 0 &&
//previousTicker.getLast().compareTo(ticker.getLast()) == 0 &&
previousTicker.getAsk().compareTo(ticker.getAsk()) == 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private void order(Ticker ticker) {
&& lastOpTime > MIN_TICKERS_BETWEEN_ORDERS) {

lastOpTime = 0;
OrderType type = getOrderType();
OrderType type = getOrderType(ticker);
BigDecimal amount;

if (type == OrderType.BID
Expand All @@ -160,7 +160,7 @@ private void order(Ticker ticker) {
}
}

private OrderType getOrderType() {
private OrderType getOrderType(Ticker ticker) {
int highers = 0;
int lowers = 0;
for (BigDecimal bigDecimal : historicShortEMA) {
Expand All @@ -170,14 +170,17 @@ private OrderType getOrderType() {
highers++;
}
}
if (isLinear() || isFakeTrend()) {

if (isLinear(ticker) || isFakeTrend()) {
return null;
}

if (highers > lowers) {
return OrderType.ASK;
} else if (lowers > highers){
return OrderType.BID;
}

return null;
}

Expand Down Expand Up @@ -244,7 +247,7 @@ private void checkDeadLimitOrders() {
}
}

private boolean isLinear() {
private boolean isLinear(Ticker ticker) {
int plain = 0;
List<BigDecimal> list = new ArrayList<BigDecimal>(historicShortEMA);
for (int i = 0; i < historicShortEMA.size() - 1; i++) {
Expand All @@ -253,7 +256,11 @@ private boolean isLinear() {
}
}
if (plain > MAX_HISTORIC_SHORT_EMA / 2 + 1) {
logger.info("plain price, not ordering");
String msg = "";
for (BigDecimal oldEMA : historicShortEMA) {
msg = msg.concat(String.format("%.1f", oldEMA));
}
logger.info("Plain Price old EMA [" + msg + "] ticker " + ticker);
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ public StrategyLoggerDecorator(TraderAgent traderAgent, boolean useTwitterAgent
}
}

@Override
protected String placeOrder(Order.OrderType orderType, BigDecimal amount, Ticker ticker) {
BigDecimal price;
if (orderType == Order.OrderType.ASK) {
price = ticker.getAsk();
} else {
price = ticker.getBid();
}
log("Ordered " + orderType.toString()
+ " [" + String.format("%.5f", amount)
+ "]BTC for [" + String.format("%.1f", price)
+ "]CNY.");
return super.placeOrder(orderType, amount, ticker);
}

@Override
public void onTickerReceived(Ticker ticker) {
pollExchangeStatus(ticker);
Expand Down Expand Up @@ -114,15 +129,16 @@ protected void logBID(Ticker ticker, BigDecimal bitCoinsToBuy, BigDecimal previo
}

public void logOrder(Ticker ticker, BigDecimal amount, Order.OrderType orderType) {
BigDecimal price = null;
BigDecimal price;
if (orderType == Order.OrderType.ASK) {
price = ticker.getAsk();
} else {
price = ticker.getBid();
}
log("Ordered "+orderType.toString() +" [" +
String.format("%.5f", amount) + "]BTC for [" +
String.format("%.1f", price) + "]CNY.");
log("Ordered " + orderType.toString()
+ " [" + String.format("%.5f", amount)
+ "]BTC for [" + String.format("%.1f", price)
+ "]CNY.");
}

void log(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public void testExponentialMovingAverageStrategyWithPlot() throws InterruptedExc
int maxIndex = new MarketExchangeAgent(BigDecimal.ZERO, BigDecimal.ZERO).getTickers();
final Plotting plotting = new Plotting();
MarketExchangeAgent testerAgent = new MarketExchangeAgent(BigDecimal.valueOf(0), BigDecimal.valueOf(0), plotting);
int[] indexes = StrategyTestHelper.getIndexes(4000, maxIndex); //{199, 699 }, {10557, 11057};// {9389, 9689};{12202 , 12502};//
int[] indexes = StrategyTestHelper.getIndexes(1000, maxIndex); //{199, 699 }, {10557, 11057};// {9389, 9689};{12202 , 12502};//
System.out.println("init " + indexes[0] + " end " + indexes[1] + " total " + maxIndex);
TestStrategyFactory strategyProvider = new TestStrategyFactory(testerAgent, false);
strategyProvider.switchStrategy(strategyProvider.buildExponentialMovingAverageStrategy());
Expand Down

0 comments on commit 2a04730

Please sign in to comment.