结算系统 v0.1

结算系统 v0.1

本文档详细说明股票/期货业务的月结结算机制,包括收益率计算公式、表驱动常数、利润处理流程。


1. 结算概述

每月点击「下一个月」时,系统会对所有在营业务进行结算:

  1. 计算收益率 P(万分比,即 P/10000 = 百分比)
  2. 计算利润 = 本金 × P / 10000
  3. 更新业务账户 余额
  4. 处理透支:若余额 < 0,弹窗让玩家选择续资或清算
  5. 划转利润(按利润策略)
  6. 经验结算:盈利则该员工经验 +3

2. 股票结算公式

2.1 收益率计算

P = 大环境因子 + 行业因子 + 个股因子 + 能力因子 + 指导因子 + 噪声
因子 来源 计算方式
大环境因子 B_STOCK_BP_BY_C[c] 根据宏观股市景气 c∈[0,4] 查表
行业因子 portfolioSectorBp 组合按权重加权平均行业beta
个股因子 portfolioBetaBp 组合按权重加权平均个股beta
能力因子 A_BP_BY_ABILITY[a-1] 员工能力 a∈[1,10] 查表
指导因子 G_STOCK_EXPECT_ADD_BP[mode] 风格模式 0保守/1平衡/2激进
噪声 NOISE_BP[H % 256] 固定256项噪声表,由RNG确定下标

2.2 表驱动常数(当前实装值)

// B_STOCK_BP_BY_C: 大环境因子(万分比)
// 繁荣/向好/平稳/低迷/冰点
[500, 250, 0, -250, -500]  // 对应 ±5% 范围

// B_FUT_BP_BY_C: 期货大环境(万分比)
[600, 300, 0, -300, -600]  // 对应 ±6% 范围

// A_BP_BY_ABILITY: 能力加算(能力1~10)
[-200, -150, -100, -50, 0, 50, 100, 150, 200, 250]

// G_STOCK_EXPECT_ADD_BP: 股票指导(保守/平衡/激进)
[-200, 0, 400]

与设计文档差异说明:原设计附录A建议 B_stock = [1200,500,100,-300,-800](±8%),实装调整为±5%以降低初期波动,让前期体验更友好。

2.3 噪声表

噪声表为固定的256个整数,单位0.01百分点(即1=0.01%),范围约±250(±2.5%)。

噪声下标计算:

H = uint32( mix(S, t, i, kind, "noise") )
noiseIndex = H % 256

其中:

  • S = 游戏主种子
  • t = 当前月份索引(从1990-01开始计)
  • i = 该业务在当月新开单中的序号
  • kind = “stock” 或 “fut”

2.4 组合投资计算

若业务采用组合投资(多支股票按权重分配):

// 组合个股加权beta
portfolioBetaBp = Σ( weight_i × betaExtraBp_i ) / 10000

// 组合行业加权beta  
portfolioSectorBp = Σ( weight_i × sectorBetaBp_i ) / 10000
  • weight_i:该股票权重(万分比,如2500=25%)
  • 所有持仓权重之和必须 = 10000(100%)

默认组合生成:员工新开股票业务时,系统自动生成前4支股票的均分组合(各25%)。


3. 期货结算公式

3.1 收益率计算

P0 = 品种因子 + 能力因子 + 噪声
P  = P0 × L  (L为杠杆倍数 1/2/3)
因子 来源
品种因子 futBpByC[c] 根据品种和景气查表
能力因子 同股票,A_BP_BY_ABILITY
噪声 同股票,NOISE_BP
杠杆 L 指导选择 1x/2x/3x,默认1x

品种基线表见 data/investment-sim/stocks-futures.json

{
  "futures": {
    "variants": {
      "composite": { "B_fut_bp_by_c": [600, 300, 0, -300, -600] },
      "energy":    { "B_fut_bp_by_c": [800, 400, 0, -400, -800] },
      "metal":     { "B_fut_bp_by_c": [500, 250, 0, -250, -500] },
      "agri":      { "B_fut_bp_by_c": [400, 200, 0, -200, -400] }
    }
  }
}

4. 利润处理流程

4.1 利润计算

profitWan = round( allocWan × P / 10000 )
  • allocWan:结算时本金
    • 若策略为「上交公司」= initialWan(初始本金)
    • 若策略为「滚存复利」= aumWan(当前AUM,含上月滚存利润)

4.2 策略处理

滚存复利 (reinvest)

新业务AUM = 原AUM + 利润
不划转至公司现金

上交公司 (remit)

公司现金 += 利润
业务AUM   = 初始本金(维持不变)

4.3 透支处理

若结算后 balance = allocWan + profitWan < 0

  1. 暂停翻月,进入透支处理阶段
  2. 玩家对该笔业务可选择:
    • 续资:从公司现金划拨资金填补透支(至少补足至≥0)
    • 清算:终止业务,损失计入公司现金,声誉-3

