lundi 26 octobre 2020

In a Python Function, write the pandas result to a CSV

Hello I am trying to write a script in which I look at some historical data (Stock Prices) and compute a rolling Linear Regression Slope.

I have a CSV file with the Close Price of AAPL US Equity from 01-01-2010 - 10-23-2020.

As you can see in the below code I have read the CSV, created a few new variables, The Moving Linear Slope etc... and wrote that data into the CSV as new columns. PS: ROC is the Rate of Change of the MLR Slope.

I am now trying to create a function which returns 1,0,-1 (1=Long, 0 = No Position, -1=Short) for each date/stock price. I have created the below function "scorer" but am unable to figure out how to get it to look at each date and write the corresponding value into a new column in the CSV.

Any help would be greatly appreciated. Thanks in advance.

import pandas as pd
import talib as ta
import matplotlib.pyplot as plt

data = pd.read_csv("Copper.csv")
score = 0

mlrs = ta.LINEARREG_SLOPE(data["Close"].values,14)*-1
sma = data.MlrSlope.rolling(14).mean()
roc = data.MlrSlope.rolling(5).mean()

#Create new columns for Roc and SMA. We then Write the new Column data to the CSV file. This should add both the ROCC and SMA Columns. 
data["Rocc"] = roc
data["SMA"] = sma
data.to_csv("copper.csv",index=True)

# The below function looks to check for the SMA & MLR Slope being Positive... ***JOSHUA BEFORE FINISHING*** add another parameter that looks at the ROC of the MLR Slope... This would be my "roc" variable. If the change is very negative very quick it could resemble an exit opportunity on the Long Side, while a sharp Up Turn could indicate a Buy Opportunity.  
def scorer(data):
    if(data["sma"] > 0 and data["MlrSlope"] >0 ):
            return 1
        elif (data["sma"] >0 and data["MlrSlope"] <0):
            return 0
        elif(data["sma"] <0 and data["MlrSlope"] >0):
            return 0
        elif (data["sma"] <0 and data["MlrSlope"] <0):
            return -1

Aucun commentaire:

Enregistrer un commentaire