Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Fix trader performance calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
byn9826 committed Sep 14, 2024
1 parent 29105ac commit 951aa6b
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const ProfileDashboard = () => {
<AddButton
data-testid='addComboBtn'
onClick={handleClickAddCombo}
disabled={!user.access.canFollowCombo}
disabled={!user.access.canFollowCombo || !user.userTraderIds.length}
title={localeTool.t('dashboard.newBtn')}
tooltip={
user.access.canFollowCombo
Expand Down
6 changes: 1 addition & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@

"lint": "npm run lint --workspace=interfaces && npm run lint --workspace=constants && npm run lint --workspace=helpers && npm run lint --workspace=client && npm run lint --workspace=server",
"lint:fix": "npm run lint:fix --workspace=interfaces && npm run lint:fix --workspace=constants && npm run lint:fix --workspace=helpers && npm run lint:fix --workspace=client && npm run lint:fix --workspace=server",
"tsc": "npm run tsc --workspace=client && npm run tsc --workspace=server",
"database:create": "createdb -h 127.0.0.1 -U admin melody",
"database:drop": "sudo -u postgres psql -c 'DROP DATABASE melody'",
"database:dump": "pg_dump -h 127.0.0.1 -Fc melody -U admin > ~/Backup/melody.bak",
"database:restore": "pg_restore -d melody -U admin ~/Backup/melody.bak"
"tsc": "npm run tsc --workspace=client && npm run tsc --workspace=server"
},
"devDependencies": {
"@swc/core": "^1.3.24",
Expand Down
3 changes: 2 additions & 1 deletion server/logics/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,15 @@ export const buyItemToHolding = (
holdingBuyPercent: number,
tickerMaxPercent: number,
): interfaces.traderHoldingModel.Detail | null => {
const maxBuyAmount = holdingDetail.totalValue & holdingBuyPercent
const maxBuyAmount = holdingDetail.totalValue * holdingBuyPercent

// Use maximum allowed cash or use total cash left if less than maximun allowed
const maxCashToUse = holdingDetail.totalCash < maxBuyAmount
? holdingDetail.totalCash
: maxBuyAmount

const sharesBought = Math.floor(maxCashToUse / tickerInfo.closePrice)

if (!sharesBought) return null

const baseSharesBought = sharesBought / tickerInfo.splitMultiplier
Expand Down
160 changes: 80 additions & 80 deletions server/migrations/0014_trader_pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,18 @@ exports.up = (knex) => {
table.smallint('nonfarmPayrollMonthlyIncreaseSell')
table.smallint('nonfarmPayrollMonthlyDecreaseBuy')
table.smallint('nonfarmPayrollMonthlyDecreaseSell')
table.smallint('gdpYearlyChangeAboveBuy')
table.smallint('gdpYearlyChangeAboveSell')
table.smallint('gdpYearlyChangeBelowBuy')
table.smallint('gdpYearlyChangeBelowSell')
table.smallint('seasonalGDPQoQAboveBuy')
table.smallint('seasonalGDPQoQAboveSell')
table.smallint('seasonalGDPQoQBelowBuy')
table.smallint('seasonalGDPQoQBelowSell')
table.smallint('seasonalGDPYoYAboveBuy')
table.smallint('seasonalGDPYoYAboveSell')
table.smallint('seasonalGDPYoYBelowBuy')
table.smallint('seasonalGDPYoYBelowSell')
table.integer('gdpYearlyChangeAboveBuy')
table.integer('gdpYearlyChangeAboveSell')
table.integer('gdpYearlyChangeBelowBuy')
table.integer('gdpYearlyChangeBelowSell')
table.integer('seasonalGDPQoQAboveBuy')
table.integer('seasonalGDPQoQAboveSell')
table.integer('seasonalGDPQoQBelowBuy')
table.integer('seasonalGDPQoQBelowSell')
table.integer('seasonalGDPYoYAboveBuy')
table.integer('seasonalGDPYoYAboveSell')
table.integer('seasonalGDPYoYBelowBuy')
table.integer('seasonalGDPYoYBelowSell')
table.smallint('peQuarterlyIncreaseBuy')
table.smallint('peQuarterlyIncreaseSell')
table.smallint('peQuarterlyDecreaseBuy')
Expand Down Expand Up @@ -144,34 +144,34 @@ exports.up = (knex) => {
table.smallint('debtEquityQuarterlyIncreaseSell')
table.smallint('debtEquityQuarterlyDecreaseBuy')
table.smallint('debtEquityQuarterlyDecreaseSell')
table.smallint('peRatioQuarterlyAboveBuy')
table.smallint('peRatioQuarterlyAboveSell')
table.smallint('peRatioQuarterlyBelowBuy')
table.smallint('peRatioQuarterlyBelowSell')
table.smallint('pbRatioQuarterlyAboveBuy')
table.smallint('pbRatioQuarterlyAboveSell')
table.smallint('pbRatioQuarterlyBelowBuy')
table.smallint('pbRatioQuarterlyBelowSell')
table.smallint('psRatioQuarterlyAboveBuy')
table.smallint('psRatioQuarterlyAboveSell')
table.smallint('psRatioQuarterlyBelowBuy')
table.smallint('psRatioQuarterlyBelowSell')
table.smallint('roaQuarterlyAboveBuy')
table.smallint('roaQuarterlyAboveSell')
table.smallint('roaQuarterlyBelowBuy')
table.smallint('roaQuarterlyBelowSell')
table.smallint('roeQuarterlyAboveBuy')
table.smallint('roeQuarterlyAboveSell')
table.smallint('roeQuarterlyBelowBuy')
table.smallint('roeQuarterlyBelowSell')
table.smallint('grossMarginQuarterlyAboveBuy')
table.smallint('grossMarginQuarterlyAboveSell')
table.smallint('grossMarginQuarterlyBelowBuy')
table.smallint('grossMarginQuarterlyBelowSell')
table.smallint('debtEquityQuarterlyAboveBuy')
table.smallint('debtEquityQuarterlyAboveSell')
table.smallint('debtEquityQuarterlyBelowBuy')
table.smallint('debtEquityQuarterlyBelowSell')
table.integer('peRatioQuarterlyAboveBuy')
table.integer('peRatioQuarterlyAboveSell')
table.integer('peRatioQuarterlyBelowBuy')
table.integer('peRatioQuarterlyBelowSell')
table.integer('pbRatioQuarterlyAboveBuy')
table.integer('pbRatioQuarterlyAboveSell')
table.integer('pbRatioQuarterlyBelowBuy')
table.integer('pbRatioQuarterlyBelowSell')
table.integer('psRatioQuarterlyAboveBuy')
table.integer('psRatioQuarterlyAboveSell')
table.integer('psRatioQuarterlyBelowBuy')
table.integer('psRatioQuarterlyBelowSell')
table.integer('roaQuarterlyAboveBuy')
table.integer('roaQuarterlyAboveSell')
table.integer('roaQuarterlyBelowBuy')
table.integer('roaQuarterlyBelowSell')
table.integer('roeQuarterlyAboveBuy')
table.integer('roeQuarterlyAboveSell')
table.integer('roeQuarterlyBelowBuy')
table.integer('roeQuarterlyBelowSell')
table.integer('grossMarginQuarterlyAboveBuy')
table.integer('grossMarginQuarterlyAboveSell')
table.integer('grossMarginQuarterlyBelowBuy')
table.integer('grossMarginQuarterlyBelowSell')
table.integer('debtEquityQuarterlyAboveBuy')
table.integer('debtEquityQuarterlyAboveSell')
table.integer('debtEquityQuarterlyBelowBuy')
table.integer('debtEquityQuarterlyBelowSell')
table.smallint('peYearlyIncreaseBuy')
table.smallint('peYearlyIncreaseSell')
table.smallint('peYearlyDecreaseBuy')
Expand All @@ -196,42 +196,42 @@ exports.up = (knex) => {
table.smallint('freeCashFlowYearlyIncreaseSell')
table.smallint('freeCashFlowYearlyDecreaseBuy')
table.smallint('freeCashFlowYearlyDecreaseSell')
table.smallint('peRatioYearlyAboveBuy')
table.smallint('peRatioYearlyAboveSell')
table.smallint('peRatioYearlyBelowBuy')
table.smallint('peRatioYearlyBelowSell')
table.smallint('pbRatioYearlyAboveBuy')
table.smallint('pbRatioYearlyAboveSell')
table.smallint('pbRatioYearlyBelowBuy')
table.smallint('pbRatioYearlyBelowSell')
table.smallint('psRatioYearlyAboveBuy')
table.smallint('psRatioYearlyAboveSell')
table.smallint('psRatioYearlyBelowBuy')
table.smallint('psRatioYearlyBelowSell')
table.smallint('fundsRateMonthlyAboveBuy')
table.smallint('fundsRateMonthlyAboveSell')
table.smallint('fundsRateMonthlyBelowBuy')
table.smallint('fundsRateMonthlyBelowSell')
table.smallint('tenYearsTreasuryMonthlyAboveBuy')
table.smallint('tenYearsTreasuryMonthlyAboveSell')
table.smallint('tenYearsTreasuryMonthlyBelowBuy')
table.smallint('tenYearsTreasuryMonthlyBelowSell')
table.smallint('thirtyYearsTreasuryMonthlyAboveBuy')
table.smallint('thirtyYearsTreasuryMonthlyAboveSell')
table.smallint('thirtyYearsTreasuryMonthlyBelowBuy')
table.smallint('thirtyYearsTreasuryMonthlyBelowSell')
table.smallint('inflationMonthlyAboveBuy')
table.smallint('inflationMonthlyAboveSell')
table.smallint('inflationMonthlyBelowBuy')
table.smallint('inflationMonthlyBelowSell')
table.smallint('consumerSentimentMonthlyAboveBuy')
table.smallint('consumerSentimentMonthlyAboveSell')
table.smallint('consumerSentimentMonthlyBelowBuy')
table.smallint('consumerSentimentMonthlyBelowSell')
table.smallint('nonfarmPayrollMonthlyAboveBuy')
table.smallint('nonfarmPayrollMonthlyAboveSell')
table.smallint('nonfarmPayrollMonthlyBelowBuy')
table.smallint('nonfarmPayrollMonthlyBelowSell')
table.integer('peRatioYearlyAboveBuy')
table.integer('peRatioYearlyAboveSell')
table.integer('peRatioYearlyBelowBuy')
table.integer('peRatioYearlyBelowSell')
table.integer('pbRatioYearlyAboveBuy')
table.integer('pbRatioYearlyAboveSell')
table.integer('pbRatioYearlyBelowBuy')
table.integer('pbRatioYearlyBelowSell')
table.integer('psRatioYearlyAboveBuy')
table.integer('psRatioYearlyAboveSell')
table.integer('psRatioYearlyBelowBuy')
table.integer('psRatioYearlyBelowSell')
table.integer('fundsRateMonthlyAboveBuy')
table.integer('fundsRateMonthlyAboveSell')
table.integer('fundsRateMonthlyBelowBuy')
table.integer('fundsRateMonthlyBelowSell')
table.integer('tenYearsTreasuryMonthlyAboveBuy')
table.integer('tenYearsTreasuryMonthlyAboveSell')
table.integer('tenYearsTreasuryMonthlyBelowBuy')
table.integer('tenYearsTreasuryMonthlyBelowSell')
table.integer('thirtyYearsTreasuryMonthlyAboveBuy')
table.integer('thirtyYearsTreasuryMonthlyAboveSell')
table.integer('thirtyYearsTreasuryMonthlyBelowBuy')
table.integer('thirtyYearsTreasuryMonthlyBelowSell')
table.integer('inflationMonthlyAboveBuy')
table.integer('inflationMonthlyAboveSell')
table.integer('inflationMonthlyBelowBuy')
table.integer('inflationMonthlyBelowSell')
table.integer('consumerSentimentMonthlyAboveBuy')
table.integer('consumerSentimentMonthlyAboveSell')
table.integer('consumerSentimentMonthlyBelowBuy')
table.integer('consumerSentimentMonthlyBelowSell')
table.integer('nonfarmPayrollMonthlyAboveBuy')
table.integer('nonfarmPayrollMonthlyAboveSell')
table.integer('nonfarmPayrollMonthlyBelowBuy')
table.integer('nonfarmPayrollMonthlyBelowSell')
table.smallint('seasonalGDPQuarterlyIncreaseBuy')
table.smallint('seasonalGDPQuarterlyIncreaseSell')
table.smallint('seasonalGDPQuarterlyDecreaseBuy')
Expand All @@ -240,10 +240,10 @@ exports.up = (knex) => {
table.smallint('gdpYearlyIncreaseSell')
table.smallint('gdpYearlyDecreaseBuy')
table.smallint('gdpYearlyDecreaseSell')
table.smallint('inflationYearlyAboveBuy')
table.smallint('inflationYearlyAboveSell')
table.smallint('inflationYearlyBelowBuy')
table.smallint('inflationYearlyBelowSell')
table.integer('inflationYearlyAboveBuy')
table.integer('inflationYearlyAboveSell')
table.integer('inflationYearlyBelowBuy')
table.integer('inflationYearlyBelowSell')
table.unique('hashCode', 'trader_pattern_ukey')
})
}
Expand Down
3 changes: 2 additions & 1 deletion server/models/traderEnvFollower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export const getEnvFollowers = async (
const records = await databaseAdapter.findAll({
tableName: TableName,
conditions: [
{ key: 'envId', value: envId },
{ key: 'traderEnvId', value: envId },
],
orderBy: [{ column: 'traderEnvId', order: 'asc' }],
})
return records
}
Expand Down
6 changes: 3 additions & 3 deletions server/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const getByActivationCode = async (
{ key: 'activationCode', value: code },
],
})
return convertToRecord(user)
return user ? convertToRecord(user) : null
}

export const getByPK = async (
Expand All @@ -35,7 +35,7 @@ export const getByPK = async (
{ key: 'id', value: id },
],
})
return convertToRecord(user)
return user ? convertToRecord(user) : null
}

