Rezzident21 / correction_by_fibonacci

0 stars 0 forks source link

Correction #1

Open Rezzident21 opened 1 year ago

Rezzident21 commented 1 year ago

import pandas as pd import plotly.graph_objects as go from plotly.subplots import make_subplots from binance.client import Client import os

class FibonacciCorrection:

def __init__(self, api_key, api_secret, symbol):
    self.client = Client(api_key, api_secret)
    self.symbol = symbol

def get_price(self):
    klines = self.client.futures_klines(symbol=self.symbol, interval='15m', limit=100)
    last_price = float(klines[0][4])
    return last_price

def calculate_fib_levels(self, df):
    df['high'] = pd.to_numeric(df['high'])
    df['low'] = pd.to_numeric(df['low'])
    diff = df['high'].max() - df['low'].min()
    levels = [df['high'].max(), df['high'].max() - 0.236 * diff, df['high'].max() - 0.382 * diff,
              df['high'].max() - 0.5 * diff, df['high'].max() - 0.618 * diff, df['high'].max() - 0.786 * diff]
    return levels

def plot_fib_levels(self, levels, df):
    fig = make_subplots(rows=1, cols=1, shared_xaxes=True, vertical_spacing=0.02)
    fig.add_trace(
        go.Candlestick(x=df['timestamp'], open=df['open'], high=df['high'], low=df['low'], close=df['close'],
                       name='Price'), row=1, col=1)
    for level in levels:
        fig.add_shape(type='line', x0=df['timestamp'].iloc[0], y0=level, x1=df['timestamp'].iloc[-1], y1=level,
                      line=dict(color='gray', width=1, dash='dash'), row=1, col=1)
        fig.add_annotation(x=df['timestamp'].iloc[0], y=level, text=f'{level:.2f}', showarrow=False, yshift=5,
                           font=dict(size=8, color='gray'), row=1, col=1)
    fig.update_layout(title=f'{self.symbol} - Fib correction')
    fig.write_image('fib_levels.png', format='png')

    #fig.show()

def run(self):
    klines = self.client.futures_klines(symbol=self.symbol, interval='1h', limit=100)
    df = pd.DataFrame(klines, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume', 'close_time',
                                       'quote_asset_volume', 'number_of_trades', 'taker_buy_base_asset_volume',
                                       'taker_buy_quote_asset_volume', 'ignore'])
    df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
    df = df[['timestamp', 'open', 'high', 'low', 'close']]
    last_price = self.get_price()
    levels = self.calculate_fib_levels(df)
    current_level = None
    for level in levels:
        if last_price <= level:
            current_level = level
            break
    self.plot_fib_levels(levels, df)

    print(f'Current price: {last_price} USD, current Fibonacci level: {current_level} USD')

api_key = '' api_secret = ''

GG = FibonacciCorrection (api_key,api_secret,'TRUUSDT') GG.run()

Rezzident21 commented 1 year ago

fib_levels