-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathcreate_position_tpsl.py
More file actions
84 lines (67 loc) · 2.21 KB
/
create_position_tpsl.py
File metadata and controls
84 lines (67 loc) · 2.21 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
import os
import time
import uuid
import requests
from dotenv import load_dotenv
from solders.keypair import Keypair
from common.constants import REST_URL
from common.utils import sign_message
# Load environment variables
load_dotenv()
# Assume a BTC long position has already been opened
API_URL = f"{REST_URL}/positions/tpsl"
PRIVATE_KEY = os.getenv("PRIVATE_KEY")
def main():
# Generate account based on private key
keypair = Keypair.from_base58_string(PRIVATE_KEY)
public_key = str(keypair.pubkey())
# Scaffold the signature header
timestamp = int(time.time() * 1_000)
signature_header = {
"timestamp": timestamp,
"expiry_window": 5_000,
"type": "set_position_tpsl",
}
# Construct the signature payload
signature_payload = {
"symbol": "BTC",
"side": "ask",
"take_profit": {
"stop_price": "120000",
"limit_price": "120300",
"amount": "0.1",
"client_order_id": str(uuid.uuid4()),
},
"stop_loss": {
"stop_price": "99800",
# omitting limit_price to place a market order at trigger
# omitting amount to use the full position size
# client_order_id is optional
},
}
# Use the helper function to sign the message
message, signature = sign_message(signature_header, signature_payload, keypair)
# Construct the request reusing the payload and constructing common request fields
request_header = {
"account": public_key,
"signature": signature,
"timestamp": signature_header["timestamp"],
"expiry_window": signature_header["expiry_window"],
}
# Send the request
headers = {"Content-Type": "application/json"}
request = {
**request_header,
**signature_payload,
}
response = requests.post(API_URL, json=request, headers=headers)
print(f"Status Code: {response.status_code}")
print(f"Response: {response.text}")
print(f"Request: {request}")
# Print details for debugging
print("\nDebug Info:")
print(f"Address: {public_key}")
print(f"Message: {message}")
print(f"Signature: {signature}")
if __name__ == "__main__":
main()