jamesmawm / Mastering-Python-for-Finance-source-codes

Accompanying source codes for my book 'Mastering Python for Finance'.
MIT License
513 stars 276 forks source link

Having trouble with while loop inside oandapy.streamer #3

Closed mahbubk9 closed 7 years ago

mahbubk9 commented 7 years ago

Hi, I am trying to use your ForexSystem.py code for trading. For my strategy I wanted to use ATR indicator, the ATR code is; #Average True Range def ATR(df, n): i = 0 TR_l = [0] while i < df.index[-1]: TR = max(df.get_value(i + 1, 'high'), df.get_value(i, 'close')) - min(df.get_value(i + 1, 'low'), df.get_value(i, 'close')) TR_l.append(TR) i = i + 1 TR_s = pd.Series(TR_l) ATR = pd.Series(TR_s.rolling(window=12,min_periods=12,center=False).mean(), name = 'ATR_' + str(n)) df = df.join(ATR) return df which i modified for your code as; `#Average True Range
def ATR(self,data):
i = 0 TR_l = [0]
while i < len(self.resampled_prices.index): #THIS IS WHERE I THINK THE PROBLEM IS BUT DON'T

KNOW HOW TO SOLVE

        TR = max(self.resampled_prices.get_value(i + 1, 'high'), self.resampled_prices.get_value(i, 'close')) - min(self.resampled_prices.get_value(i + 1, 'low'), self.resampled_prices.get_value(i, 'close'))  
        TR_l.append(TR)  
        i = i + 1  
    TR_s = pd.Series(TR_l)  
    ATR = pd.Series(TR_s.rolling(window=4,min_periods=4,center=False).mean(), name = 'ATR_' + str(4))  
    self.resampled_prices = self.resampled_prices.join(ATR)  
    return self.resampled_prices `
   By the way this resampled price is made using your self.prices as ;
   `   self.resampled_prices =  self.prices.resample('10s').ohlc().ffill() `
   Also, i am running this ATR function after having few ticks, All the code  is inside your "def parse_tick_data" function,
   SHALL I SEND YOU MY VERSION OF YOUR CODE?

Thanks, Mahbub khan

   `   `
mahbubk9 commented 7 years ago

Hi, I put the code in my forked repo of yours, in chapter 08 codes, as ForexSystem 0.2, could you please have a look at it. Thanks, Mahbub

jamesmawm commented 7 years ago

Please indent your code if you want somebody to read it.

def ATR(self,data): 
    i = 0 TR_l = [0] 
    while i < len(self.resampled_prices.index): 
        #THIS IS WHERE I THINK THE PROBLEM IS BUT DON'T 
        # KNOW HOW TO SOLVE 
       TR = max(self.resampled_prices.get_value(i + 1, 'high')

Like this. Python relies on indentation to get your code working.

jamesmawm commented 7 years ago

Managed to find your code and looked through it, seems to be syntactically correct. What was the problem or error message you got?

mahbubk9 commented 7 years ago

Many thanks for your reply, there were a lot of outputs, with last line "Keyerror: "High" Also, File "C:/Users/Mahbub/Documents/PythonTrading/ForexSystem 0.2.py", line 81, in ATR TR = max(self.resampled_prices.get_value(i + 1, 'high'), self.resampled_prices.get_value(i, 'close')) - min(self.resampled_prices.get_value(i + 1, 'low'), self.resampled_prices.get_value(i, 'close') above line were one of the outputs. That's how I guessed some how the loop is not working, and df.get_value is not getting the high column values. Could you please run it on your side, my credentials are there, i will change it later, plus it's practice account. Thanks

mahbubk9 commented 7 years ago

I just tried df.loc instead of df.get_value for resampled prices and got this error : KeyError: 'the label [high] is not in the [index]'

jamesmawm commented 7 years ago

Afraid I'm unable to afford the luxury of time to work on other stuffs now, best I could do is to look through codes in some free time. You can try using PyCharm's built-in debugger to perform step-by-step debugging.

The last line of your error is pretty intuitive - there is no column high as the index for self.resampled_prices.

It seems you're expecting a column high after performing ohlc resampling, but you're overriding the dataframe's indexes with self.resampled_prices.index=n. Please make sure the resampled_prices dataframe is in the correct format that you need.