Exemplo n.º 1
0
        public async Task OnMarketPriceChangeAsync(PriceChangeEvent change)
        {
            // Ignore non crypto trade pairs!
            if (!change.ExchangeTradePair.TradePair.FromCurrency.IsCrypto || !change.ExchangeTradePair.TradePair.ToCurrency.IsCrypto)
            {
                return;
            }

            // Get trade pair spread.
            ArbTradePairSpread tpSpread = this.spreadPerTradePair.GetOrAdd(change.ExchangeTradePair.TradePair, new ArbTradePairSpread(change.ExchangeTradePair.TradePair));

            // Update price.
            tpSpread.UpdatePrice(change.ExchangeTradePair, change.Price, change.UtcTime);


            // Calculate the margin
            var margin = CalculateOpportunityMargin(tpSpread.MinimumValuedPair, tpSpread.MaximumValuedPair);

            // If margin is zero, then return.
            if (margin == 0m)
            {
                return;
            }

            // Get or add the opportunity object.
#warning Not sure how to define the currency. This is somehow the base currency.
            var opportunity = opportunities.GetOrAdd(GetOpportunityKey(tpSpread.MinimumValuedPair, tpSpread.MaximumValuedPair), new ArbOpportunity(tpSpread.MinimumValuedPair, tpSpread.MaximumValuedPair, tpSpread.MinimumValuedPair.ExchangeTradePair.TradePair.ToCurrency));

            await opportunity.UpdateOpportunity(margin);

            //
            Console.WriteLine($"Trade Opportunity of {margin:0.00}% for {tpSpread.MinimumValuedPair.ExchangeTradePair.TradePair} with Price on {tpSpread.MinimumValuedPair.ExchangeTradePair.Exchange} of {tpSpread.MinimumValuedPair.Price}/ Age:{tpSpread.MinimumValuedPair.AgeMilliseconds}ms and {tpSpread.MaximumValuedPair.ExchangeTradePair.Exchange} of {tpSpread.MaximumValuedPair.Price} / Age:{tpSpread.MaximumValuedPair.AgeMilliseconds}ms ");
        }
Exemplo n.º 2
0
        // Callback when gdax send information.
        private async Task ParseResult(string result)
        {
            try
            {
                // We just want matches. Discard everything else.
                if (result.Contains("match"))
                {
                    var jsonObj = JsonConvert.DeserializeObject <GDAXTradeMatchJson>(result);



                    PriceChangeEvent priceChange = new PriceChangeEvent()
                    {
                        Price             = jsonObj.price,
                        UtcTime           = DateTime.Parse(jsonObj.time).ToUniversalTime(),
                        ExchangeTradePair = this.GetExchangeTradePair(this.tradingPairs.Values.First().Exchange.Name, jsonObj.product_id.Split('-')[0], jsonObj.product_id.Split('-')[1])
                    };

                    // Notify the observers!
                    await this.NotifyObserverOfPriceChange(priceChange);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Exemplo n.º 3
0
 protected async Task NotifyObserverOfPriceChange(PriceChangeEvent priceEvent)
 {
     // Loop through the observers and signal change.
     foreach (var observer in this.PriceObservers)
     {
         await observer.OnMarketPriceChangeAsync(priceEvent);
     }
 }
Exemplo n.º 4
0
        public decimal UpdatePriceAndGetValue(PriceChangeEvent e)
        {
            // Update edges;
            this.Edges.Where(x => x.CurrentPrice.ExchangeTradePair == e.ExchangeTradePair).ToList().ForEach(x =>
            {
                x.CurrentPrice.Price             = e.Price;
                x.CurrentPrice.UtcLastUpdateTime = e.UtcTime;
            });

            return(this.GetValue());
        }
Exemplo n.º 5
0
        public static List <ProductVm> UpdatePrice(this List <ProductVm> rows, PriceChangeEvent updateEvent, float newPrice)
        {
            var rowToUpdate = rows.FirstOrDefault(_ => _.Id == updateEvent.ProductId);

            if (rowToUpdate != null)
            {
                rowToUpdate.Price    = newPrice;
                rowToUpdate.Quantity = updateEvent.NewQuantity;
            }
            return(rows);
        }
Exemplo n.º 6
0
        public async Task OnMarketPriceChangeAsync(PriceChangeEvent change)
        {
            // First time we receive a price, we generate
            ExchangeTriangulation[] triangulations = this.tradePairCycles.GetOrAdd(change.ExchangeTradePair.Exchange, GetTriangulationsForExchange(change.ExchangeTradePair.Exchange));

            //
            foreach (var tria in triangulations)
            {
                var opporunity = (tria.UpdatePriceAndGetValue(change) - 1m) * 100m;
                if (opporunity > 0m)
                {
                    Console.WriteLine($"Found a triangulation opportunity on {change.ExchangeTradePair.Exchange} of {opporunity:0.00%} for cycle: {tria}");
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// On market price change this is called to persist the information to db.
        /// </summary>
        /// <param name="change"></param>
        public async Task OnMarketPriceChangeAsync(PriceChangeEvent change)
        {
            // Save to database.
            using (var ctx = new RBBotContext())
            {
                ctx.MarketPrices.Add(new Models.MarketPrice()
                {
                    ExchangeTradePairId = change.ExchangeTradePair.Id,
                    Price     = change.Price,
                    Timestamp = change.UtcTime
                });

                // Write to console.
                //Console.WriteLine($"Price Change on {change.ExchangeTradePair.Exchange.Name} for {change.ExchangeTradePair.TradePair.FromCurrency.Code} - {change.ExchangeTradePair.TradePair.ToCurrency.Code}. New Price: {change.Price}");

                await ctx.SaveChangesAsync();
            }
        }
Exemplo n.º 8
0
 protected virtual void OnPriceChanged(EventArgs e)
 {
     PriceChangeEvent?.Invoke(this, e);
 }
Exemplo n.º 9
0
 public void PublishPriceChange(string symbol, decimal newPrice)
 {
     PriceChangeEvent?.Invoke(this, new PriceChangeEventArgs(symbol, newPrice));
 }
Exemplo n.º 10
0
        public static List <LeaderboardRowModel> UpdatePrice(this List <LeaderboardRowModel> rows, PriceChangeEvent updateEvent, float newPrice)
        {
            var rowToUpdate = rows.FirstOrDefault(_ => _.ProductId == updateEvent.ProductId);

            if (rowToUpdate != null)
            {
                rowToUpdate.Price = newPrice;
            }
            return(rows);
        }
Exemplo n.º 11
0
 public async Task SendPriceChanged(PriceChangeEvent priceChangeEvent)
 {
     await Clients.All.SendAsync(SendMethods.PriceChanged.ToString(), priceChangeEvent);
 }