Type/to search
Built-in Functions
Global
Version
IsVirtual
GetOS
GetPid
GetMeta
Sleep
Unix
UnixNano
_D
GetCommand
GetLastError
SetErrorFilter
_N
_C
_Cross
JSON.parse
JSON.stringify
Encode
MD5
UUID
Log
Market
Trade
Account
Futures
Exchange
IO
Network
Storage
Threads
TA
Talib
OS
Structures
Built-in Variables and Constants
Conventions
Exchanges

A retry function used for fault-tolerant handling of interface calls.

_C(pfn)
_C(pfn, ...args)

Examples

  • Apply fault-tolerant handling to a function without parameters.

    javascript
    function main(){ var ticker = _C(exchange.GetTicker) // Change the retry interval of the _C() function to 2 seconds _CDelay(2000) var depth = _C(exchange.GetDepth) Log(ticker) Log(depth) }
    python
    def main(): ticker = _C(exchange.GetTicker) _CDelay(2000) depth = _C(exchange.GetDepth) Log(ticker) Log(depth)
    rust
    fn main() { let ticker = _C!(exchange.GetTicker(None)); // Change the retry interval of the _C!() macro to 2 seconds _CDelay(2000); let depth = _C!(exchange.GetDepth(None)); Log!(ticker); Log!(depth); }
  • Apply fault-tolerant handling to a function with parameters.

    javascript
    function main(){ var records = _C(exchange.GetRecords, PERIOD_D1) Log(records) }
    python
    def main(): records = _C(exchange.GetRecords, PERIOD_D1) Log(records)
    rust
    fn main() { let records = _C!(exchange.GetRecords(None, PERIOD_D1, None)); Log!(records); }
  • It can also be used to apply fault-tolerant handling to custom functions.

    javascript
    var test = function(a, b){ var time = new Date().getTime() / 1000 if(time % b == 3){ Log("Condition met!", "#FF0000") return true } Log("Retrying!", "#FF0000") return false } function main(){ var ret = _C(test, 1, 5) Log(ret) }
    python
    import time def test(a, b): ts = time.time() if ts % b == 3: Log("Condition met!", "#FF0000") return True Log("Retrying!", "#FF0000") return False def main(): ret = _C(test, 1, 5) Log(ret)
    rust
    fn test(a: i64, b: i64) -> Result<bool> { let time = Unix(); if time % b == 3 { Log!("Condition met!", "#FF0000"); return Ok(true); } Log!("Retrying!", "#FF0000"); Err(Error::Api("retry".to_string())) } fn main() { // In Rust, a custom function can use the _C! macro for fault tolerance as long as it returns a // Result type; it will retry when Err is returned let ret = _C!(test(1, 5)); Log!(ret); }

Returns

TypeDescription

any

The return value after the callback function is executed (never null, undefined or false).

Arguments

NameTypeRequiredDescription

pfn

function

Yes

The parameter pfn is a function reference, i.e. a callback function.

arg

string / number / bool / object / array / function / any

No

The parameters of the callback function. There can be multiple arg parameters. The type and number of the arg parameters are determined by the parameters of the callback function.

See Also

Remarks

The _C() function repeatedly calls the specified function until it returns successfully (when the function referenced by the parameter pfn returns null, undefined or false upon being called (None or False in Python), the call to pfn will be retried; other values such as 0 or an empty string are not retried and are returned directly as the result).

For example, _C(exchange.GetTicker). The default retry interval is 3 seconds, and you can call the _CDelay() function to set the retry interval.

For example, _CDelay(1000) means changing the retry interval of the _C() function to 1 second.

Fault-tolerant handling can be applied to the following functions (but is not limited to them):

  • exchange.GetTicker()
  • exchange.GetDepth()
  • exchange.GetTrades()
  • exchange.GetRecords()
  • exchange.GetAccount()
  • exchange.GetOrders()
  • exchange.GetOrder()
  • exchange.GetPositions()

All of the above functions can be called through the _C() function to achieve fault tolerance. The fault tolerance of the _C() function is not limited to the functions listed above. Please note that the parameter pfn is a function reference rather than a function call, i.e. it should be written as _C(exchange.GetTicker), not _C(exchange.GetTicker()).