CAFECA-IO / BAIFA

https://baifa.vercel.app
GNU General Public License v3.0
0 stars 0 forks source link

Validate the outcome of balance sheet #204

Closed arealclimber closed 11 months ago

arealclimber commented 1 year ago

抽樣檢查

Ref: db

arealclimber commented 1 year ago

9/5 balance sheet

API 回傳資料 https://api.tidebit-defi.com/balance-sheet?date=2023-09-05

{
    "success": true,
    "code": "00000000",
    "reason": "ERROR_MESSAGE.SUCCESS",
    "data": {
        "id": "balance_sheet-1693872000_1693958399",
        "date": "2023-09-04T16:00:00.000Z",
        "assets": {
            "totalAmountFairValue": 0,
            "weightedAverageCost": 0,
            "details": {
                "accountsReceivable": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                },
                "cryptocurrency": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                },
                "cashAndCashEquivalent": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                }
            }
        },
        "liabilities": {
            "totalAmountFairValue": 23917.334354212664,
            "weightedAverageCost": 23917.334354212664,
            "details": {
                "accountsPayable": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                },
                "userDeposit": {
                    "totalAmountFairValue": 23917.334354212664,
                    "weightedAverageCost": 23917.334354212664,
                    "breakdown": {
                        "USDT": {
                            "name": "USDT",
                            "amount": "23917.3343542126655739394",
                            "fairValue": "23917.3343542126655739394"
                        }
                    }
                }
            }
        },
        "equity": {
            "totalAmountFairValue": 5099.574867777336,
            "weightedAverageCost": 5099.574867777336,
            "details": {
                "retainedEarnings": {
                    "totalAmountFairValue": 5099.574867777336,
                    "weightedAverageCost": 5099.574867777336,
                    "breakdown": {
                        "USDT": {
                            "name": "USDT",
                            "amount": "5099.574867777335984981",
                            "fairValue": "5099.574867777335984981"
                        }
                    }
                },
                "capital": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                }
            }
        }
    }
}
arealclimber commented 1 year ago

驗算方法

(exchange snapshot 每個用戶的金額變化)

資產

從區塊鏈上拿資料驗證,現階段先不驗證

負債

user deposit: 用戶在平台上的錢=有史以來的入金-出金+CFD利潤-CFD損失

其中 CFD 的獲利與損失來自已經關倉的單,而已經關倉的單子可以透過 referenceId 找到 openPrice, closePrice, amount, typeOfPosition 來找到這個單子獲利或損失的數字

資料在 bolttransactions 上

股東權益

retained earning: 累積的未分配盈餘

盈餘都未分配,數字應為所有收入的加總,其中收入包含加密貨幣的數量乘以財報結算時的匯率:

Trading fee: 手續費率乘以標的資產數量=0

Spread Fee: 開倉價格、關倉價格跟市價(當下的價格)的差距乘以標的資產數量

Withdrawal fee: 出金手續費乘以標的資產數量=0

Deposit fee: 入金手續費乘以標的資產數量

Liquidation fee: 強制平倉手續費率乘以標的資產數量

Guaranteed stop loss fee: bolttransactions上的 guaranteedStopFee

TzuHanLiang commented 1 year ago

spreadFee 折衷的估算辦法

const spreadFee = +SafeMath.plus(
          Math.abs(
            +SafeMath.mult(
              SafeMath.mult(cfd.openPrice, cfd.amount),
              this.STOP_FEE_BASE,
            ),
          ),
          cfd.closePrice && SafeMath.gt(cfd.closePrice, 0)
            ? Math.abs(
                +SafeMath.mult(
                  SafeMath.mult(cfd.closePrice, cfd.amount),
                  this.STOP_FEE_BASE,
                ),
              )
            : 0,
        );
arealclimber commented 1 year ago

taking 0.5 hr 用 Python 整理 balance snapshot 資料

import json
import csv
from datetime import datetime

# 讀取 JSON 檔案
with open("./origin/bolttransactions_8:5-9:5.json", "r") as file:
    data = json.load(file)

# 指定要取得的欄位名稱
fields = ["_id", "apply_data", "created_at", "order_type", "receipt", "user_address"]

# 提取所需的資料
extracted_data = [{field: item.get(field) for field in fields} for item in data]

print(extracted_data)

