持倉(cāng)信息導(dǎo)出Excel并管理盈虧的代碼,精華呀! [金字塔]
- 咨詢內(nèi)容:
這個(gè)模塊需要打開(kāi)Excel文件holdhing.xlsx,請(qǐng)下載:
http://m1.mail.sina.com.cn/apps/netdisk/download.php?id=53749010fa1386e8191cbf6f39c9b367
安裝說(shuō)明:
1、在d:\下建立一個(gè)目錄GuZhi
2、將Holding.xlsx復(fù)制到D:\Guzhi下
3、建立一個(gè)VBA模塊,將下面的代碼復(fù)制進(jìn)去,保存。
4、運(yùn)行了VBA之后,在工具菜單上的擴(kuò)展項(xiàng)下有兩個(gè)選擇:1)啟動(dòng)持倉(cāng)管理 2)終止持倉(cāng)管理,要進(jìn)行管理就選擇1),不要管理就選擇2)。
5、Excel文件中有一個(gè)模式:可選擇:管理盈虧、顯示盈虧,選擇“顯示盈虧”時(shí),只實(shí)時(shí)顯示持倉(cāng)的盈虧情況,并不幫你平倉(cāng),選擇“管理盈虧”時(shí),將根據(jù)你的設(shè)置進(jìn)行盈虧管理,如果你有一個(gè)品種不想進(jìn)行管理,可以將止損點(diǎn)數(shù)、止盈點(diǎn)數(shù)、回撤點(diǎn)數(shù)、保本點(diǎn)數(shù)都設(shè)為0(0值不顯示),如果不想使用移動(dòng)止盈,可以單修改回撤點(diǎn)數(shù)為0,其他如此類推。
如果你想下載完整的東西,可以單擊:
http://m1.mail.sina.com.cn/apps/netdisk/download.php?id=9750c4bbbd07723ef3f1cb815c140482
就不用自己建立模塊了,但是1、2步還是得做的。第3步只要將這個(gè)壓縮文件中的mdHold.bas導(dǎo)入到VBA模塊中就行了。
- 金字塔客服:
public objExcel,objWorkbook
public AccountCode,Code,Market
private CodeArr(6),MarketArr(6),HoldingCount
private Report(6)
private iRow,iTimeCount
private Multipliter,MinTick,ShortPercent,LongPercent''''''''''菜單操作
Sub MENU_Show()
Menu.AddMenu 0, 0, "啟動(dòng)持倉(cāng)管理"
Menu.AddMenu 1, 1, "終止持倉(cāng)管理"
End Sub
Sub MENU_Command(Cmd)
Select Case Cmd
Case 0
StartManage
Case 1
EndManage
End Select
End Sub
Sub APPLICATION_VBAStart()
iTimeCount=0
iRow=4
GetAccountCode '將當(dāng)前登錄的賬號(hào)保存到變量
End SubSub APPLICATION_Timer(ID)
iTimeCount=iTimeCount+1
GetAccountCode
On error Resume Next
WriteNewPrice
application.PeekAndPump
End SubSub StartManage() '啟動(dòng)
GetAccountCode '將當(dāng)前登錄的賬號(hào)保存到變量
iRow=4
OpenExcel
Call Application.SetTimer(5,2000)
GetAllHolding AccountCode
End SubSub EndManage '停止管理
Application.KillTimer(5)
Set objExcel=Nothing
End SubSub WriteNewPrice()
dim i,iHold,NewPrice
i=4
On error resume next
objExcel.sheets(1).Range("TimeCount").Value = iTimeCount
Do while objExcel.sheets(1).Cells(i,2).Value<>""
Code=objExcel.sheets(1).Cells(i,2).Value
Market=objExcel.sheets(1).Cells(i,23).Value
Set Report1 = marketdata.GetReportData(Code,Market)
NewPrice=Report1.NewPrice
objExcel.sheets(1).Cells(i,19).Value = NewPrice
If NewPrice>objExcel.sheets(1).Cells(i,20).Value then
objExcel.sheets(1).Cells(i,20).Value=NewPrice
End if
If NewPrice<objExcel.sheets(1).Cells(i,21).Value then
objExcel.sheets(1).Cells(i,21).Value=NewPrice
End if
'application.MsgOut objExcel.sheets(1).Cells(i,2).Value
iHold=Abs(objExcel.sheets(1).Cells(i,3).Value) '持倉(cāng)手?jǐn)?shù)
If objExcel.sheets(1).Range("Mode").Value="管理盈虧" then
'多單
If objExcel.sheets(1).Cells(i,3).Value>0 then
If NewPrice<objExcel.sheets(1).Cells(i,10).Value And objExcel.sheets(1).Cells(i,9).Value>0 then
'止損
WriteLog "買",NewPrice,"多單止損",i
PingDuoDan 0,Code,Market,iHold
End if
If NewPrice>objExcel.sheets(1).Cells(i,12).Value And objExcel.sheets(1).Cells(i,11).Value>0 then
'止贏
WriteLog "買",NewPrice,"多單止盈",i
PingDuoDan 0,Code,Market,iHold
End if
If objExcel.sheets(1).Cells(i,13).Value>0 And NewPrice>objExcel.sheets(1).Cells(i,5).Value then '回撤止贏
If objExcel.sheets(1).Cells(i,20).Value-NewPrice>objExcel.sheets(1).Cells(i,13).Value then '回撤點(diǎn)數(shù)大于設(shè)置數(shù)
WriteLog "買",NewPrice,"多單回撤止盈",i
PingDuoDan 0,Code,Market,iHold
End if
End if
If NewPrice<objExcel.sheets(1).Cells(i,16).Value And NewPrice>objExcel.sheets(1).Cells(i,5).Value _
And objExcel.sheets(1).Cells(i,20).Value>objExcel.sheets(1).Cells(i,5).Value+2 And objExcel.sheets(1).Cells(i,15).Value>0 then
'保本:最高價(jià)大于保本價(jià),最新價(jià)小于保本價(jià),且有盈利
WriteLog "買",NewPrice,"多單保本",i
PingDuoDan 0,Code,Market,iHold
End if
End if 'End 多單
'空單
If objExcel.sheets(1).Cells(i,3).Value<0 then
If NewPrice>objExcel.sheets(1).Cells(i,10).Value And objExcel.sheets(1).Cells(i,9).Value>0 then
'止損
WriteLog "賣",NewPrice,"空單止損",i
PingKongDan 0,Code,Market,iHold
End if
If NewPrice<objExcel.sheets(1).Cells(i,12).Value And objExcel.sheets(1).Cells(i,11).Value>0 then
'止贏
WriteLog "賣",NewPrice,"空單止盈",i
PingKongDan 0,Code,Market,iHold
End if
If objExcel.sheets(1).Cells(i,13).Value>0 And NewPrice<objExcel.sheets(1).Cells(i,5).Value then '回撤止贏
If NewPrice-objExcel.sheets(1).Cells(i,21).Value>objExcel.sheets(1).Cells(i,13).Value then '回撤點(diǎn)數(shù)大于設(shè)置數(shù)
WriteLog "賣",NewPrice,"空單回撤止盈",i
PingKongDan 0,Code,Market,iHold
End if
End if
If NewPrice<objExcel.sheets(1).Cells(i,5).Value-2 And NewPrice>objExcel.sheets(1).Cells(i,16).Value _
And objExcel.sheets(1).Cells(i,20).Value>objExcel.sheets(1).Cells(i,16).Value And objExcel.sheets(1).Cells(i,15).Value>0 then
'保本
WriteLog "賣",NewPrice,"空單保本",i
PingKongDan 0,Code,Market,iHold
End if
End if 'End 空單
End if 'End 管理盈虧
i=i+1
Loop
End Sub'寫(xiě)成交日志
Sub WriteLog(sAspect,nNewPrice,sMemo,iHoldRow)
dim iLogRow 'sheets(2)已使用行數(shù)
iLogRow=objExcel.sheets(2).UsedRange.Rows.Count+1
objExcel.sheets(2).Cells(iLogRow,1).Value=CDate(Time)
objExcel.sheets(2).Cells(iLogRow,2).Value=objExcel.sheets(1).Cells(iHoldRow,2).Value
objExcel.sheets(2).Cells(iLogRow,3).Value=sAspect
if sAspect="買" then
objExcel.sheets(2).Cells(iLogRow,4).Value=objExcel.sheets(1).Cells(iHoldRow,3).Value
objExcel.sheets(2).Cells(iLogRow,5).Value=objExcel.sheets(1).Cells(iHoldRow,5).Value
objExcel.sheets(2).Cells(iLogRow,9).Value=(nNewPrice-objExcel.sheets(1).Cells(iHoldRow,5).Value)*objExcel.sheets(1).Cells(iHoldRow,22).Value*objExcel.sheets(1).Cells(iHoldRow,3).Value
else
objExcel.sheets(2).Cells(iLogRow,4).Value=Abs(objExcel.sheets(1).Cells(iHoldRow,3).Value)
objExcel.sheets(2).Cells(iLogRow,5).Value=objExcel.sheets(1).Cells(iHoldRow,5).Value
objExcel.sheets(2).Cells(iLogRow,9).Value=(objExcel.sheets(1).Cells(iHoldRow,5).Value-nNewPrice)*objExcel.sheets(1).Cells(iHoldRow,22).Value*Abs(objExcel.sheets(1).Cells(iHoldRow,3).Value)
end if
'最高價(jià)、最低價(jià)
objExcel.sheets(2).Cells(iLogRow,6).Value=objExcel.sheets(1).Cells(iHoldRow,20).Value
objExcel.sheets(2).Cells(iLogRow,7).Value=objExcel.sheets(1).Cells(iHoldRow,21).Value
objExcel.sheets(2).Cells(iLogRow,8).Value=nNewPrice
objExcel.sheets(2).Cells(iLogRow,10).Value=sMemo
End SubSub OpenExcel()
On Error Resume Next
Set objExcel = GetObject(,"Excel.Application")
if Err.number<>0 then
Set objExcel = CreateObject("Excel.Application")
'打開(kāi)指定文件
Set objExcel = GetObject("D:\GuZhi\Holding.xlsx")
else
'打開(kāi)指定文件
Set objExcel = GetObject("D:\GuZhi\Holding.xlsx")
end if
objExcel.Parent.Windows("Holding.xlsx").Activate
objExcel.Application.DisplayFormulaBar=False
objExcel.Application.Visible = True
End Sub
'成交后重新取得持倉(cāng)信息
Sub Order_OrderStatusEx2(OrderID, Status, Filled, Remaining, Price, Code, Market, OrderType, Aspect, Kaiping, Account, AccountType)
If Status="Filled" then
GetAllHolding AccountCode
End if
End Sub
'未完,待續(xù) - 用戶回復(fù):
'這里的代碼接著上面的,放在上面模塊下方。
Sub GetAllHolding(sAccount)
dim i,k
dim BuyHolding
dim BuyCost
dim BuyTodayHolding
dim SellHolding
dim SellCost
dim SellTodayHolding
dim PNL
dim UseMargin
dim Code
dim MarketOn Error resume Next
objExcel.Sheets(1).Unprotect
objExcel.Sheets(1).Range("Mode")="顯示盈虧"
objExcel.Sheets(1).Range("B4:E9").ClearContents
objExcel.Sheets(1).Range("S4:U9").ClearContents
objExcel.Sheets(1).Rows("4:9").Select
objExcel.Application.Selection.EntireRow.Hidden = False
HoldingCount=Order.Holding2(sAccount)
'Application.MsgOut "HoldingCount:" & HoldingCount
If HoldingCount>0 then
For i=0 to HoldingCount-1
Call Order.HoldingInfo2(i,BuyHolding,BuyCost,BuyTodayHolding,SellHolding,SellCost,SellTodayHolding,PNL,UseMargin,Code,Market,sAccount)
CodeArr(i)=Code
MarketArr(i)=Market
GetContract Code,Market
'Call Order.HoldingInfoByCode2(Code,Market,BuyHolding,BuyCost,BuyTodayHolding,SellHolding,SellCost,SellTodayHolding,PNL,UseMargin,sAccount)
objExcel.sheets(1).Cells(i+iRow,2).Value = Code
If BuyHolding>0 then
objExcel.sheets(1).Cells(i+iRow,3).Value = BuyHolding
objExcel.sheets(1).Cells(i+iRow,4).Value = BuyTodayHolding
If BuyHolding>0 then
objExcel.sheets(1).Cells(i+iRow,5).Value = BuyCost/Multipliter/BuyHolding
Else
objExcel.sheets(1).Cells(i+iRow,5).Value = BuyCost
End if
If objExcel.sheets(1).Cells(i+iRow,20)=0 then '如果最高價(jià)為0,將開(kāi)倉(cāng)價(jià)寫(xiě)入最高價(jià)
objExcel.sheets(1).Cells(i+iRow,20)=objExcel.sheets(1).Cells(i+iRow,5)
objExcel.sheets(1).Cells(i+iRow,21)=objExcel.sheets(1).Cells(i+iRow,5)
End if
End if
If SellHolding>0 then
objExcel.sheets(1).Cells(i+iRow,3).Value = -SellHolding
objExcel.sheets(1).Cells(i+iRow,4).Value = -SellTodayHolding
If SellHolding>0 then
objExcel.sheets(1).Cells(i+iRow,5).Value = SellCost/Multipliter/SellHolding
Else
objExcel.sheets(1).Cells(i+iRow,5).Value = SellCost
End if
If objExcel.sheets(1).Cells(i+iRow,21)=0 then '如果最低價(jià)為0,將開(kāi)倉(cāng)價(jià)寫(xiě)入最低價(jià)
objExcel.sheets(1).Cells(i+iRow,21)=objExcel.sheets(1).Cells(i+iRow,5)
objExcel.sheets(1).Cells(i+iRow,20)=objExcel.sheets(1).Cells(i+iRow,5)
End if
End if
objExcel.sheets(1).Cells(i+iRow,22).Value = Multipliter
objExcel.sheets(1).Cells(i+iRow,23).Value = Market
Next 'End i
'設(shè)置數(shù)字微調(diào)按鈕顯隱
For k=1 to HoldingCount
objExcel.Sheets(1).Shapes("SpinZsds" & Cstr(k)).Visible = True
objExcel.Sheets(1).Shapes("SpinZYds" & Cstr(k)).Visible = True
objExcel.Sheets(1).Shapes("SpinYdZy" & Cstr(k)).Visible = True
objExcel.Sheets(1).Shapes("SpinBbds" & Cstr(k)).Visible = True
'設(shè)置微調(diào)按鈕的位置
objExcel.Sheets(1).Shapes("spinZsDs" & Cstr(k)).Top = objExcel.Sheets(1).Range("J" & Cstr(k+iRow-1)).Top
objExcel.Sheets(1).Shapes("spinZsDs" & Cstr(k)).Left = objExcel.Sheets(1).Range("J" & Cstr(k+iRow-1)).Left - objExcel.Sheets(1).Shapes("spinZsDs" & Cstr(k)).Width
objExcel.Sheets(1).Shapes("spinZsDs" & Cstr(k)).Height = objExcel.Sheets(1).Range("J" & Cstr(k+iRow-1)).Height
objExcel.Sheets(1).Shapes("SpinZYds" & Cstr(k)).Top = objExcel.Sheets(1).Range("L" & Cstr(k+iRow-1)).Top
objExcel.Sheets(1).Shapes("SpinZYds" & Cstr(k)).Left = objExcel.Sheets(1).Range("L" & Cstr(k+iRow-1)).Left - objExcel.Sheets(1).Shapes("SpinZYds" & Cstr(k)).Width
objExcel.Sheets(1).Shapes("SpinZYds" & Cstr(k)).Height = objExcel.Sheets(1).Range("L" & Cstr(k+iRow-1)).Height
objExcel.Sheets(1).Shapes("SpinYdZy" & Cstr(k)).Top = objExcel.Sheets(1).Range("N" & Cstr(k+iRow-1)).Top
objExcel.Sheets(1).Shapes("SpinYdZy" & Cstr(k)).Left = objExcel.Sheets(1).Range("N" & Cstr(k+iRow-1)).Left - objExcel.Sheets(1).Shapes("SpinYdZy" & Cstr(k)).Width
objExcel.Sheets(1).Shapes("SpinYdZy" & Cstr(k)).Height = objExcel.Sheets(1).Range("N" & Cstr(k+iRow-1)).Height
objExcel.Sheets(1).Shapes("SpinBbds" & Cstr(k)).Top = objExcel.Sheets(1).Range("P" & Cstr(k+iRow-1)).Top
objExcel.Sheets(1).Shapes("SpinBbds" & Cstr(k)).Left = objExcel.Sheets(1).Range("P" & Cstr(k+iRow-1)).Left - objExcel.Sheets(1).Shapes("SpinBbds" & Cstr(k)).Width
objExcel.Sheets(1).Shapes("SpinBbds" & Cstr(k)).Height = objExcel.Sheets(1).Range("P" & Cstr(k+iRow-1)).Height
Next
If HoldingCount<6 then
For k=HoldingCount+1 to 6
objExcel.Sheets(1).Shapes("SpinZsds" & Cstr(k)).Visible = False
objExcel.Sheets(1).Shapes("SpinZYds" & Cstr(k)).Visible = False
objExcel.Sheets(1).Shapes("SpinYdZy" & Cstr(k)).Visible = False
objExcel.Sheets(1).Shapes("SpinBbds" & Cstr(k)).Visible = False
Next
objExcel.Sheets(1).Rows(HoldingCount+iRow & ":9").Select
objExcel.Application.Selection.EntireRow.Hidden = True
End if
Else
objExcel.Sheets(1).Range("S4:U9").Select
objExcel.Application.Selection.ClearContents
objExcel.Rows("5:9").Select
objExcel.Application.Selection.EntireRow.Hidden = True
End if
objExcel.Sheets(1).Range("B4").Select
objExcel.ActiveSheet.Protect
End SubSub GetContract(sCode,sMarket) '獲取合約的信息
Call Order.Contract(sCode,sMarket,Multipliter,MinTick,ShortPercent,LongPercent)
End SubSub GetAccountCode() '取得當(dāng)前登錄的帳號(hào)
Dim sAccount '記錄可能更換了的賬號(hào)
If AccountCode = "" Then
AccountCode = CStr(Trim(ORDER.ACCOUNT(1)))
End If
sAccount = CStr(Trim(ORDER.ACCOUNT(1)))
If sAccount = AccountCode Then
Exit Sub
Else
AccountCode = sAccount
GetAllHolding sAccount
End If
End Sub'平多單
Sub PingDuoDan(nPrice,sCode,sMarket,iOrdVol) '平多單,nPrice=0時(shí)為市價(jià),否則就是傳遞過(guò)來(lái)的價(jià)
If iOrdVol>0 then
If nPrice=0 then
Call Order.Sell(1,iOrdVol,0,0,sCode,sMarket,"",0) '市價(jià)平多單
Else
Call Order.Sell(0,iOrdVol,nPrice,0,sCode,sMarket,"",0) '限價(jià)平多單
End If
End If
End Sub
'平空單
Sub PingKongDan(nPrice,sCode,sMarket,iOrdVol) '平空單,nPrice=0時(shí)為市價(jià),否則就是傳遞過(guò)來(lái)的價(jià)
If iOrdVol>0 then
If nPrice=0 then
Call Order.SellShort(1,iOrdVol,0,0,sCode,sMarket,"",0) '市價(jià)平空單
Else
Call Order.SellShort(0,iOrdVol,nPrice,0,sCode,sMarket,"",0) '限價(jià)平空單
End If
End If
End Sub - 網(wǎng)友回復(fù):
精華 這個(gè)牛
- 網(wǎng)友回復(fù): 很強(qiáng)大,很強(qiáng)大
如果以上指標(biāo)公式不適用于您常用的行情軟件
或者您想改編成選股公式,以便快速選出某種形態(tài)個(gè)股的話,
- 上一篇:區(qū)間判斷程序求編
- 下一篇:模擬賬戶能使用ORDER嗎?
相關(guān)文章
-
沒(méi)有相關(guān)內(nèi)容