Example #1
0
        public async Task <Unit> UpdateOrder(CryptoOrder cryptoOrder)
        {
            try
            {
                Console.WriteLine($"Closed order {cryptoOrder.Uuid} as {cryptoOrder.OrderType} at {cryptoOrder.Limit}");
                switch (cryptoOrder.OrderType)
                {
                case CryptoOrderType.LimitSell:
                    Budget.Available += cryptoOrder.Price;
                    var tradeForSellOrder = Trades.FirstOrDefault(t => t.SellOrder.Uuid == cryptoOrder.Uuid);
                    if (tradeForSellOrder != null)
                    {
                        if (cryptoOrder.Canceled)
                        {
                            tradeForSellOrder.Status             = TradeStatus.Bought;
                            tradeForSellOrder.SellOrder.IsOpened = false;
                            return(await Task.FromResult(Unit.Default));
                        }
                        var tradeProfit = tradeForSellOrder.BuyOrder.Price.GetReadablePercentageChange(tradeForSellOrder.SellOrder.Price);
                        Budget.Profit += tradeProfit;
                        Budget.Earned += tradeForSellOrder.SellOrder.Price - tradeForSellOrder.BuyOrder.Price;
                        Console.WriteLine($"{cryptoOrder.Uuid}: SELL - {tradeProfit}");
                        await _pushManager.TriggerPush(PushMessage.FromMessage($"Sold {Market} for profit {tradeProfit}%"));

                        tradeForSellOrder.Profit    = tradeProfit;
                        tradeForSellOrder.Status    = TradeStatus.Completed;
                        tradeForSellOrder.SellOrder = cryptoOrder;
                    }
                    break;

                case CryptoOrderType.LimitBuy:
                    var tradeForBuyOrder = Trades.FirstOrDefault(t => t.BuyOrder.Uuid == cryptoOrder.Uuid);
                    if (tradeForBuyOrder != null)
                    {
                        await _pushManager.TriggerPush(PushMessage.FromMessage($"Bought {Market} at {cryptoOrder.Limit} BTC"));

                        if (cryptoOrder.Canceled)
                        {
                            tradeForBuyOrder.Status   = TradeStatus.Empty;
                            tradeForBuyOrder.BuyOrder = new CryptoOrder();
                            return(await Task.FromResult(Unit.Default));
                        }
                        tradeForBuyOrder.Status   = TradeStatus.Bought;
                        tradeForBuyOrder.BuyOrder = cryptoOrder;
                    }
                    break;
                }
            }
            finally
            {
                if (!IsInTestMode)
                {
                    var traderData = await _traderGrain.GetTraderData();

                    traderData.CurrentTicker = Ticker;
                    await _hubNotifier.UpdateTrader(traderData);
                }
            }
            return(await Task.FromResult(Unit.Default));
        }
Example #2
0
        public async Task StartTrading()
        {
            await _pushManager.TriggerPush(PushMessage.FromMessage("Started trading"));

            var traderStates = await _tradersManager.GetAllTraders();

            foreach (var market in traderStates.Select(t => t.Market))
            {
                var coinTrader = new CoinTrader(_cryptoApi, _clusterClient, _hubNotifier, _pushManager);
                coinTrader.Initialize(market);
                await coinTrader.StartAsync();
            }

            Console.WriteLine("Finished loading");
        }
Example #3
0
        private async Task StartTradingInProductionMode()
        {
            await _pushManager.TriggerPush(PushMessage.FromMessage("Started trading"));

            var traderStates = await _tradersManager.GetAllTraders();

            foreach (var market in traderStates.Select(t => t.Market))
            {
                var liveTrader = new LiveTrader(_clusterClient, _hubNotifier, _pushManager, new CoinTrader(_cryptoApi), _cryptoApi);
                liveTrader.IsInTestMode = false;
                liveTrader.Initialize(market);
                await liveTrader.StartAsync();
            }

            Console.WriteLine("Finished loading");
        }
Example #4
0
        private async Task <Trade> UpdateTrade(Trade trade)
        {
            var tradeAction = Strategy.CalculateTradeAction(Ticker, trade);

            switch (tradeAction.TradeAdvice)
            {
            case TradeAdvice.Buy:
                await _pushManager.TriggerPush(PushMessage.FromMessage($"Got buy signal {tradeAction.Reason}"));

                var buyOrder = await CreateBuyOrder(tradeAction.OrderPricePerUnit);

                if (tradeAction.Reason == TradeReason.BuyTrigger)
                {
                    Console.WriteLine($"Buy trigger at {tradeAction.OrderPricePerUnit}");
                    return(new Trade {
                        BuyOrder = buyOrder, Status = TradeStatus.Buying
                    });
                }
                trade.BuyOrder = buyOrder;
                trade.Status   = TradeStatus.Buying;
                break;

            case TradeAdvice.Sell:
                await _pushManager.TriggerPush(PushMessage.FromMessage($"Got sell signal {tradeAction.Reason}"));
                await CreateSellOrder(trade, tradeAction.OrderPricePerUnit);

                return(new Trade {
                    Status = TradeStatus.Empty
                });

            case TradeAdvice.Cancel:
                await _pushManager.TriggerPush(PushMessage.FromMessage($"Got cancel signal {tradeAction.Reason}"));

                Console.WriteLine($"{trade.BuyOrder.Uuid}: Canceling order {trade.BuyOrder.Uuid}");
                var cancelResponse = await _cryptoApi.CancelOrder(trade.BuyOrder.Uuid);

                if (cancelResponse.IsSuccessful)
                {
                    Budget.Available += trade.BuyOrder.Price;
                    trade.Status      = TradeStatus.Canceled;
                }
                break;
            }

            return(Trade.Empty);
        }
Example #5
0
        public async Task StartTradingInTestMode()
        {
            await _pushManager.TriggerPush(PushMessage.FromMessage("Started trading"));

            var traderStates = await _tradersManager.GetAllTraders();

            foreach (var market in traderStates.Select(t => t.Market))
            {
                var cryptoApi = new FakeBittrexApi(_options.Value.BittrexApiKey, _options.Value.BittrexApiSecret);
                cryptoApi.IsInTestMode = true;
                var liveTrader = new LiveTrader(_clusterClient, _hubNotifier, _pushManager, new CoinTrader(cryptoApi), cryptoApi);
                liveTrader.IsInTestMode = true;
                liveTrader.Initialize(market);
                await liveTrader.StartAsync();

                Task.Run(() => cryptoApi.SendMarketUpdates(market));
            }

            Console.WriteLine("Finished loading");
        }
Example #6
0
        private async Task <Unit> UpdateTrade(Trade trade)
        {
            switch (trade.Status)
            {
            case TradeStatus.Buying:
                await _pushManager.TriggerPush(PushMessage.FromMessage($"Got buy signal for {Market}"));

                break;

            case TradeStatus.Selling:
                await _pushManager.TriggerPush(PushMessage.FromMessage($"Got sell signal for {Market}"));

                break;

            case TradeStatus.Canceled:
                await _pushManager.TriggerPush(PushMessage.FromMessage($"Got cancel signal for {Market}"));

                break;
            }

            return(Unit.Default);
        }