Genetics front (#29)

* Add Genetics js

* Fix some error for genetics

* Update removed backtest

* Fixes
This commit is contained in:
Oda
2025-07-08 20:06:09 +07:00
committed by GitHub
parent 7a74c44739
commit 3439f13156
12 changed files with 2014 additions and 18 deletions

14
package-lock.json generated
View File

@@ -2,5 +2,17 @@
"name": "managing-apps",
"lockfileVersion": 3,
"requires": true,
"packages": {}
"packages": {
"": {
"dependencies": {
"genetic-js": "^0.1.14"
}
},
"node_modules/genetic-js": {
"version": "0.1.14",
"resolved": "https://registry.npmjs.org/genetic-js/-/genetic-js-0.1.14.tgz",
"integrity": "sha512-HHm21naCEF1EVKTWPFzKX4ENB7Nn/my4kTy2POi4u/2gB0XPUOh8oDlhhESVCZVBge3b7nuLrZNZNAt4ObH19Q==",
"license": "BSD"
}
}
}

5
package.json Normal file
View File

@@ -0,0 +1,5 @@
{
"dependencies": {
"genetic-js": "^0.1.14"
}
}

View File

@@ -1504,7 +1504,7 @@ public class TradingBot : Bot, ITradingBot
/// <returns>True if the position has exceeded the time limit, false otherwise</returns>
private bool HasPositionExceededTimeLimit(Position position, DateTime currentTime)
{
if (!Config.MaxPositionTimeHours.HasValue)
if (!Config.MaxPositionTimeHours.HasValue || Config.MaxPositionTimeHours.Value <= 0)
{
return false; // Time-based closure is disabled
}

View File

@@ -34,6 +34,7 @@
"connectkit": "^1.8.2",
"date-fns": "^2.30.0",
"elliptic": "^6.6.1",
"genetic-js": "^0.1.14",
"jotai": "^1.6.7",
"latest-version": "^9.0.0",
"lightweight-charts": "git+https://github.com/ntf/lightweight-charts.git",
@@ -65,7 +66,7 @@
"@types/react": "^18.0.9",
"@types/react-dom": "^18.0.4",
"@types/react-grid-layout": "^1.3.2",
"@types/react-plotly.js": "^2.6.0",
"@types/react-plotly.js": "^2.6.3",
"@types/react-slider": "^1.3.1",
"@types/react-table": "^7.7.12",
"@types/signalr": "^2.2.37",

View File

@@ -1,17 +1,28 @@
import {create} from 'zustand'
import type {AccountStore} from '../../global/type.tsx'
import {AccountClient} from '../../generated/ManagingApi'
import useApiUrlStore from './apiStore'
export const useAuthStore = create<AccountStore>((set) => ({
export const useAuthStore = create<AccountStore>((set, get) => ({
accounts: [],
onInitialize: () => {
console.log('useFlowStore onInitialize')
onInitialize: async () => {
console.log('useAuthStore onInitialize')
// const accountClient = new AccountClient({}, apiUrl)
// const accounts = await accountClient.account_GetAccounts()
// if (accounts.length > 0) {
// get().setAccounts(accounts)
// }
try {
const { apiUrl } = useApiUrlStore.getState()
const accountClient = new AccountClient({}, apiUrl)
const accounts = await accountClient.account_GetAccounts()
if (accounts.length > 0) {
get().setAccounts(accounts)
console.log('Accounts loaded:', accounts.length)
} else {
console.log('No accounts found')
}
} catch (error) {
console.error('Failed to load accounts:', error)
}
},
setAccounts: (accounts) => {
set((state) => ({
@@ -19,4 +30,12 @@ export const useAuthStore = create<AccountStore>((set) => ({
accounts: accounts,
}))
},
getFirstAccount: () => {
const state = get()
return state.accounts.length > 0 ? state.accounts[0] : null
},
getFirstAccountName: () => {
const state = get()
return state.accounts.length > 0 ? state.accounts[0].name : ''
},
}))

View File

@@ -288,7 +288,7 @@ const BacktestRowDetails: React.FC<IBacktestRowDetailsProps> = ({
title="Money Management"
content={
"SL: " +(config.moneyManagement?.stopLoss * 100).toFixed(2) + "% TP: " +
(config.moneyManagement?.takeProfit * 100).toFixed(2) + "%"
(config.moneyManagement?.takeProfit * 100).toFixed(2) + "%" + " Lev.: x" + config.moneyManagement?.leverage
}
></CardText>
<CardText

View File

@@ -14,11 +14,12 @@ import BacktestRowDetails from './backtestRowDetails'
interface BacktestTableProps {
list: Backtest[] | undefined
isFetching?: boolean
displaySummary?: boolean
}
const BacktestTable: React.FC<BacktestTableProps> = ({list, isFetching}) => {
const BacktestTable: React.FC<BacktestTableProps> = ({list, isFetching, displaySummary = true}) => {
const [rows, setRows] = useState<Backtest[]>([])
const {apiUrl} = useApiUrlStore()
const {removeBacktest} = useBacktestStore()
@@ -462,7 +463,7 @@ const BacktestTable: React.FC<BacktestTableProps> = ({list, isFetching}) => {
</div>
) : (
<>
{list && list.length > 0 && (
{list && list.length > 0 && displaySummary && (
<>
<div className="mb-4">
<CardText

View File

@@ -317,6 +317,9 @@ export type CardProps = {
export type AccountStore = {
setAccounts: (accounts: Account[]) => void
accounts: Account[]
onInitialize: () => Promise<void>
getFirstAccount: () => Account | null
getFirstAccountName: () => string
}
export type ILoader = {

View File

@@ -4,12 +4,14 @@ import { useAccount } from 'wagmi'
import LogIn from '../../components/mollecules/LogIn/LogIn'
import useCookie from '../../hooks/useCookie'
import {useEffect, useState} from 'react'
import {useAuthStore} from '../../app/store/accountStore'
export const Auth = ({ children }: any) => {
const { getCookie, deleteCookie } = useCookie()
const { isConnected } = useAccount()
const { login, ready, authenticated, user } = usePrivy()
const token = getCookie('token')
const onInitialize = useAuthStore((state) => state.onInitialize)
const [isLoading, setIsLoading] = useState(true);
@@ -23,6 +25,13 @@ export const Auth = ({ children }: any) => {
}
}, [ready]);
// Initialize accounts when authenticated and token is available
useEffect(() => {
if (authenticated && token) {
onInitialize()
}
}, [authenticated, token, onInitialize]);
if (!ready || isLoading) {
return <div>Loading...</div>;
}

View File

@@ -5,6 +5,7 @@ import {Tabs} from '../../components/mollecules'
import BacktestPlayground from './backtestPlayground'
import BacktestScanner from './backtestScanner'
import BacktestUpload from './backtestUpload'
import BacktestGenetic from './backtestGenetic'
import type {TabsType} from '../../global/type.tsx'
// Tabs Array
@@ -24,6 +25,11 @@ const tabs: TabsType = [
index: 3,
label: 'Upload',
},
{
Component: BacktestGenetic,
index: 4,
label: 'Genetic',
},
]
const Backtest: React.FC = () => {

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,65 @@
declare module 'genetic-js' {
interface GeneticConfig {
size?: number
crossover?: number
mutation?: number
iterations?: number
fittestAlwaysSurvives?: boolean
webWorkers?: boolean
skip?: number
maxResults?: number
}
interface GeneticStats {
generation: number
fittest: any
fitness: number
average: number
worst: number
}
interface Genetic {
seed: () => any
fitness: (individual: any) => number | Promise<number>
mutate?: (individual: any) => any
crossover?: (mother: any, father: any) => [any, any]
select1?: (population: any[]) => any
select2?: (population: any[]) => [any, any]
generation?: (population: any[], generation: number, stats: GeneticStats) => boolean | Promise<boolean>
notification?: (population: any[], generation: number, stats: GeneticStats, isFinished: boolean) => void
optimize: any
evolve: () => Promise<any>
size: number
crossover: number
mutation: number
iterations: number
fittestAlwaysSurvives: boolean
}
interface GeneticStatic {
create: (config?: GeneticConfig) => Genetic
Optimize: {
Minimize: any
Maximize: any
}
Select1: {
Tournament2: any
Tournament3: any
Fittest: any
Random: any
RandomLinearRank: any
Sequential: any
}
Select2: {
Tournament2: any
Tournament3: any
Random: any
RandomLinearRank: any
Sequential: any
FittestRandom: any
}
}
const Genetic: GeneticStatic
export = Genetic
}