# 提取所有的 balanceSnapshot
balance_snapshots = [
    json.loads(item.get("receipt", "{}")).get("balanceSnapshot", [])[0]
    for item in data
    if "balanceSnapshot" in item.get("receipt", "{}")
]

# 將 userAddress 轉換為字符串
for snapshot in balance_snapshots:
    snapshot["userAddress"] = str(snapshot.get("userAddress", ""))

# 獲取當前的日期和時間
current_time = datetime.now().strftime("%m-%d_%H:%M")

# 將 balance_snapshots 儲存到 CSV 檔案中,並在檔名中加入當前的日期和時間
with open(
    f"./outcome/raw_balance_snapshot_{current_time}.csv", "w", newline=""
) as csvfile:
    fieldnames = [
        "id",
        "userAddress",
        "currency",
        "available",
        "locked",
        "blockNumber",
        "createdAt",
        "updatedAt",
    ]
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    for snapshot in balance_snapshots:
        writer.writerow(snapshot)

remaining 3 hrs

arealclimber commented 1 year ago

驗算方法更新

Assets

none

Liabilities

Equity

taking 6 hrs 整理資料 remaining 3 hrs

arealclimber commented 1 year ago

後續產生 excel 跟 Python 記錄在 https://github.com/CAFECA-IO/BAIFA/issues/211

arealclimber commented 1 year ago

有出入的資料

balance sheet

spread fee 不一致的資料

  1. referenceId: 0x8fe6df252cac7e29219c754941a86a20
  2. referenceId: 0x489c45286bb6b02422ed5b03717ad38f
  3. referenceId: 0xe3baf1c17c4cb545c7732cf33da888f9
    • 關倉時沒有拿到quotation spotPrice,所以我這邊依照開倉的spotPrice 算出來spread_fee 為 0.04*(26050.3-26042.82)=0.3,從DB拿來的 spreadFee 則為1.084
    • 開倉
      • apply_data
        • {"instId":"BTC-USDT","targetAsset":"BTC","unitAsset":"USDT","amount":0.04,"leverage":5,"margin":{"asset":"USDT","amount":208.47},"liquidationTime":1693276399,"orderType":"CFD","operation":"CREATE","typeOfPosition":"SELL","quotation":{"instId":"BTC-USDT","targetAsset":"BTC","unitAsset":"USDT","typeOfPosition":"SELL","price":"26042.82","spotPrice":"26050.3","deadline":1692671599,"signature":"0x1331e7c559952fab8c1905f664805957"},"price":"26042.82","liquidationPrice":31251.39,"fee":"0","guaranteedStop":false,"guaranteedStopFee":0,"createTimestamp":1692671539}
    • 關倉
      • apply_data
        • {"orderType":"CFD","operation":"CLOSE","referenceId":"0xe3baf1c17c4cb545c7732cf33da888f9","quotation":{"instId":"BTC-USDT","targetAsset":"BTC","unitAsset":"USDT","typeOfPosition":"BUY","price":"25966.92"},"closePrice":"25966.92","closedType":"BY_USER"}
      • receipt
        • {….., "orderSnapshot":{"orderType":"CFD","id":"0xe3baf1c17c4cb545c7732cf33da888f9","txhash":"0x9a54e1bcd2cbeacfbd7ed960d979d27b","orderStatus":"PROCESSING","state":"CLOSED","instId":"BTC-USDT","userAddress":"0x73…524A","targetAsset":"BTC","unitAsset":"USDT","margin":{"amount":"208.47","asset":"USDT"},"spotPrice":"26050.3","openPrice":"26042.82","amount":"0.04","fee":"0","typeOfPosition":"SELL","leverage":5,"guaranteedStop":false,"liquidationPrice":"31251.39","liquidationTime":1693276399,"share":false,"updatedTimestamp":1693205870,"createTimestamp":1692671547,"guaranteedStopFee":"0","closePrice":"25966.92","closedType":"BY_USER","closeTimestamp":1693205870,"forcedClose":false,"pnl":{"type":"PROFIT","value":"3.04"},"spreadFee":"1.084"}}
  4. referenceId: 0x776b78bf74896e615ab6b89a87090f27
  5. referenceId: 0x79c5c0070c225103229e7a5c67bba01d
  6. referenceId: 0xce7893a8b04a98318efd1f505cfae200
  7. referenceId: 0x8e35534fc45a390067165f6a3f02c031

openPrice and closePrice vs quotation price

balance sheet 的 userDeposit 有出入 (驗算以 USDT 匯率為1來計算)

