-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrading.py
More file actions
135 lines (90 loc) · 6.03 KB
/
trading.py
File metadata and controls
135 lines (90 loc) · 6.03 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
135
#!/usr/bin/python
import sqlite3
import json
import time
from constants.constants import (
DB_FILE_NAME
)
from classes.wallet import UserWallet
from classes.wallets import UserWallets
from classes.swap_transaction import SwapTransaction, swap_coins
from classes.transaction_core import TransactionResult
from terra_classic_sdk.core.coin import Coin
def main():
conn = sqlite3.connect(DB_FILE_NAME)
conn.row_factory = sqlite3.Row
print ("Opened database successfully")
get_open_trades = "SELECT ID, date_added, wallet_name, coin_from, amount_from, price_from, coin_to, amount_to, price_to, fees, exit_profit, exit_loss FROM trades WHERE status = 'OPEN' and coin_to='urakoff';"
update_trade = "UPDATE trades SET linked_trade_id=?, status=? WHERE ID=?;"
wallets = UserWallets()
user_wallets = wallets.loadUserWallets()
while True:
cursor = conn.cursor()
cursor = conn.execute(get_open_trades)
for open_trades_row in cursor.fetchall():
wallet:UserWallet = None
if open_trades_row['wallet_name'] in user_wallets:
wallet = user_wallets[open_trades_row['wallet_name']]
if wallet is not None:
#print (open_trades_row['exit_profit'])
original_trade_id:int = open_trades_row['ID']
print ('*************')
#print (open_trades_row)
# for x in open_trades_row.keys():
# print (x, open_trades_row[x])
print (f"Wallet: {open_trades_row['wallet_name']} (database row id {original_trade_id})\n")
print (f"You purchased {wallet.formatUluna(open_trades_row['amount_to'], open_trades_row['coin_to'], True)} with {wallet.formatUluna(open_trades_row['amount_from'], open_trades_row['coin_from'], True)}")
incoming_fees = json.loads(open_trades_row['fees'])['LUNC']
# Set up the swap details
swap_tx = SwapTransaction().create(wallet.seed, wallet.denom)
if swap_tx != False:
swap_tx.swap_amount = float(wallet.formatUluna(open_trades_row['amount_to'], open_trades_row['coin_to'], False))
swap_tx.swap_denom = open_trades_row['coin_to']
swap_tx.swap_request_denom = open_trades_row['coin_from']
swap_tx.wallet_denom = wallet.denom
log_trade_params:dict = {}
log_trade_params['exit_profit'] = open_trades_row['exit_profit']
log_trade_params['exit_loss'] = open_trades_row['exit_loss']
# Change the contract depending on what we're doing
swap_tx.setContract()
estimated_value:float = swap_tx.swapRate()
if estimated_value is not None:
profit_target_value = float(wallet.formatUluna(open_trades_row['amount_from'], open_trades_row['coin_from'])) + float(wallet.formatUluna(open_trades_row['amount_from'] * open_trades_row['exit_profit'], open_trades_row['coin_from']))
loss_target_value = float(wallet.formatUluna(open_trades_row['amount_from'], open_trades_row['coin_from'])) + float(wallet.formatUluna(open_trades_row['amount_from'] * open_trades_row['exit_loss'], open_trades_row['coin_from']))
print (f'Not including swap fees, this is now worth {estimated_value}')
print ('---')
print (f'This will be automatically sold at a profit when it is at {profit_target_value}')
print (f'This will be automatically sold at a loss when it is at {loss_target_value}')
print (f'Swap fees are expected to be {float(incoming_fees) * 2}')
# Get the current decision:
swap_coin:Coin = Coin(open_trades_row['coin_to'], open_trades_row['amount_to'])
if estimated_value >= profit_target_value:
print ('WE NEED TO SELL FOR A PROFIT')
wallet.getBalances()
#print ('swap coin:', swap_coin)
#print (open_trades_row['coin_from'])
transaction_result:TransactionResult = swap_coins(wallet, swap_coin, open_trades_row['coin_from'], 0, True, True, log_trade_params)
trade_id:int = transaction_result.trade_id
#print ('closing trade id:', trade_id)
# Update the original trade row with this ID, and mark it as being closed
conn.execute(update_trade, [original_trade_id, 'CLOSED', trade_id])
conn.execute(update_trade, [trade_id, 'CLOSED', original_trade_id])
conn.commit()
transaction_result.showResults()
elif estimated_value <= loss_target_value:
print ('WE NEED TO SELL AT A LOSS')
# transaction_result:TransactionResult = swap_coins(wallet, swap_coin, open_trades_row['coin_from'], '', False, True)
# trade_id:int = transaction_result.trade_id
# # Update the original trade row with this ID, and mark it as being closed
# conn.execute(update_trade, [trade_id, original_trade_id])
# conn.commit()
# transaction_result.showResults()
else:
print ('do nothing, keep waiting')
print ('')
#conn.close()
print ('Finished this round!')
time.sleep(60)
if __name__ == "__main__":
""" This is executed when run from the command line """
main()