Example #1
0
        private async void ViewProfitColors(OptionTrade trade)
        {
            if (trade.Spread.ExpirationDate == Experation && trade.Spread.Options[0].Strike > 0)
            {
                double breakEven = await trade.Spread.BreakEven(trade.TradeDate);

                if (trade.Spread.ProfitDirection == -1)
                {
                    if (Strike <= breakEven)
                    {
                        labelStrike.BackColor = Color.Green;
                    }
                    else
                    {
                        labelStrike.BackColor = Color.Red;
                    }
                }
                if (trade.Spread.ProfitDirection == 1)
                {
                    if (Strike >= breakEven)
                    {
                        labelStrike.BackColor = Color.Green;
                    }
                    else
                    {
                        labelStrike.BackColor = Color.Red;
                    }
                }
            }
        }
Example #2
0
        public async void Fill(OptionTrade trade, DateTime dataDate)
        {
            Clear();
            if (trade.Spread.Options[0] != null || trade.Spread.Options[0].Strike != 0)
            {
                _trade = trade;
                if (trade.Spread.Options[0].Strike > 0 && trade.Spread.Options[1].Strike == 0)
                {
                    if ((trade.Spread.Options[0].OptionType == OptionType.Call && trade.Spread.Options[0].PositionType == PositionType.Short))
                    {
                        return;
                    }

                    Option[] newSpreadOption = new Option[1];
                    newSpreadOption[0] = trade.Spread.Options[0];
                    _trade.Spread      = new Spread(newSpreadOption);
                }
                double cost = await _trade.Spread.Cost(dataDate);

                labelCostValue.Text = cost.ToString();
                _filled             = true;
                if (cost > 0)
                {
                    labelCostValue.ForeColor = Color.Green;
                }
                else if (cost < 0)
                {
                    labelCostValue.ForeColor = Color.Red;
                }
            }
        }
Example #3
0
        private async Task <bool> OpenPosition(OptionTrade trade, Api.Interval interval)
        {
            TimeSpan  updateSpan = DateTime.UtcNow - trade.TradeDate;
            StockData data       = await StockDataBase.Get(trade.Spread.Stock, interval, updateSpan);

            OptionPosition position = GetPosition(trade.Spread);

            position.Spread = trade.Spread;

            double totalBalance = await TotalBalance(trade.TradeDate, interval);

            position.EntryDate = trade.TradeDate;
            double margin = await trade.Spread.MaxLoss(trade.TradeDate);

            double budget = totalBalance * trade.PortfolioPercentage;

            if (trade.Amount == 0)
            {
                position.Amount = (int)(budget / (margin * (1 + Slippage) + Fee));
            }
            else
            {
                double totalCost = (margin * (1 + Slippage) + Fee) * trade.Amount;
                if (Cash >= totalCost)
                {
                    position.Amount = trade.Amount;
                }
                else
                {
                    position.Amount = 0;
                }
            }
            Cash -= position.Amount * (margin * (1 + Slippage) + Fee);
            return(true);
        }
        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 #5
0
        public void SetTrade(OptionTrade trade)
        {
            _previewTrade = trade;
            foreach (OptionChainDateExpandable expandable in panelChain.Controls)
            {
                expandable.ShowTrade(trade);
            }

            TradeChanged?.Invoke(trade);
        }
Example #6
0
        private async void FillLabelsGainLoss(OptionTrade trade, DateTime dataDate)
        {
            double maxGain = await trade.Spread.MaxProfit(dataDate) * trade.Amount;

            labelMaxGainValue.Text = maxGain.ToString();

            double maxLoss = await trade.Spread.MaxLoss(dataDate) * -trade.Amount;

            labelMaxLossValue.Text = maxLoss.ToString();
        }
Example #7
0
        public async void MakeTrade(OptionTrade trade, Api.Interval interval)
        {
            if (trade.Spread != null)
            {
                ClosePosition(trade, interval);
                bool x = await OpenPosition(trade, interval);

                TradeMade?.Invoke(this, new EventArgs());
            }
        }
Example #8
0
        private async void ClosePosition(OptionTrade trade, Api.Interval interval)
        {
            TimeSpan  updateSpan = DateTime.UtcNow - trade.TradeDate;
            StockData data       = await StockDataBase.Get(trade.Spread.Stock, interval, updateSpan);

            OptionPosition position = GetPosition(trade.Spread);
            double         posValue = await GetPositionValue(position, data, trade.TradeDate);

            Cash           += posValue * (1 - Slippage) - Fee * position.Amount;
            position.Amount = 0;
        }
Example #9
0
        private void sellToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OptionTrade trade = new OptionTrade()
            {
                Amount    = 0,
                TradeDate = _dataDate,
                Spread    = _position.Spread
            };

            SellPositionEvent?.Invoke(trade);
        }
Example #10
0
 public void ChangeTradeQuantity(int quantity)
 {
     if (_previewTrade.Spread.Options[0].Strike > 0)
     {
         OptionTrade newTrade = _previewTrade;
         if (quantity >= 1)
         {
             newTrade.Amount = quantity;
             SetTrade(newTrade);
         }
     }
 }
