程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

dried food! Write the least Python code and draw the most coquettish K-line!

編輯:Python

This is Xing buidi   68  I want to quantify the sharing of the small lecture hall

author | Xing can't 、 Assistant - Fama

There was a live broadcast that showed you a tool we use internally ——  Timing policy viewer .

Timing policy viewer

On the viewer interface , Not only can we clearly see K Line graph 、 Average And other technical indicators , It can also show the timing strategy Trading signals Of course 、 Selling point .

You can see every transaction on the right side of the page :

Click on “ Show in the figure ”,K The online page will focus on the transaction , Show its Opening point 、 Closing point 、 Yield Etc .

This tool is mainly to make it easier for everyone to read the transaction by transaction of timing strategy , Help develop 、 Optimize new strategies . At first, it was only used internally , Later, it was opened to the students of the strategy sharing meeting .

This piece “ Artifact ” The main part of it is K Display of lines and related indicators , It looks so complicated , Will it be difficult ?

The answer is : In fact, just A dozen lines Python Code , Even Xiaobai, who has no programming foundation, can draw K Line graph .

This article follows the following four steps , Explain how to make personalized market analysis artifact on tall :

01 Draw ordinary K Line graph  

02 Add trading volume  

03 Join the moving average  

04 Add trading signals

If you have any questions after reading , You can scan the QR code below or add me WeChat xbx971, Communicate with me .

01

  Draw ordinary K Line graph

Here I default that everyone has installed Python And the environment , And know how to run the code .

If not , Please refer to this article I wrote before :

The simplest 、 The most detailed Python and Pandas Installation tutorial

First let's open up Anaconda, Click... Inside Spyder.

Here I have prepared three data for you , Namely Bitcoin day K Line , The currency 4 Hours K Line , as well as sh000688 kechuang 50 Index day K Line

K Line data file

sh000688 Data presentation

besides , And our 4 A program file :

If you need these codes and data , You can scan the QR code below or add me WeChat xbx971, Can be directly sent to you .

With The currency 4 Hours K Line For example , hit open “1_ draw K Line .py” This procedure :

1_ draw K Line .py

Among them the first 1、2 The code of is to import the third-party library we installed in advance , The first 5 To the first 7 Lines are the data needed to import this drawing .

Before selection 7 Line code , Right click , Click again “Run Cell”, Run these lines of code :

After selecting the code , Right click

On the right side of the interface Variable Can be seen in , The data has been imported into the program :

We double click here “df” Variable , You can see the imported K Line data :

Next, we use the imported data to draw K Line graph :

Drawing part code

The code of the drawing part is to set K Linear Color and style .

If we want to K Line color and domestic A The stock market is the same Red goes up and green goes down , Then just set the parameter to :

up=’red’  down=’green’

The first 13 Line code edge It means setting up K The color of the line border , The default is black ,edge=’inherit’ It means to keep K The color of the line border is the same as K The color of the line entity is consistent .

Run all the code , You can draw the recent period of bitcoin K Line graph :

Want to change the name of the ordinate in the diagram , Just change the code number 18 In line code ylabel Parameters can be :

Now we can draw K The line chart , How should that be in ordinary K Add trading volume to the line chart ?

If you are interested in the above data and code , You can scan the QR code below or add me WeChat xbx971, They can be sent to you for free .

02

Add trading volume

open “2_ Add trading volume .py” This new program :

2_ Add trading volume .py

Before this program 10 Line code and “1_ draw K Line .py” The procedure is completely consistent , It is also used to import third-party libraries and read data .

The difference is :

First , In the 14 Line added a volume Parameters ,volume=’inherit’ It means that Color of trading volume histogram Set to red up and green down , And K The lines are consistent .

secondly , The first 19 Two parameters are added to the line mapping code ,volume=True Add trading volume to the graph .ylabel_lower=’volume’ It means that the vertical coordinate of the trading volume chart is named “volume”.

After running the program , You can see that the trading volume has been added K Line graph :

The histogram below shows the trading volume ( Red goes up and green goes down )

Want to change the name of the volume ordinate , Just change the code number 20 In line code ylabel_lower Parameters can be :

