Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required keys not found in candle data. #344

Open
vishalmewada86 opened this issue Jun 23, 2023 · 0 comments
Open

Required keys not found in candle data. #344

vishalmewada86 opened this issue Jun 23, 2023 · 0 comments

Comments

@vishalmewada86
Copy link

Hi Anyone can help me to correct this script, I have made this script with the help of ChatGPT,

  1. Logic are when Parabolic SAR appear on Bottom of Second candle it should buy CALL trade for Binary for 1 min
  2. same goes for sell when Parabolic SAR appear on Top of Second candle it should buy PUT trade for Binary 1 min
  3. every thing is working fine in this script connection balance print login and all. after connection I am facing "Required keys not found in candle data." this message.
  4. Here is the Script.

from iqoptionapi.stable_api import IQ_Option
import time

Create an instance of IQ Option API

api = IQ_Option("mail", "password")

Select account type (PRACTICE or REAL)

account_type = "PRACTICE" # Change to "REAL" for real account

Connect to the IQ Option account

api.connect()

Check if the API connection is successful

if api.check_connect():
print("Connected to IQ Option")

# Set balance type based on selected account type
if account_type == "PRACTICE":
    api.change_balance("PRACTICE")
    print("Trading in PRACTICE account")
elif account_type == "REAL":
    api.change_balance("REAL")
    print("Trading in REAL account")
else:
    print("Invalid account type selected. Please choose either PRACTICE or REAL.")
    api.close_connect()
    exit()

# Print account balance
balance = api.get_balance()
print("Account Balance:", balance)

Define the candle interval and expiration time

candle_interval = 60 # 1 minute
expiration_time = 1 # 1 minute

Define the Parabolic SAR parameters

parabolic_sar_step = 0.02
parabolic_sar_maximum = 0.2

Define the trade amount

trade_amount = 1 # Change this to the desired trade amount

Define the trading pair

ACTIVES = "EURUSD-OTC" # Replace with your desired trading pair

Start streaming candles

max_candles = 100 # Set the maximum number of candles to receive
api.start_candles_stream(ACTIVES, candle_interval, maxdict=max_candles)

Function to calculate Parabolic SAR values

def calculate_parabolic_sar(candles, step, maximum):
sar_values = []
previous_sar = 0
previous_ep = 0
af = step
uptrend = True

for i in range(len(candles)):
    if i == 0:
        sar_values.append(candles[i]["low"])
        continue

    if uptrend:
        if candles[i]["low"] < previous_ep:
            uptrend = False
            sar_values.append(previous_ep)
            af = step
            ep = candles[i]["high"]
            continue
        sar = previous_sar + af * (previous_ep - previous_sar)
        sar_values.append(sar)

        if candles[i]["high"] > previous_ep:
            af += step
            if af > maximum:
                af = maximum
            ep = candles[i]["high"]
        else:
            ep = previous_ep
    else:
        if candles[i]["high"] > previous_ep:
            uptrend = True
            sar_values.append(previous_ep)
            af = step
            ep = candles[i]["low"]
            continue
        sar = previous_sar + af * (previous_ep - previous_sar)
        sar_values.append(sar)

        if candles[i]["low"] < previous_ep:
            af += step
            if af > maximum:
                af = maximum
            ep = candles[i]["low"]
        else:
            ep = previous_ep

    previous_sar = sar
    previous_ep = ep

return sar_values

Start the trading loop

while True:
# Get the current candles
candles = api.get_realtime_candles(ACTIVES, candle_interval)

# Check if new candles are available
if candles:
    # Check if the required number of candles is available for the time frame
    if len(candles) >= 5:
        current_candle = candles[-1]
        previous_candle = candles[-2]

        # Check if the required keys are present in the candle data
        if "low" in current_candle and "low" in previous_candle:
            # Calculate Parabolic SAR values
            parabolic_sar_values = calculate_parabolic_sar(candles, parabolic_sar_step, parabolic_sar_maximum)
            current_sar = parabolic_sar_values[-1]
            previous_sar = parabolic_sar_values[-2]

            # Check buy conditions
            if current_sar < current_candle["low"] and previous_sar < previous_candle["low"]:
                print("Placing BUY order")
                api.buy(ACTIVES, trade_amount, "call", expiration_time)

            # Check sell conditions
            if current_sar > current_candle["high"] and previous_sar > previous_candle["high"]:
                print("Placing SELL order")
                api.buy(ACTIVES, trade_amount, "put", expiration_time)
        else:
            print("Required keys not found in candle data.")
    else:
        print("Insufficient number of candles for the specified time frame.")

# Wait for some time before checking for new candles
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant