請選擇 進入手機版 | 繼續訪問電腦版

COCO研究院

 找回密碼
 註冊
搜索
查看: 1828|回復: 13

Interactive Brokers Python API (官方原生) 新手教學

[複製鏈接]
發表於 23-1-31 23:01 | 顯示全部樓層 |閱讀模式
本帖最後由 綠茶妹 於 23-1-31 23:49 編輯


Interactive Brokers Python API (官方原生)
下面這一篇寫的很清楚。
我也不知道原生API有沒有比第三方API好用。反正就學學看
https://algotrading101.com/learn ... n-api-native-guide/

今天做到中間這裡 ,還滿好上手的。
c1.png

我的測試
connection.png








評分

參與人數 1金錢 +3 收起 理由
tinyding + 3 感謝分享

查看全部評分

 樓主| 發表於 23-2-2 00:36 | 顯示全部樓層
TrendRover 發表於 23-2-1 23:51
好久不見 ,  最近好嗎?

我本業做的不錯,現在是中階主管。
但是我越來越討厭上班。
過去一年多晚上會看MYM MNQ期貨,我工作太忙了,不能長期睡這麼少。
現在想試程式交易。


回復 支持 1 反對 0

使用道具 舉報

 樓主| 發表於 23-2-12 00:00 | 顯示全部樓層
取出1分鐘線的程式碼

  1. from ibapi.client import EClient
  2. from ibapi.wrapper import EWrapper
  3. from ibapi.contract import Contract

  4. import threading
  5. import time

  6. class IBapi(EWrapper, EClient):
  7.         def __init__(self):
  8.                 EClient.__init__(self, self)
  9.                 self.data = [] #Initialize variable to store candle

  10.         def historicalData(self, reqId, bar):
  11.                 print(f'Time: {bar.date} Close: {bar.close}')
  12.                 self.data.append([bar.date, bar.open,bar.high,bar.low,bar.close])
  13.                
  14. def run_loop():
  15.         app.run()

  16. app = IBapi()
  17. app.connect('127.0.0.1', 7497, 123)

  18. #Start the socket in a thread
  19. api_thread = threading.Thread(target=run_loop, daemon=True)
  20. api_thread.start()

  21. time.sleep(1) #Sleep interval to allow time for connection to server

  22. #Create contract object
  23. contract = Contract()
  24. contract.symbol = "MYM"
  25. contract.secType = "FUT"
  26. contract.exchange = "CBOT"
  27. contract.currency = "USD"
  28. contract.lastTradeDateOrContractMonth = "202303"


  29. #Request historical candles
  30. app.reqHistoricalData(1, contract, '', '5 D', '1 min', 'TRADES', 1, 2, False, [])

  31. time.sleep(10) #sleep to allow enough time for data to be returned

  32. #Working with Pandas DataFrames
  33. import pandas

  34. df = pandas.DataFrame(app.data, columns=['DateTime', 'Open','High','Low','Close'])
  35. df['DateTime'] = pandas.to_datetime(df['DateTime'],unit='s')
  36. df.to_csv('MYM-1min.csv')  

  37. print(df)


  38. app.disconnect()

複製代碼



結果如下

MYM-1min.zip (26.79 KB, 下載次數: 45)

評分

參與人數 1金錢 +5 收起 理由
TrendRover + 5 感謝分享

查看全部評分

回復 支持 1 反對 0

使用道具 舉報

 樓主| 發表於 23-1-31 23:47 | 顯示全部樓層
MYM取資料也OK了。(一樣是上面這篇教學)
m1.png

程式碼如下:
  1. from ibapi.client import EClient
  2. from ibapi.wrapper import EWrapper
  3. from ibapi.contract import Contract

  4. import threading
  5. import time


  6. class IBapi(EWrapper, EClient):
  7.         def __init__(self):
  8.                 EClient.__init__(self, self)
  9.         def tickPrice(self, reqId, tickType, price, attrib):
  10.                 if tickType == 2 and reqId == 1:
  11.                         print('The current ask price is: ', price)

  12. def run_loop():
  13.         app.run()

  14. app = IBapi()
  15. app.connect('127.0.0.1', 7497, 123)

  16. #Start the socket in a thread
  17. api_thread = threading.Thread(target=run_loop, daemon=True)
  18. api_thread.start()

  19. time.sleep(1) #Sleep interval to allow time for connection to server

  20. #Create contract object
  21. contract = Contract()
  22. contract.symbol = "MYM"
  23. contract.secType = "FUT"
  24. contract.exchange = "CBOT"
  25. contract.currency = "USD"
  26. contract.lastTradeDateOrContractMonth = "202303"


  27. #Request Market Data
  28. app.reqMktData(1, contract, '', False, False, [])

  29. time.sleep(10) #Sleep interval to allow time for incoming price data
  30. app.disconnect()

