Keltner breakout from the indicator's own numbers
Reads the Keltner channel from `indicators/keltner_channel.py` on its own 5-minute AAPL bars, so the strategy decides on exactly the lines the chart draws. It enters on a tick that breaks the upper line while the bar is still forming, and exits when a closed bar falls back to the middle line.
Reads the Keltner channel from indicators/keltner_channel.py on its own 5-minute AAPL bars, so the strategy decides on exactly the lines the chart draws. It enters on a tick that breaks the upper line while the bar is still forming, and exits when a closed bar falls back to the middle line.
Shows: importing an indicator class from the workspace, line.indicator(cls, **params), forming=True from @on.tick, @on.bar(line) next to @on.tick, the indicator's inputs passed as keywords.
"""Keltner breakout from the indicator's own numbers
Reads the Keltner channel from `indicators/keltner_channel.py` on its own
5-minute AAPL bars, so the strategy decides on exactly the lines the chart
draws. It enters on a tick that breaks the upper line while the bar is still
forming, and exits when a closed bar falls back to the middle line.
Shows: importing an indicator class from the workspace, line.indicator(cls, **params), forming=True from @on.tick, @on.bar(line) next to @on.tick, the indicator's inputs passed as keywords.
"""
from decimal import Decimal
import while25
from while25 import Bars, Strategy, on
from while25.inputs import Float, Int
from indicators.keltner_channel import KeltnerChannel
KEY = "EQ:US:AAPL"
TAG = "aapl_keltner"
def last(series):
"""The newest value of an indicator series, or None while it warms up."""
return series[-1] if series else None
class KeltnerBreakout(Strategy):
id = "aapl_keltner_breakout"
label = "AAPL Keltner breakout"
inputs = [Int("length", default=20, minimum=2, maximum=200),
Float("mult", default=2.0, minimum=0.5, maximum=5.0, step=0.1),
Int("shares", default=10, minimum=1, maximum=10_000)]
aapl = Bars(KEY, "5m", keep=300)
def channel(self, forming):
p = while25.params
return self.aapl.indicator(KeltnerChannel, forming=forming,
length=p["length"], atr_length=10, mult=p["mult"])
def held(self):
mine = while25.portfolio.my_position(KEY)
return mine["quantity"] if mine else Decimal(0)
def on_start(self, ctx):
self.pending = False
@on.tick(KEY)
def on_tick(self, ctx, tick):
if self.pending or tick["ltp"] is None or self.held() > 0:
return
if len(self.aapl) < while25.params["length"] + 1:
return # fewer bars than the warm-up
upper = last(self.channel(forming=True)["upper"])
if upper is not None and tick["ltp"] > upper:
while25.buy(KEY, while25.params["shares"], tag=TAG)
self.pending = True
@on.bar(aapl)
def on_bar(self, ctx, bar):
held = self.held()
if held <= 0 or len(self.aapl) < while25.params["length"] + 1:
return
basis = last(self.channel(forming=False)["basis"])
if basis is not None and bar["close"] < basis:
while25.sell(KEY, held, tag=TAG)
self.pending = True
@on.order(tag=TAG)
def on_order(self, ctx, ev):
if ev["event"] in ("REJECT", "CANCEL") or (ev["event"] == "FILL" and ev["status"] == "COMPLETE"):
self.pending = FalseIndex futures trend with a manual roll
Trades one S&P 500 or Nasdaq-100 futures contract (ES, MES, NQ or MNQ) with the hourly trend: long above the 20-hour average, flat below, always with a bracket stop a few average true ranges away. `future()` names the contract the chart's continuous line is on now; when that moves to the next month, the strategy closes the old contract and opens the new one itself. A redeploy picks its bracket back up from `my_positions()`.
Patient limit entry: work a bid, follow it, give up in time
Rests a limit buy for AAPL a few ticks under the bid, moves it when the bid walks away by more than a set number of ticks, and cancels it if it has not filled within a time limit. Every price is put on the venue's tick grid before it is sent, so nothing is refused for an off-grid price.