public static async Task Returns_Correct_Movement(decimal price1, decimal price2, PriceMovement expected)
        {
            var subject = new ReplaySubject<IPrice>();

            var result = subject.ToPriceMovementStream();
            subject.OnNext(new Price { Mid = price1 });
            subject.OnNext(new Price { Mid = price2 });
            subject.OnCompleted();

            // Assert
            await result.SingleAsync(movement => movement == expected);
        }
示例#2
0
        public static NSAttributedString ToAttributedArrow(this PriceMovement priceMovement, IPrice price)
        {
            if (price == null)
            {
                return(new NSAttributedString(string.Empty));
            }

            var movementText = new Dictionary <PriceMovement, NSAttributedString>
            {
                { PriceMovement.Up, new NSAttributedString("▲", _arrowFont, UIColor.Green) },
                { PriceMovement.Down, new NSAttributedString("▼", _arrowFont, UIColor.Red) },
                { PriceMovement.None, new NSAttributedString("▼", _arrowFont, UIColor.Clear) }
            };

            return(movementText[priceMovement]);
        }
示例#3
0
        private Color IndicatorColor(PriceMovement priceMovement)
        {
            Color returnColor;

            switch (priceMovement)
            {
            case (PriceMovement.Up):
                returnColor = Color.Red;
                break;

            case (PriceMovement.Down):
                returnColor = Color.LightGreen;
                break;

            default:
                returnColor = Color.Orange;
                break;
            }
            return(returnColor);
        }
示例#4
0
        public static NSAttributedString ToAttributedString(this PriceMovement priceMovement, IPrice price)
        {
            if (price == null)
            {
                return(new NSAttributedString(string.Empty));
            }

            var movementText = new Dictionary <PriceMovement, NSAttributedString>
            {
                { PriceMovement.Up, new NSAttributedString("▲", _arrowFont, UIColor.Green) },
                { PriceMovement.Down, new NSAttributedString("▼", _arrowFont, UIColor.Red) },
                { PriceMovement.None, new NSAttributedString("▼", _arrowFont, UIColor.Clear) }
            };


            var spread = new NSAttributedString(price.Spread.ToString("0.0") + "   ");
            var text   = new NSMutableAttributedString(spread);

            text.Append(movementText[priceMovement]);
            return(text);
        }
