I have the following view function which takes the daily stock prices, and convert them to weekly prices. When I return JsonResponse
it does not output the values in the correct form.
def weeklyPrices(request, ticker):
prices = DailyCandles.objects.filter(symbol=ticker.upper()).all().values()
weekly = pd.DataFrame(list(prices))
logic = {'open' : 'first',
'high' : 'max',
'low' : 'min',
'close' : 'last',
'volume': 'sum'}
weekly['date'] = pd.to_datetime(weekly['date'])
weekly.set_index('date', inplace=True)
weekly.sort_index()
weekly = weekly.resample('W').apply(logic)
weekly.index = weekly.index - pd.tseries.frequencies.to_offset('6D')
weekly.reset_index(drop=True, inplace=True)
return JsonResponse(weekly.to_dict(), safe=False)
The output:
open
0 "134.19"
1 "137.0"
2 "136.63"
high
0 "137.73"
1 "138.4"
2 "137.74"
The expected output is where all the values (open, high, low, close) in row 0 are places together, rather then being placed separately.