Files
aiomql/examples/candles.py
T

52 lines
1.8 KiB
Python
Raw Normal View History

2023-10-11 09:49:06 +01:00
import asyncio
2024-01-18 02:26:27 +01:00
from aiomql import Symbol, TimeFrame, Account, Candle, Candles
2023-10-11 09:49:06 +01:00
async def main():
2024-01-18 02:26:27 +01:00
"""Example of using the Candle and Candles classes.
The candle class is a single price bar. Holding the OHLCV data for a single price bar.
The Candles class is a container of Candle objects. It is an Iterable of Candle objects.
It is sliceable and indexable. It can also be accessed with keywords.
It is a wrapper around a pandas DataFrame. Which is what it uses to store the data.
"""
2023-10-11 09:49:06 +01:00
async with Account():
2024-01-18 02:26:27 +01:00
sym = Symbol(name="EURUSD")
2023-10-11 09:49:06 +01:00
# Get EURUSD price bars for the past 48 hours
2024-01-18 02:26:27 +01:00
candles: Candles = await sym.copy_rates_from_pos(timeframe=TimeFrame.H1, count=48, start_position=0)
# get size of candles
2023-10-11 09:49:06 +01:00
print(len(candles)) # 48
# get the latest candle by accessing the last one.
2024-01-18 02:26:27 +01:00
last: Candle = candles[-1] # A Candle object
2023-10-11 09:49:06 +01:00
print(type(last))
2024-01-18 02:26:27 +01:00
print(last.Index)
2023-10-11 09:49:06 +01:00
2024-01-18 02:26:27 +01:00
# slicing returns a Candles object
half = candles[24:]
print(type(half))
print(len(half))
2023-10-11 09:49:06 +01:00
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)
2024-01-18 02:26:27 +01:00
2023-10-11 09:49:06 +01:00
# 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)
asyncio.run(main())