import pandas as pd
from pprint import pprint

stream = pd.read_csv('/home/ubuntu/tessa/BANKNIFTY2171534900PE_ticks.csv')

tick_store = []
oi_store = []
candles = []
count = 0
prev_oi = stream.iloc[0].oi
for index, row in stream.iterrows():
    tick_store.append(row.ltp)
    
    if row.oi != prev_oi:
        count += 1
        oi_store.append(row.oi)
        candles.append([row.timestamp, tick_store[0], max(tick_store), min(tick_store), tick_store[-1], row.oi])
        tick_store = []
        
        
    prev_oi = row.oi
pprint(candles)
    
open_position = False
last_candle = candles[0]
profit = 0
trails = []
for candle in candles:
    if not open_position:
        if candle[1] > last_candle [1]:
            if candle[4] > last_candle[4]:
                open_position = True
            
                buy_price = candle[3]

    elif open_position:
        if candle[1] < last_candle[1]:
            open_position = False
            print("Buy", buy_price, "Sell", candle[3], trails)
            print(candle[3] - buy_price)
            profit += candle[3] - buy_price

            buy_price = 0
            trails = []
        else:
            trails.append(candle[3])

    last_candle = candle
print("Overall PNL:", profit)
              


