-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrun_test_trading.py
More file actions
94 lines (77 loc) · 2.96 KB
/
Copy pathrun_test_trading.py
File metadata and controls
94 lines (77 loc) · 2.96 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
from src.solana_trader import SolanaTrader
import os
import logging
import time
import random
from datetime import datetime, timezone
import json
from dotenv import load_dotenv
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s'
)
logger = logging.getLogger(__name__)
def simulate_trade(trader):
"""Simulate a trade with random profit/loss"""
try:
# Random trade parameters
amount = random.uniform(0.1, 1.0)
price = random.uniform(70, 80)
base_profit = random.uniform(-5, 15)
# 1% chance of moonshot
if random.random() < 0.01:
base_profit *= random.uniform(50, 200)
# Execute trade
trade = {
'timestamp': datetime.now(timezone.utc).isoformat(),
'token': f"TOKEN{random.randint(1,5)}",
'type': "BUY" if random.random() > 0.5 else "SELL",
'amount': amount,
'price': price,
'profit': base_profit,
'status': 'COMPLETED'
}
# Update trade history
trader.trade_history['test_trades'].append(trade)
# Update wallet balance
wallet_path = os.path.join('database', 'wallet.json')
with open(wallet_path, 'r') as f:
wallet_data = json.load(f)
new_balance = wallet_data['balance'] + base_profit
wallet_data['balance'] = new_balance
wallet_data['last_updated'] = datetime.now(timezone.utc).isoformat()
with open(wallet_path, 'w') as f:
json.dump(wallet_data, f, indent=4)
# Update trade history
trader.trade_history['portfolio']['total_value'] = new_balance
with open(trader.trade_history_path, 'w') as f:
json.dump(trader.trade_history, f, indent=4)
logger.info(f"Trade executed: {trade['type']} {trade['token']} - Profit/Loss: ${base_profit:.2f}")
logger.info(f"New balance: ${new_balance:.2f}")
return True
except Exception as e:
logger.error(f"Error simulating trade: {e}")
return False
def main():
"""Start test trading with $500"""
try:
# Load environment variables
load_dotenv()
# Initialize trader
trader = SolanaTrader(
wallet_address=os.getenv('WALLET_ADDRESS'),
private_key=os.getenv('PRIVATE_KEY'),
ocean_config_path=os.path.join('config', 'ocean.json')
)
logger.info("Starting test trading with $500 initial balance...")
logger.info("Simulating trades every 10 seconds...")
# Start trading loop
while True:
simulate_trade(trader)
time.sleep(10) # Wait 10 seconds between trades
except Exception as e:
logger.error(f"Error in test trading: {e}")
raise
if __name__ == '__main__':
main()