173 KiB
173 KiB
In [1]:
import pandas as pd
import pandas_ta as ta
import MetaTrader5 as mt
import keyring as krIn [2]:
# login to your Trading Account - sign up in the description
mt.initialize()
login = 10800246
server = 'VantageInternational-Demo'
password = kr.get_password(server, str(login))
mt.login(login, password, server)Out [2]:
True
In [3]:
project = "trading-" + server[-4::1]
project = project.lower()
projectOut [3]:
'trading-demo'
In [4]:
symbols = ['XAUUSD']In [5]:
periods_dict = {
'BTCUSD' : ['h1', 'm30', 'm15', 'm5', 'm1'],
'ETHUSD' : ['h1', 'm30', 'm15', 'm5', 'm1'],
'XRPUSD': ['h1', 'm30', 'm15', 'm5', 'm1'],
'XAUUSD': [ 'm5'],
'EURUSD': ['h1', 'm30', 'm15', 'm5', 'm1'],
'EURNZD': ['m5', 'm2', 'm1'],
}In [6]:
symbol = 'XAUUSD'In [ ]:
In [107]:
def get_rates():
ohlc = mt.copy_rates_from_pos(symbol, mt.TIMEFRAME_M5, 0, 200)
df = pd.DataFrame(ohlc)
df['time']=pd.to_datetime(df['time'], unit='s')
#df = df[["time","open","high","low","close"]]
df["open"] = df.open.astype(float)
df["high"] = df.high.astype(float)
df["low"] = df.low.astype(float)
df["close"] = df.close.astype(float)
## Take the rolling atr so the yaxis doesn't shake too much
df["atr"] = ta.atr(high=df.high, low=df.low, close=df.close)
df["atr"] = df.atr.rolling(window=30).mean()
df.set_index("time", inplace = True)
return dfIn [10]:
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from scipy.signal import savgol_filter
from scipy.signal import find_peaks
from IPython import display
from IPython.display import HTML
pd.set_option('mode.chained_assignment', None)In [20]:
#df2 = df.iloc[0:500]
df2 = df
fig, ax = plt.subplots()
plt.xticks(rotation=-30)
price, = ax.plot(df2.index, df2.close, c='grey', lw=2, alpha=0.5, zorder=5)
plt.show()In [15]:
#df2 = df.iloc[0:500]
df2 = df
df2["close_smooth"] = savgol_filter(df2.close, 49, 5)
fig, ax = plt.subplots()
plt.xticks(rotation=-30)
price, = ax.plot(df2.index, df2.close, c='grey', lw=2, alpha=0.5, zorder=5)
price_smooth, = ax.plot(df2.index, df2.close_smooth, c='b', lw=2, zorder=5)
plt.show()In [112]:
#df2 = df.iloc[0:500]
df2 = get_rates()
df2["close_smooth"] = savgol_filter(df2.close, 49, 5)
fig, ax = plt.subplots()
plt.xticks(rotation=-30)
price, = ax.plot(df2.index, df2.close, c='grey', lw=2, alpha=0.5, zorder=5)
price_smooth, = ax.plot(df2.index, df2.close_smooth, c='b', lw=2, zorder=5)
atr = df2.atr.iloc[-1] # all the first atrs are NaN
peaks_idx, _ = find_peaks(df2.close_smooth, distance = 15,
width = 3, prominence=atr)
troughs_idx, _ = find_peaks(-1*df2.close_smooth, distance = 15,
width = 3, prominence=atr)
peaks, = ax.plot(df2.index[peaks_idx], df2.close_smooth.iloc[peaks_idx], \
c="r", linestyle='None', markersize = 10.0, marker = "o", zorder=10)
troughs, = ax.plot(df2.index[troughs_idx], df2.close_smooth.iloc[troughs_idx], \
c="g", linestyle='None', markersize = 10.0, marker = "o", zorder=10)
plt.show()
#print(peaks_idx[-1], troughs_idx[-1])
if peaks_idx[-1] > troughs_idx[-1]:
print("downtrend")
else:
print("uptrend")downtrend
In [89]:
df2 = df.iloc[0:100]
#df2 = df.tail(50)
df2["close_smooth"] = savgol_filter(df2.close, 49, 5)
fig, ax = plt.subplots()
plt.xticks(rotation=-30)
plt.xticks(rotation=-30)
price, = ax.plot(df2.index, df2.close, c='grey', lw=2, alpha=0.5, zorder=5)
price_smooth, = ax.plot(df2.index, df2.close_smooth, c='b', lw=2, zorder=5)
atr = df2.atr.iloc[-1] # all the first atrs are NaN
peaks_idx, _ = find_peaks(df2.close_smooth, distance = 15,
width = 3, prominence=atr)
troughs_idx, _ = find_peaks(-1*df2.close_smooth, distance = 15,
width = 3, prominence=atr)
peaks, = ax.plot(df2.index[peaks_idx], df2.close_smooth.iloc[peaks_idx], \
c="r", linestyle='None', markersize = 10.0, marker = "o", zorder=10)
troughs, = ax.plot(df2.index[troughs_idx], df2.close_smooth.iloc[troughs_idx], \
c="g", linestyle='None', markersize = 10.0, marker = "o", zorder=10)
print(peaks_idx, troughs_idx, peaks, troughs)
up_run_length = 0
up_run = True
down_run_length = 0
while up_run:
if 2 + up_run_length > len(peaks_idx) or 2 + up_run_length > len(troughs_idx):
break
if df2.close_smooth.iloc[peaks_idx[-1 - up_run_length]] > df2.close_smooth.iloc[peaks_idx[-2 - up_run_length]] and \
df2.close_smooth.iloc[troughs_idx[-1 - up_run_length]] > df2.close_smooth.iloc[troughs_idx[-2 - up_run_length]]:
up_run_length += 1
else:
up_run = False
# down_run_length = 0
# down_run = True
# while down_run:
# if 2 + down_run_length > len(peaks_idx) or 2 + down_run_length > len(troughs_idx):
# break
# if df2.close_smooth.iloc[peaks_idx[-1 - down_run_length]] < df2.close_smooth.iloc[peaks_idx[-2 - down_run_length]] and \
# df2.close_smooth.iloc[troughs_idx[-1 - down_run_length]] < + df2.close_smooth.iloc[troughs_idx[-2 - down_run_length]]:
# down_run_length += 1
# else:
# down_run = False
if up_run_length > 0:
ax.set_facecolor((150/255, 255/255, 159/255, 0.3))
pause_trading = 0
else:# down_run_length > 0:
ax.set_facecolor((255/255, 255/255, 80/255, 0.3))
pause_trading = 1
print(pause_trading, up_run_length)
plt.show()[21] [85] Line2D(_child2) Line2D(_child3) 1 0
In [55]:
#if df2.close_smooth.iloc[peaks_idx[-1 - down_run_length]] < df2.close_smooth.iloc[peaks_idx[-2 - down_run_length]] :
#\
if df2.close_smooth.iloc[troughs_idx[-1 - down_run_length]] < df2.close_smooth.iloc[troughs_idx[-1 - down_run_length]]:
print(True)In [ ]:
!pip install ffmpeg-python In [ ]:
In [31]:
import matplotlib
matplotlib.rcParams['animation.embed_limit'] = 2**128
bars_in_frame = 300
fig, ax = plt.subplots()
# increase video quality
#fig, ax = plt.subplots(figsize=(8,8), dpi=300)
price, = ax.plot([], c='grey', lw=3, alpha=0.5, zorder=5)
price_smooth, = ax.plot([], c='b', lw=5, zorder=5)
peaks, = ax.plot([], c="r", linestyle='None', markersize = 15.0, marker = "o", zorder=10)
troughs, = ax.plot([], c="g", linestyle='None', markersize = 15.0, marker = "o", zorder=10)
## Might turn the yaxis off, can be annoying
ax.set_ylim(15000,20000)
ax.set_xlim(0,bars_in_frame)
def animate(frame):
frames_behind = 30
df2 = df.iloc[-len(df) + frame + 30:frame+bars_in_frame + 30]
df2["close_smooth"] = savgol_filter(df2.close, 49, 5)
df2 = df2.iloc[frames_behind:]
x_coords = [x for x in range(len(df2.close_smooth))]
price.set_data((x_coords, df2.close))
price_smooth.set_data((x_coords, df2.close_smooth))
first_atr = df2.atr.iloc[0]
ax.set_ylim(df2.close_smooth.min() - 10*first_atr, df2.close_smooth.max() + 10*first_atr)
peaks_idx, _ = find_peaks(df2.close_smooth, distance = 15,
width = 3, prominence=first_atr)
troughs_idx, _ = find_peaks(-1*df2.close_smooth, distance = 15,
width = 3, prominence=first_atr)
up_run_length = 0
up_run = True
while up_run:
if 2 + up_run_length > len(peaks_idx) or 2 + up_run_length > len(troughs_idx):
break
if df2.close_smooth.iloc[peaks_idx[-1 - up_run_length]] > df2.close_smooth.iloc[peaks_idx[-2 - up_run_length]] and \
df2.close_smooth.iloc[troughs_idx[-1 - up_run_length]] > df2.close_smooth.iloc[troughs_idx[-2 - up_run_length]]:
up_run_length += 1
else:
up_run = False
down_run_length = 0
down_run = True
while down_run:
if 2 + down_run_length > len(peaks_idx) or 2 + down_run_length > len(troughs_idx):
break
if df2.close_smooth.iloc[peaks_idx[-1 - down_run_length]] < df2.close_smooth.iloc[peaks_idx[-2 - down_run_length]] and \
df2.close_smooth.iloc[troughs_idx[-1 - down_run_length]] < + df2.close_smooth.iloc[troughs_idx[-2 - down_run_length]]:
down_run_length += 1
else:
down_run = False
peaks.set_data((peaks_idx, df2.close_smooth.iloc[peaks_idx]))
troughs.set_data((troughs_idx, df2.close_smooth.iloc[troughs_idx]))
if up_run_length > 0:
ax.set_facecolor((150/255, 255/255, 159/255, 0.3))
elif down_run_length > 0:
ax.set_facecolor((255/255, 150/255, 150/255, 0.3))
else:
ax.set_facecolor("white")
return price, price_smooth, peaks, troughs
anim = FuncAnimation(fig, animate, frames=len(df)-bars_in_frame, interval=40, blit=True)
video = HTML(anim.to_html5_video())
display.display(video)c:\ProgramData\anaconda3\Lib\site-packages\matplotlib\animation.py:1740: UserWarning: Can not start iterating the frames for the initial draw. This can be caused by passing in a 0 length sequence for *frames*. If you passed *frames* as a generator it may be exhausted due to a previous display or save. warnings.warn(
[1;31m---------------------------------------------------------------------------[0m [1;31mRuntimeError[0m Traceback (most recent call last) Cell [1;32mIn[31], line 85[0m [0;32m 81[0m [38;5;28;01mreturn[39;00m price, price_smooth, peaks, troughs [0;32m 84[0m anim [38;5;241m=[39m FuncAnimation(fig, animate, frames[38;5;241m=[39m[38;5;28mlen[39m(df)[38;5;241m-[39mbars_in_frame, interval[38;5;241m=[39m[38;5;241m40[39m, blit[38;5;241m=[39m[38;5;28;01mTrue[39;00m) [1;32m---> 85[0m video [38;5;241m=[39m HTML(anim[38;5;241m.[39mto_html5_video()) [0;32m 86[0m display[38;5;241m.[39mdisplay(video) File [1;32mc:\ProgramData\anaconda3\Lib\site-packages\matplotlib\animation.py:1284[0m, in [0;36mAnimation.to_html5_video[1;34m(self, embed_limit)[0m [0;32m 1281[0m path [38;5;241m=[39m Path(tmpdir, [38;5;124m"[39m[38;5;124mtemp.m4v[39m[38;5;124m"[39m) [0;32m 1282[0m [38;5;66;03m# We create a writer manually so that we can get the[39;00m [0;32m 1283[0m [38;5;66;03m# appropriate size for the tag[39;00m [1;32m-> 1284[0m Writer [38;5;241m=[39m writers[mpl[38;5;241m.[39mrcParams[[38;5;124m'[39m[38;5;124manimation.writer[39m[38;5;124m'[39m]] [0;32m 1285[0m writer [38;5;241m=[39m Writer(codec[38;5;241m=[39m[38;5;124m'[39m[38;5;124mh264[39m[38;5;124m'[39m, [0;32m 1286[0m bitrate[38;5;241m=[39mmpl[38;5;241m.[39mrcParams[[38;5;124m'[39m[38;5;124manimation.bitrate[39m[38;5;124m'[39m], [0;32m 1287[0m fps[38;5;241m=[39m[38;5;241m1000.[39m [38;5;241m/[39m [38;5;28mself[39m[38;5;241m.[39m_interval) [0;32m 1288[0m [38;5;28mself[39m[38;5;241m.[39msave([38;5;28mstr[39m(path), writer[38;5;241m=[39mwriter) File [1;32mc:\ProgramData\anaconda3\Lib\site-packages\matplotlib\animation.py:148[0m, in [0;36mMovieWriterRegistry.__getitem__[1;34m(self, name)[0m [0;32m 146[0m [38;5;28;01mif[39;00m [38;5;28mself[39m[38;5;241m.[39mis_available(name): [0;32m 147[0m [38;5;28;01mreturn[39;00m [38;5;28mself[39m[38;5;241m.[39m_registered[name] [1;32m--> 148[0m [38;5;28;01mraise[39;00m [38;5;167;01mRuntimeError[39;00m([38;5;124mf[39m[38;5;124m"[39m[38;5;124mRequested MovieWriter ([39m[38;5;132;01m{[39;00mname[38;5;132;01m}[39;00m[38;5;124m) not available[39m[38;5;124m"[39m) [1;31mRuntimeError[0m: Requested MovieWriter (ffmpeg) not available
In [ ]:
with open('video-hd.html', 'w') as f:
f.write(video.data)In [ ]: