0

I have 3 python scripts

  1. which gets the tick data from tradingview and stores in sql
  2. Fetches the data from sql and does aggregation
  3. main - file where i want to run these two parallely
# module1.py

import websocket as wb

sym = 'BINANCE:BTCUSDT'
SOCKET = "wss://data.tradingview.com/socket.io/websocket"
# i have few functions like below 

def on_close(ws)

def on_message(ws, message)

def generateSession()

def generateChartSession()

def prependHeader(st)

def constructMessage(func, paramList)

def createMessage(func, paramList)

def sendRawMessage(ws, message)

def sendMessage(ws, func, args)

session= generateSession()
print("session generated {}".format(session))

chart_session= generateChartSession()
print("chart_session generated {}".format(chart_session))

def on_open(ws)


def main():
    wb.enableTrace(False)
    ws = wb.WebSocketApp(
        SOCKET, on_message=on_message, on_open=on_open, on_close=on_close)
    wst = threading.Thread(target=ws.run_forever)
    # wst = ws.run_forever()
    wst.daemon = True
    wst.start()


    while True:
        try:
            # sleep(1)
            sleep(0.5)
            # schedule.every(5).minutes.at(':00').do(do_something_threaded)
            # schedule.run_pending()
        except KeyboardInterrupt:
            ws.close()
            print('--'*30+'start'+'--'*30)
            DF.df.to_csv(str(datetime.now())+'_'+sym+'_.csv')
            print('--'*30+'end'+'--'*30)
            logging.info('keyborad intrupt')
            break
        if ws.sock is None:
            logging.warning('ws is none')
            ws.close()
            sleep(2)
            logging.info('starting application again')
            ws.keep_running()

The above module runs smoth when i run module1.py output would be some thing like

--------------------
2023/03/25, 11:58:00 27495.66 27500.98 27489.68 27489.68 65.32257
--------------------
2023/03/25, 11:58:00 27495.66 27500.98 27489.68 27489.69 65.84401
--------------------
2023/03/25, 11:58:00 27495.66 27500.98 27489.65 27489.65 68.52807

and module2 contains some agregation functions

# module2.py
import logging
import pandas as pd
from sql_module import fetch_last_frame 

t=5

def frame_aggregate(cursor,t=1):
    logging.info('start aggregating')
    df = pd.DataFrame(cursor.fetchall())
    df.columns = [i[0] for i in cursor.description]
    global df_time
    print(df)
    df_time['time'] = pd.to_datetime(df_time['time'])
    df_time['time'] =   df.resample(f'{t}T').agg({'time': 'first'})['time']
    df_time['open'] =   df.resample(f'{t}T').agg({'open': 'first'})['open']
    df_time['high'] =   df.resample(f'{t}T').agg({'high': 'max'})['high']
    df_time['low'] =    df.resample(f'{t}T').agg({'low': 'min'})['low']
    df_time['close'] =  df.resample(f'{t}T').agg({'close': 'last'})['close']
    # sleep(2)
    print('-'*20, 'data aggregated', '-'*20)
    print(df_time)
    logging.info('end aggregating')
    return df_time


def patters_():
    # print(df.info())
    
    logging.info('in patterns')
    df = frame_aggregate(fetch_last_frame(t),5)#return cursor to fetch the data
    print(df.shape)
    BR_p = False
    BL_p = False
    print('-'*20, 'checking pattern', '-'*20)
    # print(df)
    # print(df.shape[0])
    # df =df.set_index(['time'])
    # tg('Bullish pinbar formed ad {}'.format(datetime.now()))
    if df.shape[0] > 3:
        i = df.shape[0]-2
        print(i)
        current = df.iloc[i, :]
        prev = df.iloc[i-1, :]
        prev_2 = df.iloc[i-2, :]
        # idx = df.index[i]
        realbody = abs(current['open'] - current['close'])
        candle_range = current['high'] - current['low']
        # Bullish pinbar
        print('\n checking patterns : \n')
        if realbody <= candle_range/3 and min(current['open'], current['close']) > (current['high'] + current['low'])/2 and current['low'] < prev['low']:
            print('Bullish pinbar')
            # tg('Bullish pinbar formed at {}'.format(datetime.now()))
            BL_p = True

        # Bearish pinbar
        if realbody <= candle_range/3 and max(current['open'], current['close']) < (current['high'] + current['low'])/2 and current['high'] > prev['high']:
            print('Bearish pinbar')
            # tg('Bearish pinbar formed at {}'.format(datetime.now()))
            BR_p = True
    else:
        logging.warn('insufficient data to check patterns')
        return [False, False ]

Using this module i want to observe if i can get True whenever the conditions satified for for given time.

below is the main i am trying but i am getting error and it runs module1 multiple times.

main.py

import time
import sched
import sql_module as sql
import module1 as tr_ses
import module2 as an
from multiprocessing import Pool


def do_something(scheduler):
    # schedule the next call first
    scheduler.enter(10, 2, do_something, (scheduler,))
    an.check_module()
    # then do your stuff
my_scheduler = sched.scheduler(time.time, time.sleep)
my_scheduler.enter(10, 2, do_something, (my_scheduler,))

pool = Pool()
result1 = pool.apply_async(tr_ses.main())
result2 = pool.apply_async(my_scheduler.run())
answer1 = result1.get(timeout=10)
answer2 = result2.get(timeout=10)

So is there a way to start the trading view websocket and store the data in sql in parallel run check if we have patterns formed by using module2.py

I want to see if i can observe if patterns formed and if yes do some other task based on it.

Expected output in terminal

hhhhhhhhhhhhhhhhhhhhhh ~m~5~m~~h~52


--------------------
2023/03/25, 12:48:00 27505.52 27505.65 27481.0 27495.7 103.87986
--------------------
2023/03/25, 12:48:00 27505.52 27505.65 27481.0 27498.55 120.22012
--------------------
2023/03/25, 12:48:00 27505.52 27506.91 27481.0 27506.91 130.36431
--------------------
2023/03/25, 12:48:00 27505.52 27509.45 27481.0 27509.45 135.02583
--------------------
2023/03/25, 12:48:00 27505.52 27518.32 27481.0 27518.32 156.77121
--------------------
2023/03/25, 12:48:00 27505.52 27518.68 27481.0 27518.68 165.35525



 hhhhhhhhhhhhhhhhhhhhhh ~m~5~m~~h~53


--------------------
2023/03/25, 12:48:00 27505.52 27525.99 27481.0 27511.34 178.31874
--------------------
2023/03/25, 12:48:00 27505.52 27525.99 27481.0 27511.34 178.46296



 hhhhhhhhhhhhhhhhhhhhhh ~m~5~m~~h~54


--------------------
Bearish pinbar formed
--------------------
2023/03/25, 12:49:00 27511.35 27511.35 27511.34 27511.34 2.2486
--------------------
2023/03/25, 12:49:00 27511.35 27511.35 27511.34 27511.35 2.59885



 hhhhhhhhhhhhhhhhhhhhhh ~m~5~m~~h~55


--------------------
2023/03/25, 12:49:00 27511.35 27511.35 27511.34 27511.34 3.21393
--------------------
2023/03/25, 12:49:00 27511.35 27511.35 27511.33 27511.33 4.91521
  • What is the reason you want to run them in same script? I think it should be easier to have them in different scripts. The module 1 (scraping script) sounds like that could be running in a cronjob every 5 min. Then you have more control over it and easier to debug one of the script without affecting the other. – starking Mar 26 '23 at 08:55
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Сергей Кох Mar 26 '23 at 11:11
  • @starking thanks. module1 is a scraper. and module2 is a pattern checker. I want real-time pattern identifiers from scraper data. module1 is running WebSocket and its not running every 5 minutes it was attempted to run module2 every 5 min thats the reason `# schedule.every(5).minutes.at(':00').do(do_something_threaded)` is commented in main function of module1. – lazybot Mar 26 '23 at 11:32
  • @СергейКох Sure we can put the problem statement in the simplest way as `if i want to check the pattern of data coming from WebSocket what would be the best way`. We should get web socket data every sec and for every 5 or given time interval check the pattern on data pushed to sql db. – lazybot Mar 26 '23 at 11:35

0 Answers0