Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    I am new with backtrader pls help

    Indicators/Strategies/Analyzers
    strategy
    2
    3
    125
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • masla darkshark
      masla darkshark last edited by

      I am trying to implement a simple strategy based on stochastic oscillator in uptrend market
      timeframe of 5 minute
      but my code tends to not work, can take a look please

      import backtrader as bt
      import datetime 
      
      
      class Stochstrategy(bt.Strategy):
          params = (
              ('per',7),
          
              ('size',20),
      
          )
          def __init__(self):
             
              self.pfast = bt.indicators.SmoothedMovingAverage(self.data[0],period=60)
              self.pslow = bt.indicators.ExponentialMovingAverage(self.data[0],period=1728)
      
              self.stoch =  bt.ind.Stochastic(self.data)
      
      
              self.crs = bt.ind.CrossOver(self.pfast,self.pslow)
      
      
          def notify_trade(self,trade):
              if trade.isclosed:
                  dt = self.data.datetime.date()
      
                  print('---------------------------- TRADE ---------------------------------')
                  print("1: Data Name:                            {}".format(trade.data._name))
                  print("2: Bar Num:                              {}".format(len(trade.data)))
                  print("3: Current date:                         {}".format(dt))
                  print('4: Status:                               Trade Complete')
                  print('5: Ref:                                  {}'.format(trade.ref))
                  print('6: PnL:                                  {}'.format(round(trade.pnl,2)))
                  print('--------------------------------------------------------------------')
          def next(self):
              orders = self.broker.get_orders_open()
       
              # Cancel open orders so we can track the median line
              if orders:
                  for order in orders:
                      self.broker.cancel(order)
              if not self.position:
                  if self.crs:
                      if self.stoch.l.k<25 and self.stoch.l.k==self.stoch.l.d:
                          self.buy(exectype=bt.Order.Stop, price=self.boll.lines.bot[0], size=self.params.size)
                      elif self.stoch.l.k>75 and self.stoch.l.k==self.stoch.l.d:
                           self.sell(exectype=bt.Order.Stop, price=self.boll.lines.top[0],size= self.p.size)
                  
      
      
      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        @masla-darkshark said in I am new with backtrader pls help:

        def next(self):
        orders = self.broker.get_orders_open()

            # Cancel open orders so we can track the median line
            if orders:
                for order in orders:
                    self.broker.cancel(order)
            if not self.position:
                if self.crs:
                    if self.stoch.l.k<25 and self.stoch.l.k==self.stoch.l.d:
                        self.buy(exectype=bt.Order.Stop, price=self.boll.lines.bot[0], size=self.params.size)
                    elif self.stoch.l.k>75 and self.stoch.l.k==self.stoch.l.d:
                         self.sell(exectype=bt.Order.Stop, price=self.boll.lines.top[0],size= self.p.size)
        

        You are cancelling your orders every bar. Perhaps you mean to puthem after if not self.position: or after if self.crs:?

        run-out 1 Reply Last reply Reply Quote 0
        • run-out
          run-out @run-out last edited by

          @run-out Sorry I didn't mean to put in that top block of code... wish we could edit.

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }