def get_positions(self): temp_header = Setting() self.Headers = temp_header.headers() self.POSITIONS_URL = "{}/v2/positions".format(self.BASE_URL) r = requests.get(self.POSITIONS_URL, headers=self.Headers) return json.loads(r.content)
def create_order_withoutmargin(self,symbol,qty,side,type,time_in_force,limit_price=None, stop_price=None, client_order_id=None, order_class=None, take_profit=None, stop_loss=None, trail_price=None, trail_percent=None): ch_buy=self.check_buy(qty,symbol) # print(ch_buy) # return ch_buy if(ch_buy): temp_header=Setting() self.Headers=temp_header.headers() self.ORDERS_URL="{}/v1/orders".format(self.BASE_URL) data={ "symbol":symbol, "qty":qty, "side":side, "type":type, "time_in_force":time_in_force, "limit_price":limit_price, "stop_price":stop_price, "client_order_id":client_order_id, "order_class":order_class, "take_profit":take_profit, "stop_loss":stop_loss, "trail_price":trail_price, "trail_percent":trail_percent } try: r=requests.post(self.ORDERS_URL,json=data,headers=self.Headers) return json.loads(r.content) except: return "invalid parameters" else: return "Current equity is less than the order placed"
def get_account(self): temp_header = Setting() self.Headers = temp_header.headers() ACCOUNT_URL = "{}/v2/account".format(self.BASE_URL) r = requests.get(ACCOUNT_URL, headers=self.Headers) return json.loads(r.content) print(r.content)
def create_order_withmargin(self,symbol,qty,side,type,time_in_force,limit_price=None, stop_price=None, client_order_id=None, order_class=None, take_profit=None, stop_loss=None, trail_price=None, trail_percent=None): temp_header=Setting() self.Headers=temp_header.headers() self.ORDERS_URL="{}/v2/orders".format(self.BASE_URL) data={ "symbol":symbol, "qty":qty, "side":side, "type":type, "time_in_force":time_in_force, "limit_price":limit_price, "stop_price":stop_price, "client_order_id":client_order_id, "order_class":order_class, "take_profit":take_profit, "stop_loss":stop_loss, "trail_price":trail_price, "trail_percent":trail_percent } r=requests.post(self.ORDERS_URL,json=data,headers=self.Headers) return json.loads(r.content) # x=Transaction(userid="1234userid") # y=x.create_order_withoutmargin("AAPL",1,"buy","market","gtc") # print(y)
def bought_sell_orders(self,query,limit=None): self.ORDERS_URL="{}/v2/orders?status=all".format(self.BASE_URL,limit) temp_header=Setting() self.Headers=temp_header.headers() r=requests.get(self.ORDERS_URL,headers=self.Headers) bought = json.loads(r.content) # print(bought,"bought") self.temp_list = [] for values in bought: if(values['side']==query and values['status']=='filled'): self.temp_dict = {} polygonDetails = self.polygonAssetDetails(values["symbol"]) alpacaDetails = self.alpacaAssetDetails(values["symbol"]) self.temp_dict["symbol"] = values["symbol"] self.temp_dict["symbolimage"] = polygonDetails["logo"] self.temp_dict["qty"] = values["qty"] self.temp_dict['filled_avg_price'] = values['filled_avg_price'] self.temp_dict['side'] = values['side'] self.temp_dict['name'] = alpacaDetails['assets']['name'] self.temp_dict['exchange'] = alpacaDetails['assets']['exchange'] # print(alpacaDetails) self.temp_list.append(self.temp_dict) if limit==None: return self.temp_list elif (limit==0): return self.temp_list[0] else: return self.temp_list[0:limit]
def get_orders(self,limit=None): self.ORDERS_URL="{}/v2/orders".format(self.BASE_URL) temp_header=Setting() self.Headers=temp_header.headers() r = requests.get(self.ORDERS_URL,headers=self.Headers) pending = json.loads(r.content) self.temp_list = [] # print(pending) for values in pending: self.temp_dict = {} self.temp_dict["symbol"] = values["symbol"] polygonDetails = self.polygonAssetDetails(values["symbol"]) alpacaDetails = self.alpacaAssetDetails(values["symbol"]) self.temp_dict["symbolimage"] = polygonDetails["logo"] self.temp_dict["qty"] = values["qty"] self.temp_dict["id"] = values["id"] self.temp_dict['filled_avg_price'] = values['filled_avg_price'] self.temp_dict['side'] = values['side'] self.temp_dict['name'] = alpacaDetails['assets']['name'] self.temp_dict['exchange'] = alpacaDetails['assets']['exchange'] # print(alpacaDetails) self.temp_list.append(self.temp_dict) if(limit!=None): return self.temp_list[0:limit] else: return self.temp_list
def specific_get_position(self, ticker): self.POSITIONS_URL = "{}/v2/positions/{}".format(self.BASE_URL, ticker) temp_header = Setting() self.Headers = temp_header.headers() r = requests.get(self.POSITIONS_URL, headers=self.Headers) return json.loads(r.content) # x=Positions_data(userid="1234userid") # y=x.specific_get_position("AAPL") # print(y)
def get_account_activites(self): temp_header = Setting() self.Headers = temp_header.headers() ACCOUNT_URL = "{}/v2/account/activities".format(self.BASE_URL) r = requests.get(ACCOUNT_URL, headers=self.Headers) return json.loads(r.content) print(r.content) # x = AccountData(userid="1234userid") # y=x.get_account() # x.get_account_activites() # print(y['equity'])
def get_positions_ticker(self, tickername): self.tickername = tickername temp_header = Setting() self.Headers = temp_header.headers() self.POSITIONS_URL = "{}/v2/positions/{}".format( self.BASE_URL, self.tickername) r = requests.get(self.POSITIONS_URL, headers=self.Headers) return json.loads(r.content) # x=Stocks_value(userid="1234userid") # y=x.get_positions_ticker(tickername="AAPL") # print(y)
def bid_info(self, ticker): temp_api = Setting() dict1 = {} api = temp_api.requesttradeapi() t1 = api.get_last_trade(ticker) t2 = api.get_last_quote(ticker) dict1['trade'] = t1._raw dict1['quote'] = t2._raw return dict1 # x = Bid(userid="1234userid") # y=x.bid_info(ticker="AAPL") # print(y)
def cancel_all_order(self): temp_api=Setting() api=temp_api.requesttradeapi() api.cancel_all_orders() x=self.get_orders() return x # x=Orders_data(userid="1234userid") # y1=x.get_orders(None) # print(y1) # y1=x.bought_sell_orders("buy",limit=1) # print(y1) # # # print(y) # # # y=x.update_orders(orderid="3585882b-ff35-475c-8d6f-d19fb41a0c92",qty="2") # # y=x.cancel_all_order() # y=x.cancel_single_order("f7f9a324-058f-479c-a639-904d6a2f2dd8") # print(y)
def list_details(self): temp_header = Setting() self.Headers = temp_header.headers() ASSET_URL = "{}/v2/assets/{}".format(self.BASE_URL, self.tickername) r = requests.get(ASSET_URL, headers=self.Headers) data1 = json.loads(r.content) # return json.loads(r.content) Clock_URL = "{}/v2/clock".format(self.BASE_URL) r1 = requests.get(Clock_URL, headers=self.Headers) data2 = json.loads(r1.content) self.temp_dict["assets"] = data1 self.temp_dict["clock"] = data2 # print(r1.content) return self.temp_dict # x = AlpacaAssetDetails("AAPL") # y = x.list_details() # print(y)
def check_value_symbol(self,symbol): temp_api=Setting() api=temp_api.requesttradeapi() t1=api.get_last_trade(symbol) return t1._raw['price']
def specific_get_order(self,orderid): self.ORDERS_URL="{}/v2/orders/{}".format(self.BASE_URL,orderid) temp_header=Setting() self.Headers=temp_header.headers() r=requests.get(self.ORDERS_URL,headers=self.Headers) return json.loads(r.content)
def cancel_single_order(self,orderid): temp_api = Setting() api = temp_api.requesttradeapi() api.cancel_order(orderid) x = self.get_orders() return "successfully cancel order"