複製代碼



評分

參與人數 1金錢 +2 收起 理由
CalmState951 + 2 按一個讚

查看全部評分

發表於 23-2-1 23:51 | 顯示全部樓層
好久不見 ,  最近好嗎?
發表於 23-2-3 02:29 | 顯示全部樓層
不考慮用 multichart 嗎?
amp 期或有免費的絹商版 multichart.net 用 C# 寫
或是  ninjatrader 也是 C# 寫 , ninjatrader在下單前都是免費
也就是回測開發策略階段都是免費使用
發表於 23-2-3 02:36 | 顯示全部樓層
amp地當沖保證金是 1/10-1/20,所以可以寫當沖 mym mnq 每1000U 可以下 3~5 口順勢
賺錢加碼策略 ,每天 收盤前 30分還是 15分鐘要平倉 ,沒平,他會幫你平收高手續費(以前碰過是
25U ,現在多少你要問問) ,amp  缺點是除了 mc.net 或 mc 外你無法在 ampclearing 的 user portal 裡看到 order position 等等

如果要看得到那麼可以選 deepdiscounttrading 他的 user portal 可以看order
不過他要下單前都要email 要求開放 ,做 op還不准隨便做賣方---需要嚴格批准(資金族也要request ,margin 安全控管嚴格) ---ddt 的  CQG 也比較貴
發表於 23-2-3 02:39 | 顯示全部樓層
發表於 23-2-3 02:41 | 顯示全部樓層
你如果用 python api 換一家要寫一次,很不方便
若C# 就可以換 tool 換 broker marketdata

用 powerlanguage 就簡單幾行,其餘交給 multichart
 樓主| 發表於 23-2-4 22:22 | 顯示全部樓層
我只需要MYM MNQ期貨就可以了,不用選擇權。
剛才上網找一下,DDT與AMP好像都比IB有優勢。

請問用這2家都有multichart嗎?手續費是多少?
發表於 23-2-10 02:33 | 顯示全部樓層
綠茶妹 發表於 23-2-4 22:22
我只需要MYM MNQ期貨就可以了,不用選擇權。
剛才上網找一下,DDT與AMP好像都比IB有優勢。

AMP  選CQG 平台費低於DDT   

都可以選 MC的連線
AMP有 mc.net 免平台費 卷商版
DDT要租或自備 mc
market data 的費用都可以訂 cme 一個  level1 1.5USD就好   MYM +MNQ要訂兩個交易所 cbot + cme
 樓主| 發表於 23-2-11 11:27 | 顯示全部樓層
TrendRover 發表於 23-2-10 02:33
AMP  選CQG 平台費低於DDT   

都可以選 MC的連線

感謝分享!

MC好像比較簡單,Python我覺得比較有彈性,但是基礎的環境要弄起來也要花不少時間。
每星期只有假日才有空來做。


發表於 23-2-15 02:26 | 顯示全部樓層
TrendRover 發表於 23-2-3 02:29
不考慮用 multichart 嗎?
amp 期或有免費的絹商版 multichart.net 用 C# 寫
或是  ninjatrader 也是 C# 寫 ...

請問大大amp 免費的mc.net有詳細的資料嗎,想開戶但受限於資料太少都沒開成
發表於 23-2-16 23:00 | 顯示全部樓層
s4709110.tw 發表於 23-2-15 02:26
請問大大amp 免費的mc.net有詳細的資料嗎,想開戶但受限於資料太少都沒開成
...

https://www.ampfutures.com/tradi ... arts-ampmc-net-free

評分

參與人數 1金錢 +5 收起 理由
綠茶妹 + 5 感謝分享

查看全部評分

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|站長信箱|廣告洽詢|COCO研究院

GMT+8, 24-3-29 05:53

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 | AI繪圖AI超擬真美女AI beauty AI Stable DiffusionAI正妹AI Lookbook