julia + plots + Gr 画股票k线图的方式

2020-09-03  本文已影响0人  昵称违法

python的不讲,只讲julia的玩法。

镇楼之图

image.png

第一种:plots 的 OHLC

直接上酸菜:

using Plots
gr()
Plots.GRBackend()

function draw_kline()
    n = 20
    hgt = rand(n) .+ 1
    bot = randn(n)
    openpct = rand(n)
    closepct = rand(n)
    ohlc = OHLC[(openpct[i] * hgt[i] + bot[i], bot[i] + hgt[i], bot[i], closepct[i] * hgt[i] + bot[i]) for i = 1:n] 
    ohlc |> typeof |> display
    ohlc |> display
    plot(ohlc,color = :green,label = "USA k_line")
end
draw_kline()

输出结果:这里用来查看 OHLC是什么鬼东西,大家自己看

Array{OHLC,1}
20-element Array{OHLC,1}:
 OHLC{Float64}(-0.2860714530968429, 0.2778268185293127, -1.1548390591785003, -0.27938420783147255)
 OHLC{Float64}(-0.1469282428612313, 0.1292761159889546, -1.1847360586304283, -0.9745213026881819)
 OHLC{Float64}(0.09380938339709366, 1.3323875577167963, -0.3192314912384773, -0.1321196523590998)
 OHLC{Float64}(2.0289407368673125, 3.2633800530294215, 1.4115324091609525, 1.4856713279588942)
 OHLC{Float64}(2.296949467248577, 3.5858217012862843, 1.8184195682268869, 3.039430491395562)
 OHLC{Float64}(-0.5561795725547414, -0.4855713061725839, -1.629151181061784, -0.7080441114473401)
 OHLC{Float64}(-1.0462574059230236, -0.37307210959988746, -1.5306511021918765, -0.42713847266024185)
 OHLC{Float64}(0.578581260918771, 1.1704146902944517, 0.07141299744979077, 0.23357926195279954)
 OHLC{Float64}(-0.6924380102536091, 0.47903025613035755, -0.7147905708401457, 0.3319803941583116)
 OHLC{Float64}(-0.20754264194079708, 0.5944667788496125, -0.9726586516266145, -0.6919027782576321)
 OHLC{Float64}(-0.24183131261734686, 1.2840289966800298, -0.5770484866860711, 0.8787842403514207)
 OHLC{Float64}(1.3916247374929143, 1.873973072407769, -0.10544520352762353, 0.08168845877068158)
 OHLC{Float64}(2.2027677827936105, 2.227047234651104, 0.7759702485152141, 1.5059749431487242)
 OHLC{Float64}(0.17161324482163157, 1.3357439311432069, 0.15833270699381438, 0.9297969047584426)
 OHLC{Float64}(0.47363673573446385, 0.5139173749161207, -0.6863920090804431, 0.47221365123653103)
 OHLC{Float64}(-1.3567597211135705, 0.09046705183013537, -1.5689022371679988, -0.717708115516972)
 OHLC{Float64}(0.22655372327620457, 1.4523683377886059, -0.23116580493970187, 1.2851925219593876)
 OHLC{Float64}(-0.001580497544137971, 1.7998731486566089, -0.0865665104266058, -0.06865038586014081)
 OHLC{Float64}(3.123170129826918, 3.2051385359756965, 1.4314426951785364, 2.667108241130934)
 OHLC{Float64}(-0.027436393919141433, 0.35144714757289974, -0.7197098140274829, -0.5733571496076991)
效果图 (看着不是那么顺眼,有什么办法呢,自己撸?!!!)
美版k线的效果

撸就撸,第二种,自己撸了

为了找到画竖线的方法,我查遍了Plots的官方手册,没找到vline怎么画不落地的竖线。
晚上吃完饭突发奇想,自己试了一段代码,发现就是我要的效果。

纯手工打个草稿,画几根日式k线,大家自己去写代码

function draw_line()  
    x = [1,1]  #[x1,x2]
    y = [3.5,5.5]  #[y1,y2]
    plot(x,y,w = 2,size=[200,400],color = :red)
    
    x = [1,1]  #[x1,x2]
    y = [4,5]  #[y1,y2]
    plot!(x,y,w = 15,size=[200,400],color = :red)
    
    x = [2,2]
    y = [4.5,8]
    plot!(x,y,w = 2,color = :green)       
   
    x = [2,2]
    y = [5,7]
    plot!(x,y,w = 15,color = :green)    
   
    x = [3,3]
    y = [6,7]
    plot!(x,y,w = 15,color = :red)
    
    x = [4,4]
    y = [7,8]
    plot!(x,y,w = 15,color = :red)   
   
end
draw_line()

效果图,是不是顺眼了一些,要撸你们去撸,我去锻炼身体了

中式蜡烛图 image.png

四、改进后的k线效果图

image.png image.png

最后,附上核心代码,大家自己去优化吧

4.1 使用的k线数据格式

image.png

4.2 关键部分的代码

function draw_line(df)    
    height = 200  #设置figure size
    width = 900
    
    length = size(df)[1]
    line_width = width / length * 0.7 #蜡烛图实体部分 的 宽度设置   
    
    #所有k线逐根处理
    for (idx,r) in zip(1:size(df)[1],eachrow(df))
        open,high,low,close = r.open,r.high,r.low,r.close
        
        x = [idx,idx]    #x轴刻度值
        y1 = [low,high]  #最低价到最高价 连线【瘦线】
        y2 = open !== close ? [open,close] : [open + 0.5,close - 0.5] #开盘价-收盘价 连线【胖线】,十字星线或是一字板线,为横线,宽度为1,长度为line_width
# 0.5这个值有问题,需要改成一个相对【1】,而不是绝对的【1】
        color = open >= close ? :green : :red #收盘阴阳的颜色
        
        #画蜡烛图实体部分
        if idx ==1
            plot(x,y1,w = 1,size=[width,height],color = color,label = "")
        elseif idx > 1
            plot!(x,y1,w = 1,size=[width,height],color = color,label = "")
        end    

        #画蜡烛图上下影线
        plot!(x,y2,w = line_width,color = color,label = "")
    end 
    
    #画均线
    plot!(df.ma5,label = "ma5")
    plot!(df.ma10,label = "ma10")
    plot!(df.ma20,label = "ma20")
    plot!(df.ma60,label = "ma60")
end

function test()
    kind = "股票"
    stock = "600352"
    kline_directory = "D:/juliaWorkSpace/A股统计策略/数据"  #k线位置
    df = read_kline(kind,stock,kline_directory)
    df |> display
    
    df.ma5 = ta.MA(df.close,5)
    df.ma10 = ta.MA(df.close,10)
    df.ma20 = ta.MA(df.close,20)
    df.ma60 = ta.MA(df.close,60)    
    
    df = @linq df |> where( Date("2018-01-01") .< :day .< Date("2019-12-01"))    
    draw_line(df)      
    println("ok")   
    plot!()
end

test()
上一篇下一篇

猜你喜欢

热点阅读