Пример #1
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;
        }
Пример #2
0
        private double GetPositionValue(StockPosition position, StockData data, int dataIndex)
        {
            double posValue;
            double entryPrice = data.TimeSeries.DataPoints[position.EntryIndex].Close;

            if (position.Type == StockTradeType.LongStock)
            {
                posValue = data.TimeSeries.DataPoints[dataIndex].Close * position.Amount;
            }
            else
            {
                posValue = (entryPrice + entryPrice - data.TimeSeries.DataPoints[dataIndex].Close) * position.Amount;
            }
            return(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
        private StockPosition GetPosition(Stock stock)
        {
            if (stock != null)
            {
                for (int i = 0; i < Positions.Count; i++)
                {
                    if (Positions[i].Stock.StockSymbol == stock.StockSymbol)
                    {
                        return(Positions[i]);
                    }
                }
            }
            StockPosition newPos = new StockPosition();

            newPos.Stock = stock;
            Positions.Add(newPos);
            return(Positions[Positions.Count - 1]);
        }