db_userDepositsAmount 跟 API 的 liabilities userDeposit totalAmountFairValue 不一致

balance sheet 的 retainedEarnings 有出入 (驗算以 USDT 匯率為1來計算)

db_retainedEarningsAmount 跟 API 的 equity retainedEarnings totalAmountFairValue 不一致

arealclimber commented 1 year ago

remaining

arealclimber commented 1 year ago

9/5 balance sheet

API 回傳資料 https://api.tidebit-defi.com/balance-sheet?date=2023-09-05

{
    "success": true,
    "code": "00000000",
    "reason": "ERROR_MESSAGE.SUCCESS",
    "data": {
        "id": "balance_sheet-1693872000_1693958399",
        "date": "2023-09-04T16:00:00.000Z",
        "assets": {
            "totalAmountFairValue": 0,
            "weightedAverageCost": 0,
            "details": {
                "accountsReceivable": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                },
                "cryptocurrency": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                },
                "cashAndCashEquivalent": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                }
            }
        },
        "liabilities": {
            "totalAmountFairValue": 29380.45962,
            "weightedAverageCost": 29380.45962,
            "details": {
                "accountsPayable": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                },
                "userDeposit": {
                    "totalAmountFairValue": 29380.45962,
                    "weightedAverageCost": 29380.45962,
                    "breakdown": {
                        "USDT": {
                            "name": "USDT",
                            "amount": "29380.45961999999999963",
                            "fairValue": "29380.45961999999999963"
                        }
                    }
                }
            }
        },
        "equity": {
            "totalAmountFairValue": 1645.56418,
            "weightedAverageCost": 1645.56418,
            "details": {
                "retainedEarnings": {
                    "totalAmountFairValue": 1645.56418,
                    "weightedAverageCost": 1645.56418,
                    "breakdown": {
                        "USDT": {
                            "name": "USDT",
                            "amount": "1645.56418000000000037",
                            "fairValue": "1645.56418000000000037"
                        }
                    }
                },
                "capital": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                }
            }
        }
    }
}
arealclimber commented 1 year ago

將spread fee 不一致以及 close/open price 跟 quotation price資訊標注在 remarks 欄位

有出入的資料

balance sheet

spread fee 不一致的資料

  1. referenceId: 0x8fe6df252cac7e29219c754941a86a20
  2. referenceId: 0x489c45286bb6b02422ed5b03717ad38f
  3. referenceId: 0xe3baf1c17c4cb545c7732cf33da888f9

    • 關倉時沒有拿到quotation spotPrice,所以我這邊依照開倉的spotPrice 算出來spread_fee 為 0.04*(26050.3-26042.82)=0.3,從DB拿來的 spreadFee 則為1.084
    • 開倉

      • apply_data

      • {"instId":"BTC-USDT","targetAsset":"BTC","unitAsset":"USDT","amount":0.04,"leverage":5,"margin":{"asset":"USDT","amount":208.47},"liquidationTime":1693276399,"orderType":"CFD","operation":"CREATE","typeOfPosition":"SELL","quotation":{"instId":"BTC-USDT","targetAsset":"BTC","unitAsset":"USDT","typeOfPosition":"SELL","price":"26042.82","spotPrice":"26050.3","deadline":1692671599,"signature":"0x1331e7c559952fab8c1905f664805957"},"price":"26042.82","liquidationPrice":31251.39,"fee":"0","guaranteedStop":false,"guaranteedStopFee":0,"createTimestamp":1692671539}

    • 關倉

      • apply_data

      • {"orderType":"CFD","operation":"CLOSE","referenceId":"0xe3baf1c17c4cb545c7732cf33da888f9","quotation":{"instId":"BTC-USDT","targetAsset":"BTC","unitAsset":"USDT","typeOfPosition":"BUY","price":"25966.92"},"closePrice":"25966.92","closedType":"BY_USER"}

      • receipt

      • {….., "orderSnapshot":{"orderType":"CFD","id":"0xe3baf1c17c4cb545c7732cf33da888f9","txhash":"0x9a54e1bcd2cbeacfbd7ed960d979d27b","orderStatus":"PROCESSING","state":"CLOSED","instId":"BTC-USDT","userAddress":"0x73…524A","targetAsset":"BTC","unitAsset":"USDT","margin":{"amount":"208.47","asset":"USDT"},"spotPrice":"26050.3","openPrice":"26042.82","amount":"0.04","fee":"0","typeOfPosition":"SELL","leverage":5,"guaranteedStop":false,"liquidationPrice":"31251.39","liquidationTime":1693276399,"share":false,"updatedTimestamp":1693205870,"createTimestamp":1692671547,"guaranteedStopFee":"0","closePrice":"25966.92","closedType":"BY_USER","closeTimestamp":1693205870,"forcedClose":false,"pnl":{"type":"PROFIT","value":"3.04"},"spreadFee":"1.084"}}

  4. referenceId: 0x776b78bf74896e615ab6b89a87090f27
  5. referenceId: 0x79c5c0070c225103229e7a5c67bba01d
  6. referenceId: 0xce7893a8b04a98318efd1f505cfae200
  7. referenceId: 0x8e35534fc45a390067165f6a3f02c031

