iT邦幫忙

0

(已解決)python串接元大OneAPI問題

系統windows 7 x86
python 3.8.5 x86
最近用python串接元大oneapi遇到下面的問題

Traceback (most recent call last):
  File "C:\Users\trevor\Desktop\HSUNJEN\YuantaOneOrder.py", line 9, in <module>
    objYuantaOneAPI.Login(System.String('98875005091'), System.String('1234'))
System.NullReferenceException: 並未將物件參考設定為物件的執行個體。
   於 YuantaOneAPI.YuantaOneAPITrader.Login(String Account, String Password)

原始碼:

import clr
import System
path = "c://Yuanta//ONE//YuantaOneAPI.dll"
clr.AddReference(path)

from YuantaOneAPI import YuantaOneAPITrader
objYuantaOneAPI = YuantaOneAPITrader()
objYuantaOneAPI.Open()
objYuantaOneAPI.Login(System.String('98875005091'), System.String('1234'))

HELP資訊:

Help on YuantaOneAPITrader in module YuantaOneAPI object:

class YuantaOneAPITrader(System.Object)
 |  Void .ctor()
 |  
 |  Method resolution order:
 |      YuantaOneAPITrader
 |      System.Object
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __call__(self, /, *args, **kwargs)
 |      Call self as a function.
 |  
 |  __delitem__(self, key, /)
 |      Delete self[key].
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getitem__(self, key, /)
 |      Return self[key].
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __hash__(self, /)
 |      Return hash(self).
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __setitem__(self, key, value, /)
 |      Set self[key] to value.
 |  
 |  __str__(self, /)
 |      Return str(self).
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from CLR.CLR Metatype
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  Close
 |      Void Close()
 |  
 |  Dispose
 |      Void Dispose()
 |  
 |  Finalize
 |      Void Finalize()
 |  
 |  GetQuoteList
 |      Boolean GetQuoteList()
 |  
 |  GetQuoteListDetail
 |      Boolean GetQuoteListDetail()
 |  
 |  LogOut
 |      Boolean LogOut()
 |  
 |  Login
 |      Boolean Login(System.String, System.String)
 |  
 |  OnResponse
 |  
 |  Open
 |      Void Open(YuantaOneAPI.enumEnvironmentMode)
 |  
 |  Overloads
 |  
 |  RQ
 |      Boolean RQ(System.String, YuantaOneAPI.YuantaDataHelper)
 |  
 |  SendFutureOrder
 |      Boolean SendFutureOrder(System.String, System.Collections.Generic.List`1[YuantaOneAPI.FutureOrder], YuantaOneAPI.enumLangType)
 |  
 |  SendOVFutureOrder
 |      Boolean SendOVFutureOrder(System.String, System.Collections.Generic.List`1[YuantaOneAPI.OVFutureOrder], YuantaOneAPI.enumLangType)
 |  
 |  SendStockOrder
 |      Boolean SendStockOrder(System.String, System.Collections.Generic.List`1[YuantaOneAPI.StockOrder], YuantaOneAPI.enumLangType)
 |  
 |  SetLogType
 |      Void SetLogType(YuantaOneAPI.enumLogType)
 |  
 |  ShowPostA
 |      Void ShowPostA(Boolean)
 |  
 |  SubscribeFiveTickA
 |      Boolean SubscribeFiveTickA(System.Collections.Generic.List`1[YuantaOneAPI.FiveTickA], YuantaOneAPI.enumLangType)
 |  
 |  SubscribeFiveTickB
 |      Boolean SubscribeFiveTickB(System.Collections.Generic.List`1[YuantaOneAPI.FiveTickB], YuantaOneAPI.enumLangType)
 |  
 |  SubscribeQuote
 |      Boolean SubscribeQuote(System.String, YuantaOneAPI.YuantaDataHelper)
 |  
 |  SubscribeStockTick
 |      Boolean SubscribeStockTick(System.Collections.Generic.List`1[YuantaOneAPI.StockTick], YuantaOneAPI.enumLangType)
 |  
 |  SubscribeWatchlist
 |      Boolean SubscribeWatchlist(System.Collections.Generic.List`1[YuantaOneAPI.Watchlist], YuantaOneAPI.enumLangType)
 |  
 |  SubscribeWatchlistAll
 |      Boolean SubscribeWatchlistAll(System.Collections.Generic.List`1[YuantaOneAPI.WatchlistAll], YuantaOneAPI.enumLangType)
 |  
 |  UnsubscribeFivetickA
 |      Boolean UnsubscribeFivetickA(System.Collections.Generic.List`1[YuantaOneAPI.FiveTickA], YuantaOneAPI.enumLangType)
 |  
 |  UnsubscribeFivetickB
 |      Boolean UnsubscribeFivetickB(System.Collections.Generic.List`1[YuantaOneAPI.FiveTickB], YuantaOneAPI.enumLangType)
 |  
 |  UnsubscribeQuote
 |      Boolean UnsubscribeQuote(System.String, YuantaOneAPI.YuantaDataHelper)
 |  
 |  UnsubscribeStocktick
 |      Boolean UnsubscribeStocktick(System.Collections.Generic.List`1[YuantaOneAPI.StockTick], YuantaOneAPI.enumLangType)
 |  
 |  UnsubscribeWatchlist
 |      Boolean UnsubscribeWatchlist(System.Collections.Generic.List`1[YuantaOneAPI.Watchlist], YuantaOneAPI.enumLangType)
 |  
 |  UnsubscribeWatchlistAll
 |      Boolean UnsubscribeWatchlistAll(System.Collections.Generic.List`1[YuantaOneAPI.WatchlistAll], YuantaOneAPI.enumLangType)
 |  
 |  __overloads__
 |  
 |  add_OnResponse
 |      Void add_OnResponse(YuantaOneAPI.OnResponseEventHandler)
 |  
 |  remove_OnResponse
 |      Void remove_OnResponse(YuantaOneAPI.OnResponseEventHandler)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from System.Object:
 |  
 |  Equals
 |      Boolean Equals(System.Object)
 |      Boolean Equals(System.Object, System.Object)
 |  
 |  GetHashCode
 |      Int32 GetHashCode()
 |  
 |  GetType
 |      System.Type GetType()
 |  
 |  MemberwiseClone
 |      System.Object MemberwiseClone()
 |  
 |  ReferenceEquals
 |      Boolean ReferenceEquals(System.Object, System.Object)
 |  
 |  ToString
 |      System.String ToString()

API元件:
https://drive.google.com/file/d/1OQXZIZAkwgum2f6QeTJMZluj3IacepoY/view?usp=sharing

希望高手幫幫忙XD

看更多先前的討論...收起先前的討論...
KYC iT邦新手 4 級 ‧ 2020-09-20 17:14:01 檢舉
不確定是不是你要的?參考一下吧
https://www.yuantafutures.com.tw/file-repository/content/smartAPI/page1.html
https://www.php.cn/python-tutorials-422498.html
skyksl066 iT邦新手 5 級 ‧ 2020-09-20 17:42:36 檢舉
感謝你的回答

第一個網站部分那個是元大另外一個API該API的調用方法論壇已經有解方了我也拜讀過了但是不知道怎麼調用我這個,網址https://ithelp.ithome.com.tw/m/articles/10222522

第二個網址ctypes套件的調用方法我在試試
skyksl066 iT邦新手 5 級 ‧ 2020-09-20 19:29:13 檢舉
第二個網址的方法我試了OneAPI的底層似乎是用C#開發的所以ctypes套件相容性不好,所以都會出現。

```
Traceback (most recent call last):
File "C:\Users\trevor\Desktop\HSUNJEN\YuantaOneOrder.py", line 16, in <module>
objYuantaOneAPI = Objdll.YuantaOneAPITrader
File "C:\Python38-32\lib\ctypes\__init__.py", line 386, in __getattr__
func = self.__getitem__(name)
File "C:\Python38-32\lib\ctypes\__init__.py", line 391, in __getitem__
func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'YuantaOneAPITrader' not found
```
然後根據這篇老外的回答
https://stackoverflow.com/questions/6718088/using-ctypes-in-python-to-access-a-c-sharp-dlls-methods
說使用Python for .NET這套件 就是clr這隻

還是感謝你的熱心回答
open 完設個 time.sleep(1) 再 login
不會出現 System.NullReferenceException: 並未將物件參考設定為物件的執行個體。

從上面的錯誤來看起來是還沒初始化完成馬上又執行下一段

因為沒有其他可以測試的函式 不知道怎樣測其他功能是不是正常
skyksl066 iT邦新手 5 級 ‧ 2020-09-21 13:57:48 檢舉
time.sleep後登入成功了,感恩
froce iT邦大師 1 級 ‧ 2020-09-21 15:29:45 檢舉
ㄜ...這坑也太坑人了。

尚未有邦友回答

立即登入回答