class DemoCallback: def __init__(self): self.api = TqApi() self.quote = self.api.get_quote("SHFE.cu1805") def on_data_update(self): if self.quote.get("last_price", 0) > 1000: self.api.insert_order(symbol="SHFE.cu1805", direction="BUY", offset="OPEN", volume=1, limit_price=30000) def run(self): self.api.run(self.on_data_update)
class DemoMa: def __init__(self): self.api = TqApi() self.tm = TaskManager(self.api) def task_main(self): print("start") symbol = "SHFE.cu1805" kline_serial_5s = self.api.get_kline_serial(symbol, 5) kline_serial_1m = self.api.get_kline_serial(symbol, 60) while True: yield { "KLINE_DATA_UPDATED": lambda: self.api.is_changing(kline_serial_1m) or self.api. is_changing(kline_serial_5s), } # 计算最近3根5秒线均价 average_price_15s = (kline_serial_5s[-1]["close"] + kline_serial_5s[-2]["close"] + kline_serial_5s[-3]["close"]) / 3 # 计算最近30根1分钟线均价 average_price_30m = sum(kline_serial_1m.close[-30:]) / 30 # 如果条件符合 print("average_price_15s", average_price_15s, "average_price_30m", average_price_30m) if average_price_15s > average_price_30m: self.api.insert_order(symbol=symbol, direction="BUY", offset="OPEN", volume=1, limit_price=5000) print("finish") def run(self): self.tm.start_task(self.task_main()) self.api.run()
''' 如果当前价格大于10秒K线的MA15则开多仓 如果小于则平仓 ''' api = TqApi("SIM") # 获得 m1901 10秒K线的引用 klines = api.get_kline_serial("DCE.m1901", 10) # 判断开仓条件 while True: api.wait_update() if api.is_changing(klines): ma = sum(klines.close[-15:])/15 print("最新价", klines.close[-1], "MA", ma) if klines.close[-1] > ma: print("最新价大于MA: 市价开仓") api.insert_order(symbol="DCE.m1901", direction="BUY", offset="OPEN", volume=5) break # 判断平仓条件 while True: api.wait_update() if api.is_changing(klines): ma = sum(klines.close[-15:])/15 print("最新价", klines.close[-1], "MA", ma) if klines.close[-1] < ma: print("最新价小于MA: 市价平仓") api.insert_order(symbol="DCE.m1901", direction="SELL", offset="CLOSE", volume=5) break # 关闭api,释放相应资源 api.close()
#!/usr/bin/env python # -*- coding: utf-8 -*- from tqsdk.api import TqApi api = TqApi("SIM") # 开仓两手并等待完成 order = api.insert_order(symbol="SHFE.rb1901", direction="BUY", offset="OPEN", limit_price=4310, volume=2) while order["status"] != "FINISHED": api.wait_update() print("已开仓") # 平今两手并等待完成 order = api.insert_order(symbol="SHFE.rb1901", direction="SELL", offset="CLOSETODAY", limit_price=3925, volume=2) while order["status"] != "FINISHED": api.wait_update() print("已平今") # 关闭api,释放相应资源 api.close()
#!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = 'chengzhi' from tqsdk.api import TqApi api = TqApi("SIM") # 获得 cu1812 的持仓引用,当持仓有变化时 position 中的字段会对应更新 position = api.get_position("SHFE.cu1812") # 获得资金账户引用,当账户有变化时 account 中的字段会对应更新 account = api.get_account() # 下单并返回委托单的引用,当该委托单有变化时 order 中的字段会对应更新 order = api.insert_order(symbol="SHFE.cu1812", direction="BUY", offset="OPEN", volume=5) while True: api.wait_update() if api.is_changing(order, ["status", "volume_orign", "volume_left"]): print("单状态: %s, 已成交: %d 手" % (order["status"], order["volume_orign"] - order["volume_left"])) if api.is_changing(position, "volume_long_today"): print("今多头: %d 手" % (position["volume_long_today"])) if api.is_changing(account, "available"): print("可用资金: %.2f" % (account["available"]))