openPrice and closePrice vs quotation price

  • openPrice 應該與開倉時 quotation 的 price 一致,closePrice 應該與關倉時 quotaion 的 price 一致
  • referenceId:
  1. 0xfd09f18e886a9d9bec2336b7502d5580

    • closePrice 跟 quotation 有出入
  2. 0xc5169e31967fe66aa84f9fa0a3205950

    • closePrice 跟 quotation 有出入
  3. 0xd201f4de68d05936b919ccdc392fea1a

    • closePrice 跟 quotation 有出入
  4. 0x17045238850c57c61a0f9e4595caf9d0

    • openPrice 跟 quotation 有出入
  5. 0xec48d225cfe58a19cd256bb7caf14ce7

    • closePrice 跟 quotation 有出入

balance sheet 的 userDeposit 有出入 (驗算以 USDT 匯率為1來計算)

db_userDepositsAmount 跟 API 的 liabilities userDeposit totalAmountFairValue 不一致

balance sheet 的 retainedEarnings 有出入 (驗算以 USDT 匯率為1來計算)

db_retainedEarningsAmount 跟 API 的 equity retainedEarnings totalAmountFairValue 不一致

arealclimber commented 1 year ago
arealclimber commented 1 year ago
{
    "success": true,
    "code": "00000000",
    "reason": "ERROR_MESSAGE.SUCCESS",
    "data": {
        "id": "balance_sheet-1693872000_1693958399",
        "date": "2023-09-04T16:00:00.000Z",
        "assets": {
            "totalAmountFairValue": 0,
            "weightedAverageCost": 0,
            "details": {
                "accountsReceivable": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                },
                "cryptocurrency": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                },
                "cashAndCashEquivalent": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                }
            }
        },
        "nonAssets": {
            "totalAmountFairValue": 0,
            "weightedAverageCost": 0,
            "details": {
                "accountsReceivable": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                },
                "cashAndCashEquivalent": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                }
            }
        },
        "liabilities": {
            "totalAmountFairValue": 515.624737074,
            "weightedAverageCost": 515.624737074,
            "details": {
                "accountsPayable": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                },
                "userDeposit": {
                    "totalAmountFairValue": 515.624737074,
                    "weightedAverageCost": 515.624737074,
                    "breakdown": {
                        "ETH": {
                            "name": "ETH",
                            "amount": "3.56",
                            "fairValue": "5749.2398"
                        },
                        "USDT": {
                            "name": "USDT",
                            "amount": "-4185.32906292599999",
                            "fairValue": "-4185.32906292599999"
                        },
                        "BTC": {
                            "name": "BTC",
                            "amount": "-0.04",
                            "fairValue": "-1048.286"
                        }
                    }
                }
            }
        },
        "equity": {
            "totalAmountFairValue": 27.9951,
            "weightedAverageCost": 27.9951,
            "details": {
                "retainedEarnings": {
                    "totalAmountFairValue": 27.9951,
                    "weightedAverageCost": 27.9951,
                    "breakdown": {
                        "ETH": {
                            "name": "ETH",
                            "amount": "0",
                            "fairValue": "0"
                        },
                        "USDT": {
                            "name": "USDT",
                            "amount": "27.9951",
                            "fairValue": "27.9951"
                        },
                        "BTC": {
                            "name": "BTC",
                            "amount": "0",
                            "fairValue": "0"
                        }
                    }
                },
                "capital": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                }
            }
        }
    }
}
arealclimber commented 1 year ago

userDeposit 相差57倍 retainedEarnings 相差27倍

