@sasha said in How to write algorithm for both long and short selling?:
I just wanted to know how to write the code for the short and long positions.
if not self.position:
# open long
if <long entry signal>:
# open short
elif <short entry signal>:
# close long or short
if <long exit signal> or <short exit signal>:
@hamid-zargar said in What's wrong with my VWAP indicator?:
Also, I've noticed the when I initiate my VWAP indicator in my initializing function for my strategy, it alters the output of my trades and results although I havn't incorporated the VWAP in my trading logic yet
Yes you have. Because you change the minimum warm-up period.
Please read: Docs - Operating the Platform
What you probably miss is that
@jburb said in Strategy.__init__() slows down the whole backtest after it’s first execution?:
This is only a declaration. No actual calculation is taking place there. If it actually did, live data feeds would be impossible. As stated in other posts: backtrader is not pandas.
Docs - Platform Concepts and see Stage 1
@ihustling said in IndexError: array index out of range when dealing with RSI indicator:
If you were wondering where I got the EMA indicators from, you mention SMA throughout the Quickstart guide and on your Github home page
Respectfully but truly sincere: you haven't read the docs.
Sorry, you seem to be in a rush and the trees are not letting you see the forest.
@backtrader said in IndexError: array index out of range when dealing with RSI indicator:
fast_ema = bt.ind.ExponentialMovingAverage(period = 5)
slow_ema = bt.ind.ExponentialMovingAverage(period = 10)
rsi = bt.ind.RSI(period = 10)
You WILL NEVER see those indicators (nor any other) defined inside the next method.
From the Quickstart Guide:
Permalink to the section: https://www.backtrader.com/docu/quickstart/quickstart.html#adding-an-indicator
# Keep a reference to the "close" line in the data dataseries
self.dataclose = self.datas.close
# To keep track of pending orders and buy price/commission
self.order = None
self.buyprice = None
self.buycomm = None
# Add a MovingAverageSimple indicator
self.sma = bt.indicators.SimpleMovingAverage(self.datas, period=self.params.maperiod)
I know it's tempting to go very quickly and be winning the first millions during your first days of algotrading, but I do truly believe you should read the docs. Be it backtrader, be it any other platform.
Even if you look at the GitHub page the code looks like this
sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30)
Docs - Quickstart - Our First Strategy:
def log(self, txt, dt=None):
''' Logging function for this strategy'''
dt = dt or self.datas.datetime.date(0)
print('%s, %s' % (dt.isoformat(), txt))
Also bt has two functions to convert date to/from numbers: date2num and num2date.
Is it possible to apply "Cheat on Open" and Cheat on close" for specific trades (i.e. for select order placements), as opposed to applying it across the board in cerebro: cerebro = bt.Cerebro(cheat_on_open=True)?
You may of course consider:
Am I producing debugging information?
Have I activated the debug mode of the Interactive Brokers data?
Is 10-seconds asking for too many when looking backwards to 2019-01-01?
Am I attempting a historical download or do I want to trade real-time?
Am I talking to the Fake-Data Demo, Paper Trading Account or Live Trading Account?
Do I have the right data permissions?
That's the minimum information you should have at hand. Algotrading involves engineering and being thorough. And sorry, comparing a Yahoo download with connecting to a live broker (even if in Fake Data mode) is not really a comparison.
Thanks @backtrader, sounds good. Any tips how I could edit the following commission scheme to do just that?
params = (
('commtype', bt.CommInfoBase.COMM_PERC), #commission to be understood as %
# Custom broker commission fee params:
('min_fee', 0), # The broker uses a %commission fee, with the "min_fee" as the minimum fee per trade.
def _getcommission(self, size, price, pseudoexec):
commvalue = abs(size) * price * self.p.commission # %based commission value
commvalue = max(self.p.min_fee,commvalue) # choose the greater of $min_fee and percentage calculated
@backtrader thank you for reference, i took your referenced sample, and alter to make my own code.
i replaced resampledata to cerebro.adddata i use Days instead of ticks.
when i use resampledata - cerebro crashed, i open issue, but found by adddata it's run stable (without crashes)
Could you please point me what i doing wrong??
There is no trace of a single indicator in your code, neither custom nor not-custom. It is therefore 100% unclear what your code and text have to do with indicators.
There is a sell signal on the chart, right at the beginning.
You have a dataframe with a column called signal but you seem to want to act upon a column called action which isn't there.
You probably want to take two steps back, reformat your question, dataframe loading (code formatting, because that code won't execute) and let us know.
@howardhh said in Prenext - does something weird:
2015-03-30 A2M.AX Closed: PnL Gross -1894.41, Net -1907.61 ***********
It's a losing trade. What's weird?
@howardhh said in Prenext - does something weird:
I found that adding prenext allowed the code to be run over the entire testing range
This is basically wrong. Read Docs - Operating the Platform it will simply give you access to a period in which all guarantees (like values of indicators being available) cannot yet be met.