Esempio n. 1
0
 public override void OnReceiveEvent(string[] param)
 {
     if (int.TryParse(param[6], out int volume))
     {
         slim.Wait();
         SendConsecutive?.Invoke(this, new SendConsecutive(new Charts
         {
             Date   = param[0],
             Price  = param[1].StartsWith("-") ? param[1].Substring(1) : param[1],
             Volume = volume
         }));
         if (slim.Release() > 0)
         {
             Console.WriteLine(string.Concat(GetType().FullName, '_', Code));
         }
     }
     if (param.Length == 0x20 && double.TryParse(param[1].StartsWith("-") ? param[1].Substring(1) : param[1], out double price))
     {
         Current = price;
         Revenue = (long)((price - Purchase) * Quantity * TransactionMultiplier);
         Rate    = (price / Purchase - 1) * (Quantity > 0 ? 1 : -1);
     }
     else if (int.TryParse(param[1].StartsWith("-") ? param[1].Substring(1) : param[1], out int current))
     {
         Current = current;
         Revenue = (current - Purchase) * Quantity;
         Rate    = current / (double)Purchase - 1;
     }
     SendStocks?.Invoke(this, new SendHoldingStocks(Code, Quantity, Purchase, Current, Revenue, Rate, Base, Secondary, AdjustTheColorAccordingToTheCurrentSituation(WaitOrder, OrderNumber.Count)));
 }
Esempio n. 2
0
        public override void OnReceiveEvent(string[] param)
        {
            if (int.TryParse(string.Concat(param[8], param[9]), out int volume))
            {
                SendConsecutive?.Invoke(this, new SendConsecutive(new Charts
                {
                    Date   = param[0],
                    Price  = param[4],
                    Volume = volume
                }));
            }
            if (double.TryParse(param[4], out double current))
            {
                Current = current;
                Revenue = (long)((current - (Purchase ?? 0D)) * Quantity * transactionMultiplier);
                Rate    = (Quantity > 0 ? current / (double)Purchase : Purchase / (double)current) - 1;

                if (OrderNumber.Count > 0 && strategics is TrendFollowingBasicFutures && OrderNumber.ContainsValue(Bid) == false && OrderNumber.ContainsValue(Offer) == false)
                {
                    foreach (var kv in OrderNumber)
                    {
                        if (kv.Value < Bid || kv.Value > Offer)
                        {
                            SendBalance?.Invoke(this, new SendSecuritiesAPI(new Catalog.XingAPI.Order
                            {
                                FnoIsuNo = Code,
                                OrgOrdNo = kv.Key,
                                OrdQty   = "1"
                            }));
                        }
                    }
                }
            }
            if (param[0].CompareTo(end) > 0 && uint.TryParse(param[0], out uint remain) && (RollOver == false || Temporary.RemainingDay.Contains(remain)))
            {
                var quantity = Math.Abs(Quantity);
                RollOver = Temporary.RemainingDay.Remove(remain);

                if (RollOver == false)
                {
                    RollOver = true;
                }

                while (quantity > 0)
                {
                    SendBalance?.Invoke(this, new SendSecuritiesAPI(new Catalog.XingAPI.Order
                    {
                        FnoIsuNo         = Code,
                        BnsTpCode        = Quantity > 0 ? "1" : "2",
                        FnoOrdprcPtnCode = ((int)Catalog.XingAPI.FnoOrdprcPtnCode.시장가).ToString("D2"),
                        OrdPrc           = Purchase.ToString("F2"),
                        OrdQty           = "1"
                    }));
                    quantity--;
                }
            }
            SendStocks?.Invoke(this, new SendHoldingStocks(Code, Quantity, Purchase, Current, Revenue, Rate, Base, Secondary, AdjustTheColorAccordingToTheCurrentSituation(WaitOrder, OrderNumber.Count)));
        }
Esempio n. 3
0
 public override void OnReceiveEvent(string[] param)
 {
     if (int.TryParse(param[1], out int current))
     {
         Current = current;
         Revenue = (current - Purchase) * Quantity;
         Rate    = current / (double)Purchase - 1;
     }
     SendStocks?.Invoke(this, new SendHoldingStocks(Code, Quantity, Purchase, Current, Revenue, Rate));
 }
