用于期货交易的会计账簿系统
1.会计恒等式左边的科目增加记为借Debit,减少记为贷Credit;右边的科目增加记为贷Credit,减少记为借Debit。 2.有借必有贷,借贷必相等。
Python代码实现示例:
import pandas as pd
class AccountingSystemforFutureTrading:
def __init__(self):
self.FrozenFunds = {}
self.LiquidFunds = {}
self.Margin = {}
self.IdleFunds = {}
self.new_row = {}
def create_account(self, account_type, account_name):
if account_type == 'FrozenFunds':
self.FrozenFunds[account_name] = pd.DataFrame({'Date':[],'Debit':[],'Credit':[],'Balence':[],'Notes':[]})
elif account_type == 'LiquidFunds':
self.LiquidFunds[account_name] = pd.DataFrame({'Date':[],'Debit':[],'Credit':[],'Balence':[],'Notes':[]})
elif account_type == 'Margin':
self.Margin[account_name] = pd.DataFrame({'Date':[],'Debit':[],'Credit':[],'Balence':[],'Notes':[]})
elif account_type == 'IdleFunds':
self.IdleFunds[account_name] = pd.DataFrame({'Date':[],'Debit':[],'Credit':[],'Balence':[],'Notes':[]})
def debit(self, account_type, account_name, amount,date,notes=None):
if account_type == 'FrozenFunds':
self.new_row = {'Date':date,'Debit':amount,'Credit':0,'Balence':amount,'Notes':notes}
self.FrozenFunds[account_name]=self.FrozenFunds[account_name].append(self.new_row, ignore_index=True)
elif account_type == 'LiquidFunds':
self.new_row = {'Date':date,'Debit':amount,'Credit':0,'Balence':amount,'Notes':notes}
self.LiquidFunds[account_name]=self.LiquidFunds[account_name].append(self.new_row, ignore_index=True)
elif account_type == 'Margin':
self.new_row = {'Date':date,'Debit':amount,'Credit':0,'Balence':-amount,'Notes':notes}
self.Margin[account_name]=self.Margin[account_name].append(self.new_row, ignore_index=True)
elif account_type == 'IdleFunds':
self.new_row = {'Date':date,'Debit':amount,'Credit':0,'Balence':-amount,'Notes':notes}
self.IdleFunds[account_name]=self.IdleFunds[account_name].append(self.new_row, ignore_index=True)
def credit(self, account_type, account_name, amount,date,notes=None):
if account_type == 'FrozenFunds':
self.new_row = {'Date':date,'Debit':0,'Credit':amount,'Balence':-amount,'Notes':notes}
self.FrozenFunds[account_name]=self.FrozenFunds[account_name].append(self.new_row, ignore_index=True)
elif account_type == 'LiquidFunds':
self.new_row = {'Date':date,'Debit':0,'Credit':amount,'Balence':-amount,'Notes':notes}
self.LiquidFunds[account_name]=self.LiquidFunds[account_name].append(self.new_row, ignore_index=True)
elif account_type == 'Margin':
self.new_row = {'Date':date,'Debit':0,'Credit':amount,'Balence':amount,'Notes':notes}
self.Margin[account_name]=self.Margin[account_name].append(self.new_row, ignore_index=True)
elif account_type == 'IdleFunds':
self.new_row = {'Date':date,'Debit':0,'Credit':amount,'Balence':amount,'Notes':notes}
self.IdleFunds[account_name]=self.IdleFunds[account_name].append(self.new_row, ignore_index=True)
def get_balance(self, account_type, account_name):
if account_type == 'FrozenFunds':
return self.FrozenFunds[account_name].loc[0:,'Balence'].sum()
elif account_type == 'LiquidFunds':
return self.LiquidFunds[account_name].loc[0:,'Balence'].sum()
elif account_type == 'Margin':
return self.Margin[account_name].loc[0:,'Balence'].sum()
elif account_type == 'IdleFunds':
return self.IdleFunds[account_name].loc[0:,'Balence'].sum()accountkeeper = AccountingSystemforFutureTrading()
accountkeeper.create_account(FrozenFunds,frozenforfuture)
accountkeeper.create_account(LiquidFunds,cash)
accountkeeper.create_account(IdleFunds,idlefunds)
# 初始化账户,给现金账户注资
accountkeeper.debit(LiquidFunds,cash,100)
accountkeeper.credit(IdleFunds,idlefunds,100)accountkeeper.creat_account(Margin,margin_1)
accountkeeper.debit(FrozenFunds,frozenforfuture,0.005*2*200*102/100)#保证金率*手数*一张合约的面值*合约价格/100
accountkeeper.credit(Margin,margin_1,0.005*2*200*102/100)金额增加记贷方Credit,金额减少记借方Debit\ 对于多头,$capital\quad gains\quad from\quad long_{i,\tau} = value_{i,\tau} -value_{i,0}$\ 对于空头,$capital\quad gains\quad from\quad short_{i,\tau} = -(value_{i,\tau} -value_{i,0})$\ Balence[k]=sum(Credit[0:k+1])-sum(Debit[0:k+1])
| Date | Debit | Credit | Balence | Notes |
|---|---|---|---|---|
| 2020-07-05 | 0 | value_{i,1}-value_{i,0} | sum(Credit)-sum(Debit) | 开仓后第一个交易日收益为正 |
| 2020-07-05 | Balence[0] | 0 | 0 | 每个仓位每日结算后余额应该是0 |
| Date | Debit | Credit | Balence | Notes |
|---|---|---|---|---|
| 2020-07-05 | Position_Long_1.loc[0,'Credit'] | Position_Long_1.loc[0,'Debit'] | sum(Debit)-sum(Credit) | 借记Cash,如果赚钱的话 |
| Date | Debit | Credit | Balence | Notes |
|---|---|---|---|---|
| 2020-07-05 | Position_Long_1.loc[1,'Credit'] | Position_Long_1.loc[1,'Debit'] | sum(Credit)-sum(Debit) | idlefunds对于每一个仓位每日结算后多退少补 |
| Date | Debit | Credit | Balence | Notes |
|---|---|---|---|---|
| 2020-07-05 | 0 | initial cash | sum(Credit)-sum(Debit) | 首日接收注资 |
| 2020-07-05 | Position_Long_1.loc[1,'Credit'] | Position_Long_1.loc[1,'Debit'] | sum(Credit)-sum(Debit) | idlefunds对于每一个仓位每日结算后多退少补 |