-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathexecute_orders.py
More file actions
134 lines (113 loc) · 3.8 KB
/
Copy pathexecute_orders.py
File metadata and controls
134 lines (113 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from app.object_values.orders import LimitOrder, MarketOrder, Order
import sys
import argparse
from environs import Env
from pydantic import ValidationError, BaseModel
from app.client import Client
from app.object_values.args import MarketInputArgs, LimitInputArgs
from app.tools import get_formated_price
# Get Binance keys
env = Env()
env.read_env()
API_KEY = env.str("API_KEY", None)
SECRET_KEY = env.str("SECRET_KEY", None)
if API_KEY is None or SECRET_KEY is None:
sys.exit("Either `API_KEY` or `SECRET_KEY` env. variable is not defined!")
def main(input_args: BaseModel) -> None:
client = Client(api_key=API_KEY, api_secret=SECRET_KEY)
symbol = client.get_symbol(input_args.symbol)
# Place a market buy order
if input_args.buy_type == "limit":
buy_order = LimitOrder(
symbol=symbol,
side=Order.SideEnum.buy,
price=input_args.price,
quantity=input_args.quantity
)
elif input_args.buy_type == "market":
buy_order = MarketOrder(
symbol=symbol,
side=Order.SideEnum.buy,
total=input_args.total
)
else:
sys.exit("Buy order type not supported")
order_in_progress = client.execute_buy_strategy(buy_order)
print("=========================")
print("=== Buy order summary ===")
print(
f"=> Buy price: "
f"{get_formated_price(order_in_progress.info.price, symbol.price_decimal_precision)} "
f"{symbol.quoteAsset}"
)
print(
"=> Total price: "
f"{get_formated_price(order_in_progress.info.cummulative_quote_quantity, symbol.price_decimal_precision)} "
f"{symbol.quoteAsset}"
)
print(
f"=> Buy quantity: {get_formated_price(order_in_progress.info.executed_quantity, symbol.qty_decimal_precision)} "
f"{symbol.baseAsset}"
)
stop_loss_limit_order, limit_maker_order = client.execute_sell_strategy(
order_in_progress,
input_args.profit,
input_args.loss,
)
print("=========================")
print("=== OCO order summary ===")
print("== Stop loss limit order:", stop_loss_limit_order)
print("== Limit maker order:", limit_maker_order)
def input_validation(raw_input_args, input_parser: BaseModel) -> BaseModel:
try:
cleaned_input_args = input_parser(**raw_input_args)
except ValidationError as e:
sys.exit(e)
else:
return cleaned_input_args
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--symbol",
required=True,
help="define the symbol of the crypto pair to trade"
)
parser.add_argument(
"--buy_type",
required=True,
choices=["market", "limit"],
help="define the type of buy order to execute: limit or market"
)
parser.add_argument(
"--total",
required=False,
help="define the total amount to spend"
)
parser.add_argument(
"--quantity",
required=False,
help="define the quantity to buy (decimal number)"
)
parser.add_argument(
"--price",
required=False,
help="define the unit price to spend"
)
parser.add_argument(
"--profit",
required=False,
help="define the profit to make in percentage between 0 and 100"
)
parser.add_argument(
"--loss",
required=False,
help="define the stoploss in percentage between 0 and 100"
)
args = vars(parser.parse_args())
if args["buy_type"] == "market":
input_args_validated = input_validation(args, MarketInputArgs)
elif args["buy_type"] == "limit":
input_args_validated = input_validation(args, LimitInputArgs)
else:
sys.exit("The buy type argument is unknown")
main(input_args=input_args_validated)