Last active
April 28, 2024 22:38
-
-
Save Ichinga-Samuel/9cae385101f3898b4b686a9c7de0c0a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from aiomql import Symbol, TimeFrame, Account | |
async def main(): | |
async with Account(): | |
# create a symbol | |
sym = Symbol(name="EURUSD") | |
# initialize the symbol | |
res = await sym.init() | |
if not res: | |
print('Symbol not available') | |
return | |
# Get BTC price bars for the past 48 hours | |
candles = await sym.copy_rates_from_pos(timeframe=TimeFrame.H1, count=48, start_position=0) | |
print(len(candles)) # 48 | |
# get the latest candle by accessing the last one. | |
# Explore the candle object | |
last = candles[-1] # A Candle object | |
print(last) | |
# print only ohlc | |
print(last.dict(include={'open', 'high', 'low', 'close'})) | |
# get the last five hours | |
last_five = candles[-5:] # A Candles object. | |
# confirm that slicing returns a Candles object | |
print(type(last_five)) | |
print(last_five) | |
# get the close price of all the candles | |
close = candles['close'] # close price of all the candles as a pandas series | |
print(type(close)) | |
print(close) | |
# compute ema using pandas ta | |
candles.ta.ema(length=34, append=True, fillna=0) | |
# rename the column to ema | |
candles.rename(EMA_34='ema') | |
# use talib to compute crossover. This returns a series object that is not part of the candles object. | |
closeXema = candles.ta_lib.cross(candles.close, candles.ema) | |
# add to the candles | |
candles['closeXema'] = closeXema | |
print(candles) | |
# iterate over the first 5 candles | |
for candle in candles[:5]: | |
print(candle.open, candle.Index) | |
# visualize the candles | |
candles.visualize(count=20) | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment