2

I am working on a pandas Financial series dataframe and iterating through the rows to test stops and targets. With a large dataset this takes way too long. The main problem that I have is the that the data['Signal']fires only when the boundary condition exists. IT would be nice to 'latch' the signal to remain 1( via forward fill) while the target and the stop have not been touched. Currently I an checking every row and filling in the position value to perform my calculation.

update with data:

    Date    Time    Open    High    Low Close   boundry1    boundry2    L1  S1  Signal  exit_type   exit_price  Sreturn position
578 20120831    10:56:00    1272.50 1274.00 1272.50 1274.00 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
579 20120831    10:57:00    1274.00 1275.25 1274.00 1274.25 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
580 20120831    10:59:00    1274.25 1274.75 1274.25 1274.75 1274.5  1271.25 1277.75 1268.0  1   NaN NaN 0   0
581 20120831    11:00:00    1274.75 1274.75 1274.75 1274.75 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
582 20120831    11:01:00    1275.00 1275.00 1275.00 1275.00 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
583 20120831    11:02:00    1275.75 1275.75 1275.25 1275.25 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
584 20120831    11:03:00    1276.25 1276.25 1276.25 1276.25 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
585 20120831    11:04:00    1277.00 1277.00 1277.00 1277.00 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
586 20120831    11:05:00    1275.75 1275.75 1274.75 1274.75 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
587 20120831    11:06:00    1275.50 1275.50 1275.25 1275.25 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
588 20120831    11:07:00    1275.25 1275.25 1274.75 1274.75 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
589 20120831    11:09:00    1274.75 1275.00 1274.75 1275.00 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
590 20120831    11:10:00    1275.25 1275.25 1275.25 1275.25 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
591 20120831    11:11:00    1275.50 1275.50 1275.50 1275.50 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
592 20120831    11:12:00    1275.00 1275.00 1275.00 1275.00 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
593 20120831    11:16:00    1274.75 1275.00 1274.75 1275.00 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
594 20120831    11:17:00    1275.25 1275.25 1275.25 1275.25 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
595 20120831    11:19:00    1274.75 1275.00 1274.75 1275.00 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
596 20120831    11:21:00    1275.25 1275.25 1275.25 1275.25 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
597 20120831    11:24:00    1276.25 1276.25 1276.25 1276.25 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
598 20120831    11:29:00    1274.50 1274.50 1274.50 1274.50 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
599 20120831    11:31:00    1274.50 1274.50 1274.50 1274.50 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
600 20120831    11:32:00    1274.25 1274.25 1274.25 1274.25 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
601 20120831    11:33:00    1274.25 1274.25 1274.25 1274.25 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
602 20120831    11:34:00    1273.75 1274.00 1273.75 1274.00 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
603 20120831    11:39:00    1274.50 1274.50 1274.50 1274.50 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
604 20120831    11:41:00    1274.50 1274.50 1274.50 1274.50 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
605 20120831    11:47:00    1273.75 1273.75 1273.50 1273.50 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
606 20120831    11:48:00    1274.00 1274.00 1274.00 1274.00 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
607 20120831    11:49:00    1273.75 1274.00 1273.75 1274.00 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
608 20120831    11:50:00    1273.25 1273.75 1273.25 1273.75 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
609 20120831    11:53:00    1274.50 1274.50 1274.50 1274.50 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
610 20120831    11:56:00    1274.00 1274.00 1273.50 1273.50 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
611 20120831    11:59:00    1273.75 1273.75 1273.75 1273.75 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
612 20120831    12:00:00    1273.50 1273.50 1273.50 1273.50 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
613 20120831    12:05:00    1273.50 1273.50 1273.50 1273.50 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
614 20120831    12:07:00    1273.00 1273.00 1273.00 1273.00 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
615 20120831    12:08:00    1272.75 1272.75 1270.50 1270.50 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
616 20120831    12:09:00    1270.75 1270.75 1270.50 1270.50 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
617 20120831    12:10:00    1270.25 1270.25 1269.50 1269.50 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
618 20120831    12:12:00    1268.00 1268.00 1268.00 1268.00 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0
619 20120831    12:13:00    1267.75 1268.25 1267.75 1268.25 1274.5  1271.25 1277.75 1268.0  0   NaN NaN 0   0

Is there a more pythonic way of doing this? thanks and happy holidays! Edit: What I am doing here is using to the active_position to latch to 1 until one of the exit conditions are hit.
What would be great is to do a conditional fill of the 'Signal' column only when Signal is initiated and the boundry2 and L1 conditions are not exceeded. Once those conditions are exceeded. then it would reduce by active_position-1..

active_position=0
for i, row in data.iterrows():
    data.loc[i,'position']=active_position
    if row.Signal==1 and active_position==0:
        active_position=1
        data.loc[i, 'exit_type'] = None    
    elif active_position>0:
        if data.loc[i,'High']<data.loc[i,'L1']:
            if data.loc[i,'Low']<=data.loc[i,'boundry2']:
                data.loc[i, 'exit_type'] = 'stop'
                active_position=0
                bars_since_signal=0
                data.loc[i,'Sreturn']=(data.boundry2.loc[i]-data.Close.loc[i-1])*active_position
            else:
                data.loc[i, 'exit_type'] = None
                data.loc[i,'position']=active_position
                data.loc[i,'Sreturn']=(data.Close.loc[i]-data.Close.loc[i-1])*active_position
        else:
            data.loc[i, 'exit_type'] = 'Target1'
            active_position=0
            bars_since_signal=0
            data.loc[i,'Sreturn']=(data['L1'].loc[i]-data.Close.loc[i-1])*active_position
Ryan
  • 2,073
  • 1
  • 19
  • 33
user3757265
  • 427
  • 1
  • 4
  • 11

0 Answers0