def buy(data):
ic(data)
try:
trading_client = TradingClient(data[‘api_key’], data[‘api_secret’], paper=True)
account = trading_client.get_account()
print(“########## Buy ###########”)
print(account)
api = tradeapi.REST(data[‘api_key’], data[‘api_secret’], data[‘api_secret’])
position = 0
try:
api.cancel_all_orders()
except Exception as e:
logging.error(e)
print(e)
try:
position = float(api.get_position(‘BTC/USD’).qty)
except Exception as exception:
if exception.str() == “position does not exist”:
position = 0
position = 0
if position:
return “We already have a buy position”
else:
market_order_data = MarketOrderRequest(
symbol="BTC/USD",
qty=1,
side=OrderSide.BUY,
time_in_force=TimeInForce.GTC
)
# Market order
try:
market_order = trading_client.submit_order(order_data=market_order_data)
s1=json.dumps(market_order)
buyereponse=json.loads(s1)
print("-------printing------")
print(buyereponse)
except Exception as e:
print(e)
except:
print("Alpaca Api forbidden")
I want save this data into my database for that i am calling this function into my django rest api but whenever i am requesting the post api its throwing except block. How can i fix this into django rest api can any help me in this.