自己做量化交易软件(23)小白量化之MetaTrader5自动交易2
上一篇我们介绍了MetaTrader 5关于交易类的函数功能,这篇文章主要介绍行情获取等方面知识。
一、获取MetaTrader 5程序端中所有交易品种的数量。
import HP_mt5 as hmt5# 建立与MetaTrader 5程序端的连接if not hmt5.initialize():print("initialize() failed, error code =",hmt5.last_error())quit()# 获取交易品种的数量symbols=hmt5.symbols_total()if symbols>0:print("Total symbols =",symbols)else:print("symbols not found")# 断开与MetaTrader 5程序端的连接hmt5.shutdown()
运行结果:
Total symbols = 260
二、获取MetaTrader 5程序端中的所有交易品种。
import HP_mt5 as hmt5# 建立与MetaTrader 5程序端的连接if not hmt5.initialize():print("initialize() failed, error code =",hmt5.last_error())quit()# 获取所有交易品种symbols=hmt5.symbols_get()print('Symbols: ', len(symbols))count=0# 显示前五个交易品种for s in symbols:count+=1print("{}. {}".format(count,s.name))if count==5: breakprint()# 获取名称中包含RU的交易品种ru_symbols=hmt5.symbols_get(group="*RU*")print('len(*RU*): ', len(ru_symbols))for s in ru_symbols:print(s.name)print()# 获取名称中不包含USD、EUR、JPY和GBP的交易品种group_symbols=hmt5.symbols_get(group="*,!*USD*,!*EUR*,!*JPY*,!*GBP*")print('len(*,!*USD*,!*EUR*,!*JPY*,!*GBP*):', len(group_symbols))for s in group_symbols:print(s.name,":",s)# 断开与MetaTrader 5程序端的连接hmt5.shutdown()
运行结果:
Symbols: 2601. EURUSD2. GBPUSD3. USDCHF4. USDJPY5. USDCADlen(*RU*): 3EURUSDUSDRUBXBRUSD。。。后面输出信息省略
三、获取指定交易品种的数据。
import HP_mt5 as hmt5# 建立与MetaTrader 5程序端的连接if not hmt5.initialize():print("initialize() failed, error code =",hmt5.last_error())quit()# 尝试在市场报价中启用显示EURJPY交易品种selected=hmt5.symbol_select("EURJPY",True)if not selected:print("Failed to select EURJPY")hmt5.shutdown()quit()# 显示EURJPY交易品种属性symbol_info=hmt5.symbol_info("EURJPY")if symbol_info!=None:# display the terminal data 'as is' print(symbol_info)print("EURJPY: spread =",symbol_info.spread," digits =",symbol_info.digits)# display symbol properties as a listprint("Show symbol_info(\"EURJPY\")._asdict():")symbol_info_dict = hmt5.symbol_info("EURJPY")._asdict()for prop in symbol_info_dict:print(" {}={}".format(prop, symbol_info_dict[prop]))# 断开与MetaTrader 5程序端的连接hmt5.shutdown()
运行结果:
SymbolInfo(custom=False, chart_mode=0, select=True, visible=True, session_deals=0, session_buy_orders=0, session_sell_orders=0, volume=0, volumehigh=0, volumelow=0, time=0, digits=3, spread=0, spread_float=True, ticks_bookdepth=10, trade_calc_mode=0, trade_mode=4, start_time=0, expiration_time=0, trade_stops_level=0, trade_freeze_level=0, trade_exemode=2, swap_mode=1, swap_rollover3days=3, margin_hedged_use_leg=False, expiration_mode=15, filling_mode=2, order_mode=127, order_gtc_mode=0, option_mode=0, ...)EURJPY: spread = 0 digits = 3Show symbol_info("EURJPY")._asdict():custom=Falsechart_mode=0select=Truevisible=Truesession_deals=0session_buy_orders=0session_sell_orders=0volume=0volumehigh=0volumelow=0time=0digits=3spread=0spread_float=Trueticks_bookdepth=10trade_calc_mode=0trade_mode=4start_time=0expiration_time=0trade_stops_level=0trade_freeze_level=0trade_exemode=2swap_mode=1swap_rollover3days=3margin_hedged_use_leg=Falseexpiration_mode=15filling_mode=2order_mode=127order_gtc_mode=0option_mode=0option_right=0bid=0.0bidhigh=0.0bidlow=0.0ask=0.0askhigh=0.0asklow=0.0last=0.0lasthigh=0.0lastlow=0.0volume_real=0.0volumehigh_real=0.0volumelow_real=0.0option_strike=0.0point=0.001trade_tick_value=0.935182500865044trade_tick_value_profit=0.935182500865044trade_tick_value_loss=0.935182500865044trade_tick_size=0.001trade_contract_size=100000.0trade_accrued_interest=0.0trade_face_value=0.0trade_liquidity_rate=0.0volume_min=0.01volume_max=100.0volume_step=0.01volume_limit=0.0swap_long=-3.74swap_short=-1.04margin_initial=100000.0margin_maintenance=0.0session_volume=0.0session_turnover=0.0session_interest=0.0session_buy_orders_volume=0.0session_sell_orders_volume=0.0session_open=0.0session_close=0.0session_aw=0.0session_price_settlement=0.0session_price_limit_min=0.0session_price_limit_max=0.0margin_hedged=0.0price_change=0.0price_volatility=0.0price_theoretical=0.0price_greeks_delta=0.0price_greeks_theta=0.0price_greeks_gamma=0.0price_greeks_vega=0.0price_greeks_rho=0.0price_greeks_omega=0.0price_sensitivity=0.0basis=category=currency_base=EURcurrency_profit=JPYcurrency_margin=EURbank=description=Euro vs Japanese Yenexchange=formula=isin=name=EURJPYpage=path=Forex\Minors\EURJPY
四、获取行情数据及小白量化之仿通达信指标计算
import pandas as pdfrom HP_formula import *import MetaTrader5 as mt5import HP_mt5 as hmt5# 建立与MetaTrader 5程序端的连接if not hmt5.initialize():print("initialize() failed, error code =",hmt5.last_error())quit()'''TIMEFRAME 是一个包含可能图表周期值的枚举ID 描述TIMEFRAME_M11分钟TIMEFRAME_M22 分钟TIMEFRAME_M33 分钟TIMEFRAME_M44 分钟TIMEFRAME_M55 分钟TIMEFRAME_M66 分钟TIMEFRAME_M1010 分钟TIMEFRAME_M1212 分钟TIMEFRAME_M1215 分钟TIMEFRAME_M 分钟TIMEFRAME_M3030 分钟TIMEFRAME_H11 小时TIMEFRAME_H22 小时TIMEFRAME_H33 小时valuesTIMEFRAME_H44 小时TIMEFRAME_H66 小时TIMEFRAME_H88 小时TIMEFRAME_H1212 小时TIMEFRAME_D11 天TIMEFRAME_W11 周TIMEFRAME_MON11 个月'''# 在UTC时区,获取01.10.开始的10个EURUSD H4柱形图rates = hmt5.copy_rates_from_pos("XAUUSD", mt5.TIMEFRAME_H1, 0, 2440)df=hmt5.tohpdata(rates)print(df)#下面是小白仿通达信公式系统计算#首先要对数据预处理mydf=df.copy()CLOSE=mydf['close']LOW=mydf['low']HIGH=mydf['high']OPEN=mydf['open']C=mydf['close']L=mydf['low']H=mydf['high']O=mydf['open']def RSI(N1=6, N2=12, N3=24):"""RSI 相对强弱指标"""LC = REF(CLOSE, 1)RSI1 = SMA(MAX(CLOSE - LC, 0), N1, 1) / SMA(ABS(CLOSE - LC), N1, 1) * 100RSI2 = SMA(MAX(CLOSE - LC, 0), N2, 1) / SMA(ABS(CLOSE - LC), N2, 1) * 100RSI3 = SMA(MAX(CLOSE - LC, 0), N3, 1) / SMA(ABS(CLOSE - LC), N3, 1) * 100return RSI1, RSI2, RSI3#假定我们使用RSI指标r1,r2,r3=RSI()mydf = mydf.join(pd.Series( r1,name='RSI1')) mydf = mydf.join(pd.Series( r2,name='RSI2')) mydf = mydf.join(pd.Series( r3,name='RSI3')) mydf['S80']=80 #增加上轨80轨迹线mydf['X20']=20 #增加下轨20轨迹线mydf=mydf.tail(100) #显示最后100条数据线 #下面是绘线语句mydf.S80.plot.line()mydf.X20.plot.line()mydf.RSI1.plot.line(legend=True)mydf.RSI2.plot.line(legend=True)mydf.RSI2.plot.line(legend=True)# 断开与MetaTrader 5程序端的连接hmt5.shutdown()
运行结果:
timeopenhighlow close tick_volume spread real_volumedate0 -01-21 01:00:00 1560.88 1561.49 1559.95 1561.27 1512 2 0 -01-21 01:00:001 -01-21 02:00:00 1561.27 1561.96 1560.36 1560.45 2179 2 0 -01-21 02:00:002 -01-21 03:00:00 1560.46 1567.94 1560.03 1566.67 10579 2 0 -01-21 03:00:003 -01-21 04:00:00 1566.67 1568.63 1564.60 1567.01 7646 2 0 -01-21 04:00:004 -01-21 05:00:00 1567.00 1567.31 1565.53 1566.86 4221 2 0 -01-21 05:00:005 -01-21 06:00:00 1566.87 1567.75 1566.28 1566.60 2179 2 0 -01-21 06:00:00
显示图形如下:
以上文章介绍了MetaTrader5的Python方面的程序开发。MetaTrader5提供了外汇,期货,数字币等行情数据。读者不难结合前面的文章,根据自己策略写出全自动交易程序。
独狼荷蒲qq:2886002
通通小白python量化群:524949939
微信公众号:独狼股票分析
如果觉得《自己做量化交易软件(23)小白量化之MetaTrader5自动交易2》对你有帮助,请点赞、收藏,并留下你的观点哦!