示例#5
0
        public void Run(Position initialPosition, Position finalPosition, FeeType feeType, PriceMovement priceMovement, int leverage)
        {
            //Console.WriteLine("----------");
            //Console.WriteLine("PARAMETERS");
            //Console.WriteLine("Initial position: " + initialPosition);
            //Console.WriteLine("Final position: " + finalPosition);
            //Console.WriteLine("Fee type: " + feeType);
            //Console.WriteLine("Price movement: " + priceMovement);
            //Console.WriteLine("Leverage: " + leverage);
            //Console.WriteLine("----------");
            //Console.WriteLine();

            var algorithm = new QCAlgorithm();

            var security = algorithm.AddSecurity(_symbol.ID.SecurityType, _symbol.ID.Symbol);

            security.FeeModel = _feeModels[feeType];
            security.SetLeverage(leverage);

            algorithm.SetCash(Cash);

            Update(security, BasePrice);

            decimal        targetPercentage;
            OrderDirection orderDirection;
            MarketOrder    order;
            decimal        orderFee;
            OrderEvent     fill;
            int            orderQuantity;
            decimal        freeMargin;
            decimal        requiredMargin;

            if (initialPosition != Position.Zero)
            {
                targetPercentage = (decimal)initialPosition;
                orderDirection   = initialPosition == Position.Long ? OrderDirection.Buy : OrderDirection.Sell;
                orderQuantity    = algorithm.CalculateOrderQuantity(_symbol, targetPercentage);
                order            = new MarketOrder(_symbol, orderQuantity, DateTime.UtcNow);
                freeMargin       = algorithm.Portfolio.GetMarginRemaining(_symbol, orderDirection);
                requiredMargin   = security.MarginModel.GetInitialMarginRequiredForOrder(security, order);

                //Console.WriteLine("Current price: " + security.Price);
                //Console.WriteLine("Target percentage: " + targetPercentage);
                //Console.WriteLine("Order direction: " + orderDirection);
                //Console.WriteLine("Order quantity: " + orderQuantity);
                //Console.WriteLine("Free margin: " + freeMargin);
                //Console.WriteLine("Required margin: " + requiredMargin);
                //Console.WriteLine();

                Assert.That(Math.Abs(requiredMargin) <= freeMargin);

                orderFee = security.FeeModel.GetOrderFee(security, order);
                fill     = new OrderEvent(order, DateTime.UtcNow, orderFee)
                {
                    FillPrice = security.Price, FillQuantity = orderQuantity
                };
                algorithm.Portfolio.ProcessFill(fill);

                //Console.WriteLine("Portfolio.Cash: " + algorithm.Portfolio.Cash);
                //Console.WriteLine("Portfolio.TotalPortfolioValue: " + algorithm.Portfolio.TotalPortfolioValue);
                //Console.WriteLine();

                if (priceMovement == PriceMovement.RisingSmall)
                {
                    Update(security, HighPrice);
                }
                else if (priceMovement == PriceMovement.FallingSmall)
                {
                    Update(security, LowPrice);
                }
                else if (priceMovement == PriceMovement.RisingLarge)
                {
                    Update(security, VeryHighPrice);
                }
                else if (priceMovement == PriceMovement.FallingLarge)
                {
                    Update(security, VeryLowPrice);
                }
            }

            targetPercentage = (decimal)finalPosition;
            orderDirection   = finalPosition == Position.Long || (finalPosition == Position.Zero && initialPosition == Position.Short) ? OrderDirection.Buy : OrderDirection.Sell;
            orderQuantity    = algorithm.CalculateOrderQuantity(_symbol, targetPercentage);
            order            = new MarketOrder(_symbol, orderQuantity, DateTime.UtcNow);
            freeMargin       = algorithm.Portfolio.GetMarginRemaining(_symbol, orderDirection);
            requiredMargin   = security.MarginModel.GetInitialMarginRequiredForOrder(security, order);

            //Console.WriteLine("Current price: " + security.Price);
            //Console.WriteLine("Target percentage: " + targetPercentage);
            //Console.WriteLine("Order direction: " + orderDirection);
            //Console.WriteLine("Order quantity: " + orderQuantity);
            //Console.WriteLine("Free margin: " + freeMargin);
            //Console.WriteLine("Required margin: " + requiredMargin);
            //Console.WriteLine();

            Assert.That(Math.Abs(requiredMargin) <= freeMargin);

            orderFee = security.FeeModel.GetOrderFee(security, order);
            fill     = new OrderEvent(order, DateTime.UtcNow, orderFee)
            {
                FillPrice = security.Price, FillQuantity = orderQuantity
            };
            algorithm.Portfolio.ProcessFill(fill);

            //Console.WriteLine("Portfolio.Cash: " + algorithm.Portfolio.Cash);
            //Console.WriteLine("Portfolio.TotalPortfolioValue: " + algorithm.Portfolio.TotalPortfolioValue);
            //Console.WriteLine();
        }
        public static async Task Returns_Correct_Movement(decimal price1, decimal price2, PriceMovement expected)
        {
            var subject = new ReplaySubject <IPrice>();

            var result = subject.ToPriceMovementStream();

            subject.OnNext(new Price {
                Mid = price1
            });
            subject.OnNext(new Price {
                Mid = price2
            });
            subject.OnCompleted();

            // Assert
            await result.SingleAsync(movement => movement == expected);
        }