Esempio n. 4
0
 public override void OnReceiveEvent(string[] param)
 {
     if (double.TryParse(param[4], out double current))
     {
         Current = current;
         Revenue = (long)((current - Purchase) * Quantity * transactionMutiplier);
         Rate    = (Quantity > 0 ? current / (double)Purchase : Purchase / (double)current) - 1;
     }
     SendStocks?.Invoke(this, new SendHoldingStocks(Code, Quantity, Purchase, Current, Revenue, Rate));
 }
Esempio n. 5
0
 public override void OnReceiveEvent(string[] param)
 {
     if (int.TryParse(string.Concat(param[8], param[9]), out int volume))
     {
         SendConsecutive?.Invoke(this, new SendConsecutive(new Charts
         {
             Date   = param[0],
             Price  = param[4],
             Volume = volume
         }));
     }
     if (double.TryParse(param[4], out double current))
     {
         Current = current;
         Revenue = (long)((current - Purchase) * Quantity * transactionMutiplier);
         Rate    = (Quantity > 0 ? current / (double)Purchase : Purchase / (double)current) - 1;
     }
     SendStocks?.Invoke(this, new SendHoldingStocks(Code, Quantity, Purchase, Current, Revenue, Rate, Base, Secondary, AdjustTheColorAccordingToTheCurrentSituation(WaitOrder, OrderNumber.Count)));
 }
Esempio n. 6
0
 public override void OnReceiveEvent(string[] param)
 {
     if (int.TryParse(param[6], out int volume))
     {
         SendConsecutive?.Invoke(this, new SendConsecutive(new Charts
         {
             Date   = param[0],
             Price  = param[1].StartsWith("-") ? param[1].Substring(1) : param[1],
             Volume = volume
         }));
     }
     if (int.TryParse(param[1].StartsWith("-") ? param[1].Substring(1) : param[1], out int current))
     {
         Current = current;
         Revenue = (current - Purchase) * Quantity;
         Rate    = current / (double)Purchase - 1;
     }
     SendStocks?.Invoke(this, new SendHoldingStocks(Code, Quantity, Purchase, Current, Revenue, Rate, Base, Secondary, AdjustTheColorAccordingToTheCurrentSituation(WaitOrder, OrderNumber.Count)));
 }
