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/

    Cash greater than value when using Long signal

    General Code/Help
    1
    1
    19
    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.
    • E
      edw last edited by

      Hello,

      When trying to replicate the examples on the Strategy with Signals documentation page, I have run into some weird results. Specifically, I am testing out how to use long signals (bt.SIGNAL_LONG). I expect that when using long only, my cash should never exceed my value, which I do find is what happens when I am using one of the sample datasets in the Github repo (2006-day-002.txt).

      However, when I switch to using my own data source, after a sell signal, cash increases past value, and remains until a buy signal (which I think is what a short should do?).

      I suppose my first question is: When using signals as long only, should cash ever be greater than value?
      And if not, what are some possible explanations for what is happening here?

      My theory is that there is a fundamental piece of finance that I am misunderstanding.

      As an aside, I've disabled loading volume in the sample dataset because I lack volume information in my dataset. Since the signals are only based on SMA, I don't think that this would be a problem.

      I appreciate the help. Thanks.

      class SMACloseSignal(bt.Indicator):
          lines = ('signal',)
          params = (('period', 30),)
      
          def __init__(self):
              self.lines.signal = self.data - bt.indicators.SMA(period=self.p.period)
      
      
      class SMAExitSignal(bt.Indicator):
          lines = ('signal',)
          params = (('p1', 5), ('p2', 30),)
      
          def __init__(self):
              sma1 = bt.indicators.SMA(period=self.p.p1)
              sma2 = bt.indicators.SMA(period=self.p.p2)
              self.lines.signal = sma1 - sma2
      
      #########################
      data = btfeeds.GenericCSVData(
          dataname="2006-day-002.txt",
      
          nullvalue=0.0,
      
          dtformat=('%Y-%m-%d'),
      
          datetime=0,
          high=2,
          low=3,
          open=1,
          close=4,
          volume=-1,
          openinterest=-1,
      )
      
      cerebro = bt.Cerebro()
      cerebro.broker.set_cash(100000)
      cerebro.adddata(data)
      
      cerebro.add_signal(bt.SIGNAL_LONG,
                          SMACloseSignal, period=30)
      
      cerebro.run()
      cerebro.plot()
      plt.gcf().set_size_inches(20, 10)
      
      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(); }); }