Item from API from DB
userDeposit 515.624737074 29470.56572199
retainedEarnings 27.9951 781.556499999999
arealclimber commented 1 year ago

買漲1 ETH(價值 100 USDT), 保證金 20 USDT accountReceivable: 100 * exchange rate USDT (unit asset),匯率用quotation price accountPayable(以後拆分科目): 20 USDT+1ETH,匯率用 quotation price

買跌1 ETH(價值 100 USDT), 保證金 20 USDT accountReceivable: 1 ETH (targeted asset),匯率用quotation price accountPayable(以後拆分科目): 20 USDT+1ETH,匯率用 quotation price

TzuHanLiang commented 1 year ago

買漲1 ETH(價值 100 USDT), 保證金 20 USDT accountReceivable: 100 USDT,匯率用quotation price accountPayable(以後拆分科目): 20USDT+1ETH,匯率用 quotation price 的

買跌1 ETH(價值 100 USDT), 保證金 20 USDT accountReceivable: 1 ETH,匯率用quotation price accountPayable(以後拆分科目): 20USDT+1ETH,匯率用 quotation price 的

這裡是不是需要分開討論 Create CFD 跟 Close CFD 的情況: 買漲1 ETH(價值 100 USDT), 保證金 20 USDT accountReceivable: 100 USDT,匯率用quotation price accountPayable(以後拆分科目): (Create: +20 USDT| Close: -20 USDT) + 1ETH,匯率用 quotation price 的

買跌1 ETH(價值 100 USDT), 保證金 20 USDT accountReceivable: 1 ETH,匯率用quotation price accountPayable(以後拆分科目): (Create: +20 USDT| Close: -20 USDT) + 100 USDT [這裡應該是 100 USDT 而不是 1 ETH],匯率用 quotation price 的

arealclimber commented 1 year ago

買漲1 ETH(價值 100 USDT), 保證金 20 USDT accountReceivable: 100 USDT,匯率用quotation price accountPayable(以後拆分科目): 20USDT+1ETH,匯率用 quotation price 的 買跌1 ETH(價值 100 USDT), 保證金 20 USDT accountReceivable: 1 ETH,匯率用quotation price accountPayable(以後拆分科目): 20USDT+1ETH,匯率用 quotation price 的

這裡是不是需要分開討論 Create CFD 跟 Close CFD 的情況: 買漲1 ETH(價值 100 USDT), 保證金 20 USDT accountReceivable: 100 USDT,匯率用quotation price accountPayable(以後拆分科目): (Create: +20 USDT| Close: -20 USDT) + 1ETH,匯率用 quotation price 的

買跌1 ETH(價值 100 USDT), 保證金 20 USDT accountReceivable: 1 ETH,匯率用quotation price accountPayable(以後拆分科目): (Create: +20 USDT| Close: -20 USDT) + 100 USDT [這裡應該是 100 USDT 而不是 1 ETH],匯率用 quotation price 的

重新整理將情況分成 create CFD 跟 close CFD,並算上匯率,create 跟 close 應該要能夠對消

買漲1 ETH(價值 100 USDT), 保證金 20 USDT accountReceivable: 100 * exchange rate USDT (unit asset),匯率用quotation price accountPayable(以後拆分科目): 20 USDT+1ETH,匯率用 quotation price

買漲1 ETH Create Close
accountReceivable +100 USDT -100 USDT
accountPayable +20 USDT +1 ETH -20 USDT -1 ETH

買跌1 ETH(價值 100 USDT), 保證金 20 USDT accountReceivable: 1 ETH (targeted asset),匯率用quotation price accountPayable(以後拆分科目): 20 USDT+100 * exchange rate USDT,匯率用 quotation price

買跌1 ETH Create Close
accountReceivable 1 ETH -1 ETH
accountPayable +20 USDT + 100 USDT -20 USDT - 100 USDT
TzuHanLiang commented 1 year ago
  1. 情況一:開倉時做多(買漲)ETH,關倉時做空(買跌)以抵消開倉的倉位。
  2. 情況二:開倉時做空(買跌)ETH,關倉時做多(買漲)以抵消開倉的倉位。

在期貨或槓桿交易中,"開倉"意味著進入一個新的交易,而"關倉"意味著退出或抵消之前的開倉交易。

對於情況一:

對於情況二:

根據上述分析,提供下列對於情況一和情況二的表格中。

