File size: 1,729 Bytes
4a93688
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# signals/strategy.py

import pandas as pd

def generate_buy_signals(data_4h, data_1h):
    """
    Generates buy signals based on specified criteria.

    Parameters:
    - data_4h: DataFrame containing 4-hour interval stock data with SMA and price columns.
    - data_1h: DataFrame containing 1-hour interval stock data with SMA and price columns.

    Returns:
    - buy_signals: DataFrame containing timestamps and signals where buy conditions are met.
    """
    # Criteria 1 & 2 for 4-hour data
    criteria_4h = (data_4h['SMA_21'] > data_4h['SMA_50'])

    # Criteria 3 & 4 for 1-hour data
    crossed_above = (data_1h['SMA_21'].shift(2) < data_1h['SMA_50'].shift(2)) & (data_1h['SMA_21'] > data_1h['SMA_50'])
    was_below = (data_1h['SMA_21'].shift(15) < data_1h['SMA_50'].shift(15))

    # Combine criteria
    buy_signals = data_1h[crossed_above & was_below & criteria_4h.reindex(data_1h.index, method='nearest')]

    return buy_signals[['SMA_21', 'SMA_50']]

def generate_sell_signals(data_4h):
    """
    Generates sell signals based on specified criteria.

    Parameters:
    - data_4h: DataFrame containing 4-hour interval stock data with Bollinger Bands and price columns.

    Returns:
    - sell_signals: DataFrame containing timestamps and signals where sell conditions are met.
    """
    # Criteria for sell signal
    crossed_above_bb = data_4h['Close'] > data_4h['BB_Upper']

    sell_signals = data_4h[crossed_above_bb]

    return sell_signals[['Close', 'BB_Upper']]

# Example usage would require actual loaded data with the appropriate columns calculated.
# This example assumes `data_4h` and `data_1h` DataFrames are prepared and include 'Close', 'SMA_21', 'SMA_50', and Bollinger Bands columns.