File size: 4,011 Bytes
46ebf5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Defines candlestick indicators, and creates data-points for a dataset when an indicator is detected.

import streamlit as st
from collections import defaultdict


def price_at_index(index, dates, dataset):
    price = defaultdict(float)

    opens = dataset['Open']
    closes = dataset['Close']
    highs = dataset['High']
    lows = dataset['Low']

    price['Date'] = dates[index]
    price['Open'] = opens[index]
    price['Close'] = closes[index]
    price['High'] = highs[index]
    price['Low'] = lows[index]
    return price


def candle_is_green(current_price):
    return current_price['Open'] < current_price['Close']


def engulfing_candle(prev_price, current_price):
    prev_is_green = candle_is_green(prev_price)
    current_is_green = candle_is_green(current_price)
    from_negative_to_positive = not prev_is_green and current_is_green
    from_positive_to_negative = prev_is_green and not current_is_green
    is_bullish_engulfing = from_negative_to_positive and current_price['Close'] > prev_price['Open'] and current_price['Open'] < prev_price['Close']
    is_bearish_engulfing = from_positive_to_negative and current_price['Close'] < prev_price['Open'] and current_price['Open'] > prev_price['Close']
    return is_bullish_engulfing, is_bearish_engulfing


def engulfing_candle_bullish(prev_price, current_price):
    prev_is_green = candle_is_green(prev_price)
    current_is_green = candle_is_green(current_price)
    from_negative_to_positive = not prev_is_green and current_is_green
    is_bullish_engulfing = from_negative_to_positive and current_price['Close'] > prev_price['Open'] and current_price['Open'] < prev_price['Close']
    return is_bullish_engulfing


def engulfing_candle_bearish(prev_price, current_price):
    prev_is_green = candle_is_green(prev_price)
    current_is_green = candle_is_green(current_price)
    from_positive_to_negative = prev_is_green and not current_is_green
    is_bearish_engulfing = from_positive_to_negative and current_price['Close'] < prev_price['Open'] and current_price['Open'] > prev_price['Close']
    return is_bearish_engulfing


def create_engulfing_candle_bullish_indicators(dates, dataset):
    indicator = defaultdict(list)
    indicator_timestamps = indicator['Date']
    indicator_values = indicator['Values']
    
    prev_price = price_at_index(0, dates, dataset)
    for index in range(1, len(dates)):
        price = price_at_index(index, dates, dataset)
        is_engulfing = engulfing_candle_bullish(prev_price, price)
        if is_engulfing:
            indicator_timestamps.append(dates[index])
            offset = ((price['Close'] - price['Open']) - (prev_price['Open'] - prev_price['Close'])) * 5
            value = price['Close'] + offset
            indicator_values.append(value)
        prev_price = price

    indicator_dict = dict(indicator)
    indicator_dict['IsBullish'] = True

    return indicator_dict


def create_engulfing_candle_bearish_indicators(dates, dataset):
    indicator = defaultdict(list)
    indicator_timestamps = indicator['Date']
    indicator_values = indicator['Values']
    
    prev_price = price_at_index(0, dates, dataset)
    for index in range(1, len(dates)):
        price = price_at_index(index, dates, dataset)
        is_engulfing = engulfing_candle_bearish(prev_price, price)
        if is_engulfing:
            indicator_timestamps.append(dates[index])
            offset = ((price['Open'] - price['Close']) - (prev_price['Close'] - prev_price['Open'])) * 5
            value = price['Close'] - offset
            indicator_values.append(value)
        prev_price = price

    indicator_dict = dict(indicator)
    indicator_dict['IsBullish'] = False 

    return indicator_dict


def create_indicators(dates, dataset):
    indicators = defaultdict(dict)
    indicators['Engulfing Bullish'] = create_engulfing_candle_bullish_indicators(dates, dataset)
    indicators['Engulfing Bearish'] = create_engulfing_candle_bearish_indicators(dates, dataset)
    return indicators