Beispiel #1
0
        /// <summary>
        /// Clear fields and gets the fee for the selected pair
        /// </summary>
        private void OnPairChanged(PairSelected m)
        {
            if (m.SelectorType != SelectorType.Main)
            {
                return;
            }
            try
            {
                _selectedPair        = m.Pair;
                lblItem1Balance.Text = lblItem2Balance.Text = string.Empty;
                txtBuyAmount.Text    = txtBuyPrice.Text = txtSellAmount.Text = txtSellPrice.Text = string.Empty;
                var feeRes = _proxy.GetFee(m.Pair);
                lblCurrency1.Text          = lblCurrency2.Text = m.Pair.Item2;
                lblSellAmountCurrency.Text = lblBuyAmountCurrency.Text = m.Pair.Item1;

                _fee = feeRes.Success ? feeRes.Result : new Fee {
                    BuyFee = 0.5m, SellFee = 0.5m
                };
                lblBuyFee.Text   = lblSellFee.Text = "0";
                lblBuyTotal.Text = lblSellTotal.Text = "0";
                UpdateBalance(false);
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                ErrorHelper.DisplayErrorMessage(ex.ToString());
            }
        }
Beispiel #2
0
        /// <summary>
        /// Method called on each new candle
        /// </summary>
        /// <param name="candles">The list of available candles</param>
        public void OnTick(IList <OHLC> candles)
        {
            if (candles.Count == 0)
            {
                return;
            }

            var candle = candles.Last();
            var balRes = _proxy.GetBalance(_settings.Pair);

            if (!balRes.Success)
            {
                return;
            }
            var bal    = balRes.Result;
            var feeRes = _proxy.GetFee(_pair);
            var fee    = feeRes.Result;

            decimal totalAmountItem2 = bal.Balances[_pair.Item2];
            decimal totalAmountItem1 = bal.Balances[_pair.Item1];

            decimal availableAmountItem2ToBuy   = totalAmountItem2 - _settings.MinAmountItem2ToKeep;
            decimal availableAmountItem1ForSell = totalAmountItem1 - _settings.MinAmountItem1ToKeep;

            var ordersRes = _proxy.GetOpenOrders(_settings.Pair);

            if (!ordersRes.Success)
            {
                return;
            }

            bool lastOrderFilled = _lastOrder == null || ordersRes.Result.List.FirstOrDefault(o => o.Id == _lastOrder.Id) == null;

            // return;

            var advice = _tradingIndicator.GetAdvice(candles);

            if (advice != Advice.None)
            {
                System.Diagnostics.Debug.WriteLine(DateTime.Now.ToShortTimeString() + " - Advice: " + advice.ToString());
                if (advice == Advice.Buy)
                {
                    if (_lastAdvice == Advice.Sell)
                    {
                        if (!lastOrderFilled)
                        {
                            var res = _proxy.CancelOrder(_lastOrder.Id);
                            if (res.Success)
                            {
                                availableAmountItem2ToBuy += _lastOrder.Amount * _lastOrder.Price;
                            }
                        }
                    }
                    else if (_lastAdvice == Advice.Buy)
                    {
                    }

                    availableAmountItem2ToBuy = availableAmountItem2ToBuy > _settings.MaxAmountMoneyPerBuyOrder ? _settings.MaxAmountMoneyPerBuyOrder : availableAmountItem2ToBuy;
                    decimal nbUnitsToBuy = Math.Round(availableAmountItem2ToBuy / candle.Close, 8);
                    //TODO: handle fees
                    PlaceBuyOrder(nbUnitsToBuy, candle.Close, candle.Date);
                }
                else if (advice == Advice.Sell)
                {
                    if (_lastAdvice == Advice.Buy)
                    {
                        if (!lastOrderFilled)
                        {
                            var res = _proxy.CancelOrder(_lastOrder.Id);
                            if (res.Success)
                            {
                                availableAmountItem1ForSell += _lastOrder.Amount;
                            }
                        }
                    }
                    else if (_lastAdvice == Advice.Sell)
                    {
                    }
                    decimal transactionFee = Math.Round(availableAmountItem1ForSell * candle.Close * fee.SellFee / 100m, 8);

                    decimal nbUnitsToSell = availableAmountItem1ForSell > _settings.MaxNbBTCPerSellOrder ? _settings.MaxNbBTCPerSellOrder : availableAmountItem1ForSell;
                    //TODO: handle fees
                    //Sell here
                    PlaceSellOrder(nbUnitsToSell, candle.Close, candle.Date);
                }
            }
            _lastAdvice = advice;
        }