Пример #1
0
        public void Data(long tick)
        {
            if (tick % 10 == 0)
            {
                LastDecemGain  = -(LastDecemValue - Value) / LastDecemValue;
                LastDecemValue = Value;

                if (tick % 100 == 0)
                {
                    LastCentumGain  = -(LastCentumValue - Value) / LastCentumValue;
                    LastCentumValue = Value;

                    if (tick % 1000 == 0)
                    {
                        LastMilleGain  = -(LastMilleValue - Value) / LastMilleValue;
                        LastMilleValue = Value;

                        if (tick % 10000 == 0)
                        {
                            LastDeceMilleGain  = -(LastDeceMilleValue - Value) / LastDeceMilleValue;
                            LastDeceMilleValue = Value;
                        }
                        if (tick % 100000 == 0)
                        {
                            LastCentuMilleGain  = -(LastCentuMilleValue - Value) / LastCentuMilleValue;
                            LastCentuMilleValue = Value;
                        }
                    }
                }
            }

            //check high and low
            if (tick % 20 == 0)
            {
                Stock st = Master.inst.exchange.GetCheapestStock(this);
                if (st != null)
                {
                    float currentprice = (float)st.SellPrice;
                    if (currentprice > high)
                    {
                        high = currentprice;
                    }
                    if (currentprice < low)
                    {
                        low = currentprice;
                    }



                    //register datapoint
                    if (tick % 100 == 0)
                    {
                        StockPriceGraph sp = new StockPriceGraph(MainPage.master.Year, open, currentprice, high, low);
                        stockPrices1m.Add(sp);
                        ValueGraph vg = new ValueGraph(MainPage.master.Year, (float)Value);
                        values.Add(vg);
                        if (stockPrices1m.Count > 10000)
                        {
                            stockPrices1m.RemoveAt(0);
                        }

                        //create new highlow
                        if (currentprice > high5m)
                        {
                            high5m = currentprice;
                        }
                        if (currentprice < low5m)
                        {
                            low5m = currentprice;
                        }

                        if (tick % 500 == 0)
                        {
                            StockPriceGraph sp5m = new StockPriceGraph(MainPage.master.Year, open, currentprice, high, low);
                            stockPrices5m.Add(sp5m);
                            if (stockPrices5m.Count > 10000)
                            {
                                stockPrices5m.RemoveAt(0);
                            }
                        }

                        if (tick % 800000 == 0)
                        {
                            //StockPriceGraph sp = new StockPriceGraph(MainPage.master.Year, open, currentprice, high, low);


                            var ignore = MainPage.inst.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                            {
                                stockViewModel.prices1m.Add(sp);
                                ValueviewModel.values.Add(vg);

                                if (stockViewModel.prices1m.Count > 100)
                                {
                                    stockViewModel.prices1m.RemoveAt(0);
                                }
                                //MainPage.inst.SetNewYearLimit();
                            });

                            high = currentprice;
                            low  = currentprice;
                            open = currentprice;
                        }
                    }
                }
            }
        }
