-
Notifications
You must be signed in to change notification settings - Fork 1
/
trade_crypto3.py
140 lines (88 loc) · 2.82 KB
/
trade_crypto3.py
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
135
136
137
138
139
140
# this script stores the crypto orders each hour
import os
import sys
from datetime import datetime, timedelta
import time
import pandas as pd
import numpy as np
import mysql.connector
import subprocess
import pathlib
# print(pd.__version__)
# exit()
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 100)
from datetime import date, timedelta
#connect to database
con = mydb = mysql.connector.connect(user='root', password='waWWii21156!', host='127.0.0.1', database='algos', allow_local_infile=True)
# create orders table
table = "orders"
try:
sql = "create table if not exists " +table+ " (clientid varchar(100) NOT NULL,created varchar(100), price float, filled_qty float, side varchar(10), symbol varchar(10), status varchar(20),PRIMARY KEY (clientid) )"
print(sql)
cursor= con.cursor()
cursor.execute(sql)
con.commit()
cursor.close()
except:
pass
today = date.today()
yesterday = today - timedelta(days = 1)
today = str(today)
mdate = today.replace("-","")
# for testing set today to past date
today = '2020-11-20'
timestamp = int(time.time())
localtime = time.ctime(timestamp)
ux = timestamp
starttime = time.time()
ux = timestamp
uxs = str(ux)
# ------- alpaca keys ----------------------------
#pta7p key1 = PKULWOIOXYLHBFOX3O05 key2 = G5HYWE3ZUpLAEYWVB4QcTZNISbne46E3YqG2GDrW algo3 - live
print("this algo is running in pta7p")
key1 = 'PKULWOIOXYLHBFOX3O05'
key2 = 'G5HYWE3ZUpLAEYWVB4QcTZNISbne46E3YqG2GDrW'
endpoint = "https://paper-api.alpaca.markets"
#print(key1)
#print(key2)
#print(endpoint)
os.environ["APCA_API_BASE_URL"] = endpoint
os.environ["APCA_API_KEY_ID"] = key1
os.environ["APCA_API_SECRET_KEY"] = key2
import alpaca_trade_api as tradeapi
api = tradeapi.REST()
# --------------- get orders -----------------
#time.sleep(2)
closed_orders = api.list_orders(
status='all',
limit= 100
)
#print(closed_orders)
for o in closed_orders:
clientid = o.client_order_id
created_date = o.created_at
price = o.filled_avg_price
filled_qty = o.filled_qty
side = o.side
symbol = o.symbol
status = o.status
print("Clientid: ", clientid, " Date: ",created_date," Price: ",price, " Filled Qty: ",filled_qty, " Side: ", side, " Symbol: ",symbol, " Status: ",status)
tradeid = str(clientid)
created_date = str(created_date)
newdate = created_date[0:19]
print(newdate)
if price == None:
price = '0'
if filled_qty == None:
filled_qty = '0'
price = str(price)
filled_qty = str(filled_qty)
clientid = str(clientid)
cdate = str(created_date)
qy1 = "replace into " + table + " (clientid, created, price , filled_qty, side , symbol, status) values ('" +clientid+ "','" + created_date+ "','" + price + "','" + filled_qty+ "','" + side + "','" + symbol+ "','" + status+ "')"
print(qy1)
cursor = con.cursor()
cursor.execute(qy1)
con.commit()
cursor.close()