Home > Net >  Iterating through dataframe and doing calculations - stuck
Iterating through dataframe and doing calculations - stuck

Time:10-04

        Open        High         Low       Close    Adj Close   Volume  Return      Date Week_Number    Year    Day Weekday mean_return volatility                  Long/Short
754 46.900002   46.900002   45.599998   46.500000   46.498928   1077800 0.758   2020-01-02  0   2020    2   Thursday    -0.5995 1.919795        0
755 45.639999   46.160000   45.299999   45.590000   45.588947   860000  -1.957  2020-01-03  0   2020    3   Friday  -0.5995 1.919795            0
756 45.000000   46.240002   44.590000   46.189999   46.188934   951500  1.316   2020-01-06  1   2020    6   Monday  1.5750  0.738242            1
757 46.400002   47.090000   46.029999   46.840000   46.838921   729600  1.407   2020-01-07  1   2020    7   Tuesday 1.5750  0.738242            1
758 46.830002   47.590000   46.660000   47.290001   47.288910   643600  0.961   2020-01-08  1   2020    8   Wednesday   1.5750  0.738242        1

I have this dataframe and I want to executive this calculation. If "Long/Short" == 1, then buy at the "Close" and sell at the "Open" of the NEXT DAY.

Similarly, if "Long/Short" == 0, then sell at "Close" and buy at "Open" of the NEXT DAY.

I start with $100 to invest.

How do I go about iterating over that? Can i use itertuples() but if so, how do I tell python if i buy today, i sell tomorrow OR if i sell tomorrow I buy today in a for loop?

Essentially what I'm trying to ask it to do is with whatever result I get for that day, either buy or sell with the "Open" price for the row below it.

Any suggestions? Thanks!

CodePudding user response:

OP's dataframe is as follows

df = pd.DataFrame({'Open': [46.900002, 45.639999, 45.000000, 46.400002, 46.830002],
                   'High': [46.900002, 46.160000, 46.240002, 47.090000, 47.590000],
                   'Low': [45.599998, 45.299999, 44.590000, 46.029999, 46.660000],
                   'Close': [46.500000, 45.590000, 46.189999, 46.840000, 47.290001],
                   'Adj Close': [46.498928, 45.588947, 46.188934, 46.838921, 47.288910],
                   'Volume': [1077800, 860000, 951500, 729600, 643600],
                   'Return': [0.758, -1.957, 1.316, 1.407, 0.961],
                   'Date': ['2020-01-02', '2020-01-03', '2020-01-06', '2020-01-07', '2020-01-08'],
                   'Week_Number': [0, 0, 1, 1, 1],
                   'Year': [2020, 2020, 2020, 2020, 2020],
                   'Day': [2, 3, 6, 7, 8],
                   'Weekday': ['Thursday', 'Friday', 'Monday', 'Tuesday', 'Wednesday'],
                   'mean_return': [-0.5995, -0.5995, 1.5750, 1.5750, 1.5750],
                   'volatility': [1.919795, 1.919795, 0.738242, 0.738242, 0.738242],
                   'Long/Short': [0, 0, 1, 1, 1]})

[Out]:
        Open       High        Low  ...  mean_return  volatility  Long/Short
0  46.900002  46.900002  45.599998  ...      -0.5995    1.919795           0
1  45.639999  46.160000  45.299999  ...      -0.5995    1.919795           0
2  45.000000  46.240002  44.590000  ...       1.5750    0.738242           1
3  46.400002  47.090000  46.029999  ...       1.5750    0.738242           1
4  46.830002  47.590000  46.660000  ...       1.5750    0.738242           1

If I understood correctly, OP is looking for a function that will take as input the dataframe, and the initial capital, that will go through the dataframe rows and:

  • If Long/Short==1, then buy stock at the Close price and sell the stock as the Open price of the next day.

  • If Long/Short==0, then sell the stock at the Close price and buy the stock as the Open price of the next day.

That function would be something like

def simulate_capital(df, initial_capital):

    # Create a variable called capital
    capital = initial_capital

    # Create a variable called i
    i = 0

    # Create a variable called last_day
    last_day = len(df) - 1

    # Create a while loop that iterates while i is less than the last_day
    while i < last_day:

        # If the Long/Short value is 1
        if df.loc[i, 'Long/Short'] == 1:

            # Buy the stock at the "Close" price
            capital = capital - df.loc[i, 'Close']

            # Sell the stock at the "Open" price of the next day
            capital = capital   df.loc[i   1, 'Open']

        # If the Long/Short value is 0
        if df.loc[i, 'Long/Short'] == 0:

            # Sell the stock at the "Close" price
            capital = capital   df.loc[i, 'Close']

            # Buy the stock at the "Open" price of the next day
            capital = capital - df.loc[i   1, 'Open']

        # Increment i
        i = i   1

    # Return the final capital
    return capital

If one applies the function to the dataframe df using the initial_capital as 100, one will get the following

result = simulate_capital(df, 100)

[Out]: 101.65000599999999

which means that, with that dataframe, if one started with 100, one would end up with 101.65000599999999.

  • Related