#### Speeding up the following loop using Cython?

Currently my loop is taking around 160 seconds with the amount of Cython optimization I’ve been able to conduct. Is there anything else I can do to speed it up? Ideally, I’d like to get a bigger performance boost since it will amount to quite a few hours on the other end.

Please note that df is simply a pandas dataframe.

``````   cpdef get_reward(self, list weights):
cdef int mm_length = round(len(df)*0.03)
cdef int randomlower = np.random.randint(low=0, high=mm_length, size=1)
cdef int randomupper = np.random.randint(low=int(len(df)-mm_length), high=len(df), size=1)
self.trend = real_trend[randomlower:randomupper]
cdef double initial_money = self._scaled_capital
cdef double starting_money = initial_money
cdef list invests = []
self.model.weights = weights
cdef list inventory = []
cdef numpy.ndarray state
state = self.get_state(0, inventory, starting_money, self.timeseries)
cdef int positionsreward = 0
cdef int shortpositionsreward = 0
cdef int trendlength = len(self.trend)
cdef double maximumprofitachievable = maxProfit(self.trend, trendlength, int(trendlength*0.0001)) #Calculating the highest profit achievable in the price set
cdef int t1
cdef int action
for t1 in range(0, len(self.trend) - 1, self.skip):
t = t1+1
action = self.act(state)
if action == 1 and starting_money >= self.trend[t] and positionsreward < 1 and shortpositionsreward==0:
inventory.append(self.trend[t])
starting_money -= self.trend[t]
positionsreward += 1

elif action == 2 and len(inventory) and positionsreward>=1 and shortpositionsreward==0:
bought_price = inventory.pop(0)
commission = self.trend[t]*globalcomission
starting_money += self.trend[t]-commission
invest = ((self.trend[t] - bought_price) / bought_price) * 100 - globalcomission
invests.append(invest)
positionsreward -= 1

elif action == 3 and starting_money >= self.trend[t] and shortpositionsreward < 1 and positionsreward==0:
inventory.append(-self.trend[t])
starting_money -= self.trend[t]
shortpositionsreward += 1

elif action == 4 and len(inventory) and shortpositionsreward>=1 and positionsreward==0:
short_price = inventory.pop(0)*-1
commissionhereshort = self.trend[t]*globalcomission
starting_money += short_price+(short_price-self.trend[t])-commissionhereshort
shortpositionsreward -= 1
invest = ((short_price - self.trend[t])/ short_price) * 100 - globalcomission
invests.append(invest)

elif positionsreward==0 and shortpositionsreward==0: