Ejemplo n.º 1
0
        public string Protect(StringValues value, ProtectType protectType)
        {
            if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
            {
                return(value);
            }

            if (protectType == ProtectType.Truncate)
            {
                ReadOnlySpan <char> valueSpan = value.ToString();

                return(new string(valueSpan.Slice(0, valueSpan.Length / 2)));
            }
            else
            {
                using var derivedBytes = new Rfc2898DeriveBytes(value.ToString(),
                                                                saltSize: 16,
                                                                iterations: 50000,
                                                                HashAlgorithmName.SHA256);

                var key = derivedBytes.GetBytes(16);

                return(Encoding.UTF8.GetString(key));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Вычисление средней цены серии открытых позиций, зависит от настройки параметра ProtectPosType
        /// либо средняя цена всей серии либо цена худшей открытой позиции
        /// </summary>
        private float GetAveragePrice(List<MarketOrder> orders, int side, float currPrice, ProtectType type)
        {
            var price = 0f;
            switch (type)
            {
                case ProtectType.ПоУсредненнойЦене:
                    {
                        // вычисляем по позициям средневзвешенную цену
                        float sumBuys = 0, sumSell = 0;
                        var sumDeals = orders.FindAll(o => o.Symbol == ticker && o.Magic == Magic);
                        var exposition = 0;
                        foreach (var sumDeal in sumDeals)
                        {
                            exposition += sumDeal.Side * sumDeal.Volume;

                            if (sumDeal.Side > 0)
                                sumBuys += sumDeal.Volume * sumDeal.PriceEnter;
                            else
                                sumSell += sumDeal.Volume * sumDeal.PriceEnter;
                        }
                        price = exposition == 0 ? 0 : (sumBuys - sumSell) / exposition;
                    }
                    break;
                case ProtectType.ПоХудшейПозиции:
                    foreach (var order in orders)
                    {
                        if (price == 0)
                        {
                            price = order.PriceEnter;
                            continue;
                        }
                        if (order.Side != side) continue;
                        if (side == 1) //покупки
                        {
                            if (price < order.PriceEnter)
                                price = order.PriceEnter;
                        }
                        else
                        {
                            // продажи
                            if (price > order.PriceEnter)
                                price = order.PriceEnter;
                        }
                    }
                    break;
                case ProtectType.ПоЛучшейПозиции:
                    foreach (var order in orders)
                    {
                        if (price == 0)
                        {
                            price = order.PriceEnter;
                            continue;
                        }
                        if (order.Side != side) continue;
                        if (side == 1) //покупки
                        {
                            if (price > order.PriceEnter)
                                price = order.PriceEnter;
                        }
                        else
                        {
                            // продажи
                            if (price < order.PriceEnter)
                                price = order.PriceEnter;
                        }
                    }
                    break;
            }
            // price = получили цену для поджатия
            if (side == 1)
                price += DalSpot.Instance.GetAbsValue(ticker, (float)DeltaProtect);
            else
                price -= DalSpot.Instance.GetAbsValue(ticker, (float)DeltaProtect);
            return price;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Вычисление средней цены серии открытых позиций, зависит от настройки параметра ProtectPosType
        /// либо средняя цена всей серии либо цена худшей открытой позиции
        /// </summary>
        private float GetAveragePrice(List <MarketOrder> orders, int side, float currPrice, ProtectType type)
        {
            var price = 0f;

            switch (type)
            {
            case ProtectType.ПоУсредненнойЦене:
            {
                // вычисляем по позициям средневзвешенную цену
                float sumBuys = 0, sumSell = 0;
                var   sumDeals   = orders.FindAll(o => o.Symbol == ticker && o.Magic == Magic);
                var   exposition = 0;
                foreach (var sumDeal in sumDeals)
                {
                    exposition += sumDeal.Side * sumDeal.Volume;

                    if (sumDeal.Side > 0)
                    {
                        sumBuys += sumDeal.Volume * sumDeal.PriceEnter;
                    }
                    else
                    {
                        sumSell += sumDeal.Volume * sumDeal.PriceEnter;
                    }
                }
                price = exposition == 0 ? 0 : (sumBuys - sumSell) / exposition;
            }
            break;

            case ProtectType.ПоХудшейПозиции:
                foreach (var order in orders)
                {
                    if (price == 0)
                    {
                        price = order.PriceEnter;
                        continue;
                    }
                    if (order.Side != side)
                    {
                        continue;
                    }
                    if (side == 1)     //покупки
                    {
                        if (price < order.PriceEnter)
                        {
                            price = order.PriceEnter;
                        }
                    }
                    else
                    {
                        // продажи
                        if (price > order.PriceEnter)
                        {
                            price = order.PriceEnter;
                        }
                    }
                }
                break;

            case ProtectType.ПоЛучшейПозиции:
                foreach (var order in orders)
                {
                    if (price == 0)
                    {
                        price = order.PriceEnter;
                        continue;
                    }
                    if (order.Side != side)
                    {
                        continue;
                    }
                    if (side == 1)     //покупки
                    {
                        if (price > order.PriceEnter)
                        {
                            price = order.PriceEnter;
                        }
                    }
                    else
                    {
                        // продажи
                        if (price < order.PriceEnter)
                        {
                            price = order.PriceEnter;
                        }
                    }
                }
                break;
            }
            // price = получили цену для поджатия
            if (side == 1)
            {
                price += DalSpot.Instance.GetAbsValue(ticker, (float)DeltaProtect);
            }
            else
            {
                price -= DalSpot.Instance.GetAbsValue(ticker, (float)DeltaProtect);
            }
            return(price);
        }
 public static extern IntPtr VirtualAlloc(IntPtr lpAddress, uint dwSize, AllocationType flAllocationType, ProtectType flProtect);