export const getByUK = async (
Expand All @@ -47,7 +47,7 @@ export const getByUK = async (
{ key: 'email', value: email },
],
})
return convertToRecord(user)
return user ? convertToRecord(user) : null
}

export const create = async (
Expand Down
12 changes: 8 additions & 4 deletions server/services/calcTickers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,9 @@ export const groupTickerInfo = (
}
})

info.closePrice = tickerDaily.closePrice
info.splitMultiplier = tickerDaily.splitMultiplier

return info
}

Expand Down Expand Up @@ -778,11 +781,12 @@ export const calcDailyTickers = async () => {
const tickerIds = tickers.map((ticker) => ticker.id)

console.info(`checking entity: ${entity.id}`)
const lastRecord = await dailyTickersModel.getLast(entity.id)
// const lastRecord = await dailyTickersModel.getLast(entity.id)
let targetDate = dateTool.getInitialDate()
// Always double check latest 60 days
let targetDate = lastRecord && Object.keys(lastRecord.priceInfo).length === tickerIds.length
? dateTool.getPreviousDate(lastRecord.date, 60)
: dateTool.getInitialDate()
// let targetDate = lastRecord && Object.keys(lastRecord.priceInfo).length === tickerIds.length
// ? dateTool.getPreviousDate(lastRecord.date, 60)
// : dateTool.getInitialDate()

while (targetDate <= lastPriceDate) {
const nextDate = dateTool.getNextDate(targetDate)
Expand Down
4 changes: 2 additions & 2 deletions server/services/calcTraders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ const calcTraderPerformance = async (
const dailyIndicators = await getCachedDailyIndicators(tradeDate)
const indicatorInfo = dailyIndicators?.indicatorInfo || {}

// Only keep market data related to env defined tickers
const availableTickerInfos = env.tickerIds.reduce((tickers, tickerId) => {
const tickerIds = env.tickerIds ?? (await tickerModel.getAll()).map((ticker) => ticker.id)
const availableTickerInfos = tickerIds.reduce((tickers, tickerId) => {
tickers[tickerId] = tickerInfos[tickerId]
return tickers
}, {} as interfaces.dailyTickersModel.TickerInfos)
Expand Down

0 comments on commit 951aa6b

Please sign in to comment.