情況一 買漲1 ETH Create 買跌1 ETH Close
accountReceivable +100 USDT +1 ETH
accountPayable +20 USDT +1 ETH -20 USDT +100 USDT
情況二 買跌 1 ETH Create 買漲1 ETH Close
accountReceivable 1 ETH + 100 USDT
accountPayable +20 USDT + 100 USDT -20 USDT + 1ETH
TzuHanLiang commented 1 year ago
  1. 情況一:開倉時做多(買漲)ETH,關倉時做空(買跌)以抵消開倉的倉位。
  2. 情況二:開倉時做空(買跌)ETH,關倉時做多(買漲)以抵消開倉的倉位。

在期貨或槓桿交易中,"開倉"意味著進入一個新的交易,而"關倉"意味著退出或抵消之前的開倉交易。

對於情況一:

  • 開倉時做多1 ETH,相當於借款購買1 ETH,所以應付帳款增加1 ETH。同時,按照當時的報價價格,我們應該收到相應的USDT,所以應收帳款增加100 USDT。
  • 關倉時,用戶做空1 ETH以抵消開倉的多頭倉位,這意味著用戶售出之前購買的1 ETH,並獲得等價的USDT,所以應收帳款增加1 ETH,並且因為我們要將之前的保證金返回,應付帳款減少20 USDT。

對於情況二:

  • 開倉時做空1 ETH,相當於用戶賣出1 ETH並獲得100 USDT的貨幣,所以應收帳款增加1 ETH。
  • 關倉時,用戶做多1 ETH以抵消開倉的空頭倉位,這意味著用戶購買1 ETH以還原您之前售出的ETH,並支付相應的USDT,所以應收帳款增加100 USDT。

根據上述分析,提供下列對於情況一和情況二的表格中。

情況一 買漲1 ETH Create 買跌1 ETH Close accountReceivable +100 USDT +1 ETH accountPayable +20 USDT +1 ETH -20 USDT +100 USDT 情況二 買跌 1 ETH Create 買漲1 ETH Close accountReceivable 1 ETH + 100 USDT accountPayable +20 USDT + 100 USDT -20 USDT + 1ETH

我這個應該不對,這樣會隨著交易增加,accountReceivable 跟 accountPayable 都持續增加沒有衝銷

arealclimber commented 1 year ago
  1. 情況一:開倉時做多(買漲)ETH,關倉時做空(買跌)以抵消開倉的倉位。
  2. 情況二:開倉時做空(買跌)ETH,關倉時做多(買漲)以抵消開倉的倉位。

在期貨或槓桿交易中,"開倉"意味著進入一個新的交易,而"關倉"意味著退出或抵消之前的開倉交易。 對於情況一:

  • 開倉時做多1 ETH,相當於借款購買1 ETH,所以應付帳款增加1 ETH。同時,按照當時的報價價格,我們應該收到相應的USDT,所以應收帳款增加100 USDT。
  • 關倉時,用戶做空1 ETH以抵消開倉的多頭倉位,這意味著用戶售出之前購買的1 ETH,並獲得等價的USDT,所以應收帳款增加1 ETH,並且因為我們要將之前的保證金返回,應付帳款減少20 USDT。

對於情況二:

  • 開倉時做空1 ETH,相當於用戶賣出1 ETH並獲得100 USDT的貨幣,所以應收帳款增加1 ETH。
  • 關倉時,用戶做多1 ETH以抵消開倉的空頭倉位,這意味著用戶購買1 ETH以還原您之前售出的ETH,並支付相應的USDT,所以應收帳款增加100 USDT。

根據上述分析,提供下列對於情況一和情況二的表格中。 情況一 買漲1 ETH Create 買跌1 ETH Close accountReceivable +100 USDT +1 ETH accountPayable +20 USDT +1 ETH -20 USDT +100 USDT 情況二 買跌 1 ETH Create 買漲1 ETH Close accountReceivable 1 ETH + 100 USDT accountPayable +20 USDT + 100 USDT -20 USDT + 1ETH

我這個應該不對,這樣會隨著交易增加,accountReceivable 跟 accountPayable 都持續增加沒有衝銷

應收帳款跟應付帳款在 close 的時候應該就結清了,我們要給用戶的錢以及用戶應該還我們的錢都在關倉的時候還給對方,不會隨著交易增加,結清之後除了 accountReceivable 跟 accountPayable 被沖銷(其實不一定會完全沖銷,因為 create 跟 close 時的 exchange rate 不一樣),userDeposits 在 create 跟 close 也會有 margin 跟 PNL 的變化,以下補充對應表格跟 balance sheet 樣板

