Ejemplo n.º 1
0
        public async void doBuy(Asset asset)
        {
            try
            {
                if (this.isLiveTrading)
                {
                    decimal cashAvail = 0;
                    // check to see if we have enough cash.
                    if (myWallet.ContainsKey(QUOTECURRENCY))
                    {
                        cashAvail = myWallet[QUOTECURRENCY];
                    }
                    if (cashAvail < balanceLowWaterMark)
                    {
                        throw new Exception($"Insufficient funds to buy {asset.Ticker}.  Only have {cashAvail}");
                    }

                    // get the number of shares to buy.
                    var shares = RoundShares.GetRoundedShares(stakeSize, asset.Price);
                    var order  = new ExchangeOrderRequest
                    {
                        Amount       = shares,
                        IsBuy        = true,
                        Price        = asset.Ask,
                        MarketSymbol = asset.Ticker
                    };

                    var result = await api.PlaceOrderAsync(order);

                    logger.Trace($"BUY: PlaceOrderAsync. {result.MarketSymbol} ${result.Price}.  {result.Result}. {result.OrderId}");

                    // manually reduce the walletsize.  it will be for real once every 15 minutes.
                    myWallet[QUOTECURRENCY] = cashAvail - stakeSize;
                }

                asset.HasTrade    = true;
                asset.BuyPrice    = asset.Ask;
                asset.StopLoss    = 0.99m * asset.BuyPrice;
                asset.LastBuyTime = DateTime.UtcNow;

                logger.Info($"Buy, {asset.Ticker}, {asset.BuyPrice}, {asset.percentagePriceChange}");
            }
            catch (Exception ex)
            {
                logger.Info($"Buy - ERROR, {asset.Ticker}");
                logger.Trace($"Buy {asset.Ticker} failed with {ex.Message}");
            }
        }
Ejemplo n.º 2
0
        public async void doBuy(Asset asset)
        {
            try
            {
                bool isSuccess = false;
                if (this.isLiveTrading)
                {
                    decimal cashAvail = 0;
                    // check to see if we have enough cash.
                    if (myWallet.ContainsKey(QUOTECURRENCY))
                    {
                        cashAvail = myWallet[QUOTECURRENCY];
                    }

                    var coinsHeld = this.Assets.Where(a => a.HasTrade).Count();

                    if (coinsHeld >= maxCoins)
                    {
                        throw new Exception($"Insufficient funds to buy {asset.Ticker}.  CoinsHeld exceeded. {coinsHeld}");
                    }

                    // get the number of shares to buy.
                    var shares = RoundShares.GetRoundedShares(stakeSize, asset.Price);
                    if (shares < 0.01m)
                    {
                        throw new Exception($"${shares} is too small for {asset.Ticker}.");
                    }

                    var order = new ExchangeOrderRequest
                    {
                        Amount       = shares,
                        IsBuy        = true,
                        Price        = asset.Ask,
                        MarketSymbol = asset.Ticker,
                        OrderType    = OrderType.Market
                    };

                    var result = await api.PlaceOrderAsync(order);

                    logger.Trace($"TRY BUY, {result.MarketSymbol}, {result.Price}, {result.Result}, {result.OrderId}");

                    if (result.Result != ExchangeAPIOrderResult.Error)
                    {
                        isSuccess = true;
                        // manually reduce the walletsize.  it will be for real once every 15 minutes.
                        myWallet[QUOTECURRENCY] = cashAvail - stakeSize;
                    }
                }

                if (isSuccess || !isLiveTrading)
                {
                    asset.HasTrade    = true;
                    asset.BuyPrice    = asset.Ask;
                    asset.MaxPrice    = asset.BuyPrice;
                    asset.LastBuyTime = DateTime.UtcNow;
                    asset.StopLoss    = 0;   // disable stoploss.

                    logger.Info($"Buy, {asset.Ticker}, BuyPrice: {asset.BuyPrice}, StopLoss: {asset.StopLoss}");
                }
            }
            catch (Exception ex)
            {
                logger.Info($"Buy - ERROR, {asset.Ticker}");
                logger.Trace($"Buy {asset.Ticker} failed with {ex.Message}");
            }
        }