Utils Package
This package provides a series of general utility functions, including file operations, mathematical calculations, string processing, and other functionalities.
File Operations
CopyDir
Copy an entire directory and its contents to a target location.
Parameters:
src string- Source directory pathdst string- Target directory path
Returns:
error- Error information during operation
Copy
Copy a single file to a target location.
Parameters:
srcFile string- Source file pathdstFile string- Target file path
Returns:
error- Error information during operation
Exists
Check if a file or directory exists.
Parameters:
filePath string- Path of the file or directory to check
Returns:
bool- true indicates exists, false indicates does not exist
EnsureDir
Ensure a directory exists, create it if it doesn't.
Parameters:
dir string- Directory pathperm os.FileMode- Directory permissions
Returns:
error- Error information during operation
FindSubPath
Recursively search for a target file or directory in a specified directory.
Parameters:
parDir string- Parent directory pathtgtName string- Target name to findmaxDepth int- Maximum recursion depth
Returns:
string- Found patherror- Error information during search
Mathematical Calculations
DecPow
Calculate the power of a decimal number.
Parameters:
x decimal.Decimal- Basey decimal.Decimal- Exponent
Returns:
decimal.Decimal- Calculation result
DecArithMean
Calculate the arithmetic mean of a group of decimal numbers.
Parameters:
values []decimal.Decimal- Array of values
Returns:
decimal.Decimal- Mean valueerror- Error information during calculation
DecStdDev
Calculate the population standard deviation of a group of decimal numbers.
Parameters:
values []decimal.Decimal- Array of values
Returns:
decimal.Decimal- Standard deviationerror- Error information during calculation
SharpeRatio
Calculate the Sharpe ratio, using default 252 trading days and annualization.
Parameters:
moReturns []float64- Array of returnsriskFree float64- Risk-free rate
Returns:
float64- Sharpe ratioerror- Error information during calculation
SortinoRatio
Calculate the Sortino ratio, using default 252 trading days and annualization.
Parameters:
moReturns []float64- Array of returnsriskFree float64- Risk-free rate
Returns:
float64- Sortino ratioerror- Error information during calculation
String Processing
SnakeToCamel
Convert snake case to camel case.
Parameters:
input string- Input string in snake case
Returns:
string- Converted string in camel case
PadCenter
Center-pad a string to a specified width.
Parameters:
s string- Original stringwidth int- Target widthpadText string- Padding character
Returns:
string- Padded string
RandomStr
Generate a random string of specified length.
Parameters:
length int- String length
Returns:
string- Generated random string
FormatWithMap
Format a string using values from a map.
Parameters:
text string- String containing placeholdersargs map[string]interface{}- Map of replacement values
Returns:
string- Formatted string
PrintErr
Format error information.
Parameters:
e error- Error object
Returns:
string- Formatted error message
GroupByPairQuotes
Group trading pairs by quote currency.
Format: [key]:pairs... converts to:
[key]
Quote: Base1 Base2 ...Parameters:
items map[string][]string- Trading pairs mapping
Returns:
string- Formatted grouped string
CountDigit
Count the number of digit characters in a string.
Parameters:
text string- Input string
Returns:
int- Number of digit characters
SplitSolid
Split string and ignore empty strings.
Parameters:
text string- String to splitsep string- Separator
Returns:
[]string- Array of non-empty strings after splitting
Slice and Map Operations
SplitSolid
String splitting, ignoring empty strings in the result.
Parameters:
text string- String to splitsep string- Separator
Returns:
[]string- Array of strings after splitting
KeysOfMap
Get all keys of a map.
Parameters:
m M- Input map, supports generics
Returns:
[]K- Array of keys
ValsOfMap
Get all values of a map.
Parameters:
m M- Input map, supports generics
Returns:
[]V- Array of values
CutMap
Extract a subset of specified keys from a map.
Parameters:
m M- Input map, supports genericskeys ...K- List of keys to extract
Returns:
M- New map containing specified keys
UnionArr
Merge multiple arrays and remove duplicates.
Parameters:
arrs ...[]T- List of arrays to merge, supports generics
Returns:
[]T- Merged array without duplicates
ReverseArr
Reverse array elements in place.
Parameters:
s []T- Array to reverse, supports generics
ConvertArr
Convert an array of one type to another type.
Parameters:
arr []T1- Source arraydoMap func(T1) T2- Conversion function
Returns:
[]T2- Converted array
ArrToMap
Convert an array to a map, multiple elements may map to the same key.
Parameters:
arr []T2- Source arraydoMap func(T2) T1- Key mapping function
Returns:
map[T1][]T2- Converted map with array values
RemoveFromArr
Remove specified elements from an array.
Parameters:
arr []T- Source arrayit T- Element to removenum int- Number of elements to remove, negative means remove all
Returns:
[]T- New array after removing elements
UniqueItems
Get unique elements and duplicate elements from an array.
Parameters:
arr []T- Input array, supports generics
Returns:
[]T- Array of unique elements[]T- Array of duplicate elements
DeepCopyMap
Deep copy a map.
Parameters:
dst map[string]interface{}- Destination mapsrc map[string]interface{}- Source map
MapToStr
Convert a map to string representation.
Parameters:
m map[string]float64- Map to convert
Returns:
string- Converted stringint- Total length of numeric parts
Network Operations
DoHttp
Execute HTTP request and return result.
Parameters:
client *http.Client- HTTP clientreq *http.Request- HTTP request
Returns:
*banexg.HttpRes- HTTP response result
Network Communication
NewBanServer
Create a new BanServer instance for TCP network communication.
Parameters:
addr string- Server listening address (e.g., "127.0.0.1:6789")name string- Server name
Returns:
*ServerIO- Server instance
NewClientIO
Create a new BanClient instance to connect to BanServer.
Parameters:
addr string- Server address (e.g., "127.0.0.1:6789")
Returns:
*ClientIO- Client instance*errs.Error- Error information
GetServerData
Get data from BanServer or BanClient.
Parameters:
key string- Data key name
Returns:
string- Retrieved data value*errs.Error- Error information
SetServerData
Set data to BanServer or BanClient.
Parameters:
args *KeyValExpire- Data structure containing key, value, and expiration timeKey string- Key nameVal string- ValueExpireSecs int- Expiration time (seconds)
Returns:
*errs.Error- Error information
GetNetLock
Get distributed lock.
Parameters:
key string- Lock key nametimeout int- Lock acquisition timeout (seconds)
Returns:
int32- Lock value (used for unlocking)*errs.Error- Error information
DelNetLock
Delete distributed lock.
Parameters:
key string- Lock key namelockVal int32- Lock value (obtained from GetNetLock)
Returns:
*errs.Error- Error information
Other Tools
MD5
Calculate MD5 hash of data.
Parameters:
data []byte- Input data
Returns:
string- Hexadecimal string of MD5 hash
GetSystemLanguage
Get system language setting.
Returns:
string- System language code
Correlation Calculations
CalcCorrMat
Calculate correlation matrix between multiple data series.
Parameters:
arrLen int- Data lengthdataArr [][]float64- Two-dimensional data arrayuseChgRate bool- Whether to use change rate for calculation
Returns:
*mat.SymDense- Correlation matrix[]float64- Average correlation for each serieserror- Error information
GenCorrImg
Generate heatmap of correlation matrix.
Parameters:
m *mat.SymDense- Correlation matrixtitle string- Chart titlenames []string- List of series namesfontName string- Font namefontSize float64- Font size
Returns:
[]byte- PNG image dataerror- Error information
CalcEnvsCorr
Calculate correlation between multiple candlestick environments.
Parameters:
envs []*ta.BarEnv- List of indicator environmentshisNum int- Number of historical data points
Returns:
*mat.SymDense- Correlation matrix[]float64- Average correlation for each environmenterror- Error information
Performance Metrics Calculation
CalcExpectancy
Calculate profit expectancy and risk-reward ratio.
Parameters:
profits []float64- Array of profits
Returns:
float64- Expected profitfloat64- Risk-reward ratio
CalcMaxDrawDown
Calculate maximum drawdown.
Parameters:
profits []float64- Array of profitsinitBalance float64- Initial balance
Returns:
float64- Maximum drawdown amountfloat64- Maximum drawdown percentageint- Drawdown start positionint- Drawdown end positionfloat64- Balance at drawdown startfloat64- Balance at drawdown end
AutoCorrPenalty
Calculate autocorrelation penalty factor.
Parameters:
returns []float64- Array of returns
Returns:
float64- Penalty factor
KMeansVals
Perform K-means clustering on numerical series.
Parameters:
vals []float64- Array of valuesnum int- Number of clusters
Returns:
*ClusterRes- Clustering result
StdDevVolatility
Calculate standard deviation volatility.
Parameters:
data []float64- Data arrayrate float64- Decay rate
Returns:
float64- Volatility
NearScore
Calculate score for proximity to center point.
Parameters:
x float64- Current valuemid float64- Center valuerate float64- Decay rate
Returns:
float64- Score (between 0-1)
File Reading and Writing
ReadCSV
Read CSV file contents.
Parameters:
path string- CSV file path
Returns:
[][]string- Two-dimensional array of CSV contents*errs.Error- Error information
ReadXlsx
Read Excel file contents.
Parameters:
path string- Excel file pathsheet string- Worksheet name (uses first worksheet if empty)
Returns:
[][]string- Two-dimensional array of Excel contents*errs.Error- Error information
ReadTextFile
Read text file contents.
Parameters:
path string- File path
Returns:
string- File contents*errs.Error- Error information
ReadLastNLines
Read last N lines of a file.
Parameters:
filePath string- File pathlineCount int- Number of lines to read
Returns:
[]string- Last N lines contenterror- Error information
WriteCsvFile
Write to CSV file.
Parameters:
path string- File pathrows [][]string- Data to writecompress bool- Whether to compress
Returns:
*errs.Error- Error information
WriteFile
Write to regular file.
Parameters:
path string- File pathdata []byte- Data to write
Returns:
*errs.Error- Error information
KlineToStr
Convert candlestick data to string array.
Parameters:
klines []*banexg.Kline- candlestick data arrayloc *time.Location- Timezone information
Returns:
[][]string- Converted string array
GetFontData
Get font file data.
Parameters:
name string- Font name (uses arial.ttf if empty)
Returns:
[]byte- Font file dataerror- Error information
GetOpenFont
Get OpenType font.
Parameters:
name string- Font name
Returns:
*opentype.Font- OpenType font objecterror- Error information
IsTextContent
Check if data is text content.
Parameters:
data []byte- Data to check
Returns:
bool- true indicates text content, false indicates possible binary content
Utility Functions
ParallelRun
Execute tasks in parallel.
Parameters:
items []T- List of items to process, supports genericsconcurNum int- Maximum concurrencyhandle func(int, T) *errs.Error- Handler function, receives index and item
Returns:
*errs.Error- Error during execution
IsDocker
Check if currently running in Docker container.
Returns:
bool- Whether in Docker container
OpenBrowser
Open system default browser to visit specified URL.
Parameters:
url string- URL to visit
Returns:
error- Error during execution
OpenBrowserDelay
Open browser to visit URL after a delay.
Parameters:
url string- URL to visitdelayMS int- Delay in milliseconds
IntToBytes
Convert uint32 to byte array.
Parameters:
n uint32- Number to convert
Returns:
[]byte- Converted byte arrayerror- Error during conversion
MD5
Calculate MD5 hash of data.
Parameters:
data []byte- Data to hash
Returns:
string- Hexadecimal string of MD5 hash
ReadInput
Read user input from standard input.
Parameters:
tips []string- List of prompt messages
Returns:
string- User input stringerror- Error during reading
ReadConfirm
Read user confirmation from standard input.
Parameters:
tips []string- List of prompt messagesok string- Confirmation stringfail string- Cancel stringexitAny bool- Whether to allow any input to exit
Returns:
bool- Whether user confirmed
NewPrgBar
Create new progress bar.
Parameters:
totalNum int- Total progress counttitle string- Progress bar title
Returns:
*PrgBar- Progress bar object
NewStagedPrg
Create multi-stage progress bar.
Parameters:
tasks []string- List of task names, in execution order, no duplicatesweights []float64- Task weights, >0, internally normalized
Returns:
*StagedPrg- Multi-stage progress bar object
System-Related Functions
GetSystemLanguage
Get current system language code.
Returns:
string- ISO 639-1 language code (optionally with ISO 3166-1 country code), such as:- en-US: English (United States)
- zh-CN: Chinese (Simplified)
- zh-TW: Chinese (Traditional)
- ja-JP: Japanese
- ko-KR: Korean
- fr-FR: French
- de-DE: German
- es-ES: Spanish etc.