diff --git a/client/src/containers/traders/profiles/ProfileDashboard.tsx b/client/src/containers/traders/profiles/ProfileDashboard.tsx index eccfa32..97c6543 100644 --- a/client/src/containers/traders/profiles/ProfileDashboard.tsx +++ b/client/src/containers/traders/profiles/ProfileDashboard.tsx @@ -134,7 +134,7 @@ const ProfileDashboard = () => { ~/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", diff --git a/server/logics/transaction.ts b/server/logics/transaction.ts index 9101259..c147030 100644 --- a/server/logics/transaction.ts +++ b/server/logics/transaction.ts @@ -283,7 +283,7 @@ 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 @@ -291,6 +291,7 @@ export const buyItemToHolding = ( : maxBuyAmount const sharesBought = Math.floor(maxCashToUse / tickerInfo.closePrice) + if (!sharesBought) return null const baseSharesBought = sharesBought / tickerInfo.splitMultiplier diff --git a/server/migrations/0014_trader_pattern.js b/server/migrations/0014_trader_pattern.js index 3a49cf5..e7d74fe 100644 --- a/server/migrations/0014_trader_pattern.js +++ b/server/migrations/0014_trader_pattern.js @@ -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') @@ -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') @@ -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') @@ -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') }) } diff --git a/server/models/traderEnvFollower.ts b/server/models/traderEnvFollower.ts index 64130fd..305455c 100644 --- a/server/models/traderEnvFollower.ts +++ b/server/models/traderEnvFollower.ts @@ -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 } diff --git a/server/models/user.ts b/server/models/user.ts index 6367aa2..64437dc 100644 --- a/server/models/user.ts +++ b/server/models/user.ts @@ -23,7 +23,7 @@ export const getByActivationCode = async ( { key: 'activationCode', value: code }, ], }) - return convertToRecord(user) + return user ? convertToRecord(user) : null } export const getByPK = async ( @@ -35,7 +35,7 @@ export const getByPK = async ( { key: 'id', value: id }, ], }) - return convertToRecord(user) + return user ? convertToRecord(user) : null } export const getByUK = async ( @@ -47,7 +47,7 @@ export const getByUK = async ( { key: 'email', value: email }, ], }) - return convertToRecord(user) + return user ? convertToRecord(user) : null } export const create = async ( diff --git a/server/services/calcTickers.ts b/server/services/calcTickers.ts index 219cece..9fe8df8 100644 --- a/server/services/calcTickers.ts +++ b/server/services/calcTickers.ts @@ -728,6 +728,9 @@ export const groupTickerInfo = ( } }) + info.closePrice = tickerDaily.closePrice + info.splitMultiplier = tickerDaily.splitMultiplier + return info } @@ -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) diff --git a/server/services/calcTraders.ts b/server/services/calcTraders.ts index a199785..4568b72 100644 --- a/server/services/calcTraders.ts +++ b/server/services/calcTraders.ts @@ -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)