Why does this code repeatedly loop?

I am running the attached code trying to implement look back and holding from section 10, 7 of the momentum course.



I wanted to get live data so I started off by writing code to get the SPI (Aus).



However unless I put end() at the end of the function it keeps on looping and end() stops all execution.



What can I do to make it just get the prices once so I can use the data further down when I add the code from section 10, 7.



Code below:


06_find_lookback_and_holding.py


############### HEADERS #######################

# Import numpy as an alias np
import numpy as np

# Import pandas as an alias pd
import pandas as pd

# Import compute_Hc package from hurst library
from hurst import compute_Hc

# Import compute_Hc package from hurst library
from hurst import random_walk

# Import matplotlib as an alias plt and set the style
#import matplotlib.pyplot as plt

#plt.style.use('seaborn-darkgrid')

################ BODY ######################

#### GET THE DATA

def initialize(context):
    context.security = superSymbol(secType='FUT', symbol='SPI', currency='AUD', exchange='SNFE', primaryExchange='SNFE', multiplier='25', expiry='20200917')
    
def handle_data(context, data):

    print ('Historical Data of %s' % (str(context.security, ),))
    hist = request_historical_data(context.security, '1 day', '5 Y')   
    #print(hist)
    print(hist.iloc[-1]['close'])
    prices = hist['close']
    # Set the index to datetime
    prices.index = pd.to_datetime(prices.index)
    
    
    print(prices)
    

Ideally, the data should fetch only once. But to stop the loop you need to put end(). Check out this video; it did the same.