Issue
There are two python notebooks:
trading.ipynb
(1st)monitorTrades.ipynb
(2nd)
While the execution of the main block is in the first notebook, a function has to be executed in the second notebook (at the same time). This function must have 2 arguments (which are calculated during the execution in first notebook).
There are multiple functions in monitorTrades.ipynb
, but I only want to run function: checkFunc()
.
This function has 4 arguments. 2 of which came from monitorTrades.ipynb
and the other 2 arguments are calculated during the execution of trading.ipynb
.
The function must execute on monitorTrades.ipynb
.
How to do this implement this?
Solution
So, I have tried to find the solution and have tried multiple things but I didn't got the solution i was looking for so I have built a workaround for this problem.
trading.ipynb
I made a function to write these values in a csv.
import os
import csv
def write_target_sl(value1: float, value2: float) -> None:
file_path = f"monitor.csv"
try:
with open(file_path, 'r', newline='') as csvfile:
reader = csv.reader(csvfile)
header = next(reader)
data = list(reader)
except FileNotFoundError:
data = []
with open(file_path, 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=header)
writer.writeheader()
writer.writerow({'Value1': float(value1), 'value2': float(value2)})
if data:
data = data[1:]
writer.writerows(dict(row[:2]) for row in data)
This function will overwrite the same row in csv file.
Now in monitorTrades.ipynb
I made these functions:
import csv
import time
def check_csv_for_changes():
filename = "monitor.csv"
previous_values = None
while True:
try:
with open(filename, "r") as csvfile:
reader = csv.reader(csvfile)
next(reader)
row = next(reader)
current_values = float(row[0]), float(row[1])
if current_values != previous_values:
previous_values = current_values
handle_new_values(current_values[0], current_values[1])
except FileNotFoundError:
print(f"CSV file not found: {filename}")
time.sleep(50) # Check every 50 seconds
def handle_new_values(value1, value2):
print(f"Values changed, new values are value1 {value1}, value2 {value2}")
check_csv_for_changes()
Now this does solve my problem. Well if someone faces this kind of problem then yeah you can use it to solve.
Please drop solution to this if someone has one.
Thanks for all the comments.
Answered By - Sanchay Kasturey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.