예제 #1
0
 public void MakeTrade(StockTrade trade, Api.Interval interval)
 {
     if (trade.Stock != null)
     {
         ClosePosition(trade, interval);
         OpenPosition(trade, interval);
     }
 }
예제 #2
0
        private async void ClosePosition(StockTrade trade, Api.Interval interval)
        {
            StockData data = await StockDataBase.Get(trade.Stock, interval);

            StockPosition position = GetPosition(trade.Stock);
            double        posValue = GetPositionValue(position, data, trade.DataIndex);

            position.Amount = 0;
            Cash           += posValue;
        }
예제 #3
0
        private async void OpenPosition(StockTrade trade, Api.Interval interval)
        {
            StockData data = await StockDataBase.Get(trade.Stock, interval);

            StockPosition position     = GetPosition(trade.Stock);
            double        totalBalance = await TotalBalance(trade.DataIndex, interval);

            position.EntryIndex = trade.DataIndex;
            double entryPrice = data.TimeSeries.DataPoints[trade.DataIndex].Close;
            double budget     = totalBalance * trade.PortfolioPercentage;

            position.Amount = (int)(budget / entryPrice);
            position.Type   = trade.Type;//long or short
            Cash           -= position.Amount * entryPrice;
        }
예제 #4
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);
        }
예제 #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     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;
            }
        }