public void DispatchOrders(Tick tick, out List<Order> orders, out List<Position> positions, out List<Position> trades) { for (int i = 0; i < _orders.Count; i++) { Order order = _orders[i]; switch (order.State) { case OrderState.Completed: break; case OrderState.Filled: DispatchPositions(order, tick, i); break; case OrderState.Initialized: break; case OrderState.Working: _isStop = order.Type == OrderType.Stop; _isBuy = order.Operation == Operation.Buy; _isSell = order.Operation == Operation.Sell; _isTime = order.Time < tick.Time; if (_isStop && _isBuy && _isHighOverPrice && _isTime) DispatchPositions(order, tick, i); else if (_isStop && _isSell && _isPriceOverLow && _isTime) DispatchPositions(order, tick, i); break; case OrderState.Undefined: break; } } orders = _orders; positions = _positions; trades = _trades; foreach (Position t in _positions) t.Last = tick.Close; }
public int DispatchPositions(Order order, Tick tick, int j) { for (int i = _positions.Count - 1; i >= 0; i--) { Position position = _positions[i]; if (position.Operation != order.Operation) { if (position.Size <= order.Size) { order -= position.Size; _trades.Add(position); ClosePosition(i, position.Size, tick, order.Id, order.AddInfo, order.Comment); if (order == 0) break; } else { _trades.Add(position); ClosePosition(i, order.Size, tick, order.Id, order.AddInfo, order.Comment); order.Size = 0; break; } } } if (order.Size > 0) { _positions.Add(new Position(order.Account, order.Symbol, order.Id, tick.Time, order.Operation, order.Size, order.Price, tick.Close, order.Comment, order.AddInfo, false)); int size = order.Size; _orders.RemoveAt(j); return size; } _orders.RemoveAt(j); return 0; }
private void ClosePosition(int j, int size, Tick tick, int orderId, string orderAdditionalInfo, string orderComment) { if (_positions[j].Size <= size) { Position pos = _positions[j]; pos.TimeClose = tick.Time; pos.Last = tick.Close; pos.CloseOrderId = orderId; //if (OrderComment != "CloseByTrLf") // pos.Comment = "ClbyRevSig"; //else pos.Comment = orderComment; pos.AddInfo += " " + orderAdditionalInfo; _positions.Remove(pos); } else { Position pos = _positions[j]; pos -= size; _positions[j] = pos; } }
private void CreateOrder(Operation operation, Tick tick, bool isReverse, int size) { _lastOrderOperation = operation; if (isReverse && size == 1) { Log("{0} - fixed price {1} - bar close {2} = {3} REVERSE", tick.Time, _fixedPrice, tick.Close, Math.Round(_fixedPrice - tick.Close, 2)); //if (operation == Operation.Buy) //{ // _fixedPrice += _step2; //} //else if (operation == Operation.Sell) //{ // _fixedPrice -= _step2; //} } else if (size == 1) { Log("{0} - fixed price {1} - bar close {2} = {3}", tick.Time, _fixedPrice, tick.Close, Math.Round(_fixedPrice - tick.Close, 2)); if (operation == Operation.Buy) { _fixedPrice += Math.Round(_step1, 2); } else if (operation == Operation.Sell) { _fixedPrice -= Math.Round(_step1, 2); } } //_fixedPrice = bar.Close; order_id++; if (operation == Operation.Buy) { _positionsNumber++; } else { _positionsNumber--; } orders.Add(new Order(parameters.Account, parameters.Symbol, order_id, tick.Time, operation, OrderType.Market, size, tick.Close, "", "")); _lastOrderPrice = tick.Close; Log("{4} - order id {0} added op {1} at price {2} with posNumber {3}, range {5}", order_id, operation, tick.Close, _positionsNumber, tick.Time, tick.Close - _fixedPrice); }
private void AddEndDayPnl(int isExistDayTrade, double summaryChangedProfit, double lastOrderPriceChange, double dayChangedPrice, Tick tick) { DateTime temp = new DateTime(tick.Time.Year, tick.Time.Month, tick.Time.Day, 17, 0, 0); if (temp > tick.Time) temp = temp.AddDays(-1); Position endDayPNL = new Position(parameters.Account, parameters.Symbol, -1, temp, Operation.PNL, 1, _startPrice, tick.Close, "", "", true); double _dailyCommision = -isExistDayTrade * _commission * _contractSize * _ZIM; endDayPNL.TimeClose = tick.Time; endDayPNL.Trades = summaryChangedProfit; //Trades endDayPNL.Commission = _dailyCommision; endDayPNL.PosPNL = dayChangedPrice; endDayPNL.ClosePNL = lastOrderPriceChange; trades.Add(endDayPNL); /* * * _lastOrderPriceChange = (Math.Pow(bar.Close - _lastOrderPrice, 2)*_contractSize*_multiplier* _pointValue)/2; } _summaryChangedProfit = _dayChangedPrice + _lastOrderPriceChange; * */ Log("{0} - Was added summary profit - summary profit {1} = summarySqrValue ( {4} + {5}) - commission {3}", tick.Time, Math.Round(endDayPNL.Trades, 2), Math.Round(summaryChangedProfit, 2), Math.Round(endDayPNL.Commission, 2), dayChangedPrice, lastOrderPriceChange); }
public override void Trade(ref List<Position> _positions, Tick tick) { for (int i = 0; i < _positions.Count; i++) { Position _position = _positions[i]; order_id++; _position.Comment = "Closed for Exit"; if (_positionsNumber > 0) { _positionsNumber--; } else if (_positionsNumber<0) { _positionsNumber++; } Log("{0} - position closed by order id {4} with price {1} last positions are {2} with fpl {3}", tick.Time, _position.Price, _positionsNumber, Math.Round(-_position.Trades * _ZIM * _contractSize, 2), order_id, _position.TimeOpen); _position.TimeClose = tick.Time; _position.Last = tick.Close; _position.CloseOrderId = order_id; trades.Add(_position); } }
protected virtual void DispatchOrders(Tick tick) { if(backtest == null) backtest = new Backtest(orders, positions, trades); backtest.DispatchOrders(tick, out orders, out positions, out trades); }
public virtual void Trade(ref List<Position> positions, Tick tick) { //foreach (Position _position in _positions) for (int i = 0; i < positions.Count; i++) { Position _position = positions[i]; order_id++; _position.Comment = "Closed for Exit"; _position.TimeClose = tick.Time; _position.Last = tick.Close; _position.CloseOrderId = order_id; trades.Add(_position); } }