account object
The account object holds a lot of important information. There are a couple of different layers of the account object.
Attributes:Sample - Access data attribute
account.entry_pl
Name | Type | Special | Information |
---|---|---|---|
account_id | integer | 0 | This should be a unique id assigned to this account. If this parameter is not given, a unique number will be assigned. |
buying_power | float | 0.0 | This is the maximum capital that can be used to enter or increase a position. |
capital_used | float | 0.0 | The total sum of open_capital_long and open_capital_short for all positions at the moment . |
entry_pl | float | 0.0 | Holds the profit/loss from the entering price of the position. When a position is entered the same day as the simulation, this metric will be identical to the market-to-market (mtm) profit/loss. When a position is carried over from one day to another, this metric may differ from the marked-to-market profit/loss. A positive entry_pl implies a profit. A negative entry_pl implies a loss. |
max_capital_used | float | 0.0 | The maximum capital_used at any point during the simulation. |
max_capital_used_timestamp | float | 0.0 | The timestamp when the largest ammount of open capital was being used. |
mtm_pl | float | 0.0 | Holds the profit/loss from the marked-to-market price of the position. When a position is entered the same day as the simulation, this metric will be identical to the entry profit/loss. When a position is carried over from one day to another, this metric may differ from the entry profit/loss. A positive mtm_pl implies a profit. A negative mtm_pl implies a loss. |
open_capital_long | float | 0.0 | Holds the long capital for all positions, unless a filter is set. If a filter is set with account.filter(key), this metric holds the long open capital with the filter key. |
open_capital_short | float | 0.0 | Holds the short capital for all positions, unless a filter is set. If a filter is set with account.filter(key), this metric holds the short open capital with the filter key. |
open_symbols | list | [] | List of symbols with open positions. Not to be confused account.positions. |
pending_capital_long | float | 0.0 | Holds the long capital for all pending orders, unless a filter is set. If a filter is set with account.filter(key), this metric holds the long pending capital with the filter key. Note that the pending capital may change as the simulation progresses because pending capital depends on current market price since pending orders have not been filled yet. |
pending_capital_short | float | 0.0 | Holds the short capital for all pending orders, unless a filter is set. If a filter is set with account.filter(key), this metric holds the short pending capital with the filter key. Note that the pending capital may change as the simulation progresses because pending capital depends on current market price since pending orders have not been filled yet. |
pending_symbols | list | [] | List of symbols with pending orders. |
realized_entry_pl | float | 0.0 | Holds the profit/loss from the entering price of the position. When a position is entered the same day as the simulation, this metric will be identical to the market-to-market (mtm) profit/loss. When a position is carried over from one day to another, this metric may differ from the marked-to-market profit/loss. A positive realized_entry_pl implies a profit. A negative realized_entry_pl implies a loss. |
realized_mtm_pl | float | 0.0 | Holds the profit/loss from the entering price of the position. When a position is entered the same day as the simulation, this metric will be identical to the market-to-market (mtm) profit/loss. When a position is carried over from one day to another, this metric may differ from the marked-to-market profit/loss. A positive realized_entry_pl implies a profit. A negative realized_entry_pl implies a loss. |
rejected_orders | dictionary | {} | A dictionary of symbols with rejected orders. Per symbol there is a list of rejected orders (). rejected_orders details |
unrealized_entry_pl | float | 0.0 | Holds the profit/loss from the entering price of the current market price of the open position. When a position is entered the same day as the simulation, this metric will be identical to the market-to-market (mtm) profit/loss. When a position is carried over from one day to another, this metric may differ from the marked-to-market profit/loss. A positive unrealized_entry_pl implies that exiting the position at the current price will result in a profit. A negative unrealized_entry_pl implies that reducing the current position will result in a loss. |
unrealized_mtm_pl | float | 0.0 | Holds the profit/loss from the marked-to-market price of the position. When a position is entered the same day as the simulation, this metric will be identical to the unrealized entry profit/loss. When a position is carried over from one day to another, this metric may differ from the unrealized entry profit/loss. A positive unrealized_entry_pl implies that exiting the position at the current price will result in a profit. A negative unrealized_entry_pl implies that reducing the current position will result in a loss. |
- filter() - method to filter account object by user_key
- None
There are two special filtering features (symbol and user_key)
- symbol - access account object attributes by symbol
- user_key - create special view of the account object by user_key
- account has basic attributes, but also special filtering
- two special lists to quickly find pending_orders and open_positions
- two special dictionaries for pending_symbols, rejected_orders with details by symbol
- print account gives a nice display of the account object
from cloudquant.interfaces import Strategy
class ExampleWorkingWithTheAccountObject(Strategy):
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
return symbol == 'TQQQ'
def on_start(self, md, order, service, account):
# generate a reject
order.algo_buy(self.symbol, "market", intent="exit")
# generate a execution
order.algo_buy(self.symbol, "market", intent="init", order_quantity=100)
# generate a pending
order.algo_buy(self.symbol, "limit", price=0.01, intent="increase", order_quantity=100, allow_multiple_pending=True)
# generate a closed position
order.algo_sell(self.symbol, "market", intent="none", order_quantity=50, allow_multiple_pending=True)
def on_ack(self, event, md, order, service, account):
print '\n\n.............................\non_ack\n\t%s\n' % service.time_to_string(service.system_time, '%H:%M:%S.%f')
print event
def on_reject(self, event, md, order, service, account):
print '\n\n.............................\non_reject\n\t%s\n' % service.time_to_string(service.system_time, '%H:%M:%S.%f')
print event
def on_fill(self, event, md, order, service, account):
print '\n\n.............................\non_fill\n\t%s\n' % service.time_to_string(service.system_time, '%H:%M:%S.%f')
print event
def on_finish(self, md, order, service, account):
print '\n\n.............................\non_finish\n\t%s\n' % service.time_to_string(service.system_time, '%H:%M:%S.%f')
print account
print '\n\naccount.rejected_orders'
print account.rejected_orders
Console
............................. on_reject 09:30:59.888000 RejectEvent(instruction_id='671296a9-ce91-4801-8806-24d03f03d7b8', script_id=u'dda9513c-7e9b-4c10-b29b-ed39994c9c02', user_key=None, order_id='0000000000000001', state='L', reason='when intent is exit, current_position cannot be 0', timestamp=1461331859888000, symbol=u'QQQ', account_number=0, clordid=None, kind=10) ............................. on_fill 09:31:00.016000 FillEvent(instruction_id='671296a9-ce91-4801-8806-24d03f03d7b8', script_id=u'dda9513c-7e9b-4c10-b29b-ed39994c9c02', user_key='671296a9-ce91-4801-8806-24d03f03d7b8', order_id='0000000000000002', state='F', price=109.19000244140625, shares=100, timestamp=1461331860016000, symbol=u'QQQ', account_number=0, clordid=None, collect=None, kind=8) ............................. on_fill 09:31:00.016000 FillEvent(instruction_id='671296a9-ce91-4801-8806-24d03f03d7b8', script_id=u'dda9513c-7e9b-4c10-b29b-ed39994c9c02', user_key='671296a9-ce91-4801-8806-24d03f03d7b8', order_id='0000000000000004', state='F', price=109.18000030517578, shares=-50, timestamp=1461331860016000, symbol=u'QQQ', account_number=0, clordid=None, collect=None, kind=8) ............................. on_ack 09:31:00.018000 AckEvent(instruction_id='671296a9-ce91-4801-8806-24d03f03d7b8', script_id=u'dda9513c-7e9b-4c10-b29b-ed39994c9c02', user_key='671296a9-ce91-4801-8806-24d03f03d7b8', order_id='0000000000000002', state='N', price=0.0, shares=100, timestamp=1461331860018000, symbol=u'QQQ', account_number=0, clordid=None, kind=9) ............................. on_ack 09:31:00.018000 AckEvent(instruction_id='671296a9-ce91-4801-8806-24d03f03d7b8', script_id=u'dda9513c-7e9b-4c10-b29b-ed39994c9c02', user_key='671296a9-ce91-4801-8806-24d03f03d7b8', order_id='0000000000000003', state='N', price=0.01, shares=100, timestamp=1461331860018000, symbol=u'QQQ', account_number=0, clordid=None, kind=9) ............................. on_ack 09:31:00.018000 AckEvent(instruction_id='671296a9-ce91-4801-8806-24d03f03d7b8', script_id=u'dda9513c-7e9b-4c10-b29b-ed39994c9c02', user_key='671296a9-ce91-4801-8806-24d03f03d7b8', order_id='0000000000000004', state='N', price=0.0, shares=-50, timestamp=1461331860018000, symbol=u'QQQ', account_number=0, clordid=None, kind=9) WARNING: The latest event MarketCloseEvent(timestamp=1461355200000000) occurred after the requested end of the simulation. This may not have been intended. ............................. on_finish 09:31:59.858000 account_number: 0 entry_pl: -0.50 mtm_pl: -0.50 buying_power: 0.00 open_capital_long: 5459.50 open_capital_short: 0.00 pending_capital_long: 10930.00 pending_capital_short: 0.00 QQQ: Execution count : 2 realized_pl : entry_pl : -0.50 mtm_pl : -0.50 matched_trades: 1 position : shares : 50 entry_price : 109.19 mtm_price : 109.19 capital_long : 5459.50 short_capital : 0.00 pending : count_long : 1 shares_long : 100 capital_long : 10930.00 count_short : 0 shares_short : 0 captial_short : 0.00 account.rejected_orders {u'QQQ': [OrderedDict([('intent', 'exit'), ('symbol', u'QQQ'), ('price', 0.0), ('shares', 0), ('explanation', None), ('flags', 0), ('exit_script', None), ('exit_parameters', None), ('exit_options', None), ('collect', None), ('order_id', '0000000000000001'), ('order_algorithm', 'market'), ('clordid', None), ('instruction_id', '671296a9-ce91-4801-8806-24d03f03d7b8'), ('script_id', u'dda9513c-7e9b-4c10-b29b-ed39994c9c02'), ('user_key', None), ('filled_shares', 0), ('timestamp', 1461331859888000), ('state', 'L'), ('reason', 'when intent is exit, current_position cannot be 0'), ('account_number', 0), ('kind', 10)])]}