
count = 0
open_buy = False
open_sell = False
buy = 0.
sell = 0.
pnl = 0.
with open('data/a_pnl.csv', 'w') as w:
    with open('data/results.csv') as f:
        for line in f:
            count += 1
            if count == 1:
                w.write(line)
                continue
            parts = line.split(',')
            days = int(parts[8])
            future_close = float(parts[6])
            roll = float(parts[9])
            if roll > 0.1 and not open_sell:
                open_sell = True
                sell = future_close
                line += ',%s,,SELL' % str(future_close)
            elif roll < -0.1 and not open_buy:
                open_buy = True
                buy = future_close
                line += ',%s,,BUY' % str(-1*future_close)
            elif days == 1 and open_sell:
                open_sell = False
                line += ',%s,%s,' % (str(-1*future_close), sell-future_close)
                print("Profit: %s" % (sell-future_close))
                pnl += (sell-future_close)
            elif days == 1 and open_buy:
                open_buy = False
                line += ',%s,%s,' % (str(future_close), future_close - buy)
                print("Profit: %s" % (future_close - buy))
                pnl += (future_close - buy)
            w.write(line)
        print("PnL: %s" % pnl)


@@ -552,26 +552,45 @@
        line 
-+
 =
+ line.rstrip('%5Cn') +
  ',%25s,,SELL'
@@ -588,16 +588,19 @@
 %25s,,SELL
+,%5Cn
 ' %25 str(
@@ -744,26 +744,45 @@
        line 
-+
 =
+ line.rstrip('%5Cn') +
  ',%25s,,BUY' 
@@ -779,16 +779,18 @@
 ,%25s,,BUY
+%5Cn
 ' %25 str(
@@ -895,34 +895,53 @@
            line 
-+
 =
+ line.rstrip('%5Cn') +
  ',%25s,%25s,' %25 (st
@@ -925,32 +925,34 @@
 '%5Cn') + ',%25s,%25s,
+%5Cn
 ' %25 (str(-1*futu
@@ -962,16 +962,22 @@
 close), 
+round(
 sell-fut
@@ -977,32 +977,36 @@
 ell-future_close
+, 2)
 )%0A              
@@ -1187,18 +1187,37 @@
    line 
-+
 =
+ line.rstrip('%5Cn') +
  ',%25s,%25s
@@ -1217,16 +1217,18 @@
 ',%25s,%25s,
+%5Cn
 ' %25 (str
@@ -1243,16 +1243,22 @@
 close), 
+round(
 future_c
@@ -1259,32 +1259,36 @@
 ture_close - buy
+, 2)
 )%0A              

fix format