Beispiel #1
0
        public bool CheckConditions(IEnumerable <IRuleCondition> conditions, Dictionary <string, ISignal> signals, double?globalRating, string pair, ITradingPair tradingPair)
        {
            foreach (var condition in conditions)
            {
                ISignal signal = null;
                if (condition.Signal != null && signals.TryGetValue(condition.Signal, out ISignal s))
                {
                    signal = s;
                }

                if (condition.MinPrice != null && (tradingService.GetCurrentPrice(pair) < condition.MinPrice) ||
                    condition.MaxPrice != null && (tradingService.GetCurrentPrice(pair) > condition.MaxPrice) ||
                    condition.MinSpread != null && (tradingService.GetCurrentSpread(pair) < condition.MinSpread) ||
                    condition.MaxSpread != null && (tradingService.GetCurrentSpread(pair) > condition.MaxSpread) ||

                    condition.MinVolume != null && (signal == null || signal.Volume == null || signal.Volume < condition.MinVolume) ||
                    condition.MaxVolume != null && (signal == null || signal.Volume == null || signal.Volume > condition.MaxVolume) ||
                    condition.MinVolumeChange != null && (signal == null || signal.VolumeChange == null || signal.VolumeChange < condition.MinVolumeChange) ||
                    condition.MaxVolumeChange != null && (signal == null || signal.VolumeChange == null || signal.VolumeChange > condition.MaxVolumeChange) ||
                    condition.MinPriceChange != null && (signal == null || signal.PriceChange == null || signal.PriceChange < condition.MinPriceChange) ||
                    condition.MaxPriceChange != null && (signal == null || signal.PriceChange == null || signal.PriceChange > condition.MaxPriceChange) ||
                    condition.MinRating != null && (signal == null || signal.Rating == null || signal.Rating < condition.MinRating) ||
                    condition.MaxRating != null && (signal == null || signal.Rating == null || signal.Rating > condition.MaxRating) ||
                    condition.MinRatingChange != null && (signal == null || signal.RatingChange == null || signal.RatingChange < condition.MinRatingChange) ||
                    condition.MaxRatingChange != null && (signal == null || signal.RatingChange == null || signal.RatingChange > condition.MaxRatingChange) ||
                    condition.MinVolatility != null && (signal == null || signal.Volatility == null || signal.Volatility < condition.MinVolatility) ||
                    condition.MaxVolatility != null && (signal == null || signal.Volatility == null || signal.Volatility > condition.MaxVolatility) ||
                    condition.MinGlobalRating != null && (globalRating == null || globalRating < condition.MinGlobalRating) ||
                    condition.MaxGlobalRating != null && (globalRating == null || globalRating > condition.MaxGlobalRating) ||
                    condition.Pairs != null && (pair == null || !condition.Pairs.Contains(pair)) ||

                    condition.MinAge != null && (tradingPair == null || tradingPair.CurrentAge < condition.MinAge / Application.Speed) ||
                    condition.MaxAge != null && (tradingPair == null || tradingPair.CurrentAge > condition.MaxAge / Application.Speed) ||
                    condition.MinLastBuyAge != null && (tradingPair == null || tradingPair.LastBuyAge < condition.MinLastBuyAge / Application.Speed) ||
                    condition.MaxLastBuyAge != null && (tradingPair == null || tradingPair.LastBuyAge > condition.MaxLastBuyAge / Application.Speed) ||
                    condition.MinMargin != null && (tradingPair == null || tradingPair.CurrentMargin < condition.MinMargin) ||
                    condition.MaxMargin != null && (tradingPair == null || tradingPair.CurrentMargin > condition.MaxMargin) ||
                    condition.MinMarginChange != null && (tradingPair == null || tradingPair.Metadata.LastBuyMargin == null || (tradingPair.CurrentMargin - tradingPair.Metadata.LastBuyMargin) < condition.MinMarginChange) ||
                    condition.MaxMarginChange != null && (tradingPair == null || tradingPair.Metadata.LastBuyMargin == null || (tradingPair.CurrentMargin - tradingPair.Metadata.LastBuyMargin) > condition.MaxMarginChange) ||
                    condition.MinAmount != null && (tradingPair == null || tradingPair.TotalAmount < condition.MinAmount) ||
                    condition.MaxAmount != null && (tradingPair == null || tradingPair.TotalAmount > condition.MaxAmount) ||
                    condition.MinCost != null && (tradingPair == null || tradingPair.CurrentCost < condition.MinCost) ||
                    condition.MaxCost != null && (tradingPair == null || tradingPair.CurrentCost > condition.MaxCost) ||
                    condition.MinDCALevel != null && (tradingPair == null || tradingPair.DCALevel < condition.MinDCALevel) ||
                    condition.MaxDCALevel != null && (tradingPair == null || tradingPair.DCALevel > condition.MaxDCALevel) ||
                    condition.SignalRules != null && (tradingPair == null || tradingPair.Metadata.SignalRule == null || !condition.SignalRules.Contains(tradingPair.Metadata.SignalRule)))
                {
                    return(false);
                }
            }
            return(true);
        }
