goldhand 15.1__tar.gz → 15.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of goldhand might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: goldhand
3
- Version: 15.1
3
+ Version: 15.3
4
4
  Summary: A package working with financial data
5
5
  Home-page: https://github.com/misrori/goldhand
6
6
  Author: Mihaly
@@ -21,6 +21,9 @@ class GoldHand:
21
21
 
22
22
 
23
23
  def get_olhc(self):
24
+ """
25
+ Download historical stock data for the last year
26
+ """
24
27
  #scraper = cloudscraper.create_scraper()
25
28
  response = self.scraper.get(f"https://query1.finance.yahoo.com/v8/finance/chart/{self.ticker}?interval={self.interval}&range={self.range}")
26
29
  t= response.json()
@@ -32,6 +35,9 @@ class GoldHand:
32
35
  return(df)
33
36
 
34
37
  def smma(self, data, window, colname):
38
+ """
39
+ Calculate Smoothed Moving Average (SMMA)
40
+ """
35
41
  hl2 = data['hl2'].values
36
42
  smma_values = [hl2[0]]
37
43
 
@@ -141,7 +147,7 @@ class GoldHand:
141
147
  else:
142
148
  temj = '😭💔'
143
149
  self.df.loc[states[i], 'local_text'] = f'{temj}{fall}%<br>${round(current_low, 2)}'
144
-
150
+ self.df.reset_index(inplace=True, drop=True)
145
151
 
146
152
  def plotly_last_year(self, plot_title, plot_height=900, ndays=500, ad_local_min_max=True):
147
153
  tdf = self.df.tail(ndays)
@@ -8,11 +8,13 @@ from goldhand import *
8
8
 
9
9
 
10
10
 
11
- def goldhand_line_strategy(data):
11
+ def goldhand_line_strategy(data, buy_at='gold', sell_at='grey'):
12
12
  """
13
13
  This function implements the goldhand line strategy.
14
14
  Parameters:
15
15
  data (pandas.DataFrame): The dataframe containing the data.
16
+ buy_at (str): The color of the line to buy at. Default is 'gold'.
17
+ sell_at (str): The color of the line to sell at. Default is 'grey'.
16
18
  Returns:
17
19
  res_df (pandas.DataFrame): The dataframe containing the results.
18
20
  """
@@ -52,23 +54,33 @@ def goldhand_line_strategy(data):
52
54
  # Check if not already in a trade
53
55
  if not in_trade:
54
56
  # Generate buy signal
55
- if (data['color'][i] =='gold') :
56
-
57
- temp_trade['buy_price'] = data['close'][i]
57
+ if (data['color'][i] ==buy_at) :
58
+
59
+ if i == (len(data) -1):
60
+ temp_trade['buy_price'] = data['close'][i]
61
+ temp_trade.update(dict(data.iloc[i].add_prefix('buy_')))
62
+ else:
63
+ temp_trade['buy_price'] = data['open'][i+1]
64
+ temp_trade.update(dict(data.iloc[i+1].add_prefix('buy_')))
65
+
66
+
58
67
  temp_trade['trade_id'] = trade_id
59
68
  temp_trade['status'] = 'open'
60
- temp_trade.update(dict(data.iloc[i].add_prefix('buy_')))
61
69
  in_trade = True # Set flag to indicate in a trade
62
70
  else:
63
71
  # Generate sell signal
64
- if (data['color'][i] =='grey') :
65
-
66
- temp_trade['sell_price'] = data['close'][i]
72
+ if (data['color'][i] ==sell_at) :
73
+
74
+ if i == (len(data) -1):
75
+ temp_trade['sell_price'] = data['close'][i]
76
+ temp_trade.update(dict(data.iloc[i].add_prefix('sell_')))
77
+ else:
78
+ temp_trade['sell_price'] = data['open'][i+1]
79
+ temp_trade.update(dict(data.iloc[i+1].add_prefix('sell_')))
80
+
67
81
  temp_trade['trade_id'] = trade_id
68
82
  temp_trade['status'] = 'closed'
69
83
 
70
- temp_trade.update(dict(data.iloc[i].add_prefix('sell_')))
71
-
72
84
  # calculate results
73
85
  temp_trade['result'] = temp_trade['sell_price'] / temp_trade['buy_price']
74
86
  temp_trade['days_in_trade'] = (temp_trade['sell_date'] - temp_trade['buy_date']).days
@@ -98,7 +110,7 @@ def goldhand_line_strategy(data):
98
110
  return(res_df)
99
111
 
100
112
 
