Mariner Backtesting - Value Error

ValueError
  • A ValueError in Python means that there is a problem with the content of the object you tried to assign the value to.
Sample Traceback
 Traceback (most recent call last):
  File "run_simulation.py", line 550, in <module>
    main(sys.argv[1:])
  File "run_simulation.py", line 525, in main
    results = simulator()
  File "/opt/anaconda/lib/python2.7/site-packages/tradesim/engine.py", line 542, in __call__
    return self.simulate()
  File "/opt/anaconda/lib/python2.7/site-packages/tradesim/engine.py", line 672, in simulate
    self.process_orders(event, delivery_timestamp)
  File "/opt/anaconda/lib/python2.7/site-packages/tradesim/engine.py", line 1132, in process_orders
    new_config, new_order, position_changed, fill_type, filled_shares, actual_direction = self._change_account_position(order, result)
  File "/opt/anaconda/lib/python2.7/site-packages/tradesim/engine.py", line 939, in _change_account_position
    position_changed, new_order, fill_type, filled_shares ,direction = order.context.account[order.symbol]._change_position(order, fill_data)
  File "/opt/anaconda/lib/python2.7/site-packages/tradesim/account.py", line 673, in _change_position
    res = self._reduce_position(order, fill_data)
  File "/opt/anaconda/lib/python2.7/site-packages/tradesim/account.py", line 845, in _reduce_position
    raise ValueError('Not enough shares to reduce position')
ValueError: Not enough shares to reduce position
Sample Fixed Code
 shares_open = account[self.symbol].position.shares

on_ack(self, event, md, order, account, service):

    print event
Notes
  • Before placing an order to reduce your position validate how many shares you have. Between days corporate actions can modify this value, so it's best you use the account object to keep track of it
  • Be mindful of the fact latency means things dont happen instantly, but events are occuring at incredibly high frequency.
  • For example if you place an order to reduce your position, then in the next on_trade callback think you can get a better price in a new order, so you cancel the original order, and issue a new order for the same amount. It is quite likely the 1st order will fill prior to the cancel event making to the order server. In this case your first order would be filled, and the 2nd would have a ValueError.