Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. stanski
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    S
    • Profile
    • Following 0
    • Followers 0
    • Topics 2
    • Posts 6
    • Best 2
    • Groups 0

    stanski

    @stanski

    2
    Reputation
    1
    Profile views
    6
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    stanski Unfollow Follow

    Best posts made by stanski

    • RE: Problem fetching execution price

      @run-out I've tried it with the attached attribute, but unfortunately, I get an AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'enter_price

      posted in General Code/Help
      S
      stanski
    • RE: Problem fetching execution price

      @run-out well, whatever I do now with self.datas[0].enter_price in terms of multiplication or addition, I get a TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'
      I'm considering to discard this idea. This is actually an alternative try-out for a percentage-based take profit, which I couldn't get working in another way.
      Maybe I should post the initial code in a new post

      posted in General Code/Help
      S
      stanski

    Latest posts made by stanski

    • RE: Problem with multiple limit orders as take profit

      @run-out Your solution works very nice, but this won't work live, will it? At least it reads in the doc's that there's no live support with oco's, although I see reports that binance api can deal with oco orders. I'm intenting to trade live on a crypto exchange. Do you have experience with oco / live ?

      posted in General Code/Help
      S
      stanski
    • RE: Problem fetching execution price

      @run-out nonetheless, thank you for sharing the idea! I just made a new post with the initial code with my take profit problem

      posted in General Code/Help
      S
      stanski
    • Problem with multiple limit orders as take profit

      Hello
      I'm trying to set up two TP's with sell-limit orders and one SL with a sell-stop order.
      The problem I'm encountering is that the first trades seem to be correct, but later bt creates multiple sell-limit orders without the buy order.
      What am I doing wrong?

      import backtrader as bt
      from datetime import datetime
      
        
      class SmaCross(bt.Strategy):
          # list of parameters which are configurable for the strategy
          params = dict(
              pfast=100,  # period for the fast moving average
              pslow=50   # period for the slow moving average
          )
      
          def __init__(self):
              self.sma0 = bt.ind.SMA(period=800) 
              sma1 = bt.ind.SMA(period=self.p.pfast)  # fast moving average
              sma2 = bt.ind.SMA(period=self.p.pslow)  # slow moving average
              self.crossover = bt.ind.CrossOver(sma1, sma2)  # crossover signal
      
              
          def log(self, txt, dt=None):
              #Logging function fot this strategy'''
              dt = dt or self.datas[0].datetime.date(0)
              print('%s, %s' % (dt.isoformat(), txt))
              
              
          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                  return
              if order.status in [order.Completed]:
                  if order.isbuy():
                      self.sell(exectype=bt.Order.Limit, size=1, price=self.data.close[0] * 1.03) 
                      self.sell(exectype=bt.Order.Limit, size=1, price=self.data.close[0] * 1.06) 
                      self.sell(exectype=bt.Order.Stop, size=2, price=self.data.close[0] * 0.98)
                      self.log('BUY EXECUTED, %.2f' % order.executed.price)
      
                  elif order.issell():
                      self.log('SELL EXECUTED, %.2f' % order.executed.price)
                      
                      
          def next(self):
              if not self.position:  # not in the market
                  if self.crossover > 0 and self.datas[0] < self.sma0:  # if fast crosses slow to the upside
                      self.buy_order = self.buy(exectype=bt.Order.Market, size=2)  # enter long
      
      startcash = 10000
      cerebro = bt.Cerebro()  # create a "Cerebro" engine instance
      
      # Create a data feed
      data = bt.feeds.GenericCSVData(
          dataname='data/1inch_perp_28-02_09-03_10S.csv',
          fromdate=datetime(2021, 3, 7),
          todate=datetime(2021, 3, 21),
          timeframe=bt.TimeFrame.Seconds,
          headers=False,
          separator=";",
          volume=5,
          openinterest=-1,
          dtformat=('%d.%m.%Y %H:%M:%S'),
      )
      
      cerebro.adddata(data)  # Add the data feed
      
      cerebro.addstrategy(SmaCross)  # Add the trading strategy
      cerebro.run()  # run it all
      
      portvalue = cerebro.broker.getvalue()
      pnl = portvalue - startcash
      print('Final Portfolio Value: ${}'.format(portvalue))
      print('P/L: ${}'.format(pnl))
      
      cerebro.plot()  # and plot it with a single command
      
      
      2021-03-07, BUY EXECUTED, 38.52
      2021-03-07, SELL EXECUTED, 39.67
      2021-03-07, SELL EXECUTED, 40.82
      2021-03-07, BUY EXECUTED, 40.28
      2021-03-07, SELL EXECUTED, 39.52
      2021-03-07, BUY EXECUTED, 39.05
      2021-03-07, SELL EXECUTED, 38.29
      2021-03-07, BUY EXECUTED, 37.97
      2021-03-08, SELL EXECUTED, 39.13
      2021-03-08, SELL EXECUTED, 40.24
      2021-03-08, SELL EXECUTED, 40.27
      2021-03-08, SELL EXECUTED, 41.41
      2021-03-08, SELL EXECUTED, 41.53
      2021-03-08, SELL EXECUTED, 42.74
      

      link csv file

      posted in General Code/Help
      S
      stanski
    • RE: Problem fetching execution price

      @run-out well, whatever I do now with self.datas[0].enter_price in terms of multiplication or addition, I get a TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'
      I'm considering to discard this idea. This is actually an alternative try-out for a percentage-based take profit, which I couldn't get working in another way.
      Maybe I should post the initial code in a new post

      posted in General Code/Help
      S
      stanski
    • RE: Problem fetching execution price

      @run-out I've tried it with the attached attribute, but unfortunately, I get an AttributeError: 'Lines_LineSeries_DataSeries_OHLC_OHLCDateTime_Abst' object has no attribute 'enter_price

      posted in General Code/Help
      S
      stanski
    • Problem fetching execution price

      Hello everyone,

      I'm trying to exit a position if price > executed price + x.
      How can I fetch the order.executed.price?

      I tried it with _orders[data][0]but it doesn't work:

      def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  return
              if order.status in [order.Completed]:
                  if order.isbuy():
                      
                      DISTCALC = ((self.ema1[0]-order.executed.price)/order.executed.price*100)
                      self.log('BUY EXECUTED, %.2f' % order.executed.price)
                      self.log('EMA Price, %.2f' % self.ema1[0])
                      self.log('Price distance to EMA, %.2f' % DISTCALC + str('%'))
                      
                  elif order.issell():
                      self.log('SELL EXECUTED, %.2f' % order.executed.price)
                 
          def next(self):
              if not self.position and self.order==None: 
                  if self.distance and self.below800EMA:
                      self.price = self.sma1[0]
                      self.order = self.buy(self.datas[0], exectype=bt.Order.Market, size=1)
                        
                  if self.sma1[0] > self._orders[data][0]+10:
                      
                      self.order = self.close()
      
      

      The error is:
      TypeError: list indices must be integers or slices, not GenericCSVData

      posted in General Code/Help
      S
      stanski