Example #1
0
        private async void buttonRun_Click(object sender, EventArgs e)
        {
            Stock stock = GlobalObjects.Chart.Stock;

            textBoxStock.Text = stock.StockSymbol;
            StockData data = await StockDataBase.Get(stock, Api.Interval.Daily);

            int dataCount = data.TimeSeries.DataPoints.Count;

            double   simCash                = 10000;
            DateTime simStartDate           = new DateTime(2016, 1, 1);
            DateTime simEndDate             = new DateTime(2017, 5, 1);
            double   alpha                  = GetStockAlphaBalance(data, simCash, simStartDate, simEndDate);
            double   tradeResult            = 0;
            IOptionTradeAlgorithm tradeAlgo = new BuyAndManageLCV();

            OptionTradeList tradeList = await tradeAlgo.Find(stock, simStartDate, simEndDate);

            OptionTradeSimulation sim = new OptionTradeSimulation("OptionSim", simCash, simStartDate, simEndDate, 1.1, 0.04);

            sim.Run(tradeList);
            GlobalObjects.Chart.Graphs.Clear();
            GlobalObjects.Chart.Graphs.Add(sim.BalanceChart);

            tradeResult = sim.BalanceChart.DataPoints[sim.BalanceChart.DataPoints.Count - 1].GraphData;


            labelResultRightPercent.Text = tradeResult.ToString();
            labelResultAlpha.Text        = alpha.ToString();
        }
        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);
        }
Example #3
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);
        }
Example #4
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);
                }
            }
        }
Example #5
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);
        }