-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconditions.py
More file actions
186 lines (151 loc) · 4.45 KB
/
Copy pathconditions.py
File metadata and controls
186 lines (151 loc) · 4.45 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""
Declarative Conditions API — every condition builder and combinator.
Demonstrates:
- and_, or_, not_ combinators
- rsi_above, rsi_below, price_above, price_below
- crossed_above, crossed_below
- ema_above, ema_below, ema_crossed_above
- supertrend, PSAR, Donchian, Ichimoku
- MACD + price-change conditions (from Binance data via ctx.binance)
- custom when() wrapper
- Operator shortcuts: &, |, ~
Usage:
python examples/conditions.py
"""
from collections import namedtuple
import pandas as pd
from polyalpha.conditions import (
always,
and_,
crossed_below,
ema_above,
ema_crossed_above,
ichimoku_bullish_breakout,
macd_above_zero,
macd_bullish_crossover,
never,
not_,
or_,
price_above,
price_above_dc_upper,
price_below,
price_change_below,
price_down,
price_in_range,
price_up,
psar_uptrend,
rsi_above,
rsi_below,
supertrend_up,
when,
)
entry_condition = and_(
rsi_above(50),
price_above("UP", 0.85),
or_(macd_bullish_crossover(), supertrend_up(7, 3.0)),
)
exit_condition = or_(
rsi_below(30),
price_below("UP", 0.75),
crossed_below("UP", 0.80),
)
momentum_condition = and_(
price_up(1),
ema_above("UP", 20),
)
price_band = and_(
price_in_range("UP", 0.40, 0.60),
not_(price_below("DOWN", 0.10)),
)
ichimoku_condition = ichimoku_bullish_breakout("UP", 9, 26, 52)
def custom_balance_check(ctx):
return ctx.balance > 50 and ctx.trade_count < 10
custom = and_(
when(custom_balance_check),
rsi_above(40),
)
operator_style = rsi_above(50) & price_above("UP", 0.85) & ~crossed_below("UP", 0.80)
conditions = [
("Entry (function style)", entry_condition),
("Exit", exit_condition),
("Momentum", momentum_condition),
("Price band", price_band),
("Ichimoku breakout", ichimoku_condition),
("Custom when()", custom),
("Operator style (&, |, ~)", operator_style),
("always()", always()),
("never()", never()),
("price_up(1)", price_up(1)),
("price_down(1)", price_down(1)),
("price_change_below(2.0)", price_change_below(2.0)),
("ema_above('DOWN', 20)", ema_above("DOWN", 20)),
("ema_crossed_above(9, 21)", ema_crossed_above(9, 21)),
("macd_bullish_crossover()", macd_bullish_crossover()),
("macd_above_zero()", macd_above_zero()),
("psar_uptrend()", psar_uptrend()),
("price_above_dc_upper('UP', 20)", price_above_dc_upper("UP", 20)),
]
class _FakeIndicators:
@staticmethod
def ema(period=20):
return 0.83
@staticmethod
def supertrend(period=7, multiplier=3.0):
return pd.DataFrame({"direction": [1, 1, 1]})
@staticmethod
def psar(af=0.02, af_max=0.2):
return pd.DataFrame({"trend": [1, 1, 1]})
@staticmethod
def donchian(length=20):
D = namedtuple("DonchianResult", ["upper", "mid", "lower"])
return D(0.95, 0.85, 0.75)
@staticmethod
def ichimoku(tenkan=9, kijun=26, senkou=52):
return {
"tenkan": pd.Series([0.88, 0.87, 0.89]),
"kijun": pd.Series([0.85, 0.84, 0.86]),
"cloud": {
"top": pd.Series([0.86, 0.86, 0.87]),
"bottom": pd.Series([0.80, 0.81, 0.82]),
},
}
@staticmethod
def get_latest_value(series):
return float(series.iloc[-1]) if hasattr(series, "iloc") else float(series)
class _FakeBinance:
M = namedtuple("MACDResult", ["macd", "signal", "histogram"])
def macd(self, fast=12, slow=26, signal=9):
return self.M(0.02, 0.015, 0.005)
def price_above_by(self, min_change, candles_back=1):
return True
def price_change(self, candles_back=1):
return 3.0
def price_up(self, candles_back=1):
return True
class _FakePrice:
up = 0.88
down = 0.12
class FakeContext:
balance = 100
trade_count = 5
rsi = 55.0
price = _FakePrice()
indicators = _FakeIndicators()
binance = _FakeBinance()
_cross_state = {}
seconds_in = 120
candle_id = 42
def buy(self, side, amount):
pass
ctx = FakeContext()
for name, condition in conditions:
result = condition(ctx)
status = "PASS" if result else "FAIL"
print(f"[{status}] {name}")
print("\nChained example:")
chained = and_(
rsi_above(50),
price_above("UP", 0.85),
or_(macd_bullish_crossover(), psar_uptrend()),
)
print(f" rsi_above(50) & price_above('UP', 0.85) & (macd_bullish_crossover | psar_uptrend) => {chained(ctx)}")