買漲1 ETH(價值 100 USDT), 保證金 20 USDT,匯率用quotation price

accountReceivable: 100 * exchange rate USDT (unit asset)

accountPayable(以後拆分科目): 20 USDT+1ETH

買漲1 ETH Create Close
accountReceivable +100 USDT -100 USDT
accountPayable +20 USDT +1 ETH -20 USDT -1 ETH
userDeposits -100 USDT +100 USDT + PNL

買跌1 ETH(價值 100 USDT), 保證金 20 USDT,匯率用quotation price

accountReceivable: 1 ETH (targeted asset)

accountPayable(以後拆分科目): 20 USDT+100 * exchange rate USDT

買跌1 ETH Create Close
accountReceivable 1 ETH -1 ETH
accountPayable +20 USDT + 100 USDT -20 USDT - 100 USDT
userDeposits -100 USDT +100 USDT + PNL

image

arealclimber commented 1 year ago
  1. 情況一:開倉時做多(買漲)ETH,關倉時做空(買跌)以抵消開倉的倉位。
  2. 情況二:開倉時做空(買跌)ETH,關倉時做多(買漲)以抵消開倉的倉位。

在期貨或槓桿交易中,"開倉"意味著進入一個新的交易,而"關倉"意味著退出或抵消之前的開倉交易。 對於情況一:

  • 開倉時做多1 ETH,相當於借款購買1 ETH,所以應付帳款增加1 ETH。同時,按照當時的報價價格,我們應該收到相應的USDT,所以應收帳款增加100 USDT。
  • 關倉時,用戶做空1 ETH以抵消開倉的多頭倉位,這意味著用戶售出之前購買的1 ETH,並獲得等價的USDT,所以應收帳款增加1 ETH,並且因為我們要將之前的保證金返回,應付帳款減少20 USDT。

對於情況二:

  • 開倉時做空1 ETH,相當於用戶賣出1 ETH並獲得100 USDT的貨幣,所以應收帳款增加1 ETH。
  • 關倉時,用戶做多1 ETH以抵消開倉的空頭倉位,這意味著用戶購買1 ETH以還原您之前售出的ETH,並支付相應的USDT,所以應收帳款增加100 USDT。

根據上述分析,提供下列對於情況一和情況二的表格中。 情況一 買漲1 ETH Create 買跌1 ETH Close accountReceivable +100 USDT +1 ETH accountPayable +20 USDT +1 ETH -20 USDT +100 USDT 情況二 買跌 1 ETH Create 買漲1 ETH Close accountReceivable 1 ETH + 100 USDT accountPayable +20 USDT + 100 USDT -20 USDT + 1ETH

我這個應該不對,這樣會隨著交易增加,accountReceivable 跟 accountPayable 都持續增加沒有衝銷

應收帳款跟應付帳款在 close 的時候應該就結清了,我們要給用戶的錢以及用戶應該還我們的錢都在關倉的時候還給對方,不會隨著交易增加,結清之後除了 accountReceivable 跟 accountPayable 被沖銷(其實不一定會完全沖銷,因為 create 跟 close 時的 exchange rate 不一樣),userDeposits 在 create 跟 close 也會有 margin 跟 PNL 的變化,以下補充對應表格跟 balance sheet 樣板

買漲1 ETH(價值 100 USDT), 保證金 20 USDT,匯率用quotation price

accountReceivable: 100 * exchange rate USDT (unit asset)

accountPayable(以後拆分科目): 20 USDT+1ETH

買漲1 ETH Create Close accountReceivable +100 USDT -100 USDT accountPayable +20 USDT +1 ETH -20 USDT -1 ETH userDeposits -100 USDT +100 USDT + PNL 買跌1 ETH(價值 100 USDT), 保證金 20 USDT,匯率用quotation price

accountReceivable: 1 ETH (targeted asset)

accountPayable(以後拆分科目): 20 USDT+100 * exchange rate USDT

買跌1 ETH Create Close accountReceivable 1 ETH -1 ETH accountPayable +20 USDT + 100 USDT -20 USDT - 100 USDT userDeposits -100 USDT +100 USDT + PNL image

我這邊敘述有誤,locked 的值不會影響 userDeposits,下一版再實作 所以刪掉 userDeposit 的表格如以下

