account[symbol].realized_pl
account[symbol].realized_pl Overview:calculates the profit loss for finalized trades for the specified symbol.
Attributes:Sample - Access data attribute
account[self.symbol].realized_pl.entry_pl
Name | Type | Information |
---|---|---|
entry_pl | float | Profit or Loss for matched trades by entry price for symbol. |
matched_trades | list[MatchedTrade] | List of MatchedTrade objects. The details of each matched trade are encapsulated inside the list items. See below. |
mtm_pl | float | Profit or Loss for matched trades by mtm price for symbol. For trades held overnight, the mtm_price is the mark from the clearing firm. On intra-day trades, mtm_price and entry_price will match. |
MatchedTrade object - tuple
Name | Type | Information |
---|---|---|
entry_execution | Execution | Link to the entry Execution object (see below). |
entry_pl | float | Profit or Loss for matched trades by entry price for symbol. |
exit_execution | Execution | Link to the exit Execution object (see below). |
matched_shares | integer | Shares matched on entry/exit orders (may or may not match either entry or exit fill quantity) |
mtm_pl | float | Profit or Loss for matched trades by mtm price for symbol. For trades held overnight, the mtm_price is the mark from the clearing firm. On intra-day trades, mtm_price and entry_price will match. |
Execution object - tuple (pertains to both entry_execution and exit_execution)
Name | Type | Information |
---|---|---|
clordid | string | Client Order Id in FIX. Is generated when the order is sent. Will be the same on executions. |
collect | None | is a json dictionary of information collected at the time of the order. |
instruction_id | string | Unique GUID for the instance of the script that generated the initial order. Will be None if the initial order wasn't started from same machine (e.g. from trading client) |
order_id | None | Unique GUID of order being filled (very similar clordid). Will be None if the initial order wasn't started from same machine (e.g. from trading client) |
price | float | Execution price |
script_id | string | GUID of the script. Will be None if the initial order wasn't started from same machine (e.g. from trading client) |
shares | integer | Shares executed on the trade. Always positive number, whether buy or sell execution |
side | integer | Side of the trade
|
symbol | string | Symbol of trade |
time | muts | Timestamp of trade |
user_key | string | user_key set on the order. Can be used to identify trades that go together (pairs/batch/strategy). account object can be filtered on user_key |
- Works like a dictionary filtering on the account object.
- mtm_price and entry_price will match on intra-day trades.
Backtest for SPY on 2/16/16 from 9:31-9:32.
from cloudquant.interfaces import Strategy
class UnrealizedPLExample(Strategy):
@classmethod
def is_symbol_qualified(cls, symbol, md, service, account):
return symbol == 'TQQQ'
def on_start(self, md, order, service, account):
# two marketable buy orders
order.algo_buy(self.symbol, "market", intent="init", order_quantity=100)
def on_minute_bar(self, event, md, order, service, account, bar):
if event.timestamp > service.time(9, 31) and event.timestamp < service.time(9, 32):
print '\n\n.............................\non_minute_bar\n\t%s\n' % service.time_to_string(event.timestamp,
'%H:%M:%S.%f')
print('\nShares currently held in ' + self.symbol + ':\t' + str(account[self.symbol].position.shares))
print("\n\naccount[self.symbol].unrealized_pl")
print account[self.symbol].unrealized_pl
order.algo_sell(self.symbol, "market", intent="none", order_quantity=100, allow_multiple_pending=True)
elif event.timestamp > service.time(9, 32):
print '\n\n.............................\non_minute_bar\n\t%s\n' % service.time_to_string(event.timestamp,
'%H:%M:%S.%f')
print 'account'
print account
print("\n\naccount[self.symbol].unrealized_pl")
print account[self.symbol].unrealized_pl
print '\n\naccount[self.symbol].unrealized_pl.entry_pl - %s' % account[
self.symbol].unrealized_pl.entry_pl
print 'account[self.symbol].unrealized_pl.mtm_pl - %s' % account[self.symbol].unrealized_pl.mtm_pl
service.terminate()
Console
............................. on_minute_bar 09:31:05.005000 Shares currently held in TQQQ: 100 account[self.symbol].unrealized_pl entry_pl : 9.00 mtm_pl : 9.00 ............................. on_finish 16:00:00.000000 account account_id: 0 entry_pl: 1.00 mtm_pl: 1.00 buying_power: None open_capital_long: 0.00 open_capital_short: 0.00 pending_capital_long: 0.00 pending_capital_short: -12776.00 max_capital_used: 12775.00 max_capital_used_timestamp: 1481553005000001 open_symbols: [] pending_symbols: [] TQQQ: Execution count : 2 realized_pl : entry_pl : 0.00 mtm_pl : 0.00 matched_trades : 0 unrealized_pl : entry_pl : 0.00 mtm_pl : 0.00 position : shares : 0 entry_price : 0.00 mtm_price : 0.00 capital_long : 0.00 capital_short : 0.00 pending : count_long : 0 shares_long : 0 capital_long : 0.00 count_short : 0 shares_short : 0 capital_short : 0.00 account[self.symbol].unrealized_pl entry_pl : 0.00 mtm_pl : 0.00 account[self.symbol].unrealized_pl.entry_pl - 0.0 account[self.symbol].unrealized_pl.mtm_pl - 0.0