public void Close__MovesToClosed_AddsCash_AddsValueOnPosition(PositionDir dir, float open, float close, float vol)
        {
            Position pos = CreatePosition(dir, open, vol);

            _testObj.PositionsActive.Add(pos);
            _testObj.Close(0, CurrentTS, close, Commission);
            _testObj.Cash.ShouldBe(CashValue + pos.DirectionMultiplier() * (close * vol) - Commission);
            _testObj.PositionsActive.Count.ShouldBe(0);
            _testObj.PositionsClosed.Count.ShouldBe(1);
            _testObj.ClosedPositionsEquity.Count.ShouldBe(1);
            CheckClosedPosition(0, dir, open, close, vol, Commission, CurrentTS, 0);
        }
示例#2
0
 public static void CloseAll(this SystemState systemState, DateTime ts, float price, ISlippage slippage, ICommission commission)
 {
     while (systemState.PositionsActive.Count > 0)
     {
         systemState.Close(0, ts, price, slippage, commission);
     }
 }
示例#3
0
 private void CloseAllPositions(Signal signal, DateTime ts, SystemState systemState, Func <Signal, StockPricesData, int, float> openPriceSelector)
 {
     while (systemState.PositionsActive.Count > 0)
     {
         float closePrice = CalculatePrice(systemState.PositionsActive[0].Stock.FullName, systemState.PositionsActive[0].DataRange, systemState.PositionsActive[0].IntradayInterval, signal, ts, openPriceSelector);
         systemState.Close(0, ts, closePrice, _commission.Calculate(systemState.PositionsActive[0].Stock.Type, systemState.PositionsActive[0].Volume, closePrice));
     }
 }
示例#4
0
        public static void AddToPosition(this SystemState systemState, int positionIndex, DateTime ts, float price, float volume, Signal signal, ISlippage slippage, ICommission commission)
        {
            Position pos = systemState.PositionsActive[positionIndex];

            systemState.Close(positionIndex, ts, price, slippage, commission);
            if (pos.Volume + volume > 0)
            {
                float openPrice = systemState.CalculateSlippageOpen(slippage, ts, signal, price);
                systemState.Open(signal.Stock, pos.Direction, ts, openPrice, pos.Volume + volume, systemState.CalculateCommission(commission, signal, openPrice), signal.DataRange, signal.IntradayInterval, signal);
            }
        }
        private void ReversePosition(Signal signal, DateTime ts, SystemState systemState, float openPrice)
        {
            PositionDir newPosDir = signal.Direction;
            int         currPos   = FindActivePositionIndex(systemState, signal.Stock);

            if (currPos > -1)
            {
                newPosDir = systemState.PositionsActive[currPos].ReversedDirection();
                systemState.Close(currPos, ts, openPrice, _slippage, _commission);
            }
            systemState.Open(ts, newPosDir, openPrice, signal, _slippage, _commission);
        }
示例#6
0
        public static void Close(this SystemState systemState, int positionIndex, DateTime ts, float price, ISlippage slippage, ICommission commission)
        {
            float closePrice = systemState.CalculateSlippageClose(slippage, ts, positionIndex, price);

            systemState.Close(positionIndex, ts, closePrice, systemState.CalculateCommission(commission, positionIndex, closePrice));
        }
 private void ProcessPosition(DateTime ts, SystemState systemState, int positionIndex, Func<Position, StockPricesData, int, float> closePriceSelector, StockPricesData pricesData, int pricesDataIndex)
 {
     systemState.Close(positionIndex, ts, closePriceSelector(systemState.PositionsActive[positionIndex], pricesData, pricesDataIndex), _slippage, _commission);
 }