在策略訊號中,經常用到箭頭來表達開倉和平倉(多、空)等資訊表徵圖。如果能在圖中具體展示出來,是一個很直觀的表達,有利於對擇時的更精準的理解和對比。
一、如何畫上下箭頭。
具體來說,就是arrow函數。
相關參數如下:
arrow(x, y, dx, dy, **kwargs),是指畫座標 從(x, y) 到 (x + dx, y + dy).的箭頭。
其中,常見的參數(舉例)有(以下參數填寫可以亂序):
(1)head_width=0.01,=>箭頭的寬度,注意:需要和X軸的座標要相匹配
(2)width=0.00015, =>箭頭下麵條狀的寬度,注意同上
(3)head_length=0.07, =>箭頭的長度,注意:和Y軸的座標要相匹配
(4)overhang=0.5,=>越小,箭頭更象實三角形,填充更飽滿;反之填充更少。
(5)head_starts_at_zero=”true”,
(6)facecolor=”red” =>箭頭的配色
(7)length_includes_head:=”true”; =>如何你不想尾巴很長,可以通過這個來控制一下。
(8)shape=”full”; 左偏(left)或右偏(right),或正中(full)
相應官方說明如下:
Constructor argumentswidth: float (default: 0.001) width of full arrow taillength_includes_head: [True | False] (default: False) True if head is to be counted in calculating the length.head_width: float or None (default: 3*width) total width of the full arrow headhead_length: float or None (default: 1.5 * head_width) length of arrow headshape: [‘full’, ‘left’, ‘right’] (default: ‘full’) draw the left-half, right-half, or full arrowoverhang: float (default: 0) fraction that the arrow is swept back (0 overhang means triangular shape). Can be negative or greater than one.head_starts_at_zero: [True | False] (default: False) if True, the head starts being drawn at coordinate 0 instead of ending at coordinate 0.
需要注意的是:這些參數需要和X和Y軸的座標體系相匹配,不是簡單的機械的設定。
二、例子1
using PyPlotx = [DateTime(2013,10,4):Dates.Millisecond(100):DateTime(2013,10,4,1);] # Generate time arrayx = map(Float64,x)/1000/60/60/24 # Convert time from milliseconds from day 0 to days from day 0y = sin(2*pi*collect(0:2*pi/length(x):2*pi-(2*pi/length(x))))p = plot_date(x,y,linestyle="-",marker="None",label="Test Plot") arrow(x[convert(Int64,floor(length(x)/2))], 0.4, 0.000, 0.1, head_width=0.001, width=0.00015, head_length=0.07, overhang=0.5, head_starts_at_zero="true", facecolor="red")
三、例子2,如果參數設定不當時
需要指出的是,同樣差不多的Arrow函數參數,可能會得到不同的效果。
using PyPlotx =1:1:20;y =sin(2x)plot(x,y)arrow(5, -0.2, 0.0, 0.5, head_width=0.001, width=0.00015, head_length=0.07, overhang=0.5, head_starts_at_zero="true", facecolor="red")
箭頭呢。 對上面的參數進行一些修改:
arrow(5, -0.2, 0.0, 0.5, head_width=1, width=0.3, head_length=0.1, overhang=0.5, head_starts_at_zero="true", facecolor="red")
得到下圖:
四、例子3
using PyPlotx =1:1:20;y =2*xplot(x,y)arrow(5, 10, 0.0, 2, head_width=2, width=0.5, head_length=2, overhang=0.5, head_starts_at_zero="true", facecolor="red")
五、例子4、得到一個更短的箭頭
x = [DateTime(2013,10,4):Dates.Millisecond(100):DateTime(2013,10,4,1);] # Generate time arrayx = map(Float64,x)/1000/60/60/24 # Convert time from milliseconds from day 0 to days from day 0y = sin(2*pi*collect(0:2*pi/length(x):2*pi-(2*pi/length(x))))p = plot_date(x,y,linestyle="-",marker="None",label="Test Plot") #clf;#close();arrow(x[convert(Int64,floor(length(x)/2))], 0.4, 0.000, 0.1, shape="full"; head_width=0.001, width=0.00015, length_includes_head="true", head_length=0.1, overhang=0.5, head_starts_at_zero="false", facecolor="red")
六、例子5,得到一個更飽滿的箭頭
arrow(x[convert(Int64,floor(length(x)/2))], 0.4, 0.000, 0.1, shape="full"; head_width=0.001, width=0.00015, length_includes_head="true", head_length=0.1, overhang=0.1, head_starts_at_zero="false", facecolor="red")
七、例子6:策略訊號中的一個具體的應用
抽其中一段代碼:
if mkTime == opTime dx =tradeData[j].Close * 0.005; if LS>0 # open Buy PyPlot.arrow(j,tradeData[j].Close *0.995,0,dx,head_width=5,facecolor="red",length_includes_head="true",head_length=2,overhang=0.1); annotate("Open Buy",xy=[j,tradeData[j].Close*0.995]) else PyPlot.arrow(j,tradeData[j].Close*1.005 ,0,-dx,head_width=5,facecolor="green",length_includes_head="true",head_length=2,overhang=0.1); annotate("Open Sell",xy=[j,tradeData[j].Close * 1.005]) end elseif mkTime == clTime dx =tradeData[j].Close * 0.005; if LS>0 PyPlot.arrow(j,tradeData[j].Close*0.995 ,0,dx,head_width=5,facecolor="red",length_includes_head="true",head_length=2,overhang=0.1); annotate("Close Buy",xy=[j,tradeData[j].Close*0.995]) else PyPlot.arrow(j,tradeData[j].Close * 1.005,0,-dx,head_width=5,facecolor="green",length_includes_head="true",head_length=2,overhang=0.1); annotate("Close Sell",xy=[j,tradeData[j].Close*1.005]) end end