Home > Software design >  Making decisions based on the times of datetime in a dataframe
Making decisions based on the times of datetime in a dataframe

Time:01-21

I am new to programming and python. After many hours of research I want to ask the community for help. I want to use data to backtest a trading strategy based on the beginning of the London Session.

I have a dataframe:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 28766 entries, 0 to 28765
Data columns (total 6 columns):
| #  | Column | Non-Null Count  |Dtype   |      
|--- |------ |--------------  |----- |        
| 0  |Time   |28766 non-null  |datetime64[ns]|
| 1  |Open   |28766 non-null  |float64 |
| 2 |...|

|    |Time                  |Open    | High   | Low    |Close   |Volume  |
|--- |--------------------- |------- |------- |------- |------- | ------|
|8   |2017-05-23 08:00:00   |2180.7  |2187.2  |2139.2  | 2170.2 | 97.0  |

And now I want to define a function which recognizes every row with 08:00 GMT in it:

def LondonSession(df):
df = df.copy()
for t in df['Time']:
    if df[df['Time'].dt.hour == (8)]:
        df['StopLoss'] = technicals(df)['atr'] * (-1)
        df['TakeProfit'] = technicals(df)['atr'] * (2)
    else: 
        df['StopLoss'] = technicals(df)['atr'] * (0)
        df['TakeProfit'] = technicals(df)['atr'] * (0)
return df

print(LondonSession(df)[0:10])

Unfortunately I have no more ideas how to solve the error messages:

ValueError                                Traceback (most recent call last) Input In [204], in <module>
      9             df['TakeProfit'] = technicals(df)['atr'] * (0)
     10     return df
---> 11 print(LondonSession(df)[0:10])

Input In [204], in LondonSession(df)
      2 df = df.copy()
      3 for t in df['Time']:
----> 4     if df[df['Time'].dt.hour == (8)]:
      5         df['StopLoss'] = technicals(df)['atr'] * (-1)
      6         df['TakeProfit'] = technicals(df)['atr'] * (2)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\generic.py:1537, in NDFrame.__nonzero__(self)    1535 @final    1536 def
__nonzero__(self):
-> 1537     raise ValueError(    1538         f"The truth value of a {type(self).__name__} is ambiguous. "    1539         "Use a.empty, a.bool(), a.item(), a.any() or a.all()."    1540     )

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Any input is recommended. Thanks in advance!

CodePudding user response:

The problem here is what you wrote inside the first if condition : df[df['Time'].dt.hour == (8)]. This code doesn't return a boolean but rather a dataframe in which the selected rows are the ones where the attribute Time is equal to 8.

If you want to keep your for-loop structure, you should therefore change the condition with if t.hour == 8. But keep in mind that with pandas you should rather avoid for-loops and use row filtering (you can read more on that matter on this pandas official tutorial).

CodePudding user response:

The solution for me is:

test2.loc[test2['Time'].dt.hour == (8), 'TakeProfit'] = (test2['atr'] * (1.5)) test2.loc[test2['Time'].dt.hour == (8), 'StopLoss'] = (test2['atr'] * (-1)) test2.loc[test2['Time'].dt.hour != (8), 'TakeProfit'] = (0) test2.loc[test2['Time'].dt.hour != (8), 'StopLoss'] = (0)

I read about in this article: enter link description here

The article describes more options for my problem. But I had success with the first one.

  •  Tags:  
  • Related