public Wallet ComputeNewOrders(Wallet currentOrders, MarketInfo objMarket, ExchangeInfo objExchange, TradingHistory history)
        {
            //the newOrders Wallet variable will contain all ask/bid/cancel orders to issue

            var newOrders = new Wallet();

            //start with reserved resource
            //Dim askReserve As Decimal = Math.Max(Me._AskReserveAmount, currentOrders.btcs * (Me._AskReserveRate / 100))
            //Dim bidReserve As Decimal = Math.Max(Me._BidReserveValue, currentOrders.usds * (Me._BidReserveRate / 100))

            decimal avBtcsForTrading = currentOrders.PrimaryBalance;
            //- askReserve
            decimal avUsdsForTrading = currentOrders.SecondaryBalance;

            //- bidReserve

            //Then We simplify the current orders by merging orders of the same price, issueing corresponding cancel/new orders
            newOrders.ConsolidateOrders(ref currentOrders, true);

            //Feed the new orders wallet with available resources, Reserve resources for current open orders
            newOrders.PrimaryBalance   = Math.Max(avBtcsForTrading - currentOrders.GetTotalAsksPrimary(), 0);
            newOrders.SecondaryBalance = Math.Max(avUsdsForTrading - currentOrders.GetTotalBidsSecondary(), 0);

            var tContext = new TradingContext(currentOrders, newOrders, objMarket, objExchange, history.GetLastTrend(), this);

            this.ComputeNewOrders(ref tContext);


            tContext.NewOrders.FitOrders(objExchange);
            //we update the trading history with last data
            history.Update(currentOrders, objMarket, tContext.NewOrders);
            return(tContext.NewOrders);
        }
Esempio n. 2
0
        public void ExecuteOrders(MarketInfo objMarket, ref Wallet targetWallet, ref TradingHistory history)
        {
            var trades        = new List <Trade>();
            var fees          = new List <Payment>();
            var matchedOrders = this.MatchOrders(ref targetWallet, objMarket.Ticker.Last);

            foreach (var matchedOrder in matchedOrders)
            {
                Trade trade = new Trade()
                {
                    Time   = objMarket.Time,
                    Price  = matchedOrder.Price,
                    Amount = matchedOrder.Amount
                };
                Payment fee = new Payment()
                {
                    Time  = objMarket.Time,
                    Label = matchedOrder.FriendlyId
                };
                if (matchedOrder.OrderType == OrderType.Buy)
                {
                    trade.TradeType = TradeType.Buy;
                    fee.Currency    = objMarket.PrimaryCode;
                    fee.Amount      = matchedOrder.Amount * BidCommission / 100m;
                    fee.Label       = string.Format("{0} - Bid Fee: {1} % = {2} {3}"
                                                    , fee.Label
                                                    , BidCommission.ToString(CultureInfo.InvariantCulture)
                                                    , fee.Amount
                                                    , fee.Currency);
                    targetWallet.SecondaryBalance = targetWallet.SecondaryBalance - matchedOrder.Value;
                    targetWallet.PrimaryBalance   = targetWallet.PrimaryBalance + matchedOrder.Amount - fee.Amount;
                }
                else if (matchedOrder.OrderType == OrderType.Sell)
                {
                    trade.TradeType = TradeType.Sell;
                    fee.Currency    = objMarket.SecondaryCode;
                    fee.Amount      = matchedOrder.Value * AskCommission / 100m;
                    fee.Label       = string.Format("{0} - Ask Fee: {1} % = {2} {3}"
                                                    , fee.Label
                                                    , AskCommission.ToString(CultureInfo.InvariantCulture)
                                                    , fee.Amount
                                                    , fee.Currency);
                    targetWallet.SecondaryBalance = targetWallet.SecondaryBalance + matchedOrder.Value - fee.Amount;
                    targetWallet.PrimaryBalance   = targetWallet.PrimaryBalance - matchedOrder.Amount;
                }
                trades.Add(trade);
                fees.Add(fee);
                targetWallet.Orders.Remove(matchedOrder);
            }

            targetWallet.Time = objMarket.Time;
            history.Update(targetWallet, objMarket, trades, fees);
        }
        public Wallet ComputeNewOrders(Wallet currentOrders, MarketInfo objMarket, ExchangeInfo objExchange, TradingHistory history)
        {
            var newOrders  = new Wallet();
            var askReserve = Math.Max(this.AskReserveAmount, currentOrders.PrimaryBalance * AskReserveRate / 100m);
            var bidReserve = Math.Max(this.BidReserveValue, currentOrders.SecondaryBalance * BidReserveRate / 100m);

            decimal avBtcsForTrading = currentOrders.PrimaryBalance - askReserve;
            decimal avUsdsForTrading = currentOrders.SecondaryBalance - bidReserve;

            //todo: should we update the original wallet?
            newOrders.ConsolidateOrders(ref currentOrders, true);

            newOrders.PrimaryBalance   = Math.Max(avBtcsForTrading - currentOrders.GetTotalAsksPrimary(), decimal.Zero);
            newOrders.SecondaryBalance = Math.Max(decimal.Subtract(avUsdsForTrading, currentOrders.GetTotalBidsSecondary()), decimal.Zero);

            var tContext = new TradingContext(currentOrders, newOrders, objMarket, objExchange, history.GetLastTrend(), this);

            this.ComputeNewOrders(ref tContext);
            if (this.NoAsks)
            {
                tContext.NewOrders.ClearAsks();
                if (this.ClearAsks)
                {
                    tContext.NewOrders.CancelExistingOrders(tContext.CurrentOrders.OrderedAsks.ToArray());
                }
            }
            if (this.NoBids)
            {
                tContext.NewOrders.ClearBids();
                if (this.ClearBids)
                {
                    tContext.NewOrders.CancelExistingOrders(tContext.CurrentOrders.OrderedBids.ToArray());
                }
            }
            tContext.NewOrders.FitOrders(objExchange);
            history.Update(currentOrders, objMarket, tContext.NewOrders);
            return(tContext.NewOrders);
        }