示例#7
0
        void OnPrice(IPrice currentPrice)
        {
            if (!currentPrice.IsStale)
            {
                // TODO: Cover all statuses (Done, Executing)...
                if (this.Status == PriceTileStatus.DoneStale)
                {
                    this.Status = PriceTileStatus.Done;
                }

                if (this.Status == PriceTileStatus.Stale)
                {
                    this.Status = PriceTileStatus.Streaming;
                }

                var bid = PriceFormatter.GetFormattedPrice(currentPrice.Bid.Rate, currentPrice.CurrencyPair.RatePrecision, currentPrice.CurrencyPair.PipsPosition);
                var ask = PriceFormatter.GetFormattedPrice(currentPrice.Ask.Rate, currentPrice.CurrencyPair.RatePrecision, currentPrice.CurrencyPair.PipsPosition);

                LeftSideNumber    = bid.BigFigures;
                LeftSideBigNumber = bid.Pips;
                LeftSidePips      = bid.TenthOfPip;

                RightSideNumber    = ask.BigFigures;
                RightSideBigNumber = ask.Pips;
                RightSidePips      = ask.TenthOfPip;

                Spread = currentPrice.Spread.ToString("0.0");

                if (_lastPrice != null && !_lastPrice.IsStale)
                {
                    if (_lastPrice.Mid < currentPrice.Mid)
                    {
                        Movement = PriceMovement.Up;
                    }
                    else if (_lastPrice.Mid > currentPrice.Mid)
                    {
                        Movement = PriceMovement.Down;
                    }
                    else
                    {
                        Movement = PriceMovement.None;
                    }
                }
                else
                {
                    Movement = PriceMovement.None;
                }
                this.NotifyOnChanged(this);
            }
            else
            {
                // Stale!
                Movement = PriceMovement.None;

                if (this.Status == PriceTileStatus.Done)
                {
                    this.Status = PriceTileStatus.DoneStale;
                    this.NotifyOnChanged(this);
                }

                // TODO: Cover all statuses (Done, Executing)...
                if (this.Status == PriceTileStatus.Streaming)
                {
                    this.Status = PriceTileStatus.Stale;
                    this.NotifyOnChanged(this);
                }
            }
            _lastPrice = currentPrice;
        }
        public void Run(object[] parameters)
        {
            Position      initialPosition = (Position)parameters[0];
            Position      finalPosition   = (Position)parameters[1];
            FeeType       feeType         = (FeeType)parameters[2];
            PriceMovement priceMovement   = (PriceMovement)parameters[3];
            int           leverage        = (int)parameters[4];

            //Console.WriteLine("----------");
            //Console.WriteLine("PARAMETERS");
            //Console.WriteLine("Initial position: " + initialPosition);
            //Console.WriteLine("Final position: " + finalPosition);
            //Console.WriteLine("Fee type: " + feeType);
            //Console.WriteLine("Price movement: " + priceMovement);
            //Console.WriteLine("Leverage: " + leverage);
            //Console.WriteLine("----------");
            //Console.WriteLine();

            var algorithm = new QCAlgorithm();

            algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(algorithm));

            var security = algorithm.AddSecurity(_symbol.ID.SecurityType, _symbol.ID.Symbol);

            security.FeeModel = _feeModels[feeType];
            security.SetLeverage(leverage);

            var buyingPowerModel = new TestSecurityMarginModel(leverage);

            security.BuyingPowerModel = buyingPowerModel;

            algorithm.SetCash(Cash);

            Update(security, BasePrice);

            decimal        targetPercentage;
            OrderDirection orderDirection;
            MarketOrder    order;
            OrderFee       orderFee;
            OrderEvent     fill;
            decimal        orderQuantity;
            decimal        freeMargin;
            decimal        requiredMargin;

            if (initialPosition != Position.Zero)
            {
                targetPercentage = (decimal)initialPosition;
                orderDirection   = initialPosition == Position.Long ? OrderDirection.Buy : OrderDirection.Sell;
                orderQuantity    = algorithm.CalculateOrderQuantity(_symbol, targetPercentage);
                order            = new MarketOrder(_symbol, orderQuantity, DateTime.UtcNow);
                freeMargin       = buyingPowerModel.GetMarginRemaining(algorithm.Portfolio, security, orderDirection);
                requiredMargin   = buyingPowerModel.GetInitialMarginRequiredForOrder(
                    new InitialMarginRequiredForOrderParameters(
                        new IdentityCurrencyConverter(algorithm.Portfolio.CashBook.AccountCurrency), security, order));

                //Console.WriteLine("Current price: " + security.Price);
                //Console.WriteLine("Target percentage: " + targetPercentage);
                //Console.WriteLine("Order direction: " + orderDirection);
                //Console.WriteLine("Order quantity: " + orderQuantity);
                //Console.WriteLine("Free margin: " + freeMargin);
                //Console.WriteLine("Required margin: " + requiredMargin);
                //Console.WriteLine();

                Assert.That(Math.Abs(requiredMargin) <= freeMargin);

                orderFee = security.FeeModel.GetOrderFee(
                    new OrderFeeParameters(security, order));
                fill = new OrderEvent(order, DateTime.UtcNow, orderFee)
                {
                    FillPrice = security.Price, FillQuantity = orderQuantity
                };
                algorithm.Portfolio.ProcessFill(fill);

                //Console.WriteLine("Portfolio.Cash: " + algorithm.Portfolio.Cash);
                //Console.WriteLine("Portfolio.TotalPortfolioValue: " + algorithm.Portfolio.TotalPortfolioValue);
                //Console.WriteLine();

                if (priceMovement == PriceMovement.RisingSmall)
                {
                    Update(security, HighPrice);
                }
                else if (priceMovement == PriceMovement.FallingSmall)
                {
                    Update(security, LowPrice);
                }
                else if (priceMovement == PriceMovement.RisingLarge)
                {
                    Update(security, VeryHighPrice);
                }
                else if (priceMovement == PriceMovement.FallingLarge)
                {
                    Update(security, VeryLowPrice);
                }
            }

            targetPercentage = (decimal)finalPosition;
            orderDirection   = finalPosition == Position.Long || (finalPosition == Position.Zero && initialPosition == Position.Short) ? OrderDirection.Buy : OrderDirection.Sell;
            orderQuantity    = algorithm.CalculateOrderQuantity(_symbol, targetPercentage);
            order            = new MarketOrder(_symbol, orderQuantity, DateTime.UtcNow);
            freeMargin       = buyingPowerModel.GetMarginRemaining(algorithm.Portfolio, security, orderDirection);
            requiredMargin   = buyingPowerModel.GetInitialMarginRequiredForOrder(
                new InitialMarginRequiredForOrderParameters(
                    new IdentityCurrencyConverter(algorithm.Portfolio.CashBook.AccountCurrency), security, order));

            //Console.WriteLine("Current price: " + security.Price);
            //Console.WriteLine("Target percentage: " + targetPercentage);
            //Console.WriteLine("Order direction: " + orderDirection);
            //Console.WriteLine("Order quantity: " + orderQuantity);
            //Console.WriteLine("Free margin: " + freeMargin);
            //Console.WriteLine("Required margin: " + requiredMargin);
            //Console.WriteLine();

            Assert.That(Math.Abs(requiredMargin) <= freeMargin);

            orderFee = security.FeeModel.GetOrderFee(
                new OrderFeeParameters(security, order));
            fill = new OrderEvent(order, DateTime.UtcNow, orderFee)
            {
                FillPrice = security.Price, FillQuantity = orderQuantity
            };
            algorithm.Portfolio.ProcessFill(fill);

            //Console.WriteLine("Portfolio.Cash: " + algorithm.Portfolio.Cash);
            //Console.WriteLine("Portfolio.TotalPortfolioValue: " + algorithm.Portfolio.TotalPortfolioValue);
            //Console.WriteLine();
        }