Need help to get started with pre-made signal
-
Hi there
I am super new to both bt and python. Maybe someone can help.
I loaded btc price data (close only) and included a sell/buy indicator for each day via a csv.
The csv is called 'output.csv'. Price data is in column 5 and the 'signal' in column 16.
I get an output plot but the strategy is not being executed.
Where do I go wrong?
Sources I already looked into:
https://community.backtrader.com/topic/812/using-my-own-pre-calculated-indicators
https://www.backtrader.com/docu/extending-a-datafeed.htmlimport datetime import backtrader as bt import backtrader.feeds as btfeeds from backtrader.feeds import GenericCSVData class firstStrategy(bt.Strategy): def next(self): if not self.position: if self.data.signal > 0: self.buy(size=100) else: if self.data.signal < 1: self.sell(size=100) startcash = 10000 cerebro = bt.Cerebro() cerebro.addstrategy(firstStrategy) class GenericCSV_PE(GenericCSVData): lines = ('signal',) params = (('signal', 16),) data = GenericCSV_PE( dataname='output.csv', fromdate=datetime.datetime(2018, 4, 25), todate=datetime.datetime(2018, 11, 12), nullvalue=0.0, dtformat=('%Y-%m-%d'), datetime=0, close=5, signal = 16, ) cerebro.adddata(data) cerebro.broker.setcash(startcash) strategies = cerebro.run() firstStrat = strategies[0] cerebro.run() cerebro.plot(style = 'line')
Thank you
-
@valentin said in Need help to get started with pre-made signal:
strategies = cerebro.run() firstStrat = strategies[0] cerebro.run()
Why do you run twice? If the entire indication for you is: "the plot is wrong" (which we don't know because you haven't even shown that) ...
- What about a data sample?
- What about logging something of the things that happen?
-
@backtrader
thanks a lot for your answer. I don't know why I had this extra line in there. I got rid of it now but still the strategy is not being executed as intended. It is supposed to buy on 1 and sell on 0 of the 'signal' value. Currently it looks like thisthis is how the data looks like
date ... price(USD) ... signal 2018-04-25 ... 9701.03 ... 0 ... 2018-04-28 ... 8939.27 ... 1 ...
-
@valentin said in Need help to get started with pre-made signal:
close=5,
It would seem like if you fail to provide anything but the
close
(we don't know it because your data sample is not a data sample) -
See also here:https://community.backtrader.com/topic/1507/order-execution-price-nan-single-column-pandas-data-input
You seem to be the also a trader who is new to trading and you face the same issues, which one can quickly summarize as: "You cannot trade a price you have seen"
-
Thank you for your comments. Following the advice from the links I now provide a dataset which also includes OHLC. Still, the strategy of buying on 1 and selling on 0 of the signal column is not being executed. Any other ideas?
This is the code:
import datetime import backtrader as bt import backtrader.feeds as btfeeds from backtrader.feeds import GenericCSVData class firstStrategy(bt.Strategy): def next(self): if not self.position: if self.data.signal > 0: self.buy(size=100) else: if self.data.signal < 1: self.sell(size=100) startcash = 10000 cerebro = bt.Cerebro() cerebro.addstrategy(firstStrategy) class GenericCSV_PE(GenericCSVData): lines = ('signal',) params = (('signal', 1),) data = GenericCSV_PE( dataname='price_and_signals.csv', fromdate=datetime.datetime(2018, 4, 25), todate=datetime.datetime(2018, 8, 14), nullvalue=0.0, dtformat=('%Y-%m-%d'), datetime=0, high=3, low=4, open=5, close=2, openinterest=-1, ) cerebro.adddata(data) cerebro.broker.setcash(startcash) cerebro.run() cerebro.plot(style = 'line')
this is the data:
and the result looks like this:
-
@backtrader I just tried the code without the "size = 100" and simply used
self.buy()
and
self.sell()
Now it works:
Why does it not work when I use (size =100)?
and:
Unfortunately I can't adjust the position size. I assume it uses the default of 1.
When I try to use the fixed size, it does not work (with not working I mean as explained above that the strategy is not executed. Hence the output looks as in the picture from my last comment):cerebro.addsizer(bt.sizers.FixedSize, stake=10)
-
Your prices are around 6000-10000, your cash is 10000. How can you expect to buy 100 of shares in this case? Only 1 fits.
-
@ab_trader wow.
haha okay, well I think there is a long way head of me. Thank you so much for taking your time to answer -
@ab_trader said in Need help to get started with pre-made signal:
Your prices are around 6000-10000, your cash is 10000. How can you expect to buy 100 of shares in this case? Only 1 fits.
Top. I had this in mind and was completely taken off course by the chart.
Because the markers and the data are located very close to each other and where the first operations would have taken place, which also raised the question: how were the buy and sell issued on the same day? (the code didn't allow that)