Files
aiomql/docs/lib/candle.md
T

206 lines
9.3 KiB
Markdown
Raw Normal View History

2024-02-12 21:09:28 +01:00
# Candle and Candles
2023-10-11 09:49:06 +01:00
Candle and Candles classes for handling bars from the MetaTrader 5 terminal.
2024-02-12 21:09:28 +01:00
## Table of Contents
2024-11-10 12:25:50 +01:00
- [Candle](#candle.candle)
2024-02-12 21:09:28 +01:00
- [\_\_init\_\_](#candle.__init__)
- [set_attributes](#candle.set_attributes)
- [is_bullish](#candle.is_bullish)
- [is_bearish](#candle.is_bearish)
2024-11-10 12:25:50 +01:00
- [dict](#candle.dict)
- [Candles](#candles.candles)
2024-02-12 21:09:28 +01:00
- [\_\_init\_\_](#candles.__init__)
- [ta](#candles.ta)
- [ta_lib](#candles.ta_lib)
- [data](#candles.data)
- [rename](#candles.rename)
2024-11-10 12:25:50 +01:00
<a id="candle.candle"></a>
2024-02-12 21:09:28 +01:00
### Candle
2023-10-11 09:49:06 +01:00
```python
class Candle
```
A class representing bars from the MetaTrader 5 terminal as a customized class analogous to Japanese Candlesticks.
You can subclass this class for added customization.
2023-10-16 15:36:31 +01:00
### Attributes
2024-02-12 21:09:28 +01:00
| Name | Type | Description |
|---------------|---------|-------------------------------------------------------------------------|
| `time` | `int` | Period start time |
| `open` | `int` | Open price |
| `high` | `float` | The highest price of the period |
| `low` | `float` | The lowest price of the period |
| `close` | `float` | Close price |
| `tick_volume` | `float` | Tick volume |
| `real_volume` | `float` | Trade volume |
| `spread` | `float` | Spread |
| `Index` | `int` | Custom attribute representing the position of the candle in a sequence. |
<a id='candle.__init__'></a>
2023-10-11 09:49:06 +01:00
### \_\_init\_\_
```python
def __init__(**kwargs)
```
2024-02-12 21:09:28 +01:00
Create a Candle object from keyword arguments. Kwargs are set as instance attributes. Open, high, low, close must be
provided during each instantiation.
#### Parameters:
| Name | Type | Description |
|----------|-------|----------------------------------------------------|
| `kwargs` | `Any` | Candle attributes and values as keyword arguments. |
#### Raises:
| Exception | Description |
|--------------|-----------------------------------------------|
| `ValueError` | If open, high, low, or close is not provided. |
<a id="candle.set_attributes"></a>
2023-10-11 09:49:06 +01:00
### set\_attributes
```python
def set_attributes(**kwargs)
```
Set keyword arguments as instance attributes
2024-02-12 21:09:28 +01:00
#### Parameters:
| Name | Type | Description |
|----------|-------|----------------------------------------------------|
| `kwargs` | `Any` | Candle attributes and values as keyword arguments. |
2023-10-11 09:49:06 +01:00
2024-02-12 21:09:28 +01:00
<a id="candle.is_bullish"></a>
2023-10-11 09:49:06 +01:00
### is_bullish
```python
def is_bullish() -> bool
```
A simple check to see if the candle is bullish.
2023-10-16 15:36:31 +01:00
#### Returns:
2024-02-12 21:09:28 +01:00
| Type | Description |
|--------|---------------|
| `bool` | True or False |
2023-10-11 09:49:06 +01:00
2024-02-12 21:09:28 +01:00
<a id="candle.is_bearish"></a>
2023-10-11 09:49:06 +01:00
### is_bearish
```python
def is_bearish() -> bool
```
A simple check to see if the candle is bearish.
2023-10-16 15:36:31 +01:00
#### Returns:
2024-02-12 21:09:28 +01:00
| Type | Description |
|------|---------------|
| bool | True or False |
2024-05-05 00:08:57 +01:00
<a id="candle.dict"></a>
### dict
```python
def dict(self, exclude: set = None, include: set = None) -> Dict[str, Any]
```
Return a dictionary representation of the Candle object.
#### Parameters:
2024-11-10 12:25:50 +01:00
| Name | Type | Description |
|-----------|------------|-----------------------------------------------------|
| `exclude` | `set[str]` | A set of attributes to exclude from the dictionary. |
| `include` | `set[str]` | A set of attributes to include in the dictionary. |
2024-05-05 00:08:57 +01:00
#### Returns:
2024-11-10 12:25:50 +01:00
| Type | Description |
|------------------|---------------------------------------------------|
| `Dict[str, Any]` | A dictionary representation of the Candle object. |
2024-05-05 00:08:57 +01:00
2023-10-11 09:49:06 +01:00
2024-11-10 12:25:50 +01:00
### <a id="candles.candles"></a> Candles
2023-10-11 09:49:06 +01:00
```python
class Candles(Generic[_Candle])
```
2024-02-12 21:09:28 +01:00
An iterable container class of Candle objects in chronological order. It is in a way a wrapper around a Pandas DataFrame
object. All the data pulled from the chart is stored as a pandas DataFrame object. In an attribute called **data**.
This class can be sliced, iterated over, and indexed like a sequence. It also has access to the pandas_ta library.
Indexing it returns a Candle object. It can be sliced to return a new instance of the class with the sliced candles.
This slices and resets the index of the underlying dataframe object. Key based indexing is also supported on the candles
2024-11-10 12:25:50 +01:00
object for accessing the columns of the underlying data attribute.
2024-02-12 21:09:28 +01:00
2024-11-10 12:25:50 +01:00
### Attributes:
2024-02-12 21:09:28 +01:00
The attributes of this class vary depending on the columns of underlying **data** attribute. i.e. each column of the **data**
attribute is an attribute of the class.
| Name | Type | Description |
|---------------|-----------------|-------------------------------------------------------------------|
| `data` | `DataFrame` | The pandas DataFrame containing the data. |
| `Index` | `Series['int']` | A pandas Series of the indexes of all candles in the object |
| `time` | `Series['int']` | A pandas Series of the time of all candles in the object |
| `open` | `Series[float]` | A pandas Series of the opening price of all candles in the object |
| `high` | `Series[float]` | A pandas Series of the high price of all candles in the object |
| `low` | `Series[float]` | A pandas Series of the low price of all candles in the object |
| `close` | `Series[float]` | A pandas Series of the closing price of all candles in the object |
| `tick_volume` | `Series[float]` | A pandas Series of the tick volume of all candles in the object |
| `real_volume` | `Series[float]` | A pandas Series of the real volume of all candles in the object |
| `spread` | `Series[float]` | A pandas Series of the spread of all candles in the object |
| `timeframe` | `TimeFrame` | The timeframe of the candles in the object |
| `Candle` | `Type[Candle]` | The Candle class for representing the candles in the object. |
| `data` | `DataFrame` | A pandas DataFrame of all candles in the object. |
#### Notes
When subclassing this class make sure the Candle attribute is set to your desired candle class.
<a id="candles.__init__"></a>
### \_\_init\_\_
2023-10-11 09:49:06 +01:00
```python
def __init__(*,
data: DataFrame | _Candles | Iterable,
flip=False,
candle_class: Type[_Candle] = None)
```
A container class of Candle objects in chronological order.
2024-02-12 21:09:28 +01:00
#### Parameters:
| Name | Type | Description | Default |
|----------------|----------------------------------------|---------------------------------------------------------------------|---------|
| `data` | `DataFrame` or `Candles` or `Iterable` | A pandas dataframe, a Candles object or any suitable iterable |
| `flip` | `bool` | Reverse the chronological order of the candles to the oldest first. | False |
| `candle_class` | `Type[Candle]` | A subclass of Candle to use as the candle class. | Candle |
<a id="candles.ta"></a>
### ta
2023-10-11 09:49:06 +01:00
```python
@property
def ta()
```
2024-02-12 21:09:28 +01:00
Access to the pandas_ta library for performing technical analysis on the underlying data attribute. Use this as you
would use the pandas_ta library on a pandas DataFrame. For inplace operations. The underlying data attribute is modified.
2023-10-16 15:36:31 +01:00
#### Returns:
2024-02-12 21:09:28 +01:00
| Type | Description |
|-------------|-----------------------|
| `pandas_ta` | The pandas_ta library |
2023-10-11 09:49:06 +01:00
2024-02-12 21:09:28 +01:00
<a id="candles.ta_lib"></a>
### ta\_lib
2023-10-11 09:49:06 +01:00
```python
@property
def ta_lib()
```
Access to the ta library for performing technical analysis. Not dependent on the underlying data attribute. Use this for
functions that require pandas Series as input.
2023-10-16 15:36:31 +01:00
#### Returns:
2024-02-12 21:09:28 +01:00
| Type | Description |
|------|----------------|
| ta | The ta library |
2023-10-11 09:49:06 +01:00
2024-02-12 21:09:28 +01:00
<a id="candles.data"></a>
### data
2023-10-11 09:49:06 +01:00
```python
@property
def data() -> DataFrame
```
A pandas DataFrame of all candles in the object.
2024-02-12 21:09:28 +01:00
<a id="candles.rename"></a>
### rename
2023-10-11 09:49:06 +01:00
```python
def rename(inplace=True, **kwargs) -> _Candles | None
```
2024-02-12 21:09:28 +01:00
Rename columns of the data object.
#### Parameters:
| Name | Type | Description | Default |
|-----------|--------|-------------------------------------------------------------------------------------------|---------|
| `inplace` | `bool` | Rename the columns inplace or return a new instance of the class with the renamed columns | True |
| `kwargs` | `str` | The new names of the columns | |
2023-10-11 09:49:06 +01:00
2023-10-16 15:36:31 +01:00
#### Returns:
2024-02-12 21:09:28 +01:00
| Type | Description |
|-----------|---------------------------------------------------------------------------|
| `Candles` | A new instance of the class with the renamed columns if inplace is False. |