MCPcopy Create free account
hub / github.com/kernc/backtesting.py / init

Method init

doc/examples/Strategies Library.py:58–81  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

56 n2 = 25
57
58 def init(self):
59 # In init() and in next() it is important to call the
60 # super method to properly initialize the parent classes
61 super().init()
62
63 # Precompute the two moving averages
64 sma1 = self.I(SMA, self.data.Close, self.n1)
65 sma2 = self.I(SMA, self.data.Close, self.n2)
66
67 # Where sma1 crosses sma2 upwards. Diff gives us [-1,0, *1*]
68 signal = (pd.Series(sma1) > sma2).astype(int).diff().fillna(0)
69 signal = signal.replace(-1, 0) # Upwards/long only
70
71 # Use 95% of available liquidity (at the time) on each order.
72 # (Leaving a value of 1. would instead buy a single share.)
73 entry_size = signal * .95
74
75 # Set order entry sizes using the method provided by
76 # `SignalStrategy`. See the docs.
77 self.set_signal(entry_size=entry_size)
78
79 # Set trailing stop-loss to 2x ATR using
80 # the method provided by `TrailingStrategy`
81 self.set_trailing_sl(2)
82
83
84# %% [markdown]

Callers

nothing calls this directly

Calls 3

IMethod · 0.80
set_signalMethod · 0.80
set_trailing_slMethod · 0.80

Tested by

no test coverage detected