automatictrading's People
automatictrading's Issues
VBA实时行情代码
Public Function GetFontColor(ByVal price As Single) As Variant
If price > 0 Then
fontColor = RGB(255, 0, 0) '上涨
ElseIf price < 0 Then
fontColor = QBColor(2) '下跌
Else
fontColor = vbBlack
End If
GetFontColor = fontColor
End Function
Sub GetPriceDetail(ByVal sheet As Worksheet) '详细版,(名称,价格,涨幅,振幅,最高价,最低价,成交额,更新时间)
Dim rowCount As Integer
Dim url As String
Dim sTemp As String
rowCount = sheet.Range("A65535").End(xlUp).Row '获取行数
maxCountPer = 30 ' 每30行一读取
num = Int((rowCount - 1) / maxCountPer)
For kk = 0 To num
url = "http://hq.sinajs.cn/list="
For jj = 1 To maxCountPer
ii = kk * maxCountPer + jj + 1
If ii <= rowCount Then
code = sheet.Range("A" & ii).Text
If Len(code) < 6 Then
code = "unknow"
End If
If ii = 2 Then '从第二行开始
url = url & code
Else
url = url & "," & code
End If
End If
Next jj
'获取新浪股票行情数据,放入sTemp变量
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", url, False
.Send
sTemp = .responseText
End With
splits = Split(sTemp, ";")
For jj = 1 To maxCountPer
ii = kk * maxCountPer + jj + 1
If ii <= rowCount Then
mystr = splits(jj - 1)
ss = InStr(mystr, ",")
If ss > 1 Then
startindex = InStr(1, mystr, """")
endindex = InStrRev(mystr, """")
substr = Mid(mystr, startindex + 1, endindex - 1)
valuearray = Split(substr, ",") '共有32个数据 ,包括了股票名称,价格等信息
begin = Asc("B")
J = 0
sheet.Range(Chr(begin + J) & ii).Value = valuearray(0) '名称
J = J + 1
If valuearray(3) = 0 Then
sheet.Range(Chr(begin + J) & ii).Value = valuearray(2)
Else
sheet.Range(Chr(begin + J) & ii).Value = valuearray(3) '当前价
End If
'sheet.Range(Chr(begin + J) & ii).Font.Color = GetFontColor(valuearray(3) - valuearray(2))
J = J + 1
If CDbl(valuearray(3)) = 0 Then '涨幅
sheet.Range(Chr(begin + J) & ii).Value = "停牌"
Else
sheet.Range(Chr(begin + J) & ii).Value = Format(valuearray(3) / valuearray(2) - 1, "0.00%")
End If
sheet.Range(Chr(begin + J) & ii).Font.Color = GetFontColor(valuearray(3) - valuearray(2))
J = J + 1
sheet.Range(Chr(begin + J) & ii).Value = Format((valuearray(4) - valuearray(5)) / valuearray(2), "0.00%") '振幅
J = J + 1
sheet.Range(Chr(begin + J) & ii).Value = valuearray(4) '最高
sheet.Range(Chr(begin + J) & ii).Font.Color = GetFontColor(valuearray(4) - valuearray(2))
J = J + 1
sheet.Range(Chr(begin + J) & ii).Value = valuearray(5) '最低
sheet.Range(Chr(begin + J) & ii).Font.Color = GetFontColor(valuearray(5) - valuearray(2))
J = J + 1
amount = valuearray(9) / 10000 '成交额(万元)
sheet.Range(Chr(begin + J) & ii).Value = Int(amount)
J = J + 1
sheet.Range(Chr(begin + J) & ii).Value = valuearray(31) '日期 时间
End If
End If
Next jj
Next kk
End Sub
'sheet -- 作用的表单
'codeCol -- 股票代码所在列,如A,B...
'begin -- 数据开始写入的列,
Sub GetPriceConcise(ByVal sheet As Worksheet, codeCol As String, beginCol As String) '简洁版,(名称,价格,涨幅) - 支持港股实时行情,代码如hk02727,hkHSI
Dim rowCount As Integer
Dim url As String
Dim sTemp As String
Dim begin As Integer
begin = Asc(beginCol)
With Application
.Calculation = xlCalculationManual
.MaxChange = 0.001
End With
rowCount = sheet.Range("A65535").End(xlUp).Row '获取行数
maxCountPer = 30 ' 每30行一读取
num = Int((rowCount - 1) / maxCountPer)
For kk = 0 To num
url = "http://qt.gtimg.cn/q=" '腾讯行情数据接口
For jj = 1 To maxCountPer
ii = kk * maxCountPer + jj + 1
If ii <= rowCount Then
code = LCase(Trim(sheet.Range(codeCol & ii).Text))
If Len(code) < 5 Then
code = "unknow"
End If
If (Right(code, 3) = "hsi") Then
code = Left(code, 2) & "HSI"
End If
If (InStr(1, code, "hk") >= 1 And code <> "hkHSI") Then
code = "r_" & code
Else
code = "s_" & code
End If
If ii = 2 Then '从第二行开始
url = url & code
Else
url = url & "," & code
End If
End If
Next jj
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", url, False
.Send
sTemp = .responseText
End With
splits = Split(sTemp, ";")
For jj = 1 To maxCountPer
ii = kk * maxCountPer + jj + 1
If ii <= rowCount Then
mystr = splits(jj - 1)
ss = InStr(mystr, "~")
If ss > 1 Then '返回的有效数据
startindex = InStr(1, mystr, "~")
endindex = InStrRev(mystr, "~")
substr = Mid(mystr, startindex + 1, endindex - 1) '引号中的有效数据
valuearray = Split(substr, "~")
stockname = valuearray(0)
code = valuearray(1)
price = valuearray(2)
If (Len(code) = 5) Then '港股
rise = price / valuearray(3) - 1
Else
rise = valuearray(4) / 100
End If
If price = 0 Then
rise = 0
End If
J = 0
sheet.Range(Chr(begin + J) & ii).Value = stockname
J = J + 1
sheet.Range(Chr(begin + J) & ii).Value = price
J = J + 1
sheet.Range(Chr(begin + J) & ii).Value = rise
sheet.Range(Chr(begin + J) & ii).Font.Color = GetFontColor(rise)
End If
End If
Next jj
Next kk
With Application
.Calculation = xlCalculationAutomatic
.MaxChange = 0.001
End With
End Sub
Sub GetNetValueDetail(ByVal sheet As Worksheet, beginCol As String)
Dim rowCount As Integer
Dim url As String
Dim sTemp As String
rowCount = sheet.Range("A65535").End(xlUp).Row '获取行数
url = "http://hq.sinajs.cn/list=" '新浪行情数据接口
For i = 2 To rowCount '从第二行开始,第一列为股票代码
code = sheet.Range("A" & i).Text
If Len(code) < 6 Then
code = "unknow"
Else
code = "of" & Right(code, 6) '基金代码前of(open fund)
End If
If i = 2 Then
url = url & code
Else
url = url & "," & code
End If
Next i
'获取新浪股票行情数据,放入sTemp变量
With CreateObject("Microsoft.XMLHTTP")
.Open "GET", url, False
.Send
sTemp = .responseText
End With
splits = Split(sTemp, ";")
For i = 0 To rowCount - 1
mystr = splits(i)
ss = InStr(mystr, ",")
If ss > 1 Then
startindex = InStr(1, mystr, """")
endindex = InStrRev(mystr, """")
substr = Mid(mystr, startindex + 1, endindex - startindex - 1) '引号中的有效数据
valuearray = Split(substr, ",")
begin = Asc(beginCol)
J = 0
sheet.Range(Chr(begin + J) & i + 2).Value = valuearray(0) '名称
J = J + 1
sheet.Range(Chr(begin + J) & i + 2).Value = valuearray(1) '净值
J = J + 1
sheet.Range(Chr(begin + J) & i + 2).Value = valuearray(2) '累计净值
J = J + 1
sheet.Range(Chr(begin + J) & i + 2).Value = valuearray(3) '上日净值
J = J + 1
sheet.Range(Chr(begin + J) & i + 2).Value = Format(valuearray(4) / 100, "0.00%") '净值涨跌幅
sheet.Range(Chr(begin + J) & i + 2).Font.Color = GetFontColor(valuearray(1) - valuearray(3))
J = J + 1
sheet.Range(Chr(begin + J) & i + 2).Value = valuearray(5) '日期
End If
Next i
End Sub
日内交易
//均价线 参考大智慧中,分时图(白线)和均价线(黄线)
Inputs:Price(Close);
variables: SumVolume(0),SumjiaLiang(0),junjiaxian(0);
if date<>date[1] then begin
SumVolume=0; SumjiaLiang=0;
end;
if bartype<2 then begin
SumVolume= SumVolume+Ticks;
SumjiaLiang=SumjiaLiang+price*Ticks;
if SumVolume<>0 then
junjiaxian =SumjiaLiang/SumVolume;
end
else
begin
SumVolume= SumVolume+Volume;
SumjiaLiang=SumjiaLiang+price*Volume;
if SumVolume<>0 then
junjiaxian=SumjiaLiang/SumVolume;
end;
plot1(junjiaxian,"junjiaxian",yellow,3);
// 5min IF指数
Input:M(2),Z(20),starttime(1000),closetime(1505),N(81);
var:ATR(0),openday(0),hh(0),ll(0),ma5_data2(0),hh1(0),ll1(0);
if date<>date[1] then begin
openday=opend(0);
end;
ATR=averagetruerange(Z);
ma5_data2=Average(close,N) of data2;
condition1=close>ma5_data2;
condition2=close<ma5_data2;
if marketposition=0 and EntriesToday(date)<1 and condition1 and time>=starttime and Ticks>Average(ticks,20) and high>openday+Matr and time<1500 then begin
buy next bar at high stop;
end;
if marketposition=0 and EntriesToday(date)<1 and condition2 and time>=starttime and ticks >Average(ticks,20) and time<1500 and low<openday -Matr then begin
sellshort next bar at low stop;
end;
//long profit trailing
inputs: stp(10),recentl(5);
var:stopline_buy(0),stopline_short(0),beginhigh(0),beginlow(0),recentlow(0),recenthigh(0),buytype(0),shorttype(0),mp(0);
mp = marketposition;
if mp[0] =1 and mp[1] =0 then begin
beginhigh = high;
buytype =1;
end else if mp[1]=1 and mp=1 then begin
beginhigh = beginhigh[1];
end;
if mp=1 then begin
if buytype =1 then stopline_buy = entryprice - stp;
end;
if barssinceentry>=recentl and mp=1 then begin
recentlow= lowest(low,recentl);
if recentlow > beginhigh then buytype =2;
if mp =1 then begin
if buytype =2 then stopline_buy = lowest(low,recentl);
end else begin
stopline_buy = stopline_buy[1];
end;
if mp =1 then begin
sell("B") next bar at stopline_buy stop;
end;
end;
//short profit trailing
if mp=-1 and mp[1]=0 then begin
beginlow=low;
shorttype=1;
end else if mp[1]=-1 and mp=-1 then begin
beginlow=beginlow[1];
end;
if mp=-1 then begin
if shorttype=1 then stopline_short=entryprice+stp;
end;
if barssinceentry>=recentl then begin
recenthigh=Highest(high,recentl);
if recenthigh < beginlow then shorttype=2;
if mp=-1 then begin
if shorttype=2 then stopline_short=highest(high,recentl);
end else begin
stopline_short=stopline_short[1];
end;
if mp=-1 then begin
buytocover("S") next bar at stopline_short stop;
shorttype=1;
end;
end;
//Day Close
if time>=closetime then begin
if marketposition=1 then
sell next bar at market;
if marketposition=-1 then
buytocover next bar at market;
end;
// 没有加任何滤网的代码 5min
Input:M(2),Z(20),starttime(1000),closetime(1505),N(60);
var:ATR(0),openday(0),hh(0),ll(0),ma5_data2(0),hh1(0),ll1(0);
if date<>date[1] then begin
openday=opend(0);
end;
ATR=averagetruerange(Z);
if marketposition=0 then begin
buy next bar at openday+Matr stop;
end;
if marketposition=0 then begin
sellshort next bar at openday-Matr stop;
end;
if time>=closetime then begin
if marketposition=1 then
sell next bar at market;
if marketposition=-1 then
buytocover next bar at market;
end;
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.