How to execute a function and save in a new csv file? - python-3.x

This program converts coordinates. What I am trying to do is to
use a csv file as input
use the function to convert the coordinates
save the output as a new csv file.
My file (worksheet.csv) has three columns, latitude, longitude and height.
How would I approach this?
import math
import csv
# semi-major axis of earth
a = 6378137.0
# 1/f is reciprocal of flatteing
f= 0.00335281068
# converts the input from degree to radians
latitude = math.radians(float(input('Enter Latitude:')))
longitude = math.radians(float(input('Enter Longitude:')))
height = float(input('Enter Height:'))
def earthConverter(latitude, longitude, height):
e = math.sqrt((2 * f) - (f**2))
N = a / math.sqrt(1-e**2 * math.sin(latitude)**2)
x = (N + height) * math.cos(latitude) * math.cos(longitude)
y = (N + height) * math.cos(latitude) * math.sin(longitude)
z = (N * (1 - e**2 ) + height) * math.sin(latitude)
return x, y, z
############################################
with open('worksheet.csv', 'r') as csvFile:
reader = csv.reader(csvFile)
for row in reader:
writer = csv.writer(csvFile)
writer.writerow(row[0], row[1], row[2], earthConverter(math.radians(float(row[0])),
earthConverter(math.radians(float(row[1])), earthConverter(float(row[2])) )
csvFile.close()

You've very close, but there are several things that need to be changed. Here's what I think is a full solution, but below I'll work through each part of the code
import math
import csv
def earthConverter(latitude, longitude, height):
f = 0.00335281068
a = 6378137.0
e = math.sqrt((2 * f) - (f**2))
N = a / math.sqrt(1-e**2 * math.sin(latitude)**2)
x = (N + height) * math.cos(latitude) * math.cos(longitude)
y = (N + height) * math.cos(latitude) * math.sin(longitude)
z = (N * (1 - e**2 ) + height) * math.sin(latitude)
return x, y, z
with open('worksheet.csv', 'r') as Infile, open('worksheet_out.csv', 'w') as Outfile:
reader = csv.reader(Infile)
# next(reader, None)
writer = csv.writer(Outfile)
for row in reader:
lat = math.radians(float(row[0]))
lon = math.radians(float(row[1]))
ht = math.radians(float(row[2]))
x, y, z = earthConverter(lat, lon, ht)
row_out = [row[0], row[1], row[2], x, y, z]
writer.writerow(row_out)
First, you can move the definitions of f and a into the earthConverter function itself to avoid any possible problems with variable scoping. This isn't strictly necessary.
Second, you can get rid of the latitude = math.radians(float(input('Enter Latitude:'))) lines. Those ask for user input, which is not what you want here.
Third, you cannot write back to the same csv. You've opened it in read mode ('r'), but even if you changed that, this post gives some details about why that won't work/is a bad idea. You can also get rid of the separate call to close the csv at the end of your code -- the with open() construction takes care of that for you.
Fourth, your earthConverter function returns a tuple, so you need to unpack those values somehow before trying to write them out again.
Everything in the for row in reader: block could be condensed into fewer rows. I broke it up this way because it makes it a little easier to read.
Also, you didn't mention whether your input csv had a header. If it does, then uncomment the line next(reader, None), which will skip the header. If you need to write a header out again, then you could change the for row in reader: block to this:
for i, row in enumerate(reader):
if i == 1:
header_out = ['lat', 'lon', 'ht', 'x', 'y', 'z'] # or whatever
writer.writerow(header_out)
lat = math.radians(float(row[0]))
lon = math.radians(float(row[1]))
ht = math.radians(float(row[2]))
x, y, z = earthConverter(lat, lon, ht)
row_out = [row[0], row[1], row[2], x, y, z]
writer.writerow(row_out)

All you have to do is create a Dataframe to read the csv file and create a for loop to iterate through each row so and insert it into a new Dataframe. Then we let the panda library Export it into a new csv file.
import pandas as pd
import math
# semi-major axis of earth
a = 6378137.0
# 1/f is reciprocal of flatteing
f = 0.00335281068
def earthConverter(latitude, longitude, height):
e = math.sqrt((2 * f) - (f**2))
N = a / math.sqrt(1-e**2 * math.sin(latitude)**2)
x = (N + height) * math.cos(latitude) * math.cos(longitude)
y = (N + height) * math.cos(latitude) * math.sin(longitude)
z = (N * (1 - e**2 ) + height) * math.sin(latitude)
return x, y, z
def new_csv(input_file, output_file):
df = pd.read_csv(input_file)
points_df = pd.DataFrame(columns=['Latitude', 'Longitude', 'Height'])
for i, row in df.iterrows():
x1, y1, z1 = earthConverter(row['Latitude'], row['Longitude'], row['Height'])
temp_df = pd.DataFrame({'Latitude': x1,
'Longitude': y1,
'Height': z1}, index=[0])
points_df = points_df.append(temp_df, ignore_index=True)
points_df.to_csv(output_file)
new_csv('worksheet.csv', 'new_worksheet.csv')

Related

how to read csv file and pass its values to a function parameters

I have a csv file having one column and the values it contains like this:
here
and this function
def yolo_to_coco(x_center, y_center, w, h, image_w, image_h):
w = w * image_w
h = h * image_h
x1 = ((2 * x_center * image_w) - w)/2
y1 = ((2 * y_center * image_h) - h)/2
return [x1, y1, w, h]``
when I pass value to this function
image_w and image_h I am passing from my own it is not in the csv
test = yolo_to_coco(53.854, 2912.798, 398.71, 57.202,1024,1024)
the get the required result
[-148993.02399999998, 2953417.7279999997, 408279.04, 58574.848]
I want to get each value from this file and pass to this function and the store in another csv file.
You can use dataframe.apply(function) method. With this method you can apply this function to every row.
Let's create a dummy df.
import pandas as pd
d = {'bbox': [[53.854, 2912.798, 398.71, 57.202],[31, 12,120, 20],[17, 72,170, 720]]}
df = pd.DataFrame(data=d)
But you don't need to create dummy df. You will read as
df = pd.read_csv ('/content/drive/MyDrive/bbox_file.csv')
let's create function. First argument will be row value. We will specify extra 2 arguments (image_w, image_h)
def yolo_to_coco(bbox, image_w, image_h):
row_list = list(bbox[1:-1].split(","))
row_list = [float(i) for i in row_list]
x_center, y_center, w, h = row_list
w = w * image_w
h = h * image_h
x1 = ((2 * x_center * image_w) - w)/2
y1 = ((2 * y_center * image_h) - h)/2
return [x1, y1, w, h]
Now, We can apply this function to bbox column.You can pass any image_w,image_h values. It will create a pandas.series
df_coco_series = df["bbox"].apply(yolo_to_coco,image_w = 1024,image_h = 1024)
Finally converting to pandas.dataframe
df_coco = df_coco_series.to_frame()
print(df_coco)

Why can't I get this Runge-Kutta solver to converge as the time step decreases?

For reasons, I need to implement the Runge-Kutta4 method in PyTorch (so no, I'm not going to use scipy.odeint). I tried and I get weird results on the simplest test case, solving x'=x with x(0)=1 (analytical solution: x=exp(t)). Basically, as I reduce the time step, I cannot get the numerical error to go down. I'm able to do it with a simpler Euler method, but not with the Runge-Kutta 4 method, which makes me suspect some floating point issue here (maybe I'm missing some hidden conversion from double precision to single)?
import torch
import numpy as np
import matplotlib.pyplot as plt
def Euler(f, IC, time_grid):
y0 = torch.tensor([IC])
time_grid = time_grid.to(y0[0])
values = y0
for i in range(0, time_grid.shape[0] - 1):
t_i = time_grid[i]
t_next = time_grid[i+1]
y_i = values[i]
dt = t_next - t_i
dy = f(t_i, y_i) * dt
y_next = y_i + dy
y_next = y_next.unsqueeze(0)
values = torch.cat((values, y_next), dim=0)
return values
def RungeKutta4(f, IC, time_grid):
y0 = torch.tensor([IC])
time_grid = time_grid.to(y0[0])
values = y0
for i in range(0, time_grid.shape[0] - 1):
t_i = time_grid[i]
t_next = time_grid[i+1]
y_i = values[i]
dt = t_next - t_i
dtd2 = 0.5 * dt
f1 = f(t_i, y_i)
f2 = f(t_i + dtd2, y_i + dtd2 * f1)
f3 = f(t_i + dtd2, y_i + dtd2 * f2)
f4 = f(t_next, y_i + dt * f3)
dy = 1/6 * dt * (f1 + 2 * (f2 + f3) +f4)
y_next = y_i + dy
y_next = y_next.unsqueeze(0)
values = torch.cat((values, y_next), dim=0)
return values
# differential equation
def f(T, X):
return X
# initial condition
IC = 1.
# integration interval
def integration_interval(steps, ND=1):
return torch.linspace(0, ND, steps)
# analytical solution
def analytical_solution(t_range):
return np.exp(t_range)
# test a numerical method
def test_method(method, t_range, analytical_solution):
numerical_solution = method(f, IC, t_range)
L_inf_err = torch.dist(numerical_solution, analytical_solution, float('inf'))
return L_inf_err
if __name__ == '__main__':
Euler_error = np.array([0.,0.,0.])
RungeKutta4_error = np.array([0.,0.,0.])
indices = np.arange(1, Euler_error.shape[0]+1)
n_steps = np.power(10, indices)
for i, n in np.ndenumerate(n_steps):
t_range = integration_interval(steps=n)
solution = analytical_solution(t_range)
Euler_error[i] = test_method(Euler, t_range, solution).numpy()
RungeKutta4_error[i] = test_method(RungeKutta4, t_range, solution).numpy()
plots_path = "./plots"
a = plt.figure()
plt.xscale('log')
plt.yscale('log')
plt.plot(n_steps, Euler_error, label="Euler error", linestyle='-')
plt.plot(n_steps, RungeKutta4_error, label="RungeKutta 4 error", linestyle='-.')
plt.legend()
plt.savefig(plots_path + "/errors.png")
The result:
As you can see, the Euler method converges (slowly, as expected of a first order method). However, the Runge-Kutta4 method does not converge as the time step gets smaller and smaller. The error goes down initially, and then up again. What's the issue here?
The reason is indeed a floating point precision issue. torch defaults to single precision, so once the truncation error becomes small enough, the total error is basically determined by the roundoff error, and reducing the truncation error further by increasing the number of steps <=> decreasing the time step doesn't lead to any decrease in the total error.
To fix this, we need to enforce double precision 64bit floats for all floating point torch tensors and numpy arrays. Note that the right way to do this is to use respectively torch.float64 and np.float64 rather than, e.g., torch.double and np.double, because the former are fixed-sized float values, (always 64bit) while the latter depend on the machine and/or compiler. Here's the fixed code:
import torch
import numpy as np
import matplotlib.pyplot as plt
def Euler(f, IC, time_grid):
y0 = torch.tensor([IC], dtype=torch.float64)
time_grid = time_grid.to(y0[0])
values = y0
for i in range(0, time_grid.shape[0] - 1):
t_i = time_grid[i]
t_next = time_grid[i+1]
y_i = values[i]
dt = t_next - t_i
dy = f(t_i, y_i) * dt
y_next = y_i + dy
y_next = y_next.unsqueeze(0)
values = torch.cat((values, y_next), dim=0)
return values
def RungeKutta4(f, IC, time_grid):
y0 = torch.tensor([IC], dtype=torch.float64)
time_grid = time_grid.to(y0[0])
values = y0
for i in range(0, time_grid.shape[0] - 1):
t_i = time_grid[i]
t_next = time_grid[i+1]
y_i = values[i]
dt = t_next - t_i
dtd2 = 0.5 * dt
f1 = f(t_i, y_i)
f2 = f(t_i + dtd2, y_i + dtd2 * f1)
f3 = f(t_i + dtd2, y_i + dtd2 * f2)
f4 = f(t_next, y_i + dt * f3)
dy = 1/6 * dt * (f1 + 2 * (f2 + f3) +f4)
y_next = y_i + dy
y_next = y_next.unsqueeze(0)
values = torch.cat((values, y_next), dim=0)
return values
# differential equation
def f(T, X):
return X
# initial condition
IC = 1.
# integration interval
def integration_interval(steps, ND=1):
return torch.linspace(0, ND, steps, dtype=torch.float64)
# analytical solution
def analytical_solution(t_range):
return np.exp(t_range, dtype=np.float64)
# test a numerical method
def test_method(method, t_range, analytical_solution):
numerical_solution = method(f, IC, t_range)
L_inf_err = torch.dist(numerical_solution, analytical_solution, float('inf'))
return L_inf_err
if __name__ == '__main__':
Euler_error = np.array([0.,0.,0.], dtype=np.float64)
RungeKutta4_error = np.array([0.,0.,0.], dtype=np.float64)
indices = np.arange(1, Euler_error.shape[0]+1)
n_steps = np.power(10, indices)
for i, n in np.ndenumerate(n_steps):
t_range = integration_interval(steps=n)
solution = analytical_solution(t_range)
Euler_error[i] = test_method(Euler, t_range, solution).numpy()
RungeKutta4_error[i] = test_method(RungeKutta4, t_range, solution).numpy()
plots_path = "./plots"
a = plt.figure()
plt.xscale('log')
plt.yscale('log')
plt.plot(n_steps, Euler_error, label="Euler error", linestyle='-')
plt.plot(n_steps, RungeKutta4_error, label="RungeKutta 4 error", linestyle='-.')
plt.legend()
plt.savefig(plots_path + "/errors.png")
Result:
Now, as we decrease the time step, the error of the RungeKutta4 approximation decreases with the correct rate.

Function to generate more than one random point in a given circle

So I have defined a fucntion to generate a random number in a given circle:
def rand_gen(R,c):
# random angle
alpha = 2 * math.pi * random.random()
# random radius
r = R * math.sqrt(random.random())
# calculating coordinates
x = r * math.cos(alpha) + c[0]
y = r * math.sin(alpha) + c[1]
return (x,y)
Now I want to add a parameter n (rand_gen(R,c,n)) such that we can get n such numbers instead of one
You can achieve that goal also using generator function:
import random
import math
def rand_gen(R,c,n):
while n:
# random angle
alpha = 2 * math.pi * random.random()
# random radius
r = R * math.sqrt(random.random())
# calculating coordinates
x = r * math.cos(alpha) + c[0]
y = r * math.sin(alpha) + c[1]
n -= 1
yield (x,y)
points_gen = rand_gen(10, (3,4), 4)
for point in points_gen:
print(point)
points = [*rand_gen(10, (3,4), 4)]
print(points)
If you want to generate n random points, you can extend your function with a parameter and for-loop:
import math
import random
def rand_gen(R, c, n):
out = []
for i in range(n):
# random angle
alpha = 2 * math.pi * random.random()
# random radius
r = R * math.sqrt(random.random())
# calculating coordinates
x = r * math.cos(alpha) + c[0]
y = r * math.sin(alpha) + c[1]
out.append((x,y))
return out
print(rand_gen(10, (3, 4), 3))
Prints (for example):
[(-4.700562169626218, 5.62666979720004), (6.481518730246707, -1.849892172014873), (0.41713910134636345, -1.9065302305716285)]
But better approach in my opinion would be leave the function as is and generate n points using list comprehension:
lst = [rand_gen(10, (3, 4)) for _ in range(3)]
print(lst)
Prints:
[(-1.891474340814674, -2.922205399732765), (12.557063558442614, 1.9323688240857821), (-5.450160078420653, 7.974550456763403)]

Determening begin parameters 2D gaussian fit

I'm working on some code which needs to be able to preform a 2d gaussian fitting. I mostly based my code on following question: Fitting a 2D Gaussian function using scipy.optimize.curve_fit - ValueError and minpack.error . Now is problem that I don't really have an initial guess about the different parameters that need to be used.
I've tried this:
def twoD_Gaussian(x_data_tuple, amplitude, xo, yo, sigma_x, sigma_y, theta, offset):
(x,y) = x_data_tuple
xo = float(xo)
yo = float(yo)
a = (np.cos(theta)**2)/(2*sigma_x**2) + (np.sin(theta)**2)/(2*sigma_y**2)
b = -(np.sin(2*theta))/(4*sigma_x**2) + (np.sin(2*theta))/(4*sigma_y**2)
c = (np.sin(theta)**2)/(2*sigma_x**2) + (np.cos(theta)**2)/(2*sigma_y**2)
g = offset + amplitude*np.exp( - (a*((x-xo)**2) + 2*b*(x-xo)*(y-yo)
+ c*((y-yo)**2)))
return g.ravel()
The data.reshape(201,201) is just something I took from the aformentioned question.
mean_gauss_x = sum(x * data.reshape(201,201)) / sum(data.reshape(201,201))
sigma_gauss_x = np.sqrt(sum(data.reshape(201,201) * (x - mean_gauss_x)**2) / sum(data.reshape(201,201)))
mean_gauss_y = sum(y * data.reshape(201,201)) / sum(data.reshape(201,201))
sigma_gauss_y = np.sqrt(sum(data.reshape(201,201) * (y - mean_gauss_y)**2) / sum(data.reshape(201,201)))
initial_guess = (np.max(data), mean_gauss_x, mean_gauss_y, sigma_gauss_x, sigma_gauss_y,0,10)
popt, pcov = curve_fit(twoD_Gaussian, (x, y), data, p0=initial_guess)
data_fitted = twoD_Gaussian((x, y), *popt)
If I try this, I get following error message: ValueError: setting an array element with a sequence.
Is the reasoning about the begin parameters correct?
And why do I get this error?
If I use the runnable code from the linked question and substitute your definition of initial_guess:
mean_gauss_x = sum(x * data.reshape(201,201)) / sum(data.reshape(201,201))
sigma_gauss_x = np.sqrt(sum(data.reshape(201,201) * (x - mean_gauss_x)**2) / sum(data.reshape(201,201)))
mean_gauss_y = sum(y * data.reshape(201,201)) / sum(data.reshape(201,201))
sigma_gauss_y = np.sqrt(sum(data.reshape(201,201) * (y - mean_gauss_y)**2) / sum(data.reshape(201,201)))
initial_guess = (np.max(data), mean_gauss_x, mean_gauss_y, sigma_gauss_x, sigma_gauss_y,0,10)
Then
print(inital_guess)
yields
(13.0, array([...]), array([...]), array([...]), array([...]), 0, 10)
Notice that some of the values in initial_guess are arrays. The optimize.curve_fit function expects initial_guess to be a tuple of scalars. This is the source of the problem.
The error message
ValueError: setting an array element with a sequence
often arises when an array-like is supplied when a scalar value is expected. It is a hint that the source of the problem may have to do with an array having the wrong number of dimensions. For example, it might arise if you pass a 1D array to a function that expects a scalar.
Let's look at this piece of code taken from the linked question:
x = np.linspace(0, 200, 201)
y = np.linspace(0, 200, 201)
X, Y = np.meshgrid(x, y)
x and y are 1D arrays, while X and Y are 2D arrays. (I've capitalized all 2D arrays to help distinguish them from 1D arrays).
Now notice that Python sum and NumPy's sum method behave differently when applied to 2D arrays:
In [146]: sum(X)
Out[146]:
array([ 0., 201., 402., 603., 804., 1005., 1206., 1407.,
1608., 1809., 2010., 2211., 2412., 2613., 2814., 3015.,
...
38592., 38793., 38994., 39195., 39396., 39597., 39798., 39999.,
40200.])
In [147]: X.sum()
Out[147]: 4040100.0
The Python sum function is equivalent to
total = 0
for item in X:
total += item
Since X is a 2D array, the loop for item in X is iterating over the rows of X. Each item is therefore a 1D array representing a row of X. Thus, total ends up being a 1D array.
In contrast, X.sum() sums all the elements in X and returns a scalar.
Since initial_guess should be a tuple of scalars,
everywhere you use sum you should instead use the NumPy sum method. For example, replace
mean_gauss_x = sum(x * data) / sum(data)
with
mean_gauss_x = (X * DATA).sum() / (DATA.sum())
import numpy as np
import scipy.optimize as optimize
import matplotlib.pyplot as plt
# define model function and pass independant variables x and y as a list
def twoD_Gaussian(data, amplitude, xo, yo, sigma_x, sigma_y, theta, offset):
X, Y = data
xo = float(xo)
yo = float(yo)
a = (np.cos(theta) ** 2) / (2 * sigma_x ** 2) + (np.sin(theta) ** 2) / (
2 * sigma_y ** 2
)
b = -(np.sin(2 * theta)) / (4 * sigma_x ** 2) + (np.sin(2 * theta)) / (
4 * sigma_y ** 2
)
c = (np.sin(theta) ** 2) / (2 * sigma_x ** 2) + (np.cos(theta) ** 2) / (
2 * sigma_y ** 2
)
g = offset + amplitude * np.exp(
-(a * ((X - xo) ** 2) + 2 * b * (X - xo) * (Y - yo) + c * ((Y - yo) ** 2))
)
return g.ravel()
# Create x and y indices
x = np.linspace(0, 200, 201)
y = np.linspace(0, 200, 201)
X, Y = np.meshgrid(x, y)
# create data
data = twoD_Gaussian((X, Y), 3, 100, 100, 20, 40, 0, 10)
data_noisy = data + 0.2 * np.random.normal(size=data.shape)
DATA = data.reshape(201, 201)
# add some noise to the data and try to fit the data generated beforehand
mean_gauss_x = (X * DATA).sum() / (DATA.sum())
sigma_gauss_x = np.sqrt((DATA * (X - mean_gauss_x) ** 2).sum() / (DATA.sum()))
mean_gauss_y = (Y * DATA).sum() / (DATA.sum())
sigma_gauss_y = np.sqrt((DATA * (Y - mean_gauss_y) ** 2).sum() / (DATA.sum()))
initial_guess = (
np.max(data),
mean_gauss_x,
mean_gauss_y,
sigma_gauss_x,
sigma_gauss_y,
0,
10,
)
print(initial_guess)
# (13.0, 100.00000000000001, 100.00000000000001, 57.106515650488404, 57.43620227324201, 0, 10)
# initial_guess = (3,100,100,20,40,0,10)
popt, pcov = optimize.curve_fit(twoD_Gaussian, (X, Y), data_noisy, p0=initial_guess)
data_fitted = twoD_Gaussian((X, Y), *popt)
fig, ax = plt.subplots(1, 1)
ax.imshow(
data_noisy.reshape(201, 201),
cmap=plt.cm.jet,
origin="bottom",
extent=(X.min(), X.max(), Y.min(), Y.max()),
)
ax.contour(X, Y, data_fitted.reshape(201, 201), 8, colors="w")
plt.show()

python for loop calculation too inefficient/long

I am running a back-testing program on python. However, even though the maths/logic is simple, python seems to be taking an extremely long time to calculate the FOR loop.
For each row/line, it takes on average 1-sec; and when I have thousands to potentially ten-of-thousands of rows-of-data, the time-taken is impractical.
I use a panda dataframe as the base, and generate forward calculations by for-loop. Is there a more efficient way, or what could I do to reduce the computational time?
def signal_TA1(data, periods):
columns = ['x1', 'x2', 'x3', .......]
pd_Append = pd.DataFrame((np.zeros((len(data.index),len(columns)))), columns = columns) #create and initialize as zeros needed columns
data = data.join(pd_Append)
data['Size'] = data.bidQ + data.askQ
data['prx'] = (data.bid * data.askQ + data.ask * data.bidQ)/data.Size
for i in range(1, len(data.index), 1):
data.emaX.iloc[i] = data.lambda_.iloc[i] * data.Size.iloc[i] + (1 - data.lambda_.iloc[i]) * data.emaX.iloc[i-1]
xxxxxx
xxxxx
xxxxx
return data
It seems (well, it seems to be relatively known) that numpy processes looped calculations much more effectively than pandas (as it has to re-built the whole array each time).
Basically, I create a numpy array [x,y] within the function. Then, I calculate via a for-loop and populate the numpy array, row-by-row. Finally, I merely convert the finished numpy array into a pandas dataframe (for easier display and plotting).
The time difference is forever versus < 1 second for about 2,500 rows of data-and-calculation.
def signal_M2(data, weight, pandas = True):
bid = np.array(data.bid)
ask = np.array(data.ask)
askQ = np.array(data.askQ)
bidQ = np.array(data.bidQ)
size = bidQ + askQ
VWAP = (bid * askQ + ask * bidQ)/(bidQ + askQ)
columns = [x1, x2, x3, x4, x5, .....]
datB = np.zeros((len(data.index), len(columns)))
datA = pd.DataFrame(index=[0], columns = columns)
lambda_ = 0.5
weight = 0.3
x1 = VWAP[0]
x2 = VWAP[0]
x3 = VWAP[0]
x4 = VWAP[0]
x5 = size[0]
....
....
datB[0] = (bid[0], ask[0], bidQ[0], askQ[0], size[0], ..........)
for row in range(1, len(data.index), 1):
x1 = lambda_ * size[row] + (1 - lambda_) * emaInertia
x2 = weight * VWAP[row] + (1 - weight) * emaPrx
x3 = weight * VWAP[row] + (1 -weight) * emaPrxSlow
x4 = weight * VWAP[row] + (1 -weight) * emaPrxFast
x5 = weight * VWAP[row] + (1 -weight) * emaPrxLead
if pandas == True:
datB[row] = (bid[row], ask[row], bidQ[row], ...........)
else:
print(................)
if pandas == True:
datB = pd.DataFrame(datB, columns = columns)
return datB
else :
print('no pandas dataframe was asked to be be stored')

Resources