Type/to search
Built-in Functions
Global
Version
Sleep
IsVirtual
Mail
Mail_Go
SetErrorFilter
GetPid
GetLastError
GetCommand
GetMeta
Dial
HttpQuery
HttpQuery_Go
Encode
UnixNano
Unix
GetOS
MD5
DBExec
UUID
EventLoop
__Serve
_G
_D
_N
_C
_Cross
JSON.parse
JSON.stringify
SetChannelData
GetChannelData
Log
Market
Trade
Account
Futures
NetSettings
Threads
threading
Thread
getThread
mainThread
currentThread
Lock
Condition
Event
Dict
pending
Thread
ThreadLock
ThreadEvent
ThreadCondition
ThreadDict
Web3
TA
Talib
talib.CDL2CROWS
talib.CDL3BLACKCROWS
talib.CDL3INSIDE
talib.CDL3LINESTRIKE
talib.CDL3OUTSIDE
talib.CDL3STARSINSOUTH
talib.CDL3WHITESOLDIERS
talib.CDLABANDONEDBABY
talib.CDLADVANCEBLOCK
talib.CDLBELTHOLD
talib.CDLBREAKAWAY
talib.CDLCLOSINGMARUBOZU
talib.CDLCONCEALBABYSWALL
talib.CDLCOUNTERATTACK
talib.CDLDARKCLOUDCOVER
talib.CDLDOJI
talib.CDLDOJISTAR
talib.CDLDRAGONFLYDOJI
talib.CDLENGULFING
talib.CDLEVENINGDOJISTAR
talib.CDLEVENINGSTAR
talib.CDLGAPSIDESIDEWHITE
talib.CDLGRAVESTONEDOJI
talib.CDLHAMMER
talib.CDLHANGINGMAN
talib.CDLHARAMI
talib.CDLHARAMICROSS
talib.CDLHIGHWAVE
talib.CDLHIKKAKE
talib.CDLHIKKAKEMOD
talib.CDLHOMINGPIGEON
talib.CDLIDENTICAL3CROWS
talib.CDLINNECK
talib.CDLINVERTEDHAMMER
talib.CDLKICKING
talib.CDLKICKINGBYLENGTH
talib.CDLLADDERBOTTOM
talib.CDLLONGLEGGEDDOJI
talib.CDLLONGLINE
talib.CDLMARUBOZU
talib.CDLMATCHINGLOW
talib.CDLMATHOLD
talib.CDLMORNINGDOJISTAR
talib.CDLMORNINGSTAR
talib.CDLONNECK
talib.CDLPIERCING
talib.CDLRICKSHAWMAN
talib.CDLRISEFALL3METHODS
talib.CDLSEPARATINGLINES
talib.CDLSHOOTINGSTAR
talib.CDLSHORTLINE
talib.CDLSPINNINGTOP
talib.CDLSTALLEDPATTERN
talib.CDLSTICKSANDWICH
talib.CDLTAKURI
talib.CDLTASUKIGAP
talib.CDLTHRUSTING
talib.CDLTRISTAR
talib.CDLUNIQUE3RIVER
talib.CDLUPSIDEGAP2CROWS
talib.CDLXSIDEGAP3METHODS
talib.AD
talib.ADOSC
talib.OBV
talib.ACOS
talib.ASIN
talib.ATAN
talib.CEIL
talib.COS
talib.COSH
talib.EXP
talib.FLOOR
talib.LN
talib.LOG10
talib.SIN
talib.SINH
talib.SQRT
talib.TAN
talib.TANH
talib.MAX
talib.MAXINDEX
talib.MIN
talib.MININDEX
talib.MINMAX
talib.MINMAXINDEX
talib.SUM
talib.HT_DCPERIOD
talib.HT_DCPHASE
talib.HT_PHASOR
talib.HT_SINE
talib.HT_TRENDMODE
talib.ATR
talib.NATR
talib.TRANGE
talib.BBANDS
talib.DEMA
talib.EMA
talib.HT_TRENDLINE
talib.KAMA
talib.MA
talib.MAMA
talib.MIDPOINT
talib.MIDPRICE
talib.SAR
talib.SAREXT
talib.SMA
talib.T3
talib.TEMA
talib.TRIMA
talib.WMA
talib.LINEARREG
talib.LINEARREG_ANGLE
talib.LINEARREG_INTERCEPT
talib.LINEARREG_SLOPE
talib.STDDEV
talib.TSF
talib.VAR
talib.ADX
talib.ADXR
talib.APO
talib.AROON
talib.AROONOSC
talib.BOP
talib.CCI
talib.CMO
talib.DX
talib.MACD
talib.MACDEXT
talib.MACDFIX
talib.MFI
talib.MINUS_DI
talib.MINUS_DM
talib.MOM
talib.PLUS_DI
talib.PLUS_DM
talib.PPO
talib.ROC
talib.ROCP
talib.ROCR
talib.ROCR100
talib.RSI
talib.STOCH
talib.STOCHF
talib.STOCHRSI
talib.TRIX
talib.ULTOSC
talib.WILLR
talib.AVGPRICE
talib.MEDPRICE
talib.TYPPRICE
talib.WCLPRICE
OS
Structures
Built-in Variables