101
- def show_indicator_goldhand_line_strategy(ticker, plot_title = '', ndays=0, plot_height=1000, add_strategy_summary = True):
113
+ def show_indicator_goldhand_line_strategy(ticker, plot_title = '', buy_at='gold', sell_at='grey', ndays=0, plot_height=1000, add_strategy_summary = True):
102
114
  """
103
115
  This function shows the goldhand line strategy on a plotly candlestick chart.
104
116
  Parameters:
@@ -147,11 +159,10 @@ def show_indicator_goldhand_line_strategy(ticker, plot_title = '', ndays=0, plot
147
159
  # Create a 'group' column and increase the value only when there's a color change
148
160
  data['group'] = (data['color_change']).cumsum()
149
161
 
150
-
151
162
  ##### data prepar end
152
163
 
153
164
  ##### backtest
154
- backtest = Backtest( data, goldhand_line_strategy, plot_title =plot_title)
165
+ backtest = Backtest( data, goldhand_line_strategy, plot_title =plot_title, buy_at='gold', sell_at='grey')
155
166
  trades =backtest.trades
156
167
 
157
168
  if ndays!=0:
@@ -20,33 +20,43 @@ def rsi_strategy(data, buy_threshold = 30, sell_threshold = 70):
20
20
  # Check if not already in a trade
21
21
  if not in_trade:
22
22
  # Generate buy signal
23
+ #You have to change olne the buy and sell signal
23
24
  if data['rsi'][i] < buy_threshold:
24
25
 
25
- temp_trade['buy_price'] = data['close'][i]
26
+ if i == (len(data) -1):
27
+ temp_trade['buy_price'] = data['close'][i]
28
+ temp_trade.update(dict(data.iloc[i].add_prefix('buy_')))
29
+ else:
30
+ temp_trade['buy_price'] = data['open'][i+1]
31
+ temp_trade.update(dict(data.iloc[i+1].add_prefix('buy_')))
32
+
26
33
  temp_trade['trade_id'] = trade_id
27
34
  temp_trade['status'] = 'open'
28
- temp_trade.update(dict(data.iloc[i].add_prefix('buy_')))
29
35
  in_trade = True # Set flag to indicate in a trade
30
36
  else:
31
37
  # Generate sell signal
38
+ #You have to change olne the buy and sell signal
32
39
  if data['rsi'][i] > sell_threshold:
33
-
34
- temp_trade['sell_price'] = data['close'][i]
40
+ if i == (len(data) -1):
41
+ temp_trade['sell_price'] = data['close'][i]
42
+ temp_trade.update(dict(data.iloc[i].add_prefix('sell_')))
43
+ else:
44
+ temp_trade['sell_price'] = data['open'][i+1]
45
+ temp_trade.update(dict(data.iloc[i+1].add_prefix('sell_')))
46
+
35
47
  temp_trade['trade_id'] = trade_id
36
48
  temp_trade['status'] = 'closed'
37
49
 
38
- temp_trade.update(dict(data.iloc[i].add_prefix('sell_')))
39
-
50
+
40
51
  # calculate results
41
52
  temp_trade['result'] = temp_trade['sell_price'] / temp_trade['buy_price']
42
53
  temp_trade['days_in_trade'] = (temp_trade['sell_date'] - temp_trade['buy_date']).days
43
54
 
44
-
45
-
46
55
  in_trade = False # Reset flag to indicate not in a trade
47
56
  trade_id +=1
48
57
  all_trades.append(temp_trade)
49
58
  temp_trade = {}
59
+
50
60
  if temp_trade:
51
61
  temp_trade['sell_price'] = data['close'][i]
52
62
  temp_trade['trade_id'] = trade_id
@@ -59,7 +69,6 @@ def rsi_strategy(data, buy_threshold = 30, sell_threshold = 70):
59
69
  res_df = pd.DataFrame(all_trades)
60
70
 
61
71
  # change orders
62
-
63
72
  all_col = res_df.columns.tolist()
64
73
  first = ['result', 'buy_price', 'sell_price', 'buy_date', 'sell_date', 'days_in_trade']
65
74
  first.extend([x for x in all_col if x not in first])
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: goldhand
3
- Version: 15.1
3
+ Version: 15.3
4
4
  Summary: A package working with financial data
5
5
  Home-page: https://github.com/misrori/goldhand
6
6
  Author: Mihaly
@@ -8,7 +8,7 @@ long_description = (this_directory / "README.md").read_text()
8
8
 
9
9
  setup(
10
10
  name="goldhand",
11
- version="15.1",
11
+ version="15.3",
12
12
  author="Mihaly",
13
13
  author_email="ormraat.pte@gmail.com",
14
14
  description="A package working with financial data",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes