Lu-Yi-Hsun / iqoptionapi

IQ Option API
372 stars 289 forks source link

[FOREX] How to Get time range of candles? #88

Open rrfaria opened 5 years ago

rrfaria commented 5 years ago

Hi @Lu-Yi-Hsun How can I get an range of clandles? I want get candles from 2019-01-01 to today I've followed the example but it doesn't get specific time, I just use range to loop for 1000 candles

Is there any other option?

import time
import os
import json
import datetime as dt
from iqoptionapi.stable_api import IQ_Option
from argparse import ArgumentParser
import userdata

parser = ArgumentParser()
parser.add_argument("-a", "--asset", dest="asset", required=True,
                    help="Pass the asset:(EURUSD, AUDUSD, XEMUSD-l ...)", metavar="ASSET")

parser.add_argument("-i", "--interval", dest="interval", required=False,
                    help="Pass interval in seconds: [1,5,10,15,30,60,120,300,600,900,1800,3600,7200,14400,28800,43200,86400,604800,2592000,'all']", metavar="TIME")

parser.add_argument("-f", "--file-sufix", dest="filesufix", required=False,
                    help="Pass sufix to file ", metavar="SUFIX")

args = parser.parse_args()

asset = args.asset or "USDJPY"
interval = args.interval or 60
sufix = args.filesufix or ""

## AUTH
user = userdata.mainUser
Iq= IQ_Option(user["username"],user["password"])

end_from_time=time.time()
ANS=[]

for i in range(100):
    candles=[]
    candles=Iq.get_candles(asset, interval, 1000, end_from_time)
    cds = []
    for candle in candles:
        cd_handle = {}
        if candle["open"]:
            cd_handle['open'] = candle["open"]
            cd_handle['high'] = candle["max"]
            cd_handle['low'] = candle["min"]
            cd_handle['close'] = candle["close"]
            cd_handle['created_at'] = dt.datetime.fromtimestamp(candle["from"]).isoformat()
            cd_handle['timestamp'] = candle["from"]
            cd_handle['volume'] = candle["volume"]
            cds.append(cd_handle)

    ANS =cds+ANS

    end_from_time=int(cds[0]["timestamp"])-1

try:
    with open(asset + sufix + '.json', 'w') as json_file:
        json.dump(ANS, json_file)
except IOError:
    print("I/O error")
Lu-Yi-Hsun commented 5 years ago

What is specific time mean?

You can not get candles from 2019-1-1 to now?

rrfaria commented 5 years ago

I mean: I would like to get candles like this:

asset="USDJPY"
interval=60
from_date="2019-01-01"
to_date="2019-05-01"

Iq.get_candles(asset, interval, from_date, to_date)

instead of use candles amount. Now we must to set asset, interval, candlesAmount and date(to get data before it)

I would like to get all candles from this time range not some candles by amount