Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Held stock highlight #432

Merged
merged 5 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,11 @@
"default": {},
"description": "股票持仓金额(不建议手填,格式错误影响功能)"
},
"leek-fund.stockHeldTipShow": {
"type": "boolean",
"default": false,
"description": "是否开启股票持仓提示"
},
"leek-fund.funds": {
"type": "array",
"default": [
Expand Down Expand Up @@ -758,7 +763,7 @@
"@types/lodash.throttle": "^4.1.6",
"@types/mocha": "^7.0.2",
"@types/node": "^13.11.0",
"@types/vscode": "1.44.0",
"@types/vscode": "1.59.0",
"@types/ws": "^7.4.0",
"@typescript-eslint/eslint-plugin": "^2.30.0",
"@typescript-eslint/parser": "^2.30.0",
Expand Down
22 changes: 21 additions & 1 deletion src/explorer/stockService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { decode } from 'iconv-lite';
import { ExtensionContext, QuickPickItem, window } from 'vscode';
import globalState from '../globalState';
import { LeekTreeItem } from '../shared/leekTreeItem';
import { HeldData } from '../shared/typed';
import { executeStocksRemind } from '../shared/remindNotification';
import { calcFixedPriceNumber, events, formatNumber, randHeader, sortData } from '../shared/utils';
import { LeekService } from './leekService';
Expand Down Expand Up @@ -127,6 +128,16 @@ export default class StockService extends LeekService {
}
} else {
const splitData = resp.data.split(';\n');
const stockPrice: {
[key: string]: {
amount: number;
earnings: number;
name: string;
price: string;
unitPrice: number;
};
} = globalState.stockPrice;

for (let i = 0; i < splitData.length - 1; i++) {
const code = splitData[i].split('="')[0].split('var hq_str_')[1];
const params = splitData[i].split('="')[1].split(',');
Expand All @@ -136,12 +147,20 @@ export default class StockService extends LeekService {
let fixedNumber = 2;
if (params.length > 1) {
if (/^(sh|sz|bj)/.test(code)) {
// A股
let open = params[1];
let yestclose = params[2];
let price = params[3];
let high = params[4];
let low = params[5];
fixedNumber = calcFixedPriceNumber(open, yestclose, price, high, low);
const profitData = stockPrice[code] || {};
const heldData: HeldData = {};
if (profitData.amount) {
// 表示是持仓股
heldData.heldAmount = profitData.amount;
heldData.heldPrice = profitData.unitPrice;
}
stockItem = {
code,
name: params[0],
Expand All @@ -154,6 +173,7 @@ export default class StockService extends LeekService {
amount: formatNumber(params[9], 2),
time: `${params[30]} ${params[31]}`,
percent: '',
...heldData,
};
aStockCount += 1;
} else if (/^gb_/.test(code)) {
Expand Down Expand Up @@ -489,7 +509,7 @@ export default class StockService extends LeekService {

// 期货大写字母开头
const isFuture =
/^[A-Z]/.test(searchText[0]) ||
/^[A-Z]/.test(searchText.charAt(0)) ||
/nf_/.test(searchText) ||
/hf_/.test(searchText) ||
/fx_/.test(searchText);
Expand Down
3 changes: 2 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,12 @@ export function activate(context: ExtensionContext) {
function setGlobalVariable() {
const stockPrice = LeekFundConfig.getConfig('leek-fund.stockPrice') || {};
cacheStockPriceData(stockPrice);
globalState.iconType = LeekFundConfig.getConfig('leek-fund.iconType') || 'arrow';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里删除的原因是?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

误删。。。


const fundAmount = LeekFundConfig.getConfig('leek-fund.fundAmount') || {};
cacheFundAmountData(fundAmount);

globalState.stockHeldTipShow = LeekFundConfig.getConfig('leek-fund.stockHeldTipShow') ?? true;

const stocksRemind = LeekFundConfig.getConfig('leek-fund.stocksRemind') || {};
cacheStocksRemindData(stocksRemind);

Expand Down
4 changes: 4 additions & 0 deletions src/globalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ let newsIntervalTime = 20000; // 新闻刷新频率(毫秒)
let newsIntervalTimer: NodeJS.Timer | any = null; // 计算器控制
let labelFormat = DEFAULT_LABEL_FORMAT;

let stockHeldTipShow = true; // 是否开启股票持仓提示

let aStockCount = 0;
let usStockCount = 0;
let hkStockCount = 0;
Expand Down Expand Up @@ -70,4 +72,6 @@ export default {

stockPrice,
stockPriceCacheDate,

stockHeldTipShow,
};
14 changes: 14 additions & 0 deletions src/registerCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,10 @@ export function registerViewEvent(
label: globalState.kLineChartSwitch ? '🔛 切换为常规k线图' : '📴 切换为筹码分布K线图',
description: 'kLineChartSwitch',
},
{
label: globalState.stockHeldTipShow ? '关闭持仓股高亮展示' : '关闭持仓股高亮展示',
description: 'stockHeldTipShow',
},
],
{
placeHolder: '第一步:选择设置项',
Expand Down Expand Up @@ -522,6 +526,8 @@ export function registerViewEvent(
commands.executeCommand('leek-fund.toggleRemindSwitch');
} else if (type === 'kLineChartSwitch') {
commands.executeCommand('leek-fund.toggleKLineChartSwitch');
} else if (type === 'stockHeldTipShow') {
commands.executeCommand('leek-fund.toggleStockHeldTipShow');
}
});
})
Expand Down Expand Up @@ -552,6 +558,14 @@ export function registerViewEvent(
})
);

context.subscriptions.push(
commands.registerCommand('leek-fund.toggleStockHeldTipShow', () => {
const newValue = !globalState.stockHeldTipShow;
LeekFundConfig.setConfig('leek-fund.stockHeldTipShow', newValue);
globalState.stockHeldTipShow = newValue;
})
);

context.subscriptions.push(
commands.registerCommand('leek-fund.changeStatusBarItem', (stockId) => {
const stockList = stockService.stockList;
Expand Down
30 changes: 20 additions & 10 deletions src/shared/leekTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export class LeekTreeItem extends TreeItem {
cashSellPrice = 0,
conversionPrice = 0,
publishDateTime = '',
heldAmount = 0,
heldPrice = 0,
} = info;

if (_itemType) {
Expand Down Expand Up @@ -128,7 +130,7 @@ export class LeekTreeItem extends TreeItem {
)}「${name}」`; */
text = formatLabelString(
globalState.labelFormat?.['sidebarStockLabelFormat'] ??
DEFAULT_LABEL_FORMAT.sidebarStockLabelFormat,
DEFAULT_LABEL_FORMAT.sidebarStockLabelFormat,
{
...info,
icon: !isIconPath ? iconPath : '',
Expand All @@ -145,7 +147,7 @@ export class LeekTreeItem extends TreeItem {
}` + `${t2 ? `(${time})` : ''}`; */
text = formatLabelString(
globalState.labelFormat?.['sidebarFundLabelFormat'] ??
DEFAULT_LABEL_FORMAT.sidebarFundLabelFormat,
DEFAULT_LABEL_FORMAT.sidebarFundLabelFormat,
{
...info,
icon: !isIconPath ? iconPath : '',
Expand All @@ -161,7 +163,7 @@ export class LeekTreeItem extends TreeItem {
} else if (this._itemType === TreeItemType.BINANCE) {
text = formatLabelString(
globalState.labelFormat?.['sidebarBinanceLabelFormat'] ??
DEFAULT_LABEL_FORMAT.sidebarBinanceLabelFormat,
DEFAULT_LABEL_FORMAT.sidebarBinanceLabelFormat,
{
...info,
icon: !isIconPath ? iconPath : '',
Expand All @@ -171,9 +173,9 @@ export class LeekTreeItem extends TreeItem {
} else if (this._itemType === TreeItemType.FOREX) {
text = formatLabelString(
globalState.labelFormat?.['sidebarForexLabelFormat'] ??
DEFAULT_LABEL_FORMAT.sidebarForexLabelFormat,
DEFAULT_LABEL_FORMAT.sidebarForexLabelFormat,
{
...info
...info,
}
);
}
Expand All @@ -184,8 +186,15 @@ export class LeekTreeItem extends TreeItem {
? `${formatTreeText(`${_percent}%`, 11)}${formatTreeText(price, 15)} 「${code}」`
: `${formatTreeText(`${_percent}%`)}「${code}」`;
}

this.label = text;
if (heldAmount && globalState.stockHeldTipShow) {
this.label = {
label: text,
highlights: [[0, text.length]],
};
this.description = '(持仓)';
} else {
this.label = text;
}
this.id = info.id || code;

if (this._itemType === TreeItemType.STOCK || this._itemType === TreeItemType.FUND) {
Expand Down Expand Up @@ -225,9 +234,10 @@ export class LeekTreeItem extends TreeItem {
this.tooltip = '接口不支持,右键删除关注';
} else if (isFuture) {
this.tooltip = `【今日行情】${name} ${code}\n 涨跌:${updown} 百分比:${_percent}%\n 最高:${high} 最低:${low}\n 今开:${open} 昨结:${yestclose}\n 成交量:${volume} 成交额:${amount}`;
}
else {
this.tooltip = `【今日行情】${labelText}${typeText}${symbolText}\n 涨跌:${updown} 百分比:${_percent}%\n 最高:${high} 最低:${low}\n 今开:${open} 昨收:${yestclose}\n 成交量:${volume} 成交额:${amount}`;
} else {
this.tooltip = `【今日行情】${labelText}${typeText}${symbolText}\n 涨跌:${updown} 百分比:${_percent}%\n 最高:${high} 最低:${low}\n 今开:${open} 昨收:${yestclose}\n 成交量:${volume} 成交额:${amount}\n ${
heldAmount ? `持仓数:${volume} 持仓价:${heldPrice}` : ''
}`;
}
} else if (this._itemType === TreeItemType.BINANCE) {
this.tooltip = `【今日行情】${name}\n 涨跌:${updown} 百分比:${_percent}%\n 最高:${high} 最低:${low}\n 今开:${open} 昨收:${yestclose}\n 成交量:${volume} 成交额:${amount}`;
Expand Down
8 changes: 7 additions & 1 deletion src/shared/typed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export enum TreeItemType {
/** 外汇 */
FOREX = 'forex',
}

export interface IAmount {
name: string;
price: number | string;
Expand Down Expand Up @@ -81,6 +80,8 @@ export interface FundInfo {
conversionPrice?: number; // 中行折算价
publishDateTime?: string; // 发布日期:年月日 时分秒
publishTime?: string; // 发布时间:时分秒
heldAmount?: number; // 持仓数
heldPrice?: number; // 持仓价
}

export const defaultFundInfo: FundInfo = {
Expand All @@ -106,3 +107,8 @@ export interface ProfitStatusBarInfo {
fundAmount: number;
priceDate: string;
}

export type HeldData = {
heldAmount?: number;
heldPrice?: number;
};
29 changes: 8 additions & 21 deletions src/statusbar/Profit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import { StatusBarAlignment, StatusBarItem, window } from 'vscode';
import { TIPS_LOSE, TIPS_WIN } from '../shared/constant';
import { LeekFundConfig } from '../shared/leekConfig';
import { ProfitStatusBarInfo } from '../shared/typed';
import { events, formatDate } from '../shared/utils';
import { events, formatDate, toFixed } from '../shared/utils';
import StockService from '../explorer/stockService';
import globalState from '../globalState';

const PREFIX = '💰';

Expand Down Expand Up @@ -99,15 +98,6 @@ export class ProfitStatusBar {
// TODO
updateStockBarItem(data: StockService) {
if (this.stockBarItem) {
const stockPrice: {
[key: string]: {
amount: number;
earnings: number;
name: string;
price: string;
unitPrice: number;
};
} = globalState.stockPrice;
const stockList = data.getSelfSelected();
type StockInfoType = {
id: string;
Expand All @@ -126,20 +116,17 @@ export class ProfitStatusBar {
stockList.forEach((s) => {
let tmp = {} as StockInfoType;
const { id, info } = s;
const { high, low, open, yestclose, percent, price, name } = info;
const { high, low, open, yestclose, percent, price, name, heldAmount, heldPrice } = info;
if (id && open && price) {
const config = stockPrice[id];
if (!config || config.amount === 0 || config.unitPrice === 0) {
if (!heldAmount || !heldPrice) {
return false;
}
const unitPrice = config?.unitPrice || 0;
const amount = config?.amount || 0;
// const incomeTotal = amount * (Number(price).toFixed(2) - unitPrice.toFixed(2));
// const incomeToday = amount * (Number(price).toFixed(2) - Number(open).toFixed(2));
const incomeTotal = (amount * (Number(price) - unitPrice)).toFixed(2);
const incomeTotal = (heldAmount * (Number(price) - heldPrice)).toFixed(2);
// fix #399,在昨日收盘价没有的时候使用今日开盘价
const incomeToday = (amount * (Number(price) - Number(yestclose || open))).toFixed(2);
const percentTotal = ((Number(incomeTotal) / (unitPrice * amount)) * 100).toFixed(2);
const incomeToday = (heldAmount * (Number(price) - Number(yestclose || open))).toFixed(2);
const percentTotal = ((Number(incomeTotal) / (heldPrice * heldAmount)) * 100).toFixed(2);
tmp = {
id,
name,
Expand All @@ -148,7 +135,7 @@ export class ProfitStatusBar {
open,
percent,
price,
amount,
amount: heldAmount,
incomeTotal,
incomeToday,
percentTotal,
Expand All @@ -164,7 +151,7 @@ export class ProfitStatusBar {
return prev + Number(cur.incomeTotal);
}, 0);
// Use the year, month, and day variables as needed
this.stockBarItem.text = `${PREFIX} ${allIncomeTotal} | ${allIncomeToday}`;
this.stockBarItem.text = `${PREFIX} ${toFixed(allIncomeTotal)} | ${toFixed(allIncomeToday)}`;
// this.stockBarItem.color = fundProfit >= 0 ? this.riseColor : this.fallColor;
this.stockBarItem.tooltip =
`「股票收益统计」 ${date}\r\n \r\n` +
Expand Down
8 changes: 1 addition & 7 deletions types/shim-vscode.d.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为啥删了

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
declare module 'vscode' {
export interface ExtensionContext {
export interface ExtensionContext {
readonly extensionUri: Uri;
}
/**
Expand Down Expand Up @@ -136,11 +136,5 @@ declare module 'vscode' {
scopes: string[],
options?: AuthenticationGetSessionOptions
): Thenable<AuthenticationSession | undefined>;

/**
* An [event](#Event) which fires when the authentication sessions of an authentication provider have
* been added, removed, or changed.
*/
export const onDidChangeSessions: Event<AuthenticationSessionsChangeEvent>;
}
}
Loading
Loading