Ejemplo n.º 1
0
 async def calc_balance(self) -> None:
     # print("BEFORE ATR:")
     # print(f"\n{tabulate(self.portfolio, headers='keys', tablefmt='psql')}")
     for _, row in self.portfolio.iterrows():
         indicator_calculator = StockDataFrame(self.data_bars[row.symbol])
         indicator_calculator.ATR_SMMA = self.atr_days
         atr = indicator_calculator["atr"][-1]
         volatility = (
             self.data_bars[row.symbol]
             .close.pct_change()
             .rolling(20)
             .std()
             .iloc[-1]
         )
         qty = int(self.portfolio_size * self.risk_factor // volatility)
         self.portfolio.loc[
             self.portfolio.symbol == row.symbol, "ATR"
         ] = atr
         self.portfolio.loc[
             self.portfolio.symbol == row.symbol, "volatility"
         ] = volatility
         self.portfolio.loc[
             self.portfolio.symbol == row.symbol, "qty"
         ] = qty
         self.portfolio.loc[self.portfolio.symbol == row.symbol, "est"] = (
             qty * self.data_bars[row.symbol].close[-1]
         )
     self.portfolio = self.portfolio.loc[self.portfolio.qty > 0]
Ejemplo n.º 2
0
 async def calc_balance(self) -> None:
     print("BEFORE ATR:")
     print(f"\n{tabulate(self.portfolio, headers='keys', tablefmt='psql')}")
     for i, row in self.portfolio.iterrows():
         indicator_calculator = StockDataFrame(self.data_bars[row.symbol])
         indicator_calculator.ATR_SMMA = self.atr_days
         atr = indicator_calculator["atr"][-1]
         qty = int(self.portfolio_size * self.risk_factor // atr)
         self.portfolio.loc[self.portfolio.symbol == row.symbol,
                            "ATR"] = atr
         self.portfolio.loc[self.portfolio.symbol == row.symbol,
                            "qty"] = qty
         self.portfolio.loc[self.portfolio.symbol == row.symbol,
                            "est"] = (qty *
                                      self.data_bars[row.symbol].close[-1])
     self.portfolio = self.portfolio.loc[self.portfolio.qty > 0]