print(df.iloc[-1]).T

这句话是在查询pandas数据框(df)中最后一行的所有列的数据。其中,df.iloc[-1]表示最后一行,.T表示将数据转置,即将行和列调换位置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import tushare as ts import datetime # 设置 token,用于认证 ts.set_token('530fbc2b682d65696dbeec010a893f70d6953fbb6842151003c3e12f') # 初始化 tushare pro = ts.pro_api() df = pro.daily(fields = 'ts_code,trade_date,open,close',start_date='20180701', end_date='20180718') def get_stock_poll(df): stock_pool=[] for code in df['ts_code'].unique(): temp_df = df[df['ts_code'] == code ] for i in range(len(temp_df)-3): if (temp_df.iloc[i + 3]['close'] > temp_df.iloc[i + 2]['close']) and \ (temp_df.iloc[i + 2]['close'] > temp_df.iloc[i + 2]['open']) and \ (temp_df.iloc[i + 1]['close'] > temp_df.iloc[i + 1]['open']) and \ (temp_df.iloc[i]['close'] > temp_df.iloc[i]['open'])and \ (temp_df.iloc[i + 3]['close'] > temp_df.iloc[i + 2]['close'])and \ (temp_df.iloc[i + 2]['close'] > temp_df.iloc[i + 1]['close']) and\ (temp_df.iloc[i + 1]['close'] > temp_df.iloc[i]['close']): stock_pool.append(code) break return stock_pool def buy_stock(stock_pool,df): buy_list = [] for code in stock_pool: temp_df = df[df['ts_code']==code] for i in range(len(temp_df-2)): if(temp_df.iloc[i + 2]['close'] < temp_df.iloc[i + 2]['open']) and \ (temp_df.iloc[i + 1]['close'] < temp_df.iloc[i + 1]['open']) and \ (temp_df.iloc[i]['close'] < temp_df.iloc[i]['open'])and \ (temp_df.iloc[i + 2]['close'] < temp_df.iloc[i + 1]['close'])and \ (temp_df.iloc[i + 1]['close'] < temp_df.iloc[i]['close']): buy_list.append(code) break return buy_list def sell_stock(buy_stock,df): sell_list = [] for stock in buy_list: buy_date=dateime.datetime.strptime(stock[1],"%Y-%m-%d") current_date = datetime.datetime.today() days_held = (current_date - buy_date).days if days_held >=3: sell_list.append(stock[0]) print("sell list:",sell_list)要求上述代码在聚宽上运行
06-01
这段代码需要进行一些修改才能在聚宽上运行,具体修改如下: 1. 将导入的 tushare 改为聚宽提供的接口,即 import jqdatasdk as jq 2. 将 tushare 的 token 设置语句删除,因为在聚宽中不需要 token 认证 3. 将 tushare 的 pro_api() 改为聚宽的 auth(),并传入聚宽账号和密码进行认证 4. 将 tushare 的 daily() 改为聚宽的 get_price(),并传入聚宽的股票代码格式 '000001.XSHE',以及开始和结束时间 5. 将 datetime 改为聚宽提供的 jqdata.datetime,即 import jqdatasdk as jq 和 from jqdatasdk import *,并将代码中的 datetime.datetime 改为 jqdata.datetime。 修改后的代码如下: ``` import jqdatasdk as jq from jqdatasdk import * auth('聚宽账号', '聚宽密码') df = get_price('000001.XSHE', start_date='2018-07-01', end_date='2018-07-18', fields=['open', 'close']) def get_stock_poll(df): stock_pool=[] for code in df.index.unique(): temp_df = df[df.index == code ] for i in range(len(temp_df)-3): if (temp_df.iloc[i + 3]['close'] > temp_df.iloc[i + 2]['close']) and \ (temp_df.iloc[i + 2]['close'] > temp_df.iloc[i + 2]['open']) and \ (temp_df.iloc[i + 1]['close'] > temp_df.iloc[i + 1]['open']) and \ (temp_df.iloc[i]['close'] > temp_df.iloc[i]['open'])and \ (temp_df.iloc[i + 3]['close'] > temp_df.iloc[i + 2]['close'])and \ (temp_df.iloc[i + 2]['close'] > temp_df.iloc[i + 1]['close']) and\ (temp_df.iloc[i + 1]['close'] > temp_df.iloc[i]['close']): stock_pool.append(code) break return stock_pool def buy_stock(stock_pool,df): buy_list = [] for code in stock_pool: temp_df = df[df.index==code] for i in range(len(temp_df)-2): if(temp_df.iloc[i + 2]['close'] < temp_df.iloc[i + 2]['open']) and \ (temp_df.iloc[i + 1]['close'] < temp_df.iloc[i + 1]['open']) and \ (temp_df.iloc[i]['close'] < temp_df.iloc[i]['open'])and \ (temp_df.iloc[i + 2]['close'] < temp_df.iloc[i + 1]['close'])and \ (temp_df.iloc[i + 1]['close'] < temp_df.iloc[i]['close']): buy_list.append(code) break return buy_list def sell_stock(buy_list,df): sell_list = [] for stock in buy_list: buy_date=jqdata.datetime.strptime(str(stock[1]),"%Y-%m-%d") current_date = jqdata.datetime.today() days_held = (current_date - buy_date).days if days_held >=3: sell_list.append(stock[0]) print("sell list:",sell_list) ``` 注意,由于聚宽不支持 tushare 的 ts_code,因此需要将股票代码格式改为聚宽支持的 '000001.XSHE' 格式。另外需要注意代码中的大小写,聚宽中的 jqdatasdk、jqdata.datetime 都是小写。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值