Esempio n. 7
0
        public void StartProgress(double commission)
        {
            switch (strategics)
            {
            case ScenarioAccordingToTrend st:
                Commission = commission > 0 ? commission : 1.5e-4;

                if (StartProgress(strategics.Code as string) > 0)
                {
                    var price    = SendMessage.Price;
                    var estimate = EstimatedPrice.Where(o => o.Key.ToString(format.Substring(0, 6)).CompareTo(SendMessage.Date) > 0);
                    var find     = FindTheNearestQuarter(SendMessage.Date);
                    var key      = string.Concat("ST.", st.Calendar.Substring(0, 4), "15.", st.Trend, '.', st.CheckSales.ToString().Substring(0, 1), '.', st.Sales * 0x64, '.', st.CheckOperatingProfit.ToString().Substring(0, 1), '.', st.OperatingProfit * 0x64, '.', st.CheckNetIncome.ToString().Substring(0, 1), '.', st.NetIncome * 0x64);

                    if (client.PutContext(new Catalog.Request.Consensus
                    {
                        Code = st.Code,
                        Strategics = key,
                        Date = SendMessage.Date,
                        FirstQuarter = (estimate.Last(o => o.Key.ToString(format.Substring(0, 6)).Equals(find[0])).Value - price) / price,
                        SecondQuarter = (estimate.Last(o => o.Key.ToString(format.Substring(0, 6)).Equals(find[1])).Value - price) / price,
                        ThirdQuarter = (estimate.Last(o => o.Key.ToString(format.Substring(0, 6)).Equals(find[2])).Value - price) / price,
                        Quarter = (estimate.Last(o => o.Key.ToString(format.Substring(0, 6)).Equals(find[find.Length - 2])).Value - price) / price,
                        TheNextYear = (estimate.LastOrDefault(o => o.Key.ToString(format.Substring(0, 6)).Equals(find[find.Length - 1])).Value - price) / price,
                        TheYearAfterNext = (estimate.Last().Value - price) / price
                    }).Result > 0)
                    {
                        consecutive.Dispose();
                    }

                    SendMessage = new Statistics
                    {
                        Base       = SendMessage.Base,
                        Cumulative = SendMessage.Cumulative,
                        Date       = SendMessage.Date,
                        Statistic  = SendMessage.Statistic,
                        Price      = (int)estimate.Max(o => o.Value),
                        Key        = key
                    };
                    SendStocks?.Invoke(this, new SendHoldingStocks(EstimatedPrice, SendMessage.Date));
                }
                break;

            case TrendsInStockPrices ts:
                Commission = commission > 0 ? commission : 1.5e-4;

                if (StartProgress(strategics.Code as string) > 0)
                {
                    SendMessage = new Statistics
                    {
                        Base       = SendMessage.Base,
                        Cumulative = SendMessage.Cumulative,
                        Date       = SendMessage.Date,
                        Statistic  = SendMessage.Statistic,
                        Key        = string.Concat("TS.", ts.Short, '.', ts.Long, '.', ts.Trend, '.', (int)(ts.RealizeProfit * 0x2710), '.', (int)(ts.AdditionalPurchase * 0x2710), '.', ts.QuoteUnit, '.', (char)ts.LongShort, '.', (char)ts.TrendType, '.', (char)ts.Setting)
                    };
                    consecutive.Dispose();
                }
                break;

            case TrendFollowingBasicFutures _:
                Commission = commission > 0 ? commission : 3e-5;
                IsDebugging();

                if (StartProgress(strategics.Code as string) > 0)
                {
                    foreach (var con in Consecutive)
                    {
                        con.Dispose();
                    }
                }

                break;

            default:
                return;
            }
            SendBalance?.Invoke(this, new SendSecuritiesAPI(strategics, SendMessage));
        }
