from datetime import datetime, timedelta

class Instrument(object):
    def __init__(
        self, broker, instrument, time_frame,
        market_status, last_update, dt
    ):
        trading_wk_str = dt - timedelta(days=dt.weekday()+1)
        self.sw_hour = trading_wk_str.hour
        self.td = trading_wk_str.hour - 22
        # Passport
        self.instrument = instrument
        self.time_frame = time_frame
        self.market_status = market_status
        self.last_update = last_update
        self.db_min = None
        self.db_max = None
        self.fin_bar = None

    def update(
        self, lastupdate, market_status
    ):
        self.last_update = lastupdate
        self.market_status = market_status
      
    def calculate_finished_bar(self):
        """
        Create time delta resolution for finding the 
        last finished bar published by FXCM. This is to stop
        unfinished bars being written to the database.
        """
        lu = self.last_update.replace(second=0,microsecond=0)
        tf = int(self.time_frame[1:])
        
        # New Your Offset
        if self.td % 2 == 0: nyo = 1
        else: nyo = 2
          
        if self.time_frame[:1] == "m":  # Minute
            ub = lu
            l = list(range(0,60,tf))
            cm = min(l, key=lambda x:abs(x-ub.minute))
            fin = ub.replace(minute=cm)-timedelta(minutes=tf)
            
        elif self.time_frame[:1] == "H":  # Hour
            ub = lu.replace(minute=0)
            h = ub.hour
            l = [e-nyo for e in [i + tf - 2 for i in list(range(1,25,tf))]]
            ch = min(l, key=lambda x:abs(x-h))
            fin = ub.replace(hour=ch)-timedelta(hours=tf)            

        elif self.time_frame[:1] == "D":  # Day
            init_bar = lu.replace(hour=self.sw_hour,minute=0)
            fin = init_bar - timedelta(days=tf)
            
        elif self.time_frame[:1] == "M":  # Month
            init_bar = lu.replace(day=1,hour=self.sw_hour,minute=0)
            fin = init_bar - timedelta(days=1)
        else:
            raise NotImplmented("Time-frame : %s Not Supported" % self.time_frame)
            
        self.fin_bar = fin

@@ -179,16 +179,53 @@
         
+# Start of Trading Week%0A        self.
 trading_
@@ -292,16 +292,21 @@
 _hour = 
+self.
 trading_
@@ -335,16 +335,21 @@
 lf.td = 
+self.
 trading_
@@ -536,24 +536,40 @@
 last_update%0A
+        # Dates%0A
         self
@@ -654,25 +654,16 @@
  update(
-%0A        
 self, la
@@ -685,21 +685,16 @@
 t_status
-%0A    
 ):%0A     
@@ -838,53 +838,55 @@
     
-Create time delta resolution for finding
+Stops unfinished bars from being written to
  the
- 
 %0A   
@@ -894,83 +894,54 @@
     
-last finished bar published by FXCM. This is to stop%0A        un
+database by calculating the latest 
 fin
-i
 shed bar
 s be
@@ -940,39 +940,8 @@
  bar
-s being written to the database
 .%0A  
@@ -1075,18 +1075,18 @@
 # New Yo
-u
 r
+k
  Offset%0A
@@ -1212,26 +1212,27 @@
             
-ub
+adj
  = lu%0A      
@@ -1305,18 +1305,19 @@
 x:abs(x-
-ub
+adj
 .minute)
@@ -1328,34 +1328,35 @@
           fin = 
-ub
+adj
 .replace(minute=
@@ -1455,18 +1455,19 @@
         
-ub
+adj
  = lu.re
@@ -1486,32 +1486,8 @@
 =0)%0A
-            h = ub.hour%0A
     
@@ -1601,17 +1601,24 @@
 x:abs(x-
-h
+adj.hour
 ))%0A     
@@ -1634,10 +1634,11 @@
 n = 
-ub
+adj
 .rep
@@ -1740,32 +1740,27 @@
             
-init_bar
+adj
  = lu.replac
@@ -1803,32 +1803,27 @@
       fin = 
-init_bar
+adj
  - timedelta
@@ -1907,24 +1907,19 @@
         
-init_bar
+adj
  = lu.re
@@ -1980,16 +1980,11 @@
 n = 
-init_bar
+adj
  - t
@@ -2000,16 +2000,17 @@
 days=1)%0A
+%0A
         
@@ -2082,26 +2082,8 @@
 ted%22
- %25 self.time_frame
 )%0A  

Update instrument.py