netflypsb commited on
Commit
4a93688
1 Parent(s): aee56f7

Create signals/strategy.py

Browse files
Files changed (1) hide show
  1. signals/strategy.py +46 -0
signals/strategy.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # signals/strategy.py
2
+
3
+ import pandas as pd
4
+
5
+ def generate_buy_signals(data_4h, data_1h):
6
+ """
7
+ Generates buy signals based on specified criteria.
8
+
9
+ Parameters:
10
+ - data_4h: DataFrame containing 4-hour interval stock data with SMA and price columns.
11
+ - data_1h: DataFrame containing 1-hour interval stock data with SMA and price columns.
12
+
13
+ Returns:
14
+ - buy_signals: DataFrame containing timestamps and signals where buy conditions are met.
15
+ """
16
+ # Criteria 1 & 2 for 4-hour data
17
+ criteria_4h = (data_4h['SMA_21'] > data_4h['SMA_50'])
18
+
19
+ # Criteria 3 & 4 for 1-hour data
20
+ crossed_above = (data_1h['SMA_21'].shift(2) < data_1h['SMA_50'].shift(2)) & (data_1h['SMA_21'] > data_1h['SMA_50'])
21
+ was_below = (data_1h['SMA_21'].shift(15) < data_1h['SMA_50'].shift(15))
22
+
23
+ # Combine criteria
24
+ buy_signals = data_1h[crossed_above & was_below & criteria_4h.reindex(data_1h.index, method='nearest')]
25
+
26
+ return buy_signals[['SMA_21', 'SMA_50']]
27
+
28
+ def generate_sell_signals(data_4h):
29
+ """
30
+ Generates sell signals based on specified criteria.
31
+
32
+ Parameters:
33
+ - data_4h: DataFrame containing 4-hour interval stock data with Bollinger Bands and price columns.
34
+
35
+ Returns:
36
+ - sell_signals: DataFrame containing timestamps and signals where sell conditions are met.
37
+ """
38
+ # Criteria for sell signal
39
+ crossed_above_bb = data_4h['Close'] > data_4h['BB_Upper']
40
+
41
+ sell_signals = data_4h[crossed_above_bb]
42
+
43
+ return sell_signals[['Close', 'BB_Upper']]
44
+
45
+ # Example usage would require actual loaded data with the appropriate columns calculated.
46
+ # This example assumes `data_4h` and `data_1h` DataFrames are prepared and include 'Close', 'SMA_21', 'SMA_50', and Bollinger Bands columns.