Пример #2
0
            bool Patterns(Company cp, Trader trader, Exchange exchange)
            {
                #region Checks and Data Gathering
                bool         OwnsStocks = false;
                List <Stock> cpstocks   = null;
                for (int i = 0; i < trader.Stocks.Count; i++) //check on stocks
                {
                    if (trader.Stocks[i].company == cp)
                    {
                        OwnsStocks = true;
                        if (cpstocks == null)
                        {
                            cpstocks = new List <Stock>();
                        }
                        cpstocks.Add(trader.Stocks[i]);
                        break;
                    }
                }

                bool Traded = false;
                List <StockPriceGraph> stockPrices = new List <StockPriceGraph>();
                for (int i = 0; i < 100; i++)
                {
                    StockPriceGraph spg = cp.stockPrices[cp.stockPrices.Count - 1 - (int)(i * scale)];
                    if (spg != null)
                    {
                        stockPrices.Add(spg);
                    }
                    else
                    {
                        i--;
                    }
                }

                #endregion

                #region price discovery
                List <StockPriceGraph> highs = new List <StockPriceGraph>();
                List <StockPriceGraph> lows  = new List <StockPriceGraph>();
                for (int i = 1; i < 99; i++)
                {
                    if (stockPrices[i].Close > stockPrices[i - 1].Close &&
                        stockPrices[i].Close > stockPrices[i + 1].Close)     //this is a high
                    {
                        highs.Add(stockPrices[i]);
                    }
                    if (highs.Count > 0) //first top always high
                    {
                        if (stockPrices[i].Close < stockPrices[i - 1].Close &&
                            stockPrices[i].Close < stockPrices[i + 1].Close) //this is a low
                        {
                            lows.Add(stockPrices[i]);
                        }
                    }
                }

                trader.ActionTime -= 45;

                List <StockPriceGraph> avHighs = new List <StockPriceGraph>();
                List <StockPriceGraph> avLows  = new List <StockPriceGraph>();

                if (highs.Count < 5 || lows.Count < 5)
                {
                    return(Traded);
                }
                for (float i = 0; i < highs.Count; i += 5.0f / highs.Count)
                {
                    avHighs.Add(highs[(int)i]);
                }
                for (float i = 0; i < lows.Count; i += 5.0f / lows.Count)
                {
                    avLows.Add(lows[(int)i]);
                }

                #endregion

                #region Pattern Recognition

                //channel pattern
                //            _              H  H-h  H-h  H-h
                //        _  / \/              L  L-h  L-h
                //    _  / \/            =========> breakout signal
                //   / \/
                //  /
                // /


                if (avHighs[0].High > avHighs[4].High && avLows[0].Low > avLows[4].Low)
                {
                    //first check
                    if (avHighs[2].High > avHighs[4].High && avLows[2].Low > avLows[4].Low)
                    {
                        //second check
                        if (avHighs[2].High < avHighs[0].High && avLows[2].Low < avLows[0].Low)
                        {
                            //Satisfied, BUY!!
                            Traded = true;
                            for (int i = 0; i < 10; i++)
                            {
                                Stock st = exchange.GetCheapestStock(cp);
                                if (exchange.BuyStock(st, trader)) //if transaction succeeded
                                {
                                    exchange.SellStock(st, st.SellPrice * 1.01);
                                }
                            }
                        }
                    }
                }

                if (OwnsStocks)
                {
                    //headnshoulders pattern
                    //        _                 H  H-h  H-l
                    //    _  / \  _               L   L-s
                    //   / \/   \/ \          ======> Reversal signal
                    //  /
                    // /

                    if (Math.Abs(avLows[1].Low - avLows[3].Low) < 0.01)
                    {
                        //lows are same height
                        if (avHighs[3].High > avHighs[2].High && avHighs[2].High < avHighs[1].High)
                        {
                            //probably headnshoulders, try to sell at profit
                            Traded = true;
                            Stock st = exchange.GetCheapestStock(cp);
                            if (st != null)
                            {
                                foreach (Stock stock in cpstocks)
                                {
                                    exchange.SellStock(stock, st.SellPrice);
                                }
                            }
                        }
                    }
                }

                //flag pattern
                //          Flat top        H  H-s  H-s
                //    _    _  _               L  L-h  L-h
                //   / \  / \/       ======> powerful break signal
                //  /   \/
                // /

                if (Math.Abs(avHighs[0].High - avHighs[4].High) < 0.02)
                {
                    //flat top, first satisfaction
                    if (avLows[4].Low < avLows[2].Low && avLows[2].Low < avLows[0].Low)
                    {
                        //FLAG!!! BUY BUY BUY!!!!!!!
                        Traded = true;
                        for (int i = 0; i < 100; i++)
                        {
                            Stock st = exchange.GetCheapestStock(cp);
                            if (exchange.BuyStock(st, trader)) //if transaction succeeded
                            {
                                exchange.SellStock(st, st.SellPrice * 1.02);
                            }
                        }
                    }
                }

                //wedge pattern
                //
                //    /\ 
                //   /  \    /\  __       ======> direction of break uncertain
                //  /    \  /  \/
                // /      \/

                if (avHighs[4].High > avHighs[2].High && avHighs[2].High > avHighs[0].High)
                {
                    //decreasing highs
                    if (avLows[4].Low < avLows[2].Low && avLows[2].Low < avLows[0].Low)
                    {
                        //Wedge, maybe buy
                        Traded = true;
                        if (rn.NextDouble() < trader.skill)
                        {
                            //sell
                            if (OwnsStocks)
                            {
                                Stock st = exchange.GetCheapestStock(cp);
                                if (st != null)
                                {
                                    foreach (Stock stock in cpstocks)
                                    {
                                        exchange.SellStock(stock, st.SellPrice * 1.005);
                                    }
                                }
                            }
                        }
                        else
                        {
                            //buy
                            for (int i = 0; i < 10; i++)
                            {
                                Stock st = exchange.GetCheapestStock(cp);
                                if (exchange.BuyStock(st, trader)) //if transaction succeeded
                                {
                                    exchange.SellStock(st, st.SellPrice * 1.0015);
                                }
                            }
                        }
                    }
                }
                #endregion
                return(Traded);
            }