在策略信号中,经常用到箭头来表达开仓和平仓(多、空)等信息图标。如果能在图中具体展示出来,是一个很直观的表达,有利于对择时的更精准的理解和对比。
一、如何画上下箭头?
具体来说,就是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 arguments
width: float (default: 0.001)
width of full arrow tail
length_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 head
head_length: float or None (default: 1.5 * head_width)
length of arrow head
shape: [‘full’, ‘left’, ‘right’] (default: ‘full’)
draw the left-half, right-half, or full arrow
overhang: 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 PyPlot
x = [DateTime(2013,10,4):Dates.Millisecond(100):DateTime(2013,10,4,1);] # Generate time array
x = map(Float64,x)/1000/60/60/24 # Convert time from milliseconds from day 0 to days from day 0
y = 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 PyPlot
x =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 PyPlot
x =1:1:20;
y =2*x
plot(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 array
x = map(Float64,x)/1000/60/60/24 # Convert time from milliseconds from day 0 to days from day 0
y = 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