def next(self): prices = self.data.Close if len(self.data.Volume) > 19: last_price = prices[-1] if self.buy_price == 0 and Ticker.hasSignalByBollingerBandsV1(prices) == 1: self.buy_price = last_price self.buy() if self.buy_price != 0 and Ticker.hasSignalByBollingerBandsV1(prices) == -1: self.position.close() self.buy_price = 0
def next(self): prices = self.data.Close if len(self.data.Volume) > 14: last_price = prices[-1] if self.buy_price == 0 and Ticker.isPriceUpTrendByRSI12D( prices) == True: self.buy_price = last_price self.buy() # if self.buy_price != 0 and (TakeProfit.takeProfit(5, last_price, self.buy_price) or CutLoss.shouldCutLossByPercent(8, last_price, self.buy_price)): if self.buy_price != 0 and Ticker.isPriceUpTrendByRSI12D( prices) == False: self.position.close() self.buy_price = 0
def next(self): prices = self.data.Close opens = self.data.Open volumes = self.data.Volume if len(self.data.Volume) > 5: last_price = prices[-1] # price is increased if last_price > self.trailing_price: self.trailing_price = last_price if self.buy_price == 0 and Ticker.isFollowTrendingV2( prices, volumes, opens[-1], 2.5): self.buy_price = last_price self.buy() if self.buy_price != 0: # print(self.buy_price * (1 - STOPLOSS)) # print(last_price) # print(self.trailing_price * (1 - STOPLOSS)) # print('------------------------') if TakeProfit.takeProfit( 15, last_price, self.buy_price) or last_price < self.buy_price * ( 1 - STOPLOSS ) or last_price < self.trailing_price * (1 - STOPLOSS): print(self.data.Date[-1]) self.position.close() self.buy_price = 0 self.trailing_price = 0
def next(self): prices = self.data.avgHL if len(self.data.Volume) > 22: last_price = prices[-1] if self.buy_price == 0 and Ticker.isCounterTrendV2(prices): self.buy_price = last_price self.buy() if self.buy_price != 0 and ( _sr.takeProfitByPercent(5, last_price, self.buy_price) or _sr.shouldCutLossByPercent(8, last_price, self.buy_price)): self.position.close() self.buy_price = 0
def next(self): prices = self.data.Close if len(self.data.Volume) > 66: last_price = prices[-1] if self.buy_price == 0 and Ticker.isStockOut(prices): self.buy_price = last_price self.buy() if self.buy_price != 0 and (TakeProfit.takeProfit( 5, last_price, self.buy_price) or CutLoss.shouldCutLossByPercent( 8, last_price, self.buy_price)): self.position.close() self.buy_price = 0
def next(self): prices = self.data.Close opens = self.data.Open volumes = self.data.Volume if len(self.data.Volume) > 5: last_price = prices[-1] if self.buy_price == 0 and Ticker.isFollowTrendingV2( prices, volumes, opens[-1], 2.5): self.buy_price = last_price self.buy() if self.buy_price != 0 and (TakeProfit.takeProfit( 5, last_price, self.buy_price) or CutLoss.shouldCutLossByPercent( 8, last_price, self.buy_price)): self.position.close() self.buy_price = 0
def getResult(ticker_id, np_data): # rand_from = int(np_data.size / 4) # rand_to = int(np_data.size / 3) # look_back = random.randint(rand_from,rand_to) # days # price_data = np_data[:, close_col_index] # print(price_data) # print(len(price_data)) # return f = open("tracking-history.log", "w+") close_col_index = 4 date_col_index = 0 sold = True test_from = 10 test_to = len(np_data) - 5 index = 0 sold_price = 0 commission = 0 train_price = np.array([]) for data in np_data: index = index + 1 train_price = np.append(train_price, data[close_col_index]) if test_from < index < test_to: copy_data = np_data.copy() test_prices = copy_data[index:] curr_price = data[close_col_index] cut_loss_price = curr_price * 0.96 if Ticker.isStockOut(train_price): buy_log = "Mua co phieu " + ticker_id + " o gia: " + str(curr_price) + " ngay " + data[ date_col_index] + "\n" f.write(buy_log) # f.write("--" + str(index) + "\n") for np_test_price in test_prices: test_price = np_test_price[close_col_index] if test_price > curr_price: cut_loss_price = test_price * 0.96 if test_price < cut_loss_price: # Co cut loss sold_price = test_price commission = sold_price - curr_price sold_log = "Ban co phieu " + ticker_id + " o gia: " + str(sold_price) + " ngay " + \ np_test_price[date_col_index] + "\r\n" f.write(sold_log) # f.write("---" + str(index) + "\n") break f.close() print("Commission: " + str(commission))
def next(self): prices = self.data.Close opens = self.data.Open volumes = self.data.Volume lows = pd.DataFrame(data=self.data.Low).Low highs = pd.DataFrame(data=self.data.High).High if len(prices) > 23: last_price = prices[-1] atr_22 = _sr.getATR(22, highs, lows) if self.buy_price == 0 and Ticker.isFollowTrendingV2( prices, volumes, opens[-1], 2.5): self.buy_price = last_price self.buy() if self.buy_price != 0 and ( (_sr.takeProfitByPercent(5, last_price, self.buy_price) and opens[-1] < prices[-1] and abs(highs.iloc[-1] - lows.iloc[-1]) > 2 * atr_22) or _sr.shouldCutLossByPercent(8, last_price, self.buy_price)): # if self.buy_price != 0 and ((_sr.takeProfitByPercent(5, last_price, self.buy_price) and abs(highs.iloc[-1] - lows.iloc[-1]) > 2*atr_22 ) or _sr.shouldCutLossByPercent(8, last_price, self.buy_price)): self.position.close() self.buy_price = 0
def next(self): prices = self.data.Close opens = self.data.Open volumes = self.data.Volume # self._index = self._index + 1 # print("----------"+str(self._index)+"--------") if len(self.data.Volume) > 5: last_5_volumes = volumes[-5::] last_3_prices = prices[-3::] # min_volume = min(last_5_volumes) max_volume = max(last_5_volumes) last_volume = last_5_volumes[-1] mean_f = np.mean(volumes[-5:-2]) last_open_price = opens[-1] last_price = prices[-1] if self.buy_price == 0 and Ticker.isFollowTrendingV2(prices, volumes, opens[-1], 2.5): self.buy_price = last_price self.buy() if self.buy_price != 0 and (TakeProfit.takeProfit(5, last_price, self.buy_price) or CutLoss.shouldCutLossByPercent(8,last_price,self.buy_price)): self.position.close() self.buy_price = 0
def next(self): prices = self.data.Close if len(self.data.Volume) > 66: last_price = prices[-1] if self.buy_price == 0 and Ticker.isStockOut(prices): self.buy_price = last_price self.buy() if self.buy_price != 0 and (TakeProfit.takeProfit( 5, last_price, self.buy_price) or CutLoss.shouldCutLossByPercent( 8, last_price, self.buy_price)): self.position.close() self.buy_price = 0 vn30_ticker = Ticker.getListBlueChips2020() for ticker in vn30_ticker: ticker_data = _read_file(ticker + '.csv') bt = Backtest(ticker_data, StockBreakOut, commission=.005, exclusive_orders=False) stats = bt.run() # bt.plot() # print(stats) # print(ticker) # print(stats['Sharpe Ratio']) # # print(stats['_trades']) new_file = path + "/result_" + ticker + ".csv" stats['_trades'].to_csv(new_file, index=False)
if len(self.data.Volume) > 14: last_price = prices[-1] if self.buy_price == 0 and Ticker.isPriceUpTrendByRSI12D( prices) == True: self.buy_price = last_price self.buy() # if self.buy_price != 0 and (TakeProfit.takeProfit(5, last_price, self.buy_price) or CutLoss.shouldCutLossByPercent(8, last_price, self.buy_price)): if self.buy_price != 0 and Ticker.isPriceUpTrendByRSI12D( prices) == False: self.position.close() self.buy_price = 0 best_return = 0 best_return_ticker = '' all_ticker = Ticker.getAllTickers( '/home/zuongthao/GIT/zuongthaotn/backtesting.py/method/VNX.csv') for ticker in all_ticker: ticker_data = _read_file(ticker + '.csv') bt = Backtest(ticker_data, RSIuptrending, commission=.005, exclusive_orders=False) stats = bt.run() # bt.plot() # print(stats) # exit() if stats['Return [%]'] > best_return: best_return_ticker = ticker # print(ticker) # print(stats['Sharpe Ratio']) # # print(stats['_trades'])
def next(self): prices = self.data.Close opens = self.data.Open volumes = self.data.Volume if len(self.data.Volume) > 5: last_price = prices[-1] if self.buy_price == 0 and Ticker.isFollowTrendingV2( prices, volumes, opens[-1], 2.5): self.buy_price = last_price self.buy() if self.buy_price != 0 and (TakeProfit.takeProfit( 5, last_price, self.buy_price) or CutLoss.shouldCutLossByPercent( 8, last_price, self.buy_price)): self.position.close() self.buy_price = 0 vn30_ticker = Ticker.getListVN30() for ticker in vn30_ticker: BID = _read_file(ticker + '.csv') bt = Backtest(BID, FollowTheTrend, commission=.005, exclusive_orders=False) stats = bt.run() # bt.plot() print(ticker) print(stats['Sharpe Ratio']) # # print(stats['_trades']) # new_file = path+"/testcases/FollowTheTrend/result_"+ticker+".csv" # stats['_trades'].to_csv(new_file, index=False)