[Barley and Xiaomi learn quantification] Use pywencai to access the Tonghuashuncai interface to realize intelligent stock selection


I am the strongest AI

Xiaomi came to Damai with a look of joy on his face: "Brother Damai, I heard that you can use AI to trade stocks now. It doesn't matter if you can't understand the indicator K-line or something like that?" Damai asked noncommittally: "It's that stupid best friend again
. Do you think you can do it? "
"Tch? Just say it's ok. If it's ok, I can also go stock trading. I'll keep the money anyway." Xiaomi thought that he had found a way to make money. What would happen this time? Have to try it too.
"There is such an easy way to make money in the world. If you can use AI to trade stocks, who makes whose money?" Damai hit the nail on the head and made Xiaomi speechless.
Seeing Xiaomi's disappointed look, Damai continued: "Actually, that's using AI to select stocks. AI just filters quantitative data based on natural language, and it's not that magical. Don't think too much. You still have to respect the market when trading. The rule is, if it falls too much, it will rise. If it rises too much, it will naturally fall. Ups and downs are changes, and funds are the driving force behind the scenes!"
Xiaomi curled her lips and said: "I'm here to teach people a lesson again, I'll ignore you!" "
Actually, I can do that too. Help you, I am the most powerful AI. Not only can I understand what you say, but I can also know what you want? Hahaha..." "What do I want?
"
"It costs a lot of money!" Damai said proudly. Said, but the result was an angry look from Xiaomi.
"You're the only one who talks too much! Teach me how to make money by trading stocks tomorrow. If you don't make money, I'll confiscate your phone!"
Damai thought to himself, I still want to find someone to teach me how to make money. Yes, I will also study AI.

Preface

The iwencai I used before has been unable to be accessed recently. After many twists and turns, I found that the pywencai library can replace its functions. The original post can no longer be used, so I created this post and recommend that everyone use this library to access Flush Money.

1. What is pywencai?

pywencai is a third-party library that obtains Flush Wencai data through python. Here's how to install it.

1. Prepare node environment

This package needs to call js code, you need to install the js running environment first, download and install node.js

https://nodejs.org/en/

Execute the command node -v to view the node version

(base) C:\Users\Administrator>node -v
v18.16.0

2. Install pywencai library

pip install pywencai -U

In view of the frequent changes in the Tonghuashun Wencai interface, to ensure normal use, please install the latest version. When you encounter problems that cannot be used, please upgrade the library version first. Currently available is version 0.12.0.

3. git address:

https://gitcode.net/mirrors/zsrl/pywencai
https://github.com/zsrl/pywencai

2. Usage steps

1 . Demo

import pywencai
res = pywencai.get(query='连涨3天')
print(res)

For more detailed instructions on use, please see the official address. It is already very clear and will not be introduced in detail here.

2. Advanced call

import os
import pywencai

def xg_wencai(query,perpage=200,ret='df'):
    # ret为list则只反馈6位股票代码,方便交易
    df = pywencai.get(query=query, sort_key='股票代码', sort_order='asc', perpage=perpage, )
    # print('pywencai',df)
    if df is None:
        return None
    elif df.empty:
        return None
    else:
        df['code'] = df['股票代码'].str[:6]
        if ret == 'list':
            return df['股票代码'].tolist()
        else:
            return df.round(3)

if __name__ == '__main__':
	# 修改query 即可
    query = '连续3天介于1%-4%,长期横盘'
    query = '2个月内波幅在5%以内,今日成交量突然放大' #,今日成交量突然放大
    df = xg_wencai(query,perpage=200,ret='df')
    print(df)
    #定义导出的路径,并定义好文件名
    filename = os.path.dirname(__file__)+'\\py问财选股_'+query+'.xlsx'
    #导出文件
    df.to_excel(filename,sheet_name = "汇总",index = False,na_rep = 0,inf_rep = 0)


Summarize

It is convenient to realize stock selection by asking money, but for intelligent trading, this only represents another step closer. AI can help us select stocks faster, but it cannot replace our trading and risk control. Investment has always been associated with risk. How to control losses and let profits run is the simplest and most difficult knowledge on the investment road!

In addition, this library is not an official library, and maintenance and updates must be based on official conditions. Please use it and update it.

Guess you like

Origin blog.csdn.net/popboy29/article/details/132401576