For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Start trading from 2nd candle
-
Greetings,
My strategy is as follows:
get last 2 days daily OHLC bars (today's and yesterday's bar) if there is gap up get today's 15 minute Bars if the opening bar is red, place sell order on 2nd bar open if the opening bar is green, place buy order on 2nd bar open``` I am not sure how to customize the `Strategy`'s `next()` function to trade using this logic. My code is as follows: ```python import backtrader as bt from backtrader import order # Import the backtrader platform import pandas from backtest import TestStrategy from datetime import datetime, timedelta import yfinance class Backtest(): def __init__(self) -> None: self.today = datetime.today() self.yesterday = self.today - timedelta(days=1) def symbols_list(self): try: url = "https://archives.nseindia.com/content/indices/ind_nifty50list.csv" self.symbols_list = pandas.read_csv(url, usecols=[2], names=['symbol'], header = 0) self.symbols_list['gap'] = 0 except Exception as e: print(e) def check_gap(self): for index, row in self.symbols_list.iter_rows(): # get last 2 days candles from Yahoo Finance ticker = yfinance.Ticker(row['symbol'] + ".NS") self.symbol_hist_df = ticker.history(start=self.today, end=self.yesterday, interval='1d') # gap up gap down check if self.symbol_hist_df['Open'][-1] > self.symbol_hist_df['High'][-2]: self.symbols_list['gap'][index] == 1 # gap up elif self.symbol_hist_df['Open'][-1] < self.symbol_hist_df['Low'][-2]: self.symbols_list['gap'][index] == -1 # gap down else: self.symbols_list['gap'][index] == 0 # no signal def run_backtest(self): # Create a cerebro entity self.cerebro = bt.Cerebro() # Create a Data Feed data = bt.feeds.PandasData(dataname=yfinance.download('TSLA', '2018-01-01', '2019-01-01')) # # Add the Data Feed to Cerebro self.cerebro.adddata(data) # Set our desired cash start self.cerebro.broker.setcash(1000.0) # Add a FixedSize sizer according to the stake self.cerebro.addsizer(bt.sizers.FixedSize, stake=10) # Set the commission self.cerebro.broker.setcommission(commission=0.0) for index, row in self.symbols_list.iter_rows(): if row['gap'] == 1: # gap up strategy # Add gap up strategy self.strats = self.cerebro.optstrategy(TestStrategy) if row['gap'] == -1: # gap up strategy # Add gap down strategy self.strats = self.cerebro.optstrategy(TestStrategy) # Run over everything self.cerebro.run(maxcpus=1)
-
@backtrader could you kindly help me here ?
I am unable to proceed. I need help and I am unable to figure this out on my own.I need to check if the 1st 15 minute bar is a red or green bar. (I know how to calculate red and green bar.) I am not sure where i need to add this check so that next() will start placing trades from the 2nd candle.