Next, let's talk about how to K Add technical indicators to the line diagram .

03

Add technical indicators

Let's take the moving average as an example , Demonstration in K Add technical indicators to the line diagram .

The average has been calculated in the data I provided to you , Just add the moving average index to the drawing function according to the code .

Moving average data field

open “3_ Join the moving average .py” Program :

3_ Join the moving average .py

Before the program 16 The line code is the same as the above two programs .

The difference is :

The first 19 Add a new variable to the row add_plot, The assignment is 10 ma ’ma_10’ and 50 ma ’ma_50’, And will add_plot Add variables to the 21 Yes mpf.plot() In the method .

After writing , Run the program , You can get K Line graph :

You can see ,K Two new moving averages have been added to the line . Blue Yes. 50 ma , Orange Yes. 10 ma .

You should feel it , The process is actually very simple , We just need to add corresponding parameters to the drawing function like building blocks .

If you are interested in the above data and code , You can scan the QR code below or add me WeChat xbx971, They can be sent to you for free .

Finally, let's have a look at , How to signal the opening and closing of trading strategies , Add to K In line .

04

Add trading signals

The data I provide you already contains trading signals , Just add the trading signal to the drawing function according to the code .

Trading signals in the data

open “4_ Join the trading point .py” Program :

4_ Join the trading point .py

Before this program 16 Line code and “3_ Join the moving average .py” The procedure is exactly the same .

The difference is :

The first 19-22 Line code ,add_plot Variables add our trading signals . Trading signals Divided into three :

1. signal_long:  Long opening signal

2. signal_short:  Short opening signal

3. signal_0:  The signal of closing positions

marker Parameter is used to set the shape of the trading signal icon ,marker=’^’ Indicates an upward arrow , marker=’v’ Indicates a downward arrow , marker=’o’ It's a circle .

color Parameters can be used to control the color ,color=’g’ It means green (green), ‘y’ It means yellow (yellow), ‘b’ It means blue (blue), You can set different colors according to your preferences .

Run the program , Make the final Market analyzer

The red upward arrow in the figure indicates Open more warehouses The location of , The green downward arrow indicates Open an empty warehouse The location of , The yellow circle indicates close a position The location of .

05

summary

Now we have successfully mapped The currency 4 Hours K Line , Joined the volume , Average , And also put our own Trading signals Add to K In line .

You can see , Programming is not difficult . It's like building blocks , Just add it up bit by bit . And the procedure of the fourth step only needs 20 A few lines Python Code , Most of them are comments and spaces .

It is strongly recommended that interested students can try to run the code by themselves , The experience will be more different . Scan the QR code below or add me WeChat xbx971, I can send you the data and code needed for drawing .

06

Postscript

At the end of the article , Share a little with you Experience of quantifying investment .

Many people ask me how Xiaobai started Learn to quantify investment , There's nothing to The book list recommends .

My advice is Never read a book directly .

You're looking for Ben Programming Book reading , Then knock it out “Hello World” It's over ; You're looking for Ben mathematics Book reading , Then I fell asleep when I saw the formula on page 7 .

A better way to learn is to do practical projects , stay practice Learning quantitative strategies .

Research Report is a good quantitative practice project .

A research paper is a strategy , The authors are highly educated securities analysts with an annual salary of millions , All you have to do is Read the Strategy Research Report , And implement it in code .

During this period, I will learn nothing , Where will not order where , Holding Problem solving mentality To learn , Yield twice the result with half the effort .

Familiar with 300 tang poems , You can sing even if you can't write a poem .

So where can we Obtain the research report Well ?

You can scan the QR code below or add mine WeChat xbx971, I have different categories here Tens of thousands of research reports , Will Real time updates .

I can distinguish the difficulty , selected Then send it to you .

Add me WeChat xbx971 after , It's fine too Exchange quantitative investment related issues , I'm busy , The reply was slow , But I will reply to everything I see .

Have a good chat , We had a good chat , A lot of quantitative data 、 All the information is OK it's for you Of .

You can also turn over me Circle of friends The content of , quite a lot Quantify dry goods . Some content that will not be publicly developed , Will say in the circle of friends .

Contact the author


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved