public async Task <OptionTradeList> Find(Stock stock, DateTime startDate, DateTime endDate)
        {
            OptionTradeList tradeList = new OptionTradeList(stock, Api.Interval.Daily);
            StockData       data      = await StockDataBase.Get(stock, Api.Interval.Daily);

            int startIndex = data.FindDateIndex(startDate);
            int endIndex   = data.FindDateIndex(endDate);

            int    tradeHoldLenght      = 30;
            double tradeHoldMaxGain     = 0.9;
            double tradeHoldMaxLoss     = -0.25;
            double portofolioPercentage = 0.8;

            bool inTrade = false;

            for (int i = startIndex; i <= endIndex; i++)
            {
                DateTime today = data.TimeSeries.DataPoints[i].DateTime;
                if (!inTrade)
                {
                    double      close  = data.TimeSeries.DataPoints[i].Close;
                    Spread      spread = Spread.GetSpread(stock, SpreadType.LongCallVertical, close - close * 0.1, 5, today.AddDays(40), today);
                    OptionTrade trade  = new OptionTrade
                    {
                        TradeDate           = data.TimeSeries.DataPoints[i].DateTime,
                        PortfolioPercentage = portofolioPercentage,
                        Spread = spread
                    };
                    tradeList.Trades.Add(trade);
                    inTrade = true;
                }
                else
                {
                    OptionTrade lastTrade = tradeList.Trades[tradeList.Trades.Count - 1];
                    double      openPrice = await lastTrade.Spread.Price(lastTrade.TradeDate);

                    double currentPrice = await lastTrade.Spread.Price(today);

                    double percentageGain = (currentPrice - openPrice) / openPrice;
                    int    dateIndex      = data.FindDateIndex(lastTrade.TradeDate);

                    if (i - dateIndex >= tradeHoldLenght || lastTrade.Spread.DaysUntilExpiration(today) < 5 || percentageGain > tradeHoldMaxGain || percentageGain < tradeHoldMaxLoss)
                    {
                        OptionTrade trade = new OptionTrade
                        {
                            TradeDate           = data.TimeSeries.DataPoints[i].DateTime,
                            PortfolioPercentage = 0,
                            Spread = lastTrade.Spread
                        };
                        tradeList.Trades.Add(trade);
                        inTrade = false;
                    }
                }
            }
            return(tradeList);
        }
Exemplo n.º 2
0
        private double GetStockAlphaBalance(StockData data, double cash, DateTime startDate, DateTime endDate)
        {
            int dataCount  = data.TimeSeries.DataPoints.Count;
            int startIndex = data.FindDateIndex(startDate);
            int stopIndex  = data.FindDateIndex(endDate);

            if (stopIndex == 0)
            {
                stopIndex = dataCount - 1;
            }

            double alpha = Indicators.GetStockAlpha(data, startIndex, stopIndex - startIndex + 1);

            return(cash * alpha);
        }