The os library provides a complete file system operation interface for the FMZ Quant Trading Platform.

Open a file in the specified mode.

open(filename)
open(filename, mode)

Examples

Create a file, write data, then read it.

javascript
function main() { let fileHandle = os.open("output.txt", "w+") if (!fileHandle) { Log("Failed to open file") return } let bytesWritten = fileHandle.write("Hello FMZ!") Log("Bytes written:", bytesWritten) // Bytes written: 10 fileHandle.seek(0, 0) let fileContent = fileHandle.read() Log("File content read:", fileContent) // File content read: Hello FMZ! fileHandle.close() }

Returns

TypeDescription

File object

The open() function returns a File object for file operations.

Arguments

NameTypeRequiredDescription

filename

string

Yes

File name. The parameter filename is a path containing the file name. Since both the docker and backtesting system support os operations, please note that in non-backtesting environments, file operations are restricted to the files folder at the same level as the live trading database file in the docker directory, therefore absolute paths are not supported for operating any files. This note about the parameter filename will not be repeated in subsequent sections.

mode

string

No

Specify the file opening mode.

See Also

Remarks

File opening modes:

  • r : read (read-only, file must exist)

  • w : write (write-only, creates file if it doesn't exist, clears content if it exists)

  • a : append (append write, creates file if it doesn't exist, writes to the end if it exists)

  • + : Adding + after r/w/a means both read and write are allowed (e.g., "r+", "w+", "a+")

  • b : binary (binary mode, commonly used in Windows to distinguish text/binary files, e.g., "rb", "wb")

Files are opened/created in the files folder under the live trading database file directory (xxx.db3, where xxx is the live trading Id).

Read the entire file content at once.

fgets(filename)

Examples

Read the content of a configuration file.

javascript
function main() { // 先创建、写入文件 // let fileHandle = os.open("config.json", "w+") // if (!fileHandle) { // Log("Failed to open file") // return // } // let objJson = {"name": "tom", "age": 18} // fileHandle.write(JSON.stringify(objJson)) // fileHandle.close() let content = os.fgets("config.json") Log("Config content:", content) // Config content: {"name":"tom","age":18} }

Returns

TypeDescription

string

Returns the complete content of the file.

Arguments

NameTypeRequiredDescription

filename

string

Yes

File path, including the filename to be read.

See Also

Remarks

Suitable for quick reading of small files, loading the entire file content into memory at once.

If the file does not exist, the fgets() function will throw an error: InternalError: failed to open file: openat config.json: no such file or directory at main.

Write content to a file.

fputs(filename, content)
fputs(filename, content, append)

Examples

Save strategy configuration to file.

javascript
function main() { let config = '{"strategy": "MA", "period": 20}' let bytesWritten = os.fputs("strategy_config.json", config) Log("Bytes written:", bytesWritten) // Bytes written: 32 Log(`os.fgets("strategy_config.json"):`, os.fgets("strategy_config.json")) // os.fgets("strategy_config.json"): {"strategy": "MA", "period": 20} // 追加日志信息 let logInfo = "\n[" + new Date().toISOString() + "] Config saved" os.fputs("strategy_config.json", logInfo, true) Log(`os.fgets("strategy_config.json"):`, os.fgets("strategy_config.json")) // os.fgets("strategy_config.json"): {"strategy": "MA", "period": 20} [2025-09-08T07:20:30.563Z] Config saved }

Returns

TypeDescription

number

Returns the actual number of bytes written.

Arguments

NameTypeRequiredDescription

filename

string

Yes

File path.

content

string

Yes

Content to write.

append

bool

No

Whether to write in append mode, defaults to false (overwrite mode).

See Also

Remarks

Convenient file writing method, overwrites file content by default. Set append to true to append to the end of the file.

Memory-mapped file, returns the binary data of the file.

mmap(filename)

Examples

Map the binary data of a file.

javascript
function ab2str(buf) { let arr = new Uint8Array(buf) return String.fromCharCode.apply(null, arr) } function main() { let buffer = os.mmap("strategyConfig/testData.txt") Log("File size in bytes:", buffer.byteLength) let arr = Array.from(new Uint8Array(buffer)) Log("arr:", arr) // arr: [72,101,108,108,111,32,70,77,90,33] Log("ab2str(buffer):", ab2str(buffer)) // ab2str(buffer): Hello FMZ! }

Returns

TypeDescription

ArrayBuffer

Returns the binary data of the file content.

Arguments

NameTypeRequiredDescription

filename

string

Yes

File path.

See Also

Remarks

Suitable for efficient reading and processing of large files, maps the file into memory and returns it as an ArrayBuffer.

Get the root directory path for file operations.

getRootDir()

Examples

Get and display the root directory path.

javascript
function main() { let rootDir = os.getRootDir() Log("Root directory:", rootDir) }

Returns

TypeDescription

string

Returns the path of the root directory.

See Also

List files and subdirectories in the specified directory.

listFiles()
listFiles(pattern)

Examples

List matched files and directories.

javascript
function main() { // 列出所有json文件 let result = os.listFiles("*.json") Log("result:", result) // 列出所有内容 let allFiles = os.listFiles() Log("allFiles:", allFiles) }

Returns

TypeDescription

ListFilesResult object

Returns an object containing files and dirs arrays.

- files: Array of matched file names.

- dirs: Array of subdirectory names in the current directory.

Arguments

NameTypeRequiredDescription

pattern

string

No

Optional matching pattern, supports wildcards (e.g., .txt, data_.json), can specify directory path.

See Also

Remarks

When pattern parameter is not specified, lists all files and subdirectories in the current directory (../files).

Check if the specified file or directory exists.

exists(filename)

Examples

Check if a file or path exists.

javascript
function main() { Log(`os.exists("./strategyConfig"):`, os.exists("./strategyConfig")) // os.exists("./strategyConfig"): true Log(`os.exists("./strategyConfig/testData.txt"):`, os.exists("./strategyConfig/testData.txt")) // os.exists("./strategyConfig/testData.txt"): true // Log(`os.exists("/strategyConfig"):`, os.exists("/strategyConfig")) // InternalError: invalid filename: path traversal or absolute path not allowed at main Log(`os.exists("test_1.txt"):`, os.exists("test_1.txt")) // os.exists("test_1.txt"): true Log(`os.exists("test_2.txt"):`, os.exists("test_2.txt")) // os.exists("test_2.txt"): false }

Returns

TypeDescription

bool

Returns true if the file or directory exists, otherwise returns false.

Arguments

NameTypeRequiredDescription

filename

string

Yes

The file or directory path to check.

See Also

Delete the specified file.

remove(filename)

Examples

Example of deleting a file.

javascript
function main() { let tempFile = "test_1.txt" if (os.exists(tempFile)) { let success = os.remove(tempFile) Log("Temp file deleted:", success) } }

Returns

TypeDescription

bool

Returns true on successful deletion, false on failure.

Arguments

NameTypeRequiredDescription

filename

string

Yes

The file path to delete.

See Also

Remarks

This function is only for deleting files, not for deleting directories. To delete directories, please use the rmdir() function.

Create a directory.

mkdir(dirname)

Examples

Create a data storage directory, create a file and write data.

javascript
function main() { let success = os.mkdir("data/backtest/results") Log("Directory created:", success) if (success) { os.fputs("data/backtest/results/summary.txt", "Backtest completed") } }

Returns

TypeDescription

bool

Returns true if creation is successful, otherwise returns false.

Arguments

NameTypeRequiredDescription

dirname

string

Yes

The directory path to create.

See Also

Remarks

Supports recursive creation of multi-level directories. If parent directories do not exist, they will be created automatically.

Remove a directory and all its contents.

rmdir(dirname)

Examples

Remove a directory and all its contents.

javascript
function main() { let tempDir = "data" if (os.exists(tempDir)) { let success = os.rmdir(tempDir) Log("directory removed:", success) } }

Returns

TypeDescription

bool

Returns true on successful deletion, false on failure.

Arguments

NameTypeRequiredDescription

dirname

string

Yes

The directory path to be removed.

See Also

Remarks

This operation will permanently delete the directory and all its contents, please use with caution.

Rename a file or move a file.

rename(oldName, newName)

Examples

Rename a file and move it to another directory.

javascript
function main() { let oldFileName = "output.txt" let newFileName = "outputFiles/" + new Date().getTime() + "output.txt" // let retRename = os.rename(oldFileName, newFileName) // Log("retRename:", retRename) // InternalError: failed to rename file: renameat output.txt outputFiles/1757322073139output.txt: no such file or directory at main os.mkdir("outputFiles") let retRename = os.rename(oldFileName, newFileName) Log("retRename:", retRename) // retRename: true }

Returns

TypeDescription

bool

Returns true when the operation succeeds, otherwise returns false.

Arguments

NameTypeRequiredDescription

oldName

string

Yes

The name or path of the original file.

newName

string

Yes

The name or path of the new file.

See Also

Remarks

This function can be used to rename a file or move a file to another directory. If the directory of the new path does not exist, the operation will fail.

Get detailed statistics information of a file.

stat(filename)

Examples

Get file information and check file size.

javascript
function main() { if (os.exists("strategyConfig/testData.txt")) { let stat = os.stat("strategyConfig/testData.txt") // stat: {"size":10,"mode":420,"mtime":1757312981796,"atime":1757312981796,"ctime":1757312981796} Log("stat:", stat) } }

Returns

TypeDescription

FileStat object

Returns an object containing file statistics information.

Arguments

NameTypeRequiredDescription

filename

string

Yes

File path.

See Also

Remarks

The returned FileStat object contains the following fields:

  • size: File size.

  • mode: File permissions.

  • mtime: Last modification time.

  • atime: Last access time.

  • ctime: Creation time.

Exit the program.

exit()
exit(status)

Examples

Exit program after checking conditions.

javascript
function main() { if (!os.exists("required_config.json")) { Log("Required configuration file not found!") os.exit(1) // Abnormal exit } Log("Configuration found, continuing...") // Normal strategy logic... }

Returns

TypeDescription

never

This function does not return a value, the program will terminate execution directly.

Arguments

NameTypeRequiredDescription

status

number

No

Optional exit status code, default value is 0.

See Also

Remarks

Immediately terminates program execution. Status code 0 indicates normal exit, non-zero values indicate abnormal exit (will be displayed as error in live trading).