Comments (4)
I suspect the error is in the following lines of code, specifically your indexes:
if self.macd[0] > 0 and self.macd[1] > 0 and crossover(self.ema_fast, self.ema_slow):
self.sell()
elif self.macd[0] < 0 and self.macd[1] < 0 and crossover(self.ema_slow, self.ema_fast):
self.buy(size=self.order_size, limit=None, stop=None)
An index of -1
denotes the current candle, so by setting an index of 0
and 1
you are trying to ``look forward''.
from backtesting.py.
Thanks for your reply. Initially I thought you were right, but then I've checked better: self.macd[0] and self.macd[1] refer to the first two values in the MACD indicator, the MACD line and the signal line. It's correct what I copied. Indeed, if I need the current candle I use self.data.Close[-1].
Anyway, even if I replace [0] and [1] with [-1] and [-2] nothing really changes, there is still 1 trade that wipes all the cash out:
from backtesting.py.
i believe thats still an index problem for example using your original code with index 0 and 1 i get (low # trades with and maybe too long of a trade):
but when i use -1 and 0 i get this (more trades):
like this:
if self.macd[-1] > 0 and self.macd[0] > 0 and crossover(self.ema_fast, self.ema_slow):
self.sell()
elif self.macd[-1] < 0 and self.macd[0] < 0 and crossover(self.ema_slow, self.ema_fast):
self.buy(size=self.order_size, limit=None, stop=None)
from backtesting.py.
Related Issues (20)
- bokeh 3.2.2 breaks plotting
- ValueError: Short orders require: TP (1890.425) < LIMIT (1886.10523) < SL (1890.455)
- How to add a dollar amount for take profit and stop loss
- Backtest results plot break when using yfinance data HOT 3
- Timezone not respected in charts HOT 1
- Cant find open positions
- Runup and drawdown of each trade
- Plotting does not work and raises an error with Bokeh HOT 9
- How can I add compound in trades HOT 1
- How to get trades dataframe contain last trade that is still active?
- Code handling when some of the OHLC value is empty
- Why Sharpe, Sortino and Calmar Ratio all show 0? HOT 1
- Can I choose other Buy/Sell prices rather than Open/Close prices?
- BokehDeprecationWarning: Passing lists of formats for DatetimeTickFormatter scales was deprecated in Bokeh 3.0. Configure a single string format for each scale HOT 1
- How to trade multiple varieties at the same time
- The get_loc() method of the Index class in pandas does not have a method argument. HOT 2
- number of days in trade mistmached
- if sell at close, and close value is fixed, still the final equity value is strange HOT 1
- Waiting a couple bars or minutes after a trade is closed before getting into another one? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from backtesting.py.