diff options
author | loit <michael.foiani@gmail.com> | 2025-07-28 22:08:51 -0400 |
---|---|---|
committer | loit <michael.foiani@gmail.com> | 2025-07-28 22:08:51 -0400 |
commit | 2411108fe1783d5f03edaa57dad16804b2ce0445 (patch) | |
tree | 0d126a0fc7af4ef2dfac622d65f26d71d55fe690 /ema.py | |
parent | 1ed62b0e315ca1fc97b3ba8752db24e0bebd706f (diff) |
work on functionality to change parameters of charts and refactor some files out
Diffstat (limited to 'ema.py')
-rw-r--r-- | ema.py | 63 |
1 files changed, 63 insertions, 0 deletions
@@ -0,0 +1,63 @@ +def compute_buy_sell_signals_ema(period1, period2, prices): + ema_5 = calc_emas(5, prices) + ema_13 = calc_emas(13, prices) + + +def calc_first_sma(period, prices): + prices_sum = 0 + for i in range(0, period): + prices_sum += prices[i] # 0, 1, 2, 3 ("popping" order) + # print('prices_sum:\t', prices_sum) + + return prices_sum / period + +def calc_emas(period, prices): + weighted_multiplier = 2.0 / (period + 1.0) + + # calculate the first ema + first_ema = calc_first_sma(period, prices) + + # calculate the rest ema's using that first + emas = [first_ema] * period + for i in range(period + 1, len(prices)): # 4, 5, 6, ... , last + last_ema = emas[-1] + if prices[i] == None or prices[i] == 0: + print(i) + next_ema = prices[i] * weighted_multiplier + last_ema * (1 - weighted_multiplier) + emas.append(next_ema) + return emas + +def calculate_profit(buy_line, sell_line, prices, timestamps, offset=0, starting_money=10000): + if len(buy_line) != len(sell_line): + print("ERROR IN find_intersections: len of arrs not the same") + return [] + is_bought = False + curr_money = 10000 + shares_owned = 0 + buy_info = [] # coming in, (time, cash, # current shares, # shares to buy) + sell_info = [] # (time, cash, # current shares, # shares to sell,) + for i in range(offset, len(buy_line)): + current_b1 = buy_line[i] + current_sl = sell_line[i] + # if the sign is positive, we want to hold, if it's negative, we want to sell + sign_signal = current_b1 - current_sl + + if sign_signal > 0: + if not is_bought: + # buy the stock + shares_owned = curr_money / prices[i] + curr_money = 0 + buy_info.append((timestamps[i], prices[i], i)) + is_bought = True + if sign_signal < 0: + if is_bought: + # selling the stock + curr_money = prices[i] * shares_owned + shares_owned = 0 + sell_info.append((timestamps[i], prices[i], i)) + is_bought = False + + # TODO: consider end interval + total_assets = prices[-1] * shares_owned + curr_money + percent_gain = (total_assets - starting_money) / starting_money + return (percent_gain, total_assets, starting_money, buy_info, sell_info)
\ No newline at end of file |