title: How do I create pandas DataFrame based off start and end signals from other DataFrames?
body: <p>So in essence how do I take A(entry signals) and B(exit signals) and make C(trade signals)</p>
  
  <p>Note that A is the Entry signal, and that B is the exit signal.
  So:</p>
  
  <pre><code>A    B    C
  0    1    0 (no entry signal yet, so 0)
  1    0    1 (got entry signal and haven't gotten exit signal yet, so 1)
  0    0    1 (got entry signal and haven't gotten exit signal yet, so 1)
  0    1    1 (got exit signal and currently in position, so change from 1 to 0)
  0    0    0 (nothing, so stay at 0)
  1    0    1 (got entry signal and haven't gotten exit signal yet, so 1)
  0    1    1 (got exit signal and currently in position, so change from 1 to 0)
  0    1    0 (got exit signal, but already at 0, so do nothing)
  1    0    1 (got entry signal and haven't gotten exit signal yet, so 1)
  0    0    1 (nothing, so stay at 0)
  </code></pre>
  
  <p>So basically, a 1 in A serves as a signal to turn C "On", and a 1 in B serves to turn C "off".  If C was already on (previous element of C was 1), then a 1 in A does nothing, as C is already on.  Similarly, if C is already off (previous element of C was 0), then a 1 in B does nothing, as C is already off.  Basically, A has a list of signals to enter a trade, and B has a list of signals of when to exit, but you can only enter if you're not in the trade, and you can only exit if you're in a trade, so C is the list of whether you are in the trade or not.</p>
  
  <p>I tried implementing your solution, as follows:</p>
  
  <pre><code>def generate_signals(self):
        signals = pandas.DataFrame(index=self.data.index)
        signals['Date'] = self.data['Date']
        signals['Close'] = self.data['Close']
        signals['fast_MA'] = pandas.stats.moments.ewma(self.data['Close'],
                                                       span=self.short_window)
        signals['slow_MA'] = pandas.stats.moments.ewma(self.data['Close'],
                                                       span=self.long_window)
        signalinfo = pandas.DataFrame(index=signals.index)
        signalinfo['entry_signals'] = numpy.where(signals['fast_MA'] &gt;
                                                  signals['slow_MA'], 1.0, 0.0)
        signals['stop'] = (data['Open'].shift(-1)
                           [(signalinfo['entry_signals'] == 1.0)
                            &amp; (signalinfo['entry_signals']
                               .shift(1) == 0.0)])
        signals['stop'] = signals['stop'].fillna(0.0)
        signals['stop'] = signals['stop'].apply(lambda x: .97 * x)
        signals['stop'] = (signals['stop']
                           .replace(to_replace=0.0, method='ffill'))
        signalinfo['exit_signals'] = numpy.where(signals['Close'] &lt;=
                                                 signals['stop'], 1.0, 0.0)
  
        #process entry and exit signals to form trade signals
        signalinfo['Close'] = self.data['Close']
        mask = signalinfo.copy().astype(bool)
        signalinfo.entry_signals[mask.entry_signals] = signalinfo.index
        signalinfo.exit_signals[mask.exit_signals] = signalinfo.index
        signalinfo = signalinfo[mask].ffill().fillna(0)
        signalinfo['signal'] = (signalinfo['exit_signals']
                                &lt; signalinfo['entry_signals']).astype(int)
        signalinfo['entry_signals'] = numpy.where(signals['fast_MA'] &gt;
                                                  signals['slow_MA'], 1.0, 0.0)
        signalinfo['exit_signals'] = numpy.where(signals['Close'] &lt;=
                                                 signals['stop'], 1.0, 0.0)
        signals['signal'][:self.long_window] = 0.0
        print signalinfo.head(50)
        print signalinfo.tail(50)
        return signals
  </code></pre>
  
  <p>This gets the code to the following:
      A    B    C
      0    1    0 (no entry signal yet, so 0)
      1    0    1 (got entry signal and haven't gotten exit signal yet, so 1)
      0    0    1 (got entry signal and haven't gotten exit signal yet, so 1)
      0    1    0 ((SHOULD BE 1) got exit signal and currently in position, so change from 1 to 0)
      0    0    0 (nothing, so stay at 0)
      1    0    1 (got entry signal and haven't gotten exit signal yet, so 1)
      0    1    0 ((SHOULD BE 1) got exit signal and currently in position, so change from 1 to 0)
      0    1    0 (got exit signal, but already at 0, so do nothing)
      1    0    1 (got entry signal and haven't gotten exit signal yet, so 1)
      0    0    1 (nothing, so stay at 0)</p>
  
  <p>Any ideas on how to fix this?  I'm trying to use the ffill method as in your solution, but I'm getting a value error.</p>