5. 可复现性保证

5.1 确定性来源

相同种子、相同月份、相同新开单序号,必定产生相同结果:

变量 确定性来源
宏观景气 c rollMacroC(S, t, line) 固定算法
噪声下标 mix(S, t, i, kind, "noise") 固定算法
个股组合 generateEmployeeStockPortfolio() 固定算法

5.2 mix算法(简化版FNV-1a风格)

function mix32(seed, ...args) {
  let h = seed >>> 0;
  const primes = [0x9E3779B9, 0x85EBCA6B, 0xC2B2AE3D, 0x27D4EB2F];
  for (let i = 0; i < args.length; i++) {
    const x = (args[i] | 0) >>> 0;
    h = (h ^ x) >>> 0;
    h = Math.imul(h, primes[i % primes.length]) >>> 0;
  }
  return h >>> 0;
}

6. 调参与平衡建议

6.1 修改表值(无需改代码)

调整目标 修改位置
整体收益水平 B_STOCK_BP_BY_C, B_FUT_BP_BY_C
能力差距影响 A_BP_BY_ABILITY
指导效果强度 G_STOCK_EXPECT_ADD_BP
波动范围 NOISE_BP 各值
行业/个股特性 stocks-futures.json 中的 betaExtraBp

6.2 调试技巧

  1. 查看结算日志:月结时会输出每单收益率P、利润、策略类型
  2. 种子复现:记录游戏种子,可重新开局得到完全相同的行情序列
  3. 强制透支测试:可将噪声表改为全负值测试透支流程

附录:完整表数据

NOISE_BP (固化噪声表,256项)

[
  -133, 33, -74, 173, 250, 142, -167, -163, -122, 246, -181, -43, 215, 41, -97, -174,
  66, -228, -133, -29, 170, -61, -191, -60, -118, 55, 191, -59, 192, 124, 136, -29,
  -15, 145, 97, -155, 226, 26, -138, 148, 152, -16, -85, 77, -141, 75, 188, 28,
  224, 123, 102, -243, 170, 4, 142, 7, 80, -203, 107, -223, 64, -2, 3, 17,
  121, -17, 60, -99, 186, -181, -29, -103, -178, -229, 198, 65, -163, -211, 130, -128,
  -221, 223, 123, -135, -82, 187, 180, 222, -180, 91, -237, 164, -66, 55, -32, 106,
  103, -52, 207, -182, 3, -57, -177, 36, -184, -249, 245, 186, -111, 6, 96, -22,
  -176, -221, 248, 124, 104, 22, 45, 199, -88, -15, -223, -207, -1, -144, 3, 141,
  31, -220, 6, 17, -65, -208, -220, 55, 71, 162, -63, -150, -93, -234, -104, -228,
  200, -166, -150, -231, -208, 19, 78, 73, 175, 173, 62, 56, 79, 192, 3, -209,
  235, 89, 5, -183, 110, 237, -120, 7, 27, -146, -5, 138, -107, -160, -201, -51,
  4, -197, -124, -54, -52, 168, -198, 34, -76, -95, -125, -20, 132, 57, -165, 36,
  -165, -178, -200, -53, 60, 126, -107, -49, 137, -225, -59, -199, 8, 232, -3, 3,
  94, -71, -159, 233, -102, -75, -60, -209, -12, 70, -221, -224, 240, 204, -203, 133,
  192, -216, 67, 55, 175, -216, 246, 8, -144, -28, 130, 235, 22, 172, 146, 210,
  97, -185, -161, 101, -139, 230, -97, 117, 205, 134, 154, -205, 122, 90, 138, 142,
]

相关文件

  • 结算实现:investment-sim/js/core/settlement.js
  • 月结引擎:investment-sim/js/core/monthEngine.js
  • 常数表:investment-sim/js/core/tables.js
  • RNG实现:investment-sim/js/core/rng.js
  • 数据配置:data/investment-sim/stocks-futures.json

Latest Posts

Introducing some new layouts to Bulma Clean Theme
Introducing some new layouts to Bulma Clean Theme

I’ve been meaning to write about some of the new features I have been rolling out to my Jekyll theme, Bulma Clean Theme, for a while but I have only just managed to push the update for the landing page layout last weekend. This article provides some of the thinking behind the updates, as well as a brief introduction to how to get started.

Getting started with Bulma Clean Theme for Jekyll
Getting started with Bulma Clean Theme for Jekyll

I have made a theme for Jekyll which is based on the Bulma frontend framework. It might look familiar, because I am also using it for this site. If you like the look of this theme then I thought I would write a little blog post about how to use it for your own site and how you can use it with GitHub Pages.

Why use a static site generator
Why use a static site generator

There are many ways to make a website and many different CMS platforms you can use, such as WordPress and Joomla, as well as site builder tools that offer you drag and drop interfaces, but what about static site generators?