Measured by the engine 0.0.2, after costs. A record of a simulation on past data, not a forecast.
Illustration only, not a result of real trading. OrionQuant is software, not a financial service, and gives no investment advice.
The code The strategy as the app opens it.
python Copy # Internal bar strength. Where the close sits in the day's range, from 0 at the
# low to 1 at the high. Buy a close near the low, optionally only while the close
# is above its long average, and sell a close near the high. Long only. The stop
# sits `stopAtr` average true ranges away and the size risks `riskPercent` of the
# balance to that stop.
class InternalBarStrength(Strategy):
PARAMETERS = [
{"key": "buyBelow", "label": "Buy when the close is below this part of the range (0 to 1)", "default": 0.2, "min": 0.05, "max": 0.5},
{"key": "exitAbove", "label": "Sell when the close is above this part of the range (0 to 1)", "default": 0.8, "min": 0.3, "max": 0.95},
{"key": "trendPeriod", "label": "Uptrend average (days, 0 for none)", "default": 200, "min": 0, "max": 400},
{"key": "atrPeriod", "label": "ATR period", "default": 20, "min": 5, "max": 60},
{"key": "stopAtr", "label": "Stop distance (ATR)", "default": 3.0, "min": 1.0, "max": 8.0},
{"key": "riskPercent", "label": "Risk per trade (% of balance)", "default": 2.0, "min": 0.1, "max": 3.0},
]
def __init__(self, params):
self.buy_below = float(params.get("buyBelow", 0.2))
self.exit_above = float(params.get("exitAbove", 0.8))
self.trend = int(params.get("trendPeriod", 200))
self.atr_period = int(params.get("atrPeriod", 20))
self.stop_atr = float(params.get("stopAtr", 3.0))
self.risk = float(params.get("riskPercent", 2.0))
self.long = False
def on_bar(self, ctx):
bars = ctx.bars(1)
atr = ctx.indicators.atr(self.atr_period)
ma = ctx.indicators.sma(self.trend) if self.trend > 0 else None
if not bars or atr is None or (self.trend > 0 and ma is None):
return
bar = bars[-1]
span = bar.high - bar.low
if span <= 0:
return
ibs = (bar.close - bar.low) / span
stop = atr * self.stop_atr / ctx.pip()
if ibs < self.buy_below and (ma is None or bar.close > ma):
self.long = True
elif ibs > self.exit_above:
self.long = False
if self.long:
ctx.signal("LONG", sl_pips=stop, risk_percent=self.risk)
else:
ctx.signal("EXIT")