优宽量化交易平台内置集成了一些常用库。
优宽量化的TA
指标库优化了常用指标算法,支持JavaScript
、Python
、C++
语言的策略调用。
开源TA库代码、优宽量化交易平台API手册。
function main(){
// 鉴于测试代码,不使用商品期货策略一般架构,这里仅仅判断exchange.IO("status")函数,判断连接期货公司前置机成功后立即执行测试代码。股票证券无需使用exchange.IO("status")判断连接状态
while(true) {
if (exchange.IO("status")) {
exchange.SetContractType("rb888")
var records = exchange.GetRecords()
// K线需要有足够长度才可以计算指标数据
if (records && records.length > 100) {
break
}
}
Sleep(1000)
}
var r = exchange.GetRecords()
var macd = TA.MACD(r)
var atr = TA.ATR(r, 14)
// 打印出最后一组指标值
Log(macd[0][r.length-1], macd[1][r.length-1], macd[2][r.length-1])
Log(atr[atr.length-1])
}
# Python需要单独安装talib库
import talib
def main():
while True:
if exchange.IO("status"):
exchange.SetContractType("rb888")
records = exchange.GetRecords()
# records 为 None、空数组[] 判断均为假
if records and len(records) > 100:
break
Sleep(1000)
r = exchange.GetRecords()
macd = TA.MACD(r)
atr = TA.ATR(r, 14)
Log(macd[0][-1], macd[1][-1], macd[2][-1])
Log(atr[-1])
void main() {
while (true) {
if (exchange.IO("status") != 0) {
exchange.SetContractType("rb888");
auto records = exchange.GetRecords();
if (records.Valid && records.size() > 100) {
break;
}
}
Sleep(1000);
}
auto r = exchange.GetRecords();
auto macd = TA.MACD(r);
auto atr = TA.ATR(r, 14);
Log(macd[0][macd[0].size() - 1], macd[1][macd[1].size() - 1], macd[2][macd[2].size() - 1]);
Log(atr[atr.size() - 1]);
}
以下是CCI
指标调用范例代码,更多的talib指标函数可以查阅优宽量化交易平台API手册
function main() {
while(!exchange.IO("status")) {
Sleep(1000)
}
exchange.SetContractType("rb888")
var records = exchange.GetRecords()
var cci = talib.CCI(records, 14)
Log(cci)
}
import talib
def main():
while not exchange.IO("status"):
Sleep(1000)
exchange.SetContractType("rb888")
records = exchange.GetRecords()
# 14这个参数可以缺省
cci = talib.CCI(records.High, records.Low, records.Close, 14)
Log(cci)
void main() {
while(exchange.IO("status") == 0) {
Sleep(1000);
}
exchange.SetContractType("rb888");
auto records = exchange.GetRecords();
auto cci = talib.CCI(records, 14);
Log(cci);
}
function main() {
var x = -1.2
var a = Decimal.abs(x)
var b = new Decimal(x).abs()
Log(a.equals(b)) // true
var y = 2.2
var sum = Decimal.add(x, y)
Log(sum.equals(new Decimal(x).plus(y))) // true
}
function main() {
var sum = _.reduce([1, 2, 3], function(memo, num){return memo + num}, 0)
Log(sum)
}
function main() {
// 鉴于测试代码,不使用商品期货策略一般架构,这里仅仅判断exchange.IO("status")函数,判断连接期货公司前置机成功后立即执行测试代码。股票证券无需使用exchange.IO("status")判断连接状态
while (true) {
if (exchange.IO("status")) {
exchange.SetContractType("rb888")
var records = exchange.GetRecords()
// K线需要有足够长度才可以计算指标数据
if (records && records.length > 100) {
break
}
}
Sleep(1000)
}
var r = exchange.GetRecords()
// 打印所有指标数据,在优宽量化交易平台上,JavaScript语言的策略已经集成了talib库
Log(talib.MACD(r))
Log(talib.MACD(r, 12, 26, 9))
}
function main() {
// via. https://cdnjs.com/libraries
eval(HttpQuery("https://cdnjs.cloudflare.com/ajax/libs/mathjs/13.2.0/math.min.js"))
Log(math.round(math.e, 3)) // 2.718
Log(math.atan2(3, -3) / math.pi) // 0.75
Log(math.log(10000, 10)) // 4
Log(math.sqrt(-4)) // {"mathjs":"Complex","re":0,"im":2}
}
void main() {
json table = R"({"type": "table", "title": "持仓信息", "cols": ["列1", "列2"], "rows": [["abc", "def"], ["ABC", "support color #ff0000"]]})"_json;
LogStatus("`" + table.dump() + "`");
LogStatus("第一行消息\n`" + table.dump() + "`\n第三行消息");
json arr = R"([])"_json;
arr.push_back(table);
arr.push_back(table);
LogStatus("`" + arr.dump() + "`");
table = R"({
"type" : "table",
"title" : "持仓操作",
"cols" : ["列1", "列2", "Action"],
"rows" : [
["abc", "def", {"type": "button", "cmd": "coverAll", "name": "平仓"}]
]
})"_json;
LogStatus("`" + table.dump() + "`", "\n`" + R"({"type": "button", "cmd": "coverAll", "name": "平仓"})"_json.dump() + "`");
}