-
Notifications
You must be signed in to change notification settings - Fork 5
/
My_Strategy.py
104 lines (86 loc) · 3.85 KB
/
My_Strategy.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
from strategy.Strategy import Strategy
import pandas_ta as pta
from utils import play_sound
from utils.dataIO import is_market_hours
class My_Strategy(Strategy):
"""
Class Name must be the same as the file name
"""
def __init__(self):
super().__init__()
self.strategy_name = 'My_Strategy'
self.cash_balance = 0
self.market_value = 0
self.dayBuyingPower = 0
self.overnightBuyingPower = 0
self.net_account_value = 0
"""
You only need to define the strategy_decision() function, and click run strategy button on the App.
Other functions are defined in the parent class, Strategy.py, you can use them directly.
Define your own strategy here:
"""
def strategy_decision(self):
"""This is a simple example strategy from chatGPT, you can overwrite it in your own strategy"""
# You can extract the code to several functions, and call them in strategy_decision()
# -------------------------
# Strategy starts here:
# 0. define the stock ticker you are going to trade
stock = 'TQQQ'
# 1. get the stock data from quoter, return a pandas dataframe
data = self.quoter.get_15min_bar(stock=stock, extend_trading=True)
# 2. calculate the indicator
data['RSI'] = pta.rsi(data['Close'], length=14)
# 3. check the strategy condition,
# for example, simply buy when rsi < 30, sell when rsi > 70
current_rsi = data['RSI'][-1]
action = None
if current_rsi < 30:
# print for your reference
print(f"{stock}: current_rsi: {current_rsi}, price: {data['Close'][-1]}")
print('buy point start')
print('-------------------------')
action = 'buy'
elif current_rsi > 70:
print(f"{stock}: current_rsi: {current_rsi}, price: {data['Close'][-1]}")
print('sell point start')
print('-------------------------')
action = 'sell'
else:
pass
# 4. place order if needed, using the trader object to place order
action_res = {}
if action == 'buy':
if is_market_hours():
action_res = self.parent.trader.order_market_buy(stock, quantity=1)
else:
price = self.parent.trader.get_bid_price(stock)
action_res = self.parent.trader.order_limit_buy_day(stock, price, quantity=1)
# for GTC order, use self.parent.trader.order_limit_buy_gtc(stock, price, qty)
play_sound.order_placed()
elif action == 'sell':
if is_market_hours():
action_res = self.parent.trader.order_market_sell(stock, quantity=1)
else:
price = self.parent.trader.get_ask_price(stock)
action_res = self.parent.trader.order_limit_sell_gtc(stock, price, quantity=1)
play_sound.order_placed()
else:
# do whatever you want
pass
# Strategy ends here
# -----------------------------
# Set the info you need to show in the GUI
info = f"{data.index[-1].strftime('%Y-%m-%d %H:%M:%S')}, 15min bar\n" \
f"Stock: {stock}\n" \
f"Price: {round(data['Close'][-1], 2)}\n" \
f"RSI: {round(data['RSI'][-1], 2)}\n"
self.update_quoter_stream(info)
if action == 'buy' or action == 'sell':
if action_res['result'] == 'success':
# update the strategy stream after action
self.update_strategy_stream(action_res)
# save the trading history
self.save_strategy_actions(action_res)
self.update_strategy_profile()
# Send the trading action email and notification to the GUI
self.send_notification_via_email(action_res)