Repository navigation
Expand file tree
/
Copy pathPython Valuewhen Function.py
More file actions
41 lines (35 loc) · 1.92 KB
/
Copy pathPython Valuewhen Function.py
File metadata and controls
41 lines (35 loc) · 1.92 KB
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
"""
Sure, here's an example of how you might implement the same functionality in Python using the ta library, which is a technical analysis
library for financial data:
"""
import ta
import pandas as pd
# Load data
data = pd.read_csv("stock_data.csv")
# Calculate RSI
rsi = ta.rsi(data["close"])
# Find pivot lows and pivot highs
pivot_lows = ta.pivot_lows(data["close"], lbL, lbR)
pivot_highs = ta.pivot_highs(data["close"], lbL, lbR)
# Find bullish and bearish signals
regular_bull_signals = (rsi > ta.valuewhen(pivot_lows, rsi, 1)) & (ta.barssince(pivot_lows) >= rangeLower) & (ta.barssince(pivot_lows) <= rangeUpper) & (data["low"] < ta.valuewhen(pivot_lows, data["low"], 1))
hidden_bull_signals = (rsi < ta.valuewhen(pivot_lows, rsi, 1)) & (ta.barssince(pivot_lows) >= rangeLower) & (ta.barssince(pivot_lows) <= rangeUpper) & (data["low"] > ta.valuewhen(pivot_lows, data["low"], 1))
regular_bear_signals = (rsi < ta.valuewhen(pivot_highs, rsi, 1)) & (ta.barssince(pivot_highs) >= rangeLower) & (ta.barssince(pivot_highs) <= rangeUpper) & (data["high"] > ta.valuewhen(pivot_highs, data["high"], 1))
hidden_bear_signals = (rsi > ta.valuewhen(pivot_highs, rsi, 1)) & (ta.barssince(pivot_highs) >= rangeLower) & (ta.barssince(pivot_highs) <= rangeUpper) & (data["high"] < ta.valuewhen(pivot_highs, data["high"], 1))
# Plot the signals
if plotBull:
data["regular_bull_signals"] = regular_bull_signals
if plotHiddenBull:
data["hidden_bull_signals"] = hidden_bull_signals
if plotBear:
data["regular_bear_signals"] = regular_bear_signals
if plotHiddenBear:
data["hidden_bear_signals"] = hidden_bear_signals
# Plotting the data or signals
import matplotlib.pyplot as plt
plt.plot(data["close"])
plt.plot(data["regular_bull_signals"], 'go', markersize=5)
plt.plot(data["hidden_bull_signals"], 'yo', markersize=5)
plt.plot(data["regular_bear_signals"], 'ro', markersize=5)
plt.plot(data["hidden_bear_signals"], 'mo', markersize=5)
plt.show()