Skip to content

Commit

Permalink
add balance
Browse files Browse the repository at this point in the history
  • Loading branch information
albanx committed Feb 12, 2020
1 parent 37ccfa9 commit 1a098ad
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 34 deletions.
40 changes: 36 additions & 4 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const store = createStore(MongoStore.NAME, {
dbName: config.DB_NAME
});

const startDatabase = async () => {
export const startDatabase = async () => {
dashboard.log('Connecting to database...');
const coinExchangeCollection = await store.createCollection('coin_exchange');
const orderCollection = await store.createCollection('orders');
Expand All @@ -154,20 +154,53 @@ const startDatabase = async () => {
return { orderRepo, coinExchangeRepo };
};

const loadBalances = async () => {
export const loadBalances = async () => {
const exchangeService = new ExchangeService();
const balances = await exchangeService.getBalances(BitstampExchange.NAME);

return balances;
};

const viewBalances = balances => {
const balancesAsArray = balances.map(b => [
b.exchange,
b.coin,
b.balance,
`${b.value} ${b.valueCurr}`
]);

dashboard.addBalancesRows(balancesAsArray);
};

const createCoinsToTradeFromBalance = balances => {
return balances
.filter(coin => coin.coin === 'ltc')
.map(coin =>
createCoinExchange({
coin: coin.coin,
exchange: coin.exchange,
baseCoin: 'EUR',
amount: coin.balance,
priceOrder: coin.balance / coin.value,
tradeMode: TradeMonitorService.TRADE_MODE_MONITOR,
lastOrderType: OrderService.ORDER_BUY,
strategy: {
name: PeakDetectorStrategy.NAME,
params: {
threshold: 15,
prices: [],
maxLimit: 30
}
}
})
);
};

const startTradingBot = async () => {
const { orderRepo, coinExchangeRepo } = await startDatabase();
const balances = await loadBalances();
// const tradeCoins = createCoinsToTradeFromBalance(balances);

const mediator = new EventMediator();
const coinExchangeService = new CoinExchangeService(coinExchangeRepo);
const orderService = new OrderService(orderRepo);
Expand All @@ -178,9 +211,8 @@ const startTradingBot = async () => {
tradeCoins
);

await loadBalances();

//display events
viewBalances(balances);
const dashboardViewer = new DashboardViewer(dashboard, orderService);
mediator.on(EVENTS.MONITOR_START, () => dashboard.log('Monitor start'));
mediator.on(EVENTS.MONITOR_LOAD_COINS, () => dashboard.log('Loading coins'));
Expand Down
52 changes: 33 additions & 19 deletions src/DashboardViewer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import SimpleStrategy from "./strategies/SimpleStrategy";
import SimpleStrategy from './strategies/SimpleStrategy';
import { formatDate } from './Utils';

export default class DashboardViewer {
Expand All @@ -9,24 +9,38 @@ export default class DashboardViewer {

showCoins(coins) {
this.dashboard.setPriceMonitorLabel(`Price Monitor - ${new Date()}`);
this.dashboard.addPriceMonitorRows(
coins.map(c => {
const nextOrderType = this.orderService.getOrderType(c, true);
const priceNextOrder = this.orderService.getPreviewPriceNextOrder(c, nextOrderType);
const nextText = `${nextOrderType}@${priceNextOrder.toFixed(2)}`;
return [
c.getId().toString().substring(0, 4),
`${c.coin}@${c.exchange}`,
`${c.priceExchange.toFixed(4)} / ${(c.priceExchange * c.amount).toFixed(4)}`,
`${c.priceOrder.toFixed(4)} / ${(c.priceOrder * c.amount).toFixed(4)}`,
`${c.priceChange.diff.toFixed(4)} / ${(c.priceChange.diff * c.amount).toFixed(4)} €`,
c.priceChange.percent.toFixed(4) + ' %',
c.tradeMode,
nextText,
c.strategy.name
]
})
this.dashboard.addPriceMonitorRows(coins.map(c => this.mapCoinToViewer(c)));
}

mapCoinToViewer(coin) {
const nextOrderType = this.orderService.getOrderType(coin, true);
const priceNextOrder = this.orderService.getPreviewPriceNextOrder(
coin,
nextOrderType
);
const nextText = `${nextOrderType}@${priceNextOrder.toFixed(2)}`;
const id = coin
.getId()
.toString()
.substring(0, 4);

return [
id,
`${coin.coin}@${coin.exchange}`,
`${coin.priceExchange.toFixed(4)} / ${(
coin.priceExchange * coin.amount
).toFixed(4)}`,
`${coin.priceOrder.toFixed(4)} / ${(
coin.priceOrder * coin.amount
).toFixed(4)}`,
`${coin.priceChange.diff.toFixed(4)} / ${(
coin.priceChange.diff * coin.amount
).toFixed(4)} €`,
coin.priceChange.percent.toFixed(4) + ' %',
coin.tradeMode,
nextText,
coin.strategy.name
];
}

showCurrentOrders(orders) {
Expand All @@ -42,4 +56,4 @@ export default class DashboardViewer {
])
);
}
}
}
7 changes: 4 additions & 3 deletions src/exchanges/BitstampExchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ export default class BitstampExchange extends ExchangeInterface {
balances
.filter(item => item.balance > 0)
.map(async item => {
item.value =
(await this.getCoinPrice(item.coin, item.valueCurr, 0)) *
item.balance;
const price = await this.getCoinPrice(item.coin, item.valueCurr, 0);
const value = price * item.balance;
item.value = value.toFixed(2);

return item;
})
);
Expand Down
19 changes: 11 additions & 8 deletions src/services/TradeMonitorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,17 @@ export default class TradeMonitorService {

async loadCoins() {
const storedCoins = await this.coinExchangeService.getCoinsToTrade();
// const newCoins = this.coinsToTradeDefault.filter(item => !storedCoins.find(item2 =>
// ( item.coin === item2.coin &&
// item.exchange === item2.exchange &&
// item.baseCoin === item2.baseCoin &&
// item.tradeMode === item2.tradeMode &&
// item.strategy.name === item2.strategy.name )
// )
// );
const newCoins = this.coinsToTradeDefault.filter(
item =>
!storedCoins.find(
item2 =>
item.coin === item2.coin &&
item.exchange === item2.exchange &&
item.baseCoin === item2.baseCoin &&
item.tradeMode === item2.tradeMode &&
item.strategy.name === item2.strategy.name
)
);

this.coinsToTrade = storedCoins.concat(this.coinsToTradeDefault);
this.coinsToTrade.forEach(coin => this.initCoin(coin, 0));
Expand Down

0 comments on commit 1a098ad

Please sign in to comment.