Home > database >  How to make the below code run for entire data set in python?
How to make the below code run for entire data set in python?

Time:01-12

I have stock market data, I would like to have the code purchase stock (buy) and wait until it gets sold (sell) and again purchases stock after selling. The below code only runs for the initial buy and sell. what if I want to do that for the entire dataset, instead of breaking at first buy and first sell.

i=0
j=0
while (i<1) and (j <len(data)):
    if data['RSI'][j]>=70:
        print(j,data['RSI'][j])
        print("BUY")
        k=j
        i =1
    j =1
    while (i==1) and ((j>k) and (j<len(data))):
        if data['RSI'][j]<=30:
            print(j,data['RSI'][j])
            print("Sell")
            i =1
        j =1

the output of the above code in a tabular format(for better understanding):

index RSI tag
0 100.0 BUY
7 25.38 SELL

Expected Output:

index RSI tag
0 100.0 BUY
7 25.38 SELL
10 80.24 BUY
20 40.20 SELL
25 81.24 BUY
30 41.20 SELL

and it goes on like this for entire dataset

CodePudding user response:

Your first for loop exists after one iteration, because you increment i to 1 (and possibly to higher values). Since your while loop only keeps iterating if i<1 and some other stuff, it exists when it finds i to be 1.

I can not give you advice on how to fix that, because I can not entirely see what you are trying to achieve using j and i, but you will sure figure that out :)

CodePudding user response:

I applied for loop and (k!='NA') in the second while which did the work.

i=0
j=0
for row in range(0,len(data)):
    while (i<row 1) and (j<len(data)):
        if data['RSI'][j]>=70:
            print(j,data['RSI'][j])
            print("BUY")
            k=j
            i =1
        j =1
        while (k!='NA') and ((j>k) and (j<len(data))):
            if data['RSI'][j]<=30:
                print(j,data['RSI'][j])
                print("Sell")
                k='NA'
            j =1
  •  Tags:  
  • Related