实时算法交易专家的逻辑



我做了一个交易专家,通过烛台检查是否找到信号,然后执行买入,卖出订单

for i in range (len(df['Open'])) : 
if some logic :
buy or sell

现在我希望这位专家处理实时数据,而不是历史数据,我正在努力制定其工作原理的逻辑。

我想做的是:

寻找最后 30 根柱线,然后对它们进行一些计算,然后 循环检查最后 2 根烛条以查看是否有某些信号 已找到 ..我希望循环每 4 小时工作一次,因为我正在工作 4 小时 时间框架,每个新烛台也是如此

我正在尝试使用MetaTrader5函数库

copy_rates_from_pos(
symbol,       // symbol name
timeframe,    // timeframe
start_pos,    // initial bar index
count         // number of bars
)

这段代码将帮助我找到最后 30 根柱线,但仍然无法弄清楚如何制作 for 循环!

你可以使用这样的东西

import pytz
import pandas as pd
import MetaTrader5 as mt5
import time
from datetime import datetime
from threading import Timer

server_name = "AMPGlobalUSA-Demo"
server_num = # your server num
password = # password

#------------------------------------------------------------------------------
def actualtime():
# datetime object containing current date and time
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
#print("date and time =", dt_string)
return str(dt_string)
#------------------------------------------------------------------------------
def sync_60sec(op):
info_time_new = datetime.strptime(str(actualtime()), '%d/%m/%Y %H:%M:%S')
waiting_time = 60 - info_time_new.second
t = Timer(waiting_time, op)
t.start()
print(actualtime(), f'waiting till next minute and 00 sec...')
#------------------------------------------------------------------------------
def program(symbol):
if not mt5.initialize(login=server_num, server=server_name, password=password):
print("initialize() failed, error code =",mt5.last_error())
quit()
timezone = pytz.timezone("Etc/UTC")
utc_from = datetime.now()
######### Change here the timeframe
rates = mt5.copy_rates_from(symbol, mt5.TIMEFRAME_M1, utc_from, 70)
mt5.shutdown()
rates_frame = pd.DataFrame(rates)
rates_frame['time']=pd.to_datetime(rates_frame['time'], unit='s')
# If you want to work only with open, high, low, close you could use
#rates_frame = rates_frame.drop(['tick_volume', 'real_volume'], axis=1)
print(f"n", actualtime(),f"|| waiting for signals {symbol} ||n")
if not mt5.initialize():
print("initialize() failed, error code =",mt5.last_error())
quit()
point = mt5.symbol_info(symbol).point
price = mt5.symbol_info_tick(symbol).ask
request = {
"action": mt5.TRADE_ACTION_PENDING,
"symbol": symbol,
"volume": 1.0,
"type": mt5.ORDER_TYPE_BUY_LIMIT,
"price": price,
"sl": price + 40 * point,
"tp": price - 80 * point,
"deviation": 20,
"magic": 234000,
"comment": "st_1_min_mod_3",
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_RETURN,
}

condition_buy_1 = (
(rates_frame.close.iloc[-2] > rates_frame.open.iloc[-2])& 
(rates_frame.close.iloc[-2] > rates_frame.close.iloc[-3])
)
if condition_buy_1:
#result = mt5.order_send(request)
print('Sending Order!')

# Im using AMPGlobalUSA-Demo Server
# starting mt5
if not mt5.initialize(login=server_num, server=server_name, password=password):
print("initialize() failed, error code =",mt5.last_error())
quit()          
#------------------------------------------------------------------------------
#                   S T A R T I N G   M T 5 
#------------------------------------------------------------------------------
authorized=mt5.login(server_num, password=password)
if authorized:
account_info=mt5.account_info()
if account_info!=None:       
account_info_dict = mt5.account_info()._asdict()
df=pd.DataFrame(list(account_info_dict.items()),columns=['property','value'])
print("account_info() as dataframe:")
print(df)
else:
print(f"failed to connect to trade account {server_num} with password={password}, error code =",mt5.last_error())
mt5.shutdown()
#------------------------------------------------------------------------------
def trading_bot():
symbol_1 = 'EURUSD'
symbol_2 = 'EURCAD'
while True:
program(symbol_1)
program(symbol_2)
time.sleep(59.8) # it depends on your computer and ping
sync_60sec(trading_bot)

在这里,您拥有如何使用Python和MT5连接和操作的基础知识。您可以将其另存为 py 文件。您有第一个脚本在 1 分钟图表中为您的交易品种寻找信号。您可以使用两个不同的脚本在 5 分钟和 15 分钟图表(program_5min.py 和 program_15min.py(中查找信号。然后,您应该添加新的同步功能。例如,对于 5 分钟,您必须等待一小时和 0,5,10,15 分钟,依此类推。

希望它对你有用,玩得开心!

相关内容

  • 没有找到相关文章

最新更新