I am working on an algorithmic trading strategy. In df1 I have rows corresponding to times 9.15 am and 9.20 am for each day in a month In df2 I have rows corresponding to time 9.26 for each day in a month In df3 I have rows corresponding to each minute starting from 9.15 am for each day in a month.
I have to compare the closing price at 9.20 am and 9.25 am for each day in a month and subsequently make decisions.
So I started with x as 1 and df1 and df2 and then iterated till the end of df1. Did x =1 as I am doing x-1 so for the first iteration it might give me -1 as the index, so to avoid that did x=1 and similarly started with y=0 for df3.
Below is the code of the iteration:
profit=0
x=1
y=1
for x in range(len(df)):
# green candle. i.e closing price of 9.25 is greater than closing price of 9.20, i.e prices have increased
# then we keep target as price at 9.26-50 and sl as price at 9.26 + 50
if df['close'].iloc[x] > df['close'].iloc[x-1]:
short=df['close'].iloc[x]
sl = df2['open'].iloc[x]+50
tgt =df2['open'].iloc[x]-50
for y in range (len(df3)):
if(sl==df3['open'].iloc[y]):
profit+=(df3['open'].iloc[y]-short)
elif(tgt==df3['open'].iloc[y]):
profit+=(df3['open'].iloc[y]-short)
x+=1
elif df['close'].iloc[x] < df['close'].iloc[x-1]:
long=df['close'].iloc[x]
sl = df2['open'].iloc[x]-50
tgt =df2['open'].iloc[x]+50
for y in range(len(df3)):
if(sl==df3['open'].iloc[y]):
profit+=(df3['open'].iloc[y]-short)
elif(tgt==df3['open'].iloc[y]):
profit+=(df3['open'].iloc[y]-short)
x+=1
profit
Am getting the error single positional indexer is out-of-bounds. I tried changing the starting limit of x and y but still, the error is there.
Below I have also attached the pic of the df
Any help is highly appreciated!!