Exemplo n.º 3
0
        public void RenderChart(PictureBox renderbox, StockData stockData, int startIndex, int count, GraphData[] additionalGraphs)
        {
            RenderChart(renderbox, stockData, startIndex, count);

            if (additionalGraphs.Length > 0)
            {
                for (int i = 0; i < additionalGraphs.Length; i++)
                {
                    if (count >= 2 && stockData.TimeSeries.DataPoints.Count >= startIndex + count)
                    {
                        DateTime startDate           = stockData.TimeSeries.DataPoints[startIndex].DateTime;
                        DateTime endDate             = stockData.TimeSeries.DataPoints[startIndex + count - 1].DateTime;
                        int      graphDataStartIndex = additionalGraphs[i].FindDateIndex(startDate);
                        int      graphCount          = additionalGraphs[i].DataPoints.Count - graphDataStartIndex >= count ? count : additionalGraphs[i].DataPoints.Count - graphDataStartIndex;
                        DateTime graphStartDate      = additionalGraphs[i].DataPoints[graphDataStartIndex].DateTime;
                        int      graphStockDataIndex = stockData.FindDateIndex(graphStartDate);
                        int      rightShift          = graphStockDataIndex - startIndex;
                        Color    graphColor          = GraphColors[i % 4];

                        if (additionalGraphs[i].DataPoints[graphDataStartIndex].DateTime >= startDate && additionalGraphs[i].DataPoints[graphDataStartIndex].DateTime <= endDate)
                        {
                            DrawGraphData(additionalGraphs[i], graphDataStartIndex, graphCount, rightShift, graphColor);
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        public async Task <OptionTradeList> Find(Stock stock, DateTime startDate, DateTime endDate)
        {
            OptionTradeList tradeList = new OptionTradeList(stock, Api.Interval.Daily);
            StockData       data      = await StockDataBase.Get(stock, Api.Interval.Daily);

            int startIndex = data.FindDateIndex(startDate);
            int endIndex   = data.FindDateIndex(endDate);

            int tradeHoldLenght = 2;

            bool inTrade = false;

            for (int i = startIndex; i <= endIndex; i++)
            {
                if (!inTrade)
                {
                    Option[] options = new Option[2];
                    options[0] = new Option(stock, OptionType.Put, PositionType.Short, 230, startDate.AddDays(25));
                    options[1] = new Option(stock, OptionType.Put, PositionType.Long, 240, startDate.AddDays(25));
                    Spread      spread = new Spread(options);
                    OptionTrade trade  = new OptionTrade
                    {
                        TradeDate           = data.TimeSeries.DataPoints[i].DateTime,
                        PortfolioPercentage = 1,
                        Spread = spread
                    };
                    tradeList.Trades.Add(trade);
                    inTrade = true;
                }
                else
                {
                    OptionTrade lastTrade = tradeList.Trades[tradeList.Trades.Count - 1];
                    int         dateIndex = data.FindDateIndex(lastTrade.TradeDate);
                    if (i - dateIndex >= tradeHoldLenght)
                    {
                        OptionTrade trade = new OptionTrade
                        {
                            TradeDate           = data.TimeSeries.DataPoints[i].DateTime,
                            PortfolioPercentage = 0,
                            Spread = lastTrade.Spread
                        };
                        tradeList.Trades.Add(trade);
                    }
                }
            }
            return(tradeList);
        }
Exemplo n.º 5
0
        public async void Run(StockTradeList tradeList)
        {
            if (tradeList != null && tradeList.Trades.Count > 0)
            {
                //find all stocks traded
                StockList stocks = new StockList("TradeList");
                foreach (StockTrade trade in tradeList.Trades)
                {
                    stocks.Add(trade.Stock);
                }

                //find DataRange
                StockData firstData = await StockDataBase.Get(stocks.GetStock(0), tradeList.Interval);

                int startIndex = firstData.FindDateIndex(_startDate);
                int stopIndex  = firstData.FindDateIndex(_endDate);
                int dataCount  = firstData.TimeSeries.DataPoints.Count;

                if (stopIndex == 0)
                {
                    stopIndex = dataCount - 1;
                }

                //Simulate trades
                double         startBalance     = _balanceChart[0];
                StockPortfolio portfolio        = new StockPortfolio(startBalance);
                List <double>  balanceChartList = new List <double>();
                balanceChartList.Add(startBalance);

                for (int i = startIndex; i <= stopIndex; i++)
                {
                    foreach (StockTrade trade in tradeList.Trades)
                    {
                        if (trade.DataIndex == i)
                        {
                            portfolio.MakeTrade(trade, tradeList.Interval);
                        }
                    }
                    balanceChartList.Add(await portfolio.TotalBalance(i, tradeList.Interval));
                }
                _balanceChart = balanceChartList.ToArray();
            }
        }
Exemplo n.º 6
0
        public async Task <double> Price(DateTime date)
        {
            double    price      = 0;
            TimeSpan  updateSpan = DateTime.UtcNow - date;
            StockData data       = await StockDataBase.Get(this, Api.Interval.Daily, updateSpan);

            if (data != null)
            {
                price = data.TimeSeries.DataPoints[data.FindDateIndex(date)].Close;
            }
            return(price);
        }
Exemplo n.º 7
0
        public async static Task <GraphData> PriceGraph(DateTime openDate, Option option)
        {
            StockData data = await StockDataBase.Get(option.Stock, Api.Interval.Daily);

            int startIndex = data.FindDateIndex(openDate);
            int endIndex   = data.FindDateIndex(option.ExpirationDate);

            GraphData graph = new GraphData("PriceGraph " + option.Stock.StockSymbol + " " + option.Strike.ToString() + option.OptionType.ToString());

            for (int i = startIndex; i <= endIndex; i++)
            {
                DateTime dataPointDate = data.TimeSeries.DataPoints[i].DateTime;
                Option   opt           = Option.GetOption(option.Stock, option.OptionType, option.PositionType, option.Strike, option.ExpirationDate, dataPointDate);
                double   price         = await opt.Price(dataPointDate);

                GraphDataPoint point = new GraphDataPoint(dataPointDate, price);
                graph.DataPoints.Add(point);
            }

            return(graph);
        }
Exemplo n.º 8
0
        public async void Run(OptionTradeList tradeList)
        {
            if (tradeList != null && tradeList.Trades.Count > 0)
            {
                //find DataRange
                StockData firstData = await StockDataBase.Get(tradeList.Stock, tradeList.Interval);

                int startIndex = firstData.FindDateIndex(_startDate);
                int stopIndex  = firstData.FindDateIndex(_endDate);
                int dataCount  = firstData.TimeSeries.DataPoints.Count;

                if (stopIndex == 0)
                {
                    stopIndex = dataCount - 1;
                }

                //Simulate trades
                OptionPortfolio portfolio = new OptionPortfolio(_startBalance, _fee, _slippage);
                _balanceChart = new GraphData(_name + " Balance");

                for (int i = startIndex; i <= stopIndex; i++)
                {
                    foreach (OptionTrade trade in tradeList.Trades)
                    {
                        int dateIndex = firstData.FindDateIndex(trade.TradeDate);
                        if (dateIndex == i)
                        {
                            portfolio.MakeTrade(trade, tradeList.Interval);
                        }
                    }
                    DateTime       date = firstData.TimeSeries.DataPoints[i].DateTime;
                    GraphDataPoint balanceGraphDataPoint = new GraphDataPoint(date, await portfolio.TotalBalance(date, tradeList.Interval));
                    _balanceChart.DataPoints.Add(balanceGraphDataPoint);
                }
            }
        }
Exemplo n.º 9
0
        public async void ChangeStock(Stock stock)
        {
            TimeSpan  updateSpan = DateTime.UtcNow - _simDate;
            StockData stockData  = await StockDataBase.Get(stock, Api.Interval.Daily, updateSpan);

            if (stockData.TimeSeries.DataPoints[0].DateTime <= _simDate)
            {
                _stockData          = new StockData();
                _stockData.MetaData = stockData.MetaData;
                _dataIndex          = stockData.FindDateIndex(_simDate);
                if (_dataIndex <= stockData.TimeSeries.DataPoints.Count)
                {
                    _stockData.TimeSeries.DataPoints = stockData.TimeSeries.DataPoints.GetRange(0, _dataIndex + 1);
                }
            }
        }
Exemplo n.º 10
0
            public async Task <double> Price(DateTime date)
            {
                double      price = 0;
                OptionChain chain = OptionDataBase.Get(_stock, date);

                if (chain != null)
                {
                    OptionChainDataPoint option = chain.FindOption(_optionType, _expDate, _strike);
                    price = (option.Bid + option.Ask) * 0.5;
                }
                else
                {
                    TimeSpan  updateSpan = DateTime.UtcNow - date;
                    StockData data       = await StockDataBase.Get(_stock, Api.Interval.Daily, updateSpan);

                    int index      = data.FindDateIndex(date);
                    int expiration = DaysUntilExpiration(date);
                    price = Indicators.Options.AproxPrice(data, index, _optionType, _strike, expiration);
                }
                return(price);
            }
Exemplo n.º 11
0
        public async Task <OptionTradeList> Find(Stock stock, DateTime startDate, DateTime endDate)
        {
            OptionTradeList tradeList = new OptionTradeList(stock, Api.Interval.Daily);
            StockData       data      = await StockDataBase.Get(stock, Api.Interval.Daily);

            int startIndex = data.FindDateIndex(startDate);
            int endIndex   = data.FindDateIndex(endDate);

            int    period         = 40;
            double priceGainGate  = -0.5;
            double volumeGainGate = 0.5;

            int    tradeHoldLenght      = 15;
            double tradeHoldMaxGain     = 0.9;
            double tradeHoldMaxLoss     = -0.25;
            double portofolioPercentage = 0.8;

            bool inTrade = false;

            for (int i = startIndex; i <= endIndex; i++)
            {
                if (!inTrade)
                {
                    {
                        DateTime today          = data.TimeSeries.DataPoints[i].DateTime;
                        double   open           = data.TimeSeries.DataPoints[i].Open;
                        double   close          = data.TimeSeries.DataPoints[i].Close;
                        double   percentageGain = (close - open) / open;
                        if (percentageGain > priceGainGate)
                        {
                            double volume               = data.TimeSeries.DataPoints[i].Volume;
                            double volumeSMA            = Indicators.GetSMA(data, i, period, PricePoint.Volume);
                            double volumePercentageGain = (volume - volumeSMA) / volumeSMA;
                            if (volumePercentageGain > volumeGainGate)
                            {
                                Option[] options = new Option[2];
                                options[0] = new Option(stock, OptionType.Call, PositionType.Long, Math.Round(close, 0) - 5, today.AddDays(40));
                                options[1] = new Option(stock, OptionType.Call, PositionType.Short, Math.Round(close, 0) + 0, today.AddDays(40));
                                Spread      spread = new Spread(options);
                                OptionTrade trade  = new OptionTrade
                                {
                                    TradeDate           = data.TimeSeries.DataPoints[i].DateTime,
                                    PortfolioPercentage = portofolioPercentage,
                                    Spread = spread
                                };
                                tradeList.Trades.Add(trade);
                                inTrade = true;
                            }
                        }
                    }
                }
                else
                {
                    OptionTrade lastTrade = tradeList.Trades[tradeList.Trades.Count - 1];
                    DateTime    today     = data.TimeSeries.DataPoints[i].DateTime;
                    int         dateIndex = data.FindDateIndex(lastTrade.TradeDate);
                    double      openPrice = await lastTrade.Spread.Price(lastTrade.TradeDate);

                    double currentPrice = await lastTrade.Spread.Price(today);

                    double percentageGain = (currentPrice - openPrice) / openPrice;

                    if (i - dateIndex >= tradeHoldLenght || lastTrade.Spread.DaysUntilExpiration(today) < 5 || percentageGain > tradeHoldMaxGain || percentageGain < tradeHoldMaxLoss)
                    {
                        OptionTrade trade = new OptionTrade
                        {
                            TradeDate           = data.TimeSeries.DataPoints[i].DateTime,
                            PortfolioPercentage = 0,
                            Spread = lastTrade.Spread
                        };
                        tradeList.Trades.Add(trade);
                        inTrade = false;
                    }
                }
            }
            return(tradeList);
        }
Exemplo n.º 12
0
        public async Task <StockTradeList> Find(Stock stock, DateTime startDate, DateTime endDate)
        {
            StockTradeList tradeList = new StockTradeList(Api.Interval.Daily);
            StockData      data      = await StockDataBase.Get(stock, Api.Interval.Daily);

            int    startIndex     = data.FindDateIndex(startDate);
            int    period         = 40;
            double priceGainGate  = -0.5;
            double volumeGainGate = 0.5;

            int    tradeHoldLenght  = 15;
            double tradeHoldMaxGain = 0.04;
            double tradeHoldMaxLoss = -0.04;

            bool inTrade = false;

            for (int i = startIndex; i < data.TimeSeries.DataPoints.Count; i++)
            {
                if (!inTrade)
                {
                    {
                        double open           = data.TimeSeries.DataPoints[i].Open;
                        double close          = data.TimeSeries.DataPoints[i].Close;
                        double percentageGain = (close - open) / open;
                        if (percentageGain > priceGainGate)
                        {
                            double volume               = data.TimeSeries.DataPoints[i].Volume;
                            double volumeSMA            = Indicators.GetSMA(data, i, period, PricePoint.Volume);
                            double volumePercentageGain = (volume - volumeSMA) / volumeSMA;
                            if (volumePercentageGain > volumeGainGate)
                            {
                                StockTrade trade = new StockTrade
                                {
                                    DataIndex           = i,
                                    PortfolioPercentage = 1,
                                    Stock = stock,
                                    Type  = StockTradeType.LongStock
                                };
                                tradeList.Trades.Add(trade);
                                inTrade = true;
                            }
                        }
                    }
                }
                else
                {
                    StockTrade lastTrade      = tradeList.Trades[tradeList.Trades.Count - 1];
                    double     posOpen        = data.TimeSeries.DataPoints[lastTrade.DataIndex].Close;
                    double     currentPrice   = data.TimeSeries.DataPoints[i].Close;
                    double     percentageGain = (currentPrice - posOpen) / posOpen;

                    if (i - lastTrade.DataIndex >= tradeHoldLenght)
                    {
                        StockTrade trade = new StockTrade
                        {
                            DataIndex           = i,
                            PortfolioPercentage = 0,
                            Stock = stock,
                            Type  = StockTradeType.LongStock
                        };
                        tradeList.Trades.Add(trade);
                        inTrade = false;
                    }
                    else if (percentageGain > tradeHoldMaxGain || percentageGain < tradeHoldMaxLoss)
                    {
                        StockTrade trade = new StockTrade
                        {
                            DataIndex           = i,
                            PortfolioPercentage = 0,
                            Stock = stock,
                            Type  = StockTradeType.LongStock
                        };
                        tradeList.Trades.Add(trade);
                        inTrade = false;
                    }
                }
            }

            return(tradeList);
        }
Exemplo n.º 13
0
        public async void Run(StockTradeList tradeList)
        {
            if (tradeList != null && tradeList.Trades.Count > 0)
            {
                //find all stocks traded
                StockList stocks = new StockList("TradeList");
                foreach (StockTrade trade in tradeList.Trades)
                {
                    stocks.Add(trade.Stock);
                }

                //find DataRange
                StockData firstData = await StockDataBase.Get(stocks.GetStock(0), tradeList.Interval);

                int startIndex = firstData.FindDateIndex(_startDate);
                int stopIndex  = firstData.FindDateIndex(_endDate);
                int dataCount  = firstData.TimeSeries.DataPoints.Count;

                if (stopIndex == 0)
                {
                    stopIndex = dataCount - 1;
                }

                //Simulate trades
                double     timesTraded  = 0;
                double     timesRight   = 0;
                StockTrade currentTrade = new StockTrade();
                StockData  data         = firstData;
                for (int i = startIndex; i <= stopIndex; i++)
                {
                    foreach (StockTrade trade in tradeList.Trades)
                    {
                        if (trade.DataIndex == i)
                        {
                            if (trade.PortfolioPercentage > 0)
                            {
                                data = await StockDataBase.Get(trade.Stock, tradeList.Interval);

                                currentTrade = trade;
                            }
                            else
                            {
                                timesTraded++;
                                double openPrice  = data.TimeSeries.DataPoints[currentTrade.DataIndex].Close;
                                double closePrice = data.TimeSeries.DataPoints[i].Close;

                                if (currentTrade.Type == StockTradeType.LongStock && closePrice > openPrice)
                                {
                                    timesRight++;
                                }
                                if (currentTrade.Type == StockTradeType.ShortStock && closePrice < openPrice)
                                {
                                    timesRight++;
                                }
                            }
                        }
                    }
                }

                //save results
                _rightPredictPercentage = timesRight / timesTraded;
                _tradesMade             = (int)timesTraded;
            }
        }