Pandas Trade Signal Columns
I am trying to add columns showing a value of 1 where a trade signal is seen. I have put together the following test data to show what I am trying to do. Create test dataframe: im
Solution 1:
Hope this helps. I changed a bit in the data generation code.
import pandas as pd
import random
periods = 48*7
index = pd.date_range('2013-1-1',periods=periods,freq='30Min')
data = pd.DataFrame({'value':[random.randint(0,100)+i/10for i inrange(periods)], 'enter_long':[False]*periods, 'enter_short':[False]*periods}, index=index)
daystart = '9:30'
dayend = '16:14:59'
day_session = data.between_time(daystart,dayend, include_start=True, include_end=True)
day_high = day_session['value'].rolling(window=1,freq='D').max()
day_low = day_session['value'].rolling(window=1,freq='D').min()
print(day_high)
day high for seven week period
2013-01-01 97.12013-01-02 104.92013-01-03 109.72013-01-04 113.52013-01-05 104.32013-01-06 121.72013-01-07 113.6
day low for the same 7 day period
2013-01-01 9.02013-01-02 7.32013-01-03 13.52013-01-04 18.92013-01-05 24.52013-01-06 46.62013-01-07 42.
Finds signals
for i, date inenumerate(day_high.index):
df_sub=data[data.index.day==date.day]
d = df_sub[(df_sub.value>day_high[i]) | (df_sub.value<day_low[i])].iloc[0:1]
try:
if d.value[0]>day_high[i]:
data.loc[d.index,'enter_long']=Trueelse:
data.loc[d.index,'enter_short']=Trueexcept IndexError:
print("No signals")
print(data[(data['enter_long']==True) | (data['enter_short']==True)])
Returns
enter_longenter_shortvalue2013-01-01 01:30:00 FalseTrue3.32013-01-02 07:30:00 TrueFalse105.32013-01-03 07:30:00 FalseTrue13.12013-01-04 01:00:00 FalseTrue17.62013-01-05 00:00:00 FalseTrue24.22013-01-06 02:00:00 TrueFalse123.42013-01-07 02:00:00 TrueFalse129.2
Post a Comment for "Pandas Trade Signal Columns"