@Sumeet-Badiger after you load the data, just multiply the entire series by 1000 and then does it work?
Latest posts made by adamb032
-
RE: Crossover doesn't working with prices which move in small decimal differences.
-
RE: Trade on next bar at close
@adamb032 i found the solution to use exectype=Order.Close in the self.buy/sell parameters.
-
Trade on next bar at close
I know that by default trades are executed on the next bar at the open price of it, and you can use cheat-on-close to trade on the current bar at the close, but what I was wondering if there is a way to trade on the next bar, but instead of the open, on the close.
its looks like I could re-write the _try_exec_market function in the Broker class, but wondering if there is an easier way?
My hack to do this has been to create a datas where the open=close, so that the open price is the close price, but that seems like a weird workaround.
-
RE: Numpy Array to LineBuffer
im not particularly sure that it matters, but there is a workaround:
inside your indicator class
from copy import deepcopy some_data_linebuff = deepcopy(self.data.close) my_new_array = [.....] #same len as self.data.close for idx in range(len(my_new_array)): some_data_linebuff.set(my_new_array[idx], ago=idx) self.lines.new_line = some_data_linebuff
basically whats happening is you make a copy of a linebuffer object, with all the dates already determined, and then reassign all the values for your array into it. presto chango.
-
notify_trade()
I am opening a position (self.buy(), self.sell(), and then closing it by issuing an opposite side order but not calling self.close()
I see that my position does get flattened out, however the trade object is never updated to be closed. is that expected behavior ?
-
strategy next() out of alginment
I am noticing something a little weird, and not exactly sure whats happening. I have a simple SMA strategy with a 5/20 cross, just like in the example in my init function:
self.sma_fast = self.p._movav(period=self.p.fast) self.sma_slow = self.p._movav(period=self.p.slow)
in my next() function:
self.logger.info(f'current pos: {self.position.size}') self.logger.info(f'{num2date(self.data.datetime[0])} \ close: {self.data.close[0]}') self.logger.info(f'current bar: {len(self)}') #so the indicators self.logger.info(self.sma_fast.lines[0].array[len(self)]) self.logger.info(self.sma_slow.lines[0].array[len(self)])
i verified with my data, that the date, and close price are accurate for the given bar, however the indicators produce the average(5/20) with the next bar's closing price
should this be the case? i would expect that they include up to the current bar. or am i misunderstanding how this works? thanks-
-
RE: TimeReturn doesn't seem to work
@run-out ah i see, i have another question, it looks like the first bar's returns are close/open-1 (ie, the open->close) returns, is there a way to force TimeReturn to only use close->close (so the first value is nan/0) or would that require basically writing my own wrapper around TimeReturn ?
-
TimeReturn doesn't seem to work
I am trying to get a basic buy and hold strategy working, but TimeReturn does not seem to be working... my code:
start_date = datetime(2019,9,30) end_date = datetime(2019,10,5) data_df = get_eod_data('SPY', start_date, end_date) data_df['close_change'] = data_df['close'].pct_change() print(data_df[['close', 'close_change']]) class BuyAndHold(bt.Strategy): def __init__(self): self.logger = logging.getLogger(__name__) self.logger.info(f'Initializing strategy: BuyAndHold') def nextstart(self): self.logger.info(f"BUY {self.data.datetime.date(0)}: {self.data.close[0]:.2f}") self.order_target_value(target=self.broker.get_cash()) def stop(self): self.logger.info(f"starting value: {self.broker.startingcash}") self.logger.info(f"ending value : {self.broker.getvalue()}") roi = (self.broker.get_value() / self.broker.startingcash) - 1.0 self.logger.info(f"roi: {roi}") data = bt.feeds.PandasData(dataname=data_df, openinterest=None) cerebro = bt.Cerebro(stdstats=False) cerebro.adddata(data) cerebro.addanalyzer(btanalyzers.TimeReturn, _name='returns') cerebro.addobserver(bt.observers.Broker) cerebro.addobserver(bt.observers.DrawDown) cerebro.addsizer(bt.sizers.PercentSizer, percents=100) cerebro.addstrategy(BuyAndHold) results = cerebro.run() # run it all #cerebro.plot(style='bar') # and plot it with a single command returns_in_ordered_dict = results[0].analyzers.getbyname('returns').get_analysis() returns_df = pd.DataFrame([returns_in_ordered_dict]).T.rename(columns={0: 'return'}) print(returns_df) print(286.601471 /288.957764-1)
the output:
close close_change trade_date 2019-09-30 288.957764 NaN 2019-10-01 285.520630 -0.011895 2019-10-02 280.477051 -0.017664 2019-10-03 282.774933 0.008193 2019-10-04 286.601471 0.013532 13:41:53 [ buy_hold]:INFO: Initializing strategy: BuyAndHold 13:41:53 [ buy_hold]:INFO: BUY 2019-09-30: 288.96 13:41:53 [ buy_hold]:INFO: starting value: 10000.0 13:41:53 [ buy_hold]:INFO: ending value : 9887.776226027483 13:41:53 [ buy_hold]:INFO: roi: -0.011222377397251737 return 2019-09-30 0.000000 2019-10-01 -0.014897 2019-10-02 -0.017407 2019-10-03 0.008071 2019-10-04 0.013333 -0.008154454711242876
since this is a buy and hold, and it buys on the first bar (288.96) (the close on 2019-9-30), over these five days, the buy and hold should return -81.544bps, however using hte TimeReturn, it says my return is -112.22bps. also the daily returns are wrong. is there something I am not understanding here ?