Home > other >  Pandas dataframe trading gain/loss
Pandas dataframe trading gain/loss

Time:02-11

Dataframe

(Disregard the two index columns)

level_0 index Year Month Day Open High Low Close Volume Length Polarity Sentiment_Negative Sentiment_Neutral Sentiment_Positive Target_variable Predicted
0 0 0 2020 1 19 8941.45 9164.36 8620.08 8706.25 3.42173e 10 937.167 0.0884653 0 0 1 0 0
1 1 1 2020 1 18 8927.21 9012.2 8827.33 8942.81 3.23378e 10 1177.5 0.176394 0 0 1 1 1
2 2 2 2020 1 17 8725.21 8958.12 8677.32 8929.04 3.63721e 10 1580 0.216762 0 0 1 0 0
3 3 3 2020 1 16 8812.48 8846.46 8612.1 8723.79 3.1314e 10 1336.33 0.182707 0 0 1 0 0
Description

The value of the target_variable is 1 if todays closing price is greater than yesterdays closing price

The value of the target_variable is 0 if todays closing price is less than yesterdays closing price

The predicted value is the output of my classifier.

Problem

I need to run some code that tracks how much money is gained if I invest when the classifier tells me to invest

I have started to code this

credit = 10000
for index, row in df.iterrows():
    if row["Predicted"] == 1:
        #print(row["Percentage_diff"])
        credit = credit - 100
        credit = credit   (100 * row["Percentage_diff"]) 
print(credit)

The idea is that I start off with a balance of 10,000 and invest 100 every time the classifier signals to. The only problem is that when I lose 8000 credits. Is the code correct and the classifier is very poor?

Or have I made an error in the code?

CodePudding user response:

I am not a trading expert, so I assume that every day the classifier tells you to trade, you will buy with the opening price and sell with the close price.

You can start by calculating the percentage of profit or loss when the classifier tells you to trade. You can do that by subtracting the closing price from the opening and dividing it by the opening price.

df["perc_diff"] = (df["Close"] - df["Open"])/df["open"]

Of course, this will be negative when the classifier is wrong. To compute the cumulative profits/losses, all you want to do is to iteratively add/subtract your profit/loss to your capital. This means at a day with a profit/loss percentage of r, if you invest x dollars, your new credit is (1 r)*x. So a simple for loop can do it like that:

credit = 1 # your capital
for label, row in df.iterrows():
    credit = (1   row["Predicted"] * r) * row["perc_diff"] 
print(credit)

Edit to address your updated problem:

If you want to specify an amount to invest rather than all your capital, then you can use this:

credit = 1 # your capital
to_invest = 0.1 # money to invest 
for label, row in df.iterrows():
    # update invest 
    invest_update = (1   row["Predicted"] * row["perc_diff"]) * to_invest
    credit = credit - to_invest   invest_update
print(credit)

The last two lines can be combined into one line:

credit = credit   row["Predicted"] * row["perc_diff"] * to_invest

I think the code is correct, and if you lose, then it is probably due to poor performance from your classifier, but this should be evident from your evaluation of the model (like accuracy and precision metrics). Also, if it is a classifier that is not made for time series (e.g. logistic regression), then it is very reasonable that it performs poorly.

CodePudding user response:

Solution


df["Percentage_diff"] = (df["Close"] - df["Open"])/df["Open"]

credit = 10000
for index, row in df.iterrows():
    if row["Predicted"] == 1:
        #print(row["Percentage_diff"])
        credit = credit - 100
        credit = credit   ((100 * row["Percentage_diff"])   100)
print(credit)

This was the solution thanks to Ahmed.

If I start with an original balance of 10000 every time the classifier signals to invest I invest 100 dollars at opening and withdraw at close this calculates the balance.

  • Related