買漲1 ETH(價值 100 USDT), 保證金 20 USDT,匯率用quotation price

accountReceivable: 100 * exchange rate USDT (unit asset)

accountPayable(以後拆分科目): 20 USDT+1ETH

買漲1 ETH Create Close
accountReceivable 應收帳款 +100 USDT -100 USDT
accountPayable 應付帳款 +20 USDT +1 ETH -20 USDT -1 ETH

買跌1 ETH(價值 100 USDT), 保證金 20 USDT,匯率用quotation price

accountReceivable: 1 ETH (targeted asset)

accountPayable(以後拆分科目): 20 USDT+100 * exchange rate USDT

買跌1 ETH Create Close
accountReceivable 應收帳款 1 ETH -1 ETH
accountPayable 應付帳款 +20 USDT + 100 USDT -20 USDT - 100 USDT
arealclimber commented 1 year ago

https://api.tidebit-defi.com/balance-sheet?date=2023-09-05

{
    "success": true,
    "code": "00000000",
    "reason": "ERROR_MESSAGE.SUCCESS",
    "data": {
        "id": "balance_sheet-1693872000_1693958399",
        "date": "2023-09-04T16:00:00.000Z",
        "assets": {
            "totalAmountFairValue": -2464.337454738,
            "weightedAverageCost": -2464.337454738,
            "details": {
                "accountsReceivable": {
                    "totalAmountFairValue": -2464.337454738,
                    "weightedAverageCost": -2464.337454738,
                    "breakdown": {
                        "USDT": {
                            "name": "USDT",
                            "amount": "1726.57194526199999",
                            "fairValue": "1726.57194526199999"
                        },
                        "ETH": {
                            "name": "ETH",
                            "amount": "-2.24",
                            "fairValue": "-3658.1104"
                        },
                        "BTC": {
                            "name": "BTC",
                            "amount": "-0.02",
                            "fairValue": "-532.799"
                        }
                    }
                },
                "cryptocurrency": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                },
                "cashAndCashEquivalent": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                }
            }
        },
        "nonAssets": {
            "totalAmountFairValue": 0,
            "weightedAverageCost": 0,
            "details": {
                "accountsReceivable": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                },
                "cashAndCashEquivalent": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                }
            }
        },
        "liabilities": {
            "totalAmountFairValue": -2572.547157664,
            "weightedAverageCost": -2572.547157664,
            "details": {
                "accountsPayable": {
                    "totalAmountFairValue": -4039.707157664,
                    "weightedAverageCost": -4039.707157664,
                    "breakdown": {
                        "USDT": {
                            "name": "USDT",
                            "amount": "-4596.982357664",
                            "fairValue": "-4596.982357664"
                        },
                        "ETH": {
                            "name": "ETH",
                            "amount": "1.32",
                            "fairValue": "2155.6722"
                        },
                        "BTC": {
                            "name": "BTC",
                            "amount": "-0.06",
                            "fairValue": "-1598.397"
                        }
                    }
                },
                "userDeposit": {
                    "totalAmountFairValue": 1467.16,
                    "weightedAverageCost": 1467.16,
                    "breakdown": {
                        "USDT": {
                            "name": "USDT",
                            "amount": "1467.16",
                            "fairValue": "1467.16"
                        },
                        "ETH": {
                            "name": "ETH",
                            "amount": "0",
                            "fairValue": "0"
                        },
                        "BTC": {
                            "name": "BTC",
                            "amount": "0",
                            "fairValue": "0"
                        }
                    }
                }
            }
        },
        "equity": {
            "totalAmountFairValue": 27.9951,
            "weightedAverageCost": 27.9951,
            "details": {
                "retainedEarnings": {
                    "totalAmountFairValue": 27.9951,
                    "weightedAverageCost": 27.9951,
                    "breakdown": {
                        "USDT": {
                            "name": "USDT",
                            "amount": "27.9951",
                            "fairValue": "27.9951"
                        },
                        "ETH": {
                            "name": "ETH",
                            "amount": "0",
                            "fairValue": "0"
                        },
                        "BTC": {
                            "name": "BTC",
                            "amount": "0",
                            "fairValue": "0"
                        }
                    }
                },
                "capital": {
                    "totalAmountFairValue": 0,
                    "weightedAverageCost": 0,
                    "breakdown": {}
                }
            }
        }
    }
}
arealclimber commented 1 year ago

retained earnings 驗算方法更新