Beispiel #2
0
        public virtual void AddBuyOrder(IOrderDetails order)
        {
            lock (SyncRoot)
            {
                if (order.Side == OrderSide.Buy && (order.Result == OrderResult.Filled || order.Result == OrderResult.FilledPartially))
                {
                    decimal balanceDifference  = -order.AverageCost;
                    decimal feesPairCurrency   = 0;
                    decimal feesMarketCurrency = 0;
                    decimal amountAfterFees    = order.AmountFilled;

                    if (order.Fees != 0 && order.FeesCurrency != null)
                    {
                        if (order.FeesCurrency == tradingService.Config.Market)
                        {
                            feesMarketCurrency = order.Fees;
                            balanceDifference -= order.Fees;
                        }
                        else
                        {
                            string feesPair = order.FeesCurrency + tradingService.Config.Market;
                            if (feesPair == order.Pair)
                            {
                                feesPairCurrency = order.Fees;
                                amountAfterFees -= order.Fees;
                            }
                            else
                            {
                                feesMarketCurrency = tradingService.GetCurrentPrice(feesPair) * order.Fees;
                            }
                        }
                    }
                    balance += balanceDifference;

                    if (tradingPairs.TryGetValue(order.Pair, out TradingPair tradingPair))
                    {
                        if (!tradingPair.OrderIds.Contains(order.OrderId))
                        {
                            tradingPair.OrderIds.Add(order.OrderId);
                            tradingPair.OrderDates.Add(order.Date);
                        }
                        tradingPair.AveragePricePaid    = (tradingPair.AverageCostPaid + order.AverageCost) / (tradingPair.TotalAmount + order.AmountFilled);
                        tradingPair.FeesPairCurrency   += feesPairCurrency;
                        tradingPair.FeesMarketCurrency += feesMarketCurrency;
                        tradingPair.TotalAmount        += amountAfterFees;
                        tradingPair.Metadata            = tradingPair.Metadata.MergeWith(order.Metadata);
                    }
                    else
                    {
                        tradingPair = new TradingPair
                        {
                            Pair     = order.Pair,
                            OrderIds = new List <string> {
                                order.OrderId
                            },
                            OrderDates = new List <DateTimeOffset> {
                                order.Date
                            },
                            AveragePricePaid   = order.AveragePrice,
                            FeesPairCurrency   = feesPairCurrency,
                            FeesMarketCurrency = feesMarketCurrency,
                            TotalAmount        = amountAfterFees,
                            Metadata           = order.Metadata
                        };
                        tradingPairs.TryAdd(order.Pair, tradingPair);
                        tradingPair.SetCurrentValues(tradingService.GetCurrentPrice(tradingPair.Pair), tradingService.GetCurrentSpread(tradingPair.Pair));
                        tradingPair.Metadata.CurrentRating = tradingPair.Metadata.Signals != null?signalsService.GetRating(tradingPair.Pair, tradingPair.Metadata.Signals) : null;

                        tradingPair.Metadata.CurrentGlobalRating = signalsService.GetGlobalRating();
                    }
                }
            }
        }