Created
September 3, 2019 17:57
-
-
Save lamres/af9f8027ea2dd0dc791746e962858b19 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def handle_data(context, data): | |
current_date = get_datetime().date() | |
current_time = get_datetime().time() | |
# Just one time in a day (first minute) | |
if current_time.hour == 0 and current_time.minute == 0 and current_time.second == 0: | |
prices = pd.DataFrame() | |
volumes = pd.DataFrame() | |
try: | |
prices = data.history(context.asset, | |
fields = 'price', | |
bar_count = context.n_periods, | |
frequency = context.tf) | |
volumes = data.history(context.asset, | |
fields = 'volume', | |
bar_count = context.n_periods, | |
frequency = context.tf) | |
except: | |
print('NO DATA') | |
if prices.shape[0] == context.n_periods and volumes.shape[0] == context.n_periods: | |
features = pd.DataFrame() | |
features['price'] = prices | |
features['volume'] = volumes | |
features['last_return'] = features['price'].pct_change() | |
features['std_normalized'] = features['price'].rolling(context.std_period).apply(std_normalized) | |
features['ma_ratio'] = features['price'].rolling(context.ma_period).apply(ma_ratio) | |
features['price_deviation'] = features['price'].rolling(context.price_deviation_period).apply(values_deviation) | |
features['volume_deviation'] = features['volume'].rolling(context.volume_deviation_period).apply(values_deviation) | |
state = context.random_states[0] | |
if features.dropna().shape[0] == (context.n_periods - context.ma_period + 1): | |
state = int(context.model.predict(features[context.cols_features].dropna())[-1]) | |
else: | |
print('PROBLEM: features dataframe is too small') | |
print('State on ' + str(current_date) + ' ' + str(current_time) + ': ' + str(state)) | |
print('Amount on ' + str(current_date) + ' ' + str(current_time) + ': ' + str(context.portfolio.positions[context.asset].amount)) | |
print(prices.dropna()) | |
print(volumes.dropna()) | |
if context.portfolio.positions[context.asset].amount <= 0 and state in context.long_states: | |
print('LONG on ' + str(current_date) + ' ' + str(current_time)) | |
order_target_percent(context.asset, 1.0 * context.leverage) | |
context.best_price_ts = data.current(context.asset, 'close') | |
if context.portfolio.positions[context.asset].amount != 0 and state in context.random_states: | |
print('CLOSE on ' + str(current_date) + ' ' + str(current_time)) | |
order_target_percent(context.asset, 0.0) | |
if context.portfolio.positions[context.asset].amount >= 0 and state in context.short_states: | |
print('SHORT on ' + str(current_date) + ' ' + str(current_time)) | |
order_target_percent(context.asset, -1.0 * context.leverage) | |
context.best_price_ts = data.current(context.asset, 'close') | |
record(price = prices[-1], | |
state = state, | |
amount = context.portfolio.positions[context.asset].amount) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment