· 5 years ago · Jun 29, 2020, 10:34 PM
1import requests
2import alpaca_trade_api as tradeapi
3from datetime import datetime as dt
4import json
5
6BASE_URL = "https://paper-api.alpaca.markets"
7from settings import ALPACA_API_ENDPOINT, API_VERSION, API_KEY, SECRET_KEY
8ACCOUNT_URL = "{}/v2/account".format(ALPACA_API_ENDPOINT)
9ORDERS_URL = "{}/v2/orders".format(ALPACA_API_ENDPOINT)
10
11headers = {
12 "APCA-API-KEY-ID": API_KEY,
13 "APCA-API-SECRET-KEY": SECRET_KEY
14}
15
16
17def send_to_api_server():
18 domain = ALPACA_API_ENDPOINT + API_VERSION + "/account"
19 response = requests.get(domain, headers=headers)
20 print(response.json())
21
22
23# send_to_api_server()
24
25
26# def get_account():
27# domain = ALPACA_API_ENDPOINT + API_VERSION + "/account"
28# r = requests.get(domain, headers=headers)
29# return json.loads(r.content)
30
31
32def create_order(symbol, quantity, side, order_type, time_in_force, limit_price=None, stop_price=None, extend_hours=False, order_class=None, client_order_id=None):
33 """
34
35 :param symbol: Stock symbol (String, required)
36 :param quantity: Number of shares (Int, required)
37 :param side: "buy" or "sell" (String, required)
38 :param order_type: "market", "limit", "stop", or "stop_limit" (String, required)
39 :param time_in_force: "day", "gtc", "opg", "cls", "ioc", "fok" (String, required) https://alpaca.markets/docs/trading-on-alpaca/orders/#time-in-force
40 :param limit_price: Decimal and required if type = "limit"
41 :param stop_price: Decimal and required if type = "stop" or "stop_limit"
42 :param extend_hours: (Bool), set to True if you would like this to execute in premarket/afterhours (only works with type="limit" and time_in_force="day")
43 :param order_class: "simple", "bracket", "oco" or "oto" https://alpaca.markets/docs/trading-on-alpaca/orders/#bracket-orders
44 :param client_order_id:
45 :return:
46 """
47 body = {
48 "symbol": symbol,
49 "qty": quantity,
50 "side": side,
51 "type": order_type,
52 "time_in_force": time_in_force
53 }
54 domain = ALPACA_API_ENDPOINT + API_VERSION + "/orders"
55 r = requests.post(domain, json=body, headers=headers)
56 # order_time = dt.now()
57 # print("Order " + str(side) + " " + str(quantity) + " of " + str(symbol) + " for $" + str(json.loads(r.content)["filled_avg_price"]) + " at " + order_time.strftime("%H:%M:%S.%f %p on %m/%d/%Y") + ".")
58 return json.loads(r.content)
59
60
61def get_orders():
62 domain = ALPACA_API_ENDPOINT + API_VERSION + "/orders"
63 r = requests.get(domain, headers=headers)
64 return json.loads(r.content)
65
66# Parameters
67# Body Parameters
68# symbol
69# stringRequired
70# symbol or asset ID to identify the asset to trade
71# qty
72# string<int>Required
73# number of shares to trade
74# side
75# stringRequired
76# buy or sell
77# type
78# stringRequired
79# market, limit, stop, or stop_limit
80# time_in_force
81# stringRequired
82# day, gtc, opg, cls, ioc, fok. Please see Understand Orders for more info.
83# limit_price
84# string<number>Required
85# required if type is limit or stop_limit
86# stop_price
87# string<number>Required
88# required if type is stop or stop_limit
89# extended_hours
90# boolean
91# (default) false. If true, order will be eligible to execute in premarket/afterhours. Only works with type limit and time_in_force day.
92# client_order_id
93# string (<= 48 characters)
94# A unique identifier for the order. Automatically generated if not sent.
95# order_class
96# string
97# simple, bracket, oco or oto. For details of non-simple order classes, please see Bracket Order Overview
98# take_profit
99# object
100# Additional parameters for take-profit leg of advanced orders
101# limit_price
102# string<number>Required
103# required for bracket orders
104# stop_loss
105# object
106# Additional parameters for stop-loss leg of advanced orders
107# stop_price
108# string<number>Required
109# required for bracket orders
110# limit_price
111# string<number>
112# the stop-loss order becomes a stop-limit order if specified
113
114# Response is new Order object