当前位置: 首页 > news >正文

QMT学习课程Day1

我们先从交易的最基础,如何进行下单,最为简答的下单,帮助大家建立自信心。

首先导入相关函数:

#encoding:gbk
import pandas as pd
import numpy as np
import datetime
import pandas as pd
import numpy as np
import talib
import time 
import math
"""
示例说明:双均线实盘策略,通过计算快慢双均线,在金叉时买入,死叉时做卖出
"""class a():pass
A = a() #创建空的类的实例 用来保存委托状态 
#ContextInfo对象在盘中每次handlebar调用前都会被深拷贝, 如果调用handlebar的分笔不是k线最后分笔 ContextInfo会被回退到深拷贝的内容 所以ContextInfo不能用来记录快速交易的信号def init(C):C.etf_pool = ['518880.SH', #黄金ETF(大宗商品)'513100.SH', #纳指100(海外资产)'159915.SZ', #创业板100(成长股,科技股,中小盘)'510180.SH', #上证180(价值股,蓝筹股,中大盘)]A.acct= '39130129'#账号为模型交易界面选择账号A.acct_type= 'STOCK'#账号类型为模型交易界面选择账号C.account= '39130129'#账号为模型交易界面选择账号C.acctount_type= 'STOCK'#账号类型为模型交易界面选择账号A.amount = 10000 #单笔买入金额 触发买入信号后买入指定金额A.waiting_list = [] #未查到委托列表 存在未查到委托情况暂停后续报单 防止超单A.buy_code = 23 if A.acct_type == 'STOCK' else 33 #买卖代码 区分股票 与 两融账号A.sell_code = 24 if A.acct_type == 'STOCK' else 34#C.run_time("run_buy_func","15nSecond","2024-07-25 14:45:00")C.run_time("run_buy_func","1nDay","2024-07-25 14:47:00")#指定时间交易C.run_time("reverse_repurchase_of_treasury_bonds_1","1nDay","2024-07-25 14:57:00")C.run_time("trader_info","3nSecond","2024-07-25 13:20:00")

代码解析:

C.run_time("run_buy_func","15nSecond","2024-07-25 14:45:00")

效果图:

可以让买入命令在指定时间交易

C.run_time("run_buy_func","1nDay","2024-07-25 14:47:00")#指定时间交易

可以让买入命令每过15秒运行

分析买入函数:

def run_buy_func(C):print("开始交易")#跳过历史k线if not C.is_last_bar():returnnow = datetime.datetime.now()now_time = now.strftime('%H%M%S')#跳过非交易时间if now_time < '093000' or now_time > "150000":returnaccount = get_trade_detail_data(A.acct, A.acct_type, 'account')if len(account)==0:print(f'账号{A.acct} 未登录 请检查')returnaccount = account[0]available_cash = int(account.m_dAvailable)#如果有未查到委托 查询委托if A.waiting_list:found_list = []orders = get_trade_detail_data(A.acct, A.acct_type, 'order')for order in orders:if order.m_strRemark in A.waiting_list:found_list.append(order.m_strRemark)A.waiting_list = [i for i in A.waiting_list if i not in found_list]if A.waiting_list:print(f"当前有未查到委托 {A.waiting_list} 暂停后续报单")returnholdings = get_trade_detail_data(A.acct, A.acct_type, 'position')holdings = {i.m_strInstrumentID + '.' + i.m_strExchangeID : i.m_nCanUseVolume for i in holdings}#获取行情数据vol = 100for i in C.etf_pool:passorder(A.buy_code, 1101, A.acct, i, 14, -1, vol, '双均线实盘', 1 ,'' , C)#print(msg)A.waiting_list.append('')

卖出函数解析:

def run_sell_func(C):#跳过历史k线if not C.is_last_bar():returnnow = datetime.datetime.now()now_time = now.strftime('%H%M%S')#跳过非交易时间if now_time < '093000' or now_time > "150000":returnaccount = get_trade_detail_data(A.acct, A.acct_type, 'account')if len(account)==0:print(f'账号{A.acct} 未登录 请检查')returnaccount = account[0]available_cash = int(account.m_dAvailable)#如果有未查到委托 查询委托if A.waiting_list:found_list = []orders = get_trade_detail_data(A.acct, A.acct_type, 'order')for order in orders:if order.m_strRemark in A.waiting_list:found_list.append(order.m_strRemark)A.waiting_list = [i for i in A.waiting_list if i not in found_list]if A.waiting_list:print(f"当前有未查到委托 {A.waiting_list} 暂停后续报单")#returnholdings = get_trade_detail_data(A.acct, A.acct_type, 'position')holdings = {i.m_strInstrumentID + '.' + i.m_strExchangeID : i.m_nCanUseVolume for i in holdings}#获取行情数据#vol = 100for i in C.etf_pool:passorder(A.sell_code, 1101, A.acct, i, 14, -1, holdings.get(i,0), '双均线实盘', 1 ,'' , C)#print(msg)A.waiting_list.append('')

总结:

通过本文的学习,相信你已经掌握如何在QMT学会使用买入卖出函数以及如何定时周期执行。希望本文可以帮助你!

http://www.xdnf.cn/news/116029.html

相关文章:

  • Web3钱包开发功能部署设计
  • 大语言模型时代,单细胞注释也需要集思广益(mLLMCelltype)
  • 论文笔记(七十九)STOMP: Stochastic Trajectory Optimization for Motion Planning
  • 【基础】Node.js 介绍、安装及npm 和 npx功能了解
  • MySQL 事务(详细版)
  • 【一览表】病理图像处理流程
  • leetcode 2799. 统计完全子数组的数目 中等
  • 立马耀:通过阿里云 Serverless Spark 和 Milvus 构建高效向量检索系统,驱动个性化推荐业务
  • Vue实战(08)解决 Vue 项目中路径别名 `@` 在 IDE 中报错无法识别的问题
  • 如何调用大语言模型的API?
  • C#中实现JSON解析器
  • 精益数据分析(19/126):走出数据误区,拥抱创业愿景
  • 快速上手GO的net/http包,个人学习笔记
  • 深入理解MVP架构:让UI层与业务逻辑完美分离的设计模式
  • 学习ros过程中常用指令
  • 【数据可视化-30】Netflix电影和电视节目数据集可视化分析
  • vue3实现v-directive;vue3实现v-指令;v-directive不触发
  • 用 Python 实现基于 Open CASCADE 的 CAD 绘图工具
  • 深入浅出JavaScript常见设计模式:从原理到实战(1)
  • 【Hive入门】Hive查询语言(DQL)完全指南:从基础查询到高级分析
  • Redis学习
  • 今日CSS学习浮动->定位
  • Vue3 ref与props
  • 进入救援模式(物理服务器)
  • SAP计划在2025年推出400个人工智能用例
  • 【信息系统项目管理师】高分论文:论进度管理和成本管理(智慧城管平台项目)
  • ShenNiusModularity项目源码学习(21:ShenNius.Admin.Mvc项目分析-6)
  • CentOS 7 磁盘分区详细教程
  • 学习MySQL的第十一天
  • 秒出PPT推出更强版本,AI PPT工具进入新纪元!