Example #11
0
 public void ShowTrade(OptionTrade trade)
 {
     if (trade.Spread.Options.Length > 0)
     {
         foreach (Control c in panelChain.Controls)
         {
             if (c is OptionChainStrikeLine)
             {
                 ((OptionChainStrikeLine)c).ShowTrade(trade);
             }
         }
     }
 }
Example #12
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 #13
0
 public void CloseExpiredPositions(DateTime date)
 {
     foreach (OptionPosition position in Positions)
     {
         if (position.Spread.DaysUntilExpiration(date) <= 0)
         {
             OptionTrade closeTrade = new OptionTrade()
             {
                 Amount              = 0,
                 TradeDate           = date,
                 PortfolioPercentage = 0,
                 Spread              = position.Spread
             };
             ClosePosition(closeTrade, Api.Interval.Daily);
         }
     }
 }
Example #14
0
        public void ShowTrade(OptionTrade trade)
        {
            CallUnselect();
            PutUnselect();
            ClearProfitColors();
            if (trade.Spread != null)
            {
                if (trade.Spread.Options.Length > 0)
                {
                    ViewOption(trade.Spread.Options[0]);
                }

                if (trade.Spread.Options.Length > 1)
                {
                    ViewOption(trade.Spread.Options[1]);
                }

                ViewProfitColors(trade);
            }
        }
Example #15
0
        public void MakeTrade(OptionTrade trade)
        {
            if (trade.Spread.Options[0] != null || trade.Spread.Options[0].Strike != 0)
            {
                if (trade.Spread.Options.Length == 2)
                {
                    if (trade.Spread.Options[0].Strike > 0 && trade.Spread.Options[1].Strike == 0)
                    {
                        if ((trade.Spread.Options[0].OptionType == OptionType.Call && trade.Spread.Options[0].PositionType == PositionType.Short))
                        {
                            return;
                        }

                        Option[] newSpreadOption = new Option[1];
                        newSpreadOption[0] = trade.Spread.Options[0];
                        trade.Spread       = new Spread(newSpreadOption);
                    }
                }
                _portfolio.MakeTrade(trade, Api.Interval.Daily);
            }
        }
Example #16
0
 public void Fill(OptionTrade trade, DateTime dataDate)
 {
     Clear();
     if (trade.Spread.Options[0] != null)
     {
         if (trade.Spread.Options[0].Strike > 0)
         {
             OptionChain          chain   = OptionDataBase.Get(trade.Spread.Stock, dataDate);
             OptionChainDataPoint option1 = chain.FindOption(trade.Spread.Options[0].OptionType, trade.Spread.Options[0].ExpirationDate, trade.Spread.Options[0].Strike);
             if (trade.Spread.Options[0].PositionType == PositionType.Long)
             {
                 orderLineUI1.Fill(option1, trade.Amount);
             }
             else if (trade.Spread.Options[0].PositionType == PositionType.Short)
             {
                 orderLineUI1.Fill(option1, trade.Amount * -1);
             }
         }
     }
     if (trade.Spread.Options[1] != null)
     {
         if (trade.Spread.Options[1].Strike > 0)
         {
             OptionChain          chain   = OptionDataBase.Get(trade.Spread.Stock, dataDate);
             OptionChainDataPoint option1 = chain.FindOption(trade.Spread.Options[1].OptionType, trade.Spread.Options[1].ExpirationDate, trade.Spread.Options[1].Strike);
             if (trade.Spread.Options[1].PositionType == PositionType.Long)
             {
                 orderLineUI2.Fill(option1, trade.Amount);
             }
             else if (trade.Spread.Options[1].PositionType == PositionType.Short)
             {
                 orderLineUI2.Fill(option1, trade.Amount * -1);
             }
         }
     }
 }
Example #17
0
 public void ViewTrade(OptionTrade trade, DateTime date)
 {
     _currentDate = date;
     _chainUI.SetTrade(trade);
     orderUI1.ViewOrder(trade, _currentDate);
 }
Example #18
0
 private void FireOrderPlaced(OptionTrade trade)
 {
     OrderPlacedClicked?.Invoke(trade);
 }
Example #19
0
 public void ViewOrder(OptionTrade trade, DateTime dataDate)
 {
     orderListUI1.Fill(trade, dataDate);
     orderBuyUI1.Fill(trade, dataDate);
     FillLabelsGainLoss(trade, dataDate);
 }
Example #20
0
 private void PlaceOrder(OptionTrade trade)
 {
     _sim.MakeTrade(trade);
 }
Example #21
0
        private void QuantityMoreClicked(object sender, EventArgs e)
        {
            OptionTrade currentTrade = _chainUI.CurrentTrade;

            _chainUI.ChangeTradeQuantity(currentTrade.Amount + 1);
        }
Example #22
0
 private void ChainUITradeChanged(OptionTrade trade)
 {
     orderUI1.ViewOrder(trade, _currentDate);
 }
Example #23
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);
        }
Example #24
0
 private void FireSellEvent(OptionTrade trade)
 {
     SellPositionEvent?.Invoke(trade);
 }
Example #25
0
 private void OrderPlacedClicked(OptionTrade trade)
 {
     Clear();
     OrderPlaced?.Invoke(trade);
 }