Esempio n. 8
0
        internal void OnReceiveTrendsInPrices(SendConsecutive e, double gap, double sShort, double sLong, double trend)
        {
            var date = e.Date.Substring(6, 4);

            switch (strategics)
            {
            case ScenarioAccordingToTrend st:
                if (e.Date.Length > 8 && date.CompareTo(start) > 0 && date.CompareTo(transmit) < 0 && DateTime.TryParseExact(e.Date.Substring(0, 12), format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime interval))
                {
                    if (NextOrderTime == null)
                    {
                        NextOrderTime = interval;
                    }

                    else if (Quantity > st.Quantity - 1 && OrderNumber.Any(o => o.Key.StartsWith("2") && o.Value == e.Price - GetQuoteUnit(e.Price, Market)))
                    {
                        CumulativeFee += (uint)(e.Price * st.Quantity * (Commission + tax));
                        Revenue       += (long)((e.Price - (Purchase ?? 0D)) * st.Quantity);
                        Quantity      -= st.Quantity;
                        var profit = OrderNumber.First(o => o.Key.StartsWith("2") && o.Value == e.Price - GetQuoteUnit(e.Price, Market));

                        if (OrderNumber.Remove(profit.Key) && Verify)
                        {
                            OnReceiveBalance(new string[]
                            {
                                string.Concat(interval.ToShortDateString(), " ", interval.ToLongTimeString()),
                                profit.Key,
                                profit.Value.ToString("N0")
                            });
                        }
                        Base -= profit.Value * st.Quantity;
                    }
                    else if (OrderNumber.Any(o => o.Key.StartsWith("1") && o.Value == e.Price + GetQuoteUnit(e.Price, Market)))
                    {
                        CumulativeFee += (uint)(e.Price * Commission * st.Quantity);
                        Purchase       = (double)((e.Price * st.Quantity + (Purchase ?? 0D) * Quantity) / (Quantity + st.Quantity));
                        Quantity      += st.Quantity;
                        var profit = OrderNumber.First(o => o.Key.StartsWith("1") && o.Value == e.Price + GetQuoteUnit(e.Price, Market));

                        if (OrderNumber.Remove(profit.Key) && Verify)
                        {
                            OnReceiveBalance(new string[]
                            {
                                string.Concat(interval.ToShortDateString(), " ", interval.ToLongTimeString()),
                                profit.Key,
                                profit.Value.ToString("N0")
                            });
                        }
                        Base += profit.Value * st.Quantity;
                    }
                    else if (Quantity > st.Quantity - 1 && OrderNumber.ContainsValue(e.Price) == false && e.Price > trend * (1 + st.ErrorRange) && e.Price > (Purchase ?? 0D) && gap < 0 && (st.IntervalInSeconds == 0 || st.IntervalInSeconds > 0 && interval.CompareTo(NextOrderTime) > 0))
                    {
                        var unit = GetQuoteUnit(e.Price, Market);

                        if (Verify && VerifyAmount > Quantity && DateTime.TryParseExact(e.Date.Substring(0, 12), format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime dt))
                        {
                            OnReceiveConclusion(new string[]
                            {
                                string.Concat(OpenOrderType.신규매도, " ", dt.ToShortDateString(), " ", dt.ToLongTimeString(), " ", NextOrderTime),
                                Quantity.ToString("N0"),
                                (Purchase ?? 0D).ToString("N2"),
                                (e.Price + unit).ToString("N0"),
                                (Revenue - CumulativeFee).ToString("C0"),
                                OrderNumber.Max(o => o.Key)
                            });
                            VerifyAmount = Quantity;
                        }
                        if (OrderNumber.ContainsValue(e.Price + unit) == false)
                        {
                            OrderNumber[GetOrderNumber((int)OpenOrderType.신규매도)] = e.Price + unit;
                        }

                        if (st.IntervalInSeconds > 0)
                        {
                            NextOrderTime = MeasureTheDelayTime(st.IntervalInSeconds, interval);
                        }
                    }
                    else if (OrderNumber.ContainsValue(e.Price) == false && e.Price < trend * (1 - st.ErrorRange) && gap > 0 && (st.IntervalInSeconds == 0 || st.IntervalInSeconds > 0 && interval.CompareTo(NextOrderTime) > 0))
                    {
                        var unit = GetQuoteUnit(e.Price, Market);

                        if (Verify && VerifyAmount < Quantity && DateTime.TryParseExact(e.Date.Substring(0, 12), format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime dt))
                        {
                            OnReceiveConclusion(new string[]
                            {
                                string.Concat(OpenOrderType.신규매수, " ", dt.ToShortDateString(), " ", dt.ToLongTimeString(), " ", NextOrderTime),
                                Quantity.ToString("N0"),
                                (Purchase ?? 0D).ToString("N2"),
                                (e.Price - unit).ToString("N0"),
                                (Revenue - CumulativeFee).ToString("C0"),
                                OrderNumber.Where(o => o.Key.StartsWith("1")).Max(o => o.Key)
                            });
                            VerifyAmount = Quantity;
                        }
                        if (OrderNumber.ContainsValue(e.Price - unit) == false)
                        {
                            OrderNumber[GetOrderNumber((int)OpenOrderType.신규매수)] = e.Price - unit;
                        }

                        if (st.IntervalInSeconds > 0)
                        {
                            NextOrderTime = MeasureTheDelayTime(st.IntervalInSeconds, interval);
                        }
                    }
                }
                else if (date.CompareTo(transmit) > 0)
                {
                    OrderNumber.Clear();
                    Count = 0;
                    long revenue = Revenue - CumulativeFee, unrealize = (long)((e.Price - (Purchase ?? 0D)) * Quantity);
                    var  avg = EMA.Make(++Accumulative, revenue - TodayRevenue + unrealize - TodayUnrealize, Before);
                    SendMessage = new Statistics
                    {
                        Date       = e.Date.Substring(0, 6),
                        Cumulative = (revenue + unrealize) / st.Quantity,
                        Base       = SendMessage.Base > Base / st.Quantity ? SendMessage.Base : Base / st.Quantity,
                        Statistic  = (int)(avg / st.Quantity),
                        Price      = e.Price
                    };
                    SendStocks?.Invoke(this, new SendHoldingStocks(e.Date, e.Price, sShort, sLong, trend, revenue + unrealize, (long)(Base > 0 ? Base : 0)));
                    Before         = avg;
                    TodayRevenue   = revenue;
                    TodayUnrealize = unrealize;
                }
                break;

            case TrendsInStockPrices ts:
                if (e.Date.Length > 8 && date.CompareTo(start) > 0 && date.CompareTo(transmit) < 0)
                {
                    if (Quantity > ts.Quantity - 1 && OrderNumber.Any(o => o.Key.StartsWith("2") && o.Value == e.Price - GetQuoteUnit(e.Price, Market)))
                    {
                        CumulativeFee += (uint)(e.Price * ts.Quantity * (Commission + tax));
                        Revenue       += (long)((e.Price - (Purchase ?? 0D)) * ts.Quantity);
                        Quantity      -= ts.Quantity;
                        var profit = OrderNumber.First(o => o.Key.StartsWith("2") && o.Value == e.Price - GetQuoteUnit(e.Price, Market));

                        if (OrderNumber.Remove(profit.Key) && Verify && DateTime.TryParseExact(e.Date.Substring(0, 12), format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime dt))
                        {
                            OnReceiveBalance(new string[]
                            {
                                string.Concat(dt.ToShortDateString(), " ", dt.ToLongTimeString()),
                                profit.Key,
                                profit.Value.ToString("N0")
                            });
                        }
                        Base -= profit.Value * ts.Quantity;
                    }
                    else if (OrderNumber.Any(o => o.Key.StartsWith("1") && o.Value == e.Price + GetQuoteUnit(e.Price, Market)))
                    {
                        CumulativeFee += (uint)(e.Price * Commission * ts.Quantity);
                        Purchase       = (double)((e.Price * ts.Quantity + (Purchase ?? 0D) * Quantity) / (Quantity + ts.Quantity));
                        Quantity      += ts.Quantity;
                        var profit = OrderNumber.First(o => o.Key.StartsWith("1") && o.Value == e.Price + GetQuoteUnit(e.Price, Market));

                        if (OrderNumber.Remove(profit.Key) && Verify && DateTime.TryParseExact(e.Date.Substring(0, 12), format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime dt))
                        {
                            OnReceiveBalance(new string[]
                            {
                                string.Concat(dt.ToShortDateString(), " ", dt.ToLongTimeString()),
                                profit.Key,
                                profit.Value.ToString("N0")
                            });
                        }
                        Base += profit.Value * ts.Quantity;
                    }
                    else if (Quantity > ts.Quantity - 1 && OrderNumber.ContainsValue(e.Price) == false && e.Price > trend * (1 + ts.RealizeProfit) && e.Price > (Purchase ?? 0D) && gap < 0)
                    {
                        var quote = 0;

                        for (int i = 0; i < ts.QuoteUnit; i++)
                        {
                            quote += GetQuoteUnit(e.Price, Market);
                        }

                        if (Verify && VerifyAmount > Quantity && DateTime.TryParseExact(e.Date.Substring(0, 12), format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime dt))
                        {
                            OnReceiveConclusion(new string[]
                            {
                                string.Concat(OpenOrderType.신규매도, " ", dt.ToShortDateString(), " ", dt.ToLongTimeString()),
                                Quantity.ToString("N0"),
                                (Purchase ?? 0D).ToString("N2"),
                                (e.Price + quote).ToString("N0"),
                                (Revenue - CumulativeFee).ToString("C0"),
                                OrderNumber.Max(o => o.Key)
                            });
                            VerifyAmount = Quantity;
                        }
                        if (OrderNumber.ContainsValue(e.Price + quote) == false)
                        {
                            OrderNumber[GetOrderNumber((int)OpenOrderType.신규매도)] = e.Price + quote;
                        }
                    }
                    else if (OrderNumber.ContainsValue(e.Price) == false && e.Price < trend * (1 - ts.AdditionalPurchase) && gap > 0)
                    {
                        var quote = 0;

                        for (int i = 0; i < ts.QuoteUnit; i++)
                        {
                            quote += GetQuoteUnit(e.Price, Market);
                        }

                        if (Verify && VerifyAmount < Quantity && DateTime.TryParseExact(e.Date.Substring(0, 12), format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime dt))
                        {
                            OnReceiveConclusion(new string[]
                            {
                                string.Concat(OpenOrderType.신규매수, " ", dt.ToShortDateString(), " ", dt.ToLongTimeString()),
                                Quantity.ToString("N0"),
                                (Purchase ?? 0D).ToString("N2"),
                                (e.Price - quote).ToString("N0"),
                                (Revenue - CumulativeFee).ToString("C0"),
                                OrderNumber.Where(o => o.Key.StartsWith("1")).Max(o => o.Key)
                            });
                            VerifyAmount = Quantity;
                        }
                        if (OrderNumber.ContainsValue(e.Price - quote) == false)
                        {
                            OrderNumber[GetOrderNumber((int)OpenOrderType.신규매수)] = e.Price - quote;
                        }
                    }
                }
                else if (date.CompareTo(transmit) > 0)
                {
                    OrderNumber.Clear();
                    Count = 0;
                    long revenue = Revenue - CumulativeFee, unrealize = (long)((e.Price - (Purchase ?? 0D)) * Quantity);
                    var  avg = EMA.Make(++Accumulative, revenue - TodayRevenue + unrealize - TodayUnrealize, Before);

                    if (ts.Setting.Equals(Setting.Reservation) && Quantity > ts.Quantity - 1)
                    {
                        var stock = Market;
                        int quantity = Quantity / ts.Quantity, price = e.Price, sell = (int)((Purchase ?? 0D) * (1 + ts.RealizeProfit)), buy = (int)((Purchase ?? 0D) * (1 - ts.AdditionalPurchase)), upper = (int)(price * 1.3), lower = (int)(price * 0.7), bPrice = GetStartingPrice(lower, stock), sPrice = GetStartingPrice(sell, stock);
                        sPrice = sPrice < lower ? lower + GetQuoteUnit(sPrice, stock) : sPrice;

                        while (sPrice < upper && quantity-- > 0)
                        {
                            OrderNumber[GetOrderNumber((int)OpenOrderType.신규매도)] = sPrice;

                            for (int i = 0; i < ts.QuoteUnit; i++)
                            {
                                sPrice += GetQuoteUnit(sPrice, stock);
                            }
                        }
                        while (bPrice < upper && bPrice < buy)
                        {
                            OrderNumber[GetOrderNumber((int)OpenOrderType.신규매수)] = bPrice;

                            for (int i = 0; i < ts.QuoteUnit; i++)
                            {
                                bPrice += GetQuoteUnit(bPrice, stock);
                            }
                        }
                        if (Verify && DateTime.TryParseExact(e.Date.Substring(0, 12), format, CultureInfo.CurrentCulture, DateTimeStyles.None, out DateTime dt))
                        {
                            OnReceiveEvent(new string[]
                            {
                                string.Concat(dt.ToShortDateString(), " ", dt.ToLongTimeString()),
                                OrderNumber.Where(o => o.Key.StartsWith("1")).Max(o => o.Key),
                                OrderNumber.Where(o => o.Key.StartsWith("2")).Max(o => o.Key)
                            });
                        }
                    }
                    SendMessage = new Statistics
                    {
                        Date       = e.Date.Substring(0, 6),
                        Cumulative = (revenue + unrealize) / ts.Quantity,
                        Base       = SendMessage.Base > Base / ts.Quantity ? SendMessage.Base : Base / ts.Quantity,
                        Statistic  = (int)(avg / ts.Quantity)
                    };
                    SendStocks?.Invoke(this, new SendHoldingStocks(e.Date, e.Price, sShort, sLong, trend, revenue + unrealize, (long)(Base > 0 ? Base : 0)));
                    Before         = avg;
                    TodayRevenue   = revenue;
                    TodayUnrealize = unrealize;
                }
                break;

            case TrendFollowingBasicFutures tf:
                break;
            }
        }