# 1.如何获取所有股票信息
a=get_all_securities(types=["stock"])
a# 1.如何获取所有期货信息
a=get_all_securities(types=["futures"])
a
# 2.获取指数成分股
security=get_index_stocks("000300.XSHG")
security[:10] #取前10只股票作为股池
# 3.获取行情数据
price_1=attribute_history("000002.XSHE",5,unit="1d",fields=["open","close"])
price_1price_2=history(5,unit="1d",field="open",security_list=["000001.XSHE","000002.XSHE"])
price_2price_3=get_price("000001.XSHE",start_date="2023-01-01",end_date="2023-05-01",fields=["open"])
price_3 # price_3["open"].mean()
# 4.获取财务数据
q=query(valuation).filter(valuation.code=="000001.XSHE")
get_fundamentals(q)q=query(valuation.market_cap,valuation.pe_ratio).filter(valuation.code=="000001.XSHE") #获取单个数据字段
get_fundamentals(q)q=query(indicator).filter(indicator.code=="000001.XSHE")
get_fundamentals(q,date="2022-05-01")q=query(indicator).filter(indicator.code.in_(["000001.SXHE","000002.XSHE"]))
get_fundamentals(q,date="2022-05-01")
# 导入函数库
from jqdata import *
# 初始化函数,设定基准等等
def initialize(context):
# 1.设定沪深300作为基准
set_benchmark('000300.XSHG')
# 2.开启动态复权模式(真实价格)
set_option('use_real_price', True)
### 股票相关设定 ###
# 3.股票类每笔交易时的手续费是:买入时佣金万分之三,卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣5块钱
set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')
# 4.设定股票标的
g.security="000001.XSHE"
def handle_data(context, data):
get_current_data()[g.security].day.open
print(price)
# 导入函数库
from jqdata import *
# 初始化函数,设定基准等等
def initialize(context):
# 1.设定沪深300作为基准
set_benchmark('000300.XSHG')
# 2.开启动态复权模式(真实价格)
set_option('use_real_price', True)
### 股票相关设定 ###
# 3.股票类每笔交易时的手续费是:买入时佣金万分之三,卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣5块钱
set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')
# 4.设定股票标的
g.security="000001.XSHE"
def handle_data(context, data):
print(context.portfolio.positions[g.security].total_amount)
一个简单交易策略的实现:
# 导入函数库
from jqdata import *
# 初始化函数,设定基准等等
def initialize(context):
# 1.设定沪深300作为基准
set_benchmark('000300.XSHG')
# 2.开启动态复权模式(真实价格)
set_option('use_real_price', True)
### 股票相关设定 ###
# 3.股票类每笔交易时的手续费是:买入时佣金万分之三,卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣5块钱
set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')
# 4.设定股票标的,设置股票池
g.security=get_index_stocks("000300.XSHG")
def handle_data(context, data):
to_buy=[]
for stock in g.security:
p = get_current_data()[stock],last_price
amount = context.portfolio.positions[stock].total_amount
cost = context.portfolio.positions[stock].avg_cost
if p > 1.25*cost and amount!=0:
order_target(stock,0)
if p < 0.9*cost and amount!=0:
order_target(stock,0)
if p < 10 and amount == 0:
to_buy.append(stock)
cash=context.portfolio.available_cash
cash_per_stock=cash/len(to_buy)
for i in to_buy:
order_value(i,cash_per_stock)