0

I am trying to convert candlesticks into pips however i am getting a strange output, the code is below and following output:

import MetaTrader5 as mt5
import pandas as pd
from creds import user, password
from sigfig import round

def convert(open, high, low, close):
    conversionArray = [open, high, low, close]

    candlestickData = [0,0,0,0]

    # Conversion for green candle
    if conversionArray[3] > conversionArray[0]:
        candleType = 1
        wicksUp = conversionArray[1] - conversionArray[3]
        wicksDown = conversionArray[2] - conversionArray[0]
        bodySize = conversionArray[3] - conversionArray[0]

    # Conversion for red candle
    else:
        candleType = 0
        wicksUp = conversionArray[1] - conversionArray[0]
        wicksDown = conversionArray[2] - conversionArray[3]
        bodySize = conversionArray[1] - conversionArray[3]

    if wicksUp < 0:
        wicksUp = wicksUp * (-1)
    if wicksDown < 0:
        wicksDown = wicksDown * (-1)
    if bodySize < 0:
        bodySize = bodySize * (-1)

    candlestickData[0] = candleType
    candlestickData[1] = round(wicksUp, sigfig = 2)
    candlestickData[2] = round(wicksDown, sigfig = 2)
    candlestickData[3] = round(bodySize, sigfig = 2)

    print(conversionArray)
    print(candlestickData)


if not mt5.initialize(login=user, password=password, server="FTMO-Demo"):
    print("initialize() failed, error code =", mt5.last_error())
    quit()

symbols = mt5.symbols_get()

for s in symbols:
    rates = mt5.copy_rates_from_pos(s.name, mt5.TIMEFRAME_M15, 0, 100)
    ticker = pd.DataFrame(rates)
    ticker['time'] = pd.to_datetime(ticker['time'], unit='s')

    for candle in rates:
        convert(candle['open'], candle['high'], candle['low'], candle['close'])

I am unsure why im getting such long decimal numbers. This is just a section of the output but pretty much all of the numbers are wrong with simple subtraction??

[0.97355, 0.97372, 0.97202, 0.97211]
[0, 0.00017000000000000348, 9.00000000000345e-05, 0.0016100000000000003]
[0.9721, 0.97241, 0.97092, 0.97183]
[0, 0.00031000000000003247, 0.0009099999999999664, 0.0005800000000000249]
[0.97185, 0.97269, 0.97152, 0.97163]
[0, 0.0008400000000000629, 0.00010999999999994348, 0.001060000000000061]
[0.97163, 0.97274, 0.97146, 0.97221]
[1, 0.0005300000000000304, 0.00017000000000000348, 0.0005800000000000249]
[0.97209, 0.97355, 0.97166, 0.97351]
[1, 4.0000000000040004e-05, 0.00043000000000004146, 0.0014199999999999768]
[0.97352, 0.97768, 0.97324, 0.9773]
[1, 0.00038000000000004697, 0.000280000000000058, 0.0037799999999998946]
[0.97731, 0.97998, 0.97671, 0.97906]
[1, 0.0009199999999999209, 0.0006000000000000449, 0.0017500000000000293]
[0.97904, 0.97917, 0.97482, 0.97495]
[0, 0.00012999999999996348, 0.00012999999999996348, 0.0042200000000000015]
[0.97498, 0.97593, 0.97325, 0.97386]
[0, 0.0009500000000000064, 0.0006099999999999994, 0.0020700000000000163]
[0.97385, 0.97477, 0.97302, 0.97363]
[0, 0.0009200000000000319, 0.0006099999999999994, 0.0011400000000000299]
[0.97193, 0.97368, 0.9707, 0.9723]
[1, 0.0013799999999999368, 0.0012299999999999534, 0.0003700000000000925]
[0.97231, 0.97445, 0.97188, 0.974]
[1, 0.00045000000000006146, 0.00043000000000004146, 0.0016899999999999693]
[0.97399, 0.97512, 0.97326, 0.97348]
[0, 0.0011299999999999644, 0.00021999999999999797, 0.0016399999999999748]
[0.9735, 0.97541, 0.97304, 0.97536]
[1, 4.999999999999449e-05, 0.00046000000000001595, 0.0018599999999999728]

for the first output it should be:

0.97372 - 0.97211 = 0.00161
0.97202 - 0.97355 = 0.00153
0.97211 - 0.97355 = 0.00144

not whatever I got in that output. Any ideas?

  • for your first example `0.97372 - 0.97211` python gives `0016100000000000003` which is extremely close to what you expect. you might consider the [`decimal`](https://docs.python.org/3/library/decimal.html#module-decimal) module if you want things to be exact up to a certain decimal value. or you might consider formatting your output up to the decimal place you need. – hiro protagonist Oct 17 '22 at 12:37
  • It is a known issue, check [Python floating documentation](https://docs.python.org/3/tutorial/floatingpoint.html#tut-fp-issues). As @hiroprotagonist said, you can use [Decimal](https://docs.python.org/3/library/decimal.html#module-decimal) module – ErnestBidouille Oct 17 '22 at 12:41

0 Answers0