Skip to content

Commit

Permalink
alpha simpler examples
Browse files Browse the repository at this point in the history
  • Loading branch information
javifalces committed Feb 14, 2024
1 parent 1345f8c commit 9edfa38
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 121 deletions.
65 changes: 18 additions & 47 deletions python/trading_algorithms/market_making/alpha_avellaneda_stoikov.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,9 @@ def plot_params(

if __name__ == '__main__':
import os
import datetime

os.environ["LOG_STATE_STEPS"] = "1" # print each step state in logs
# os.environ["LOG_STATE_STEPS"] = "1" # print each step state in logs
Algorithm.MULTITHREAD_CONFIGURATION = MultiThreadConfiguration.singlethread
Algorithm.FEES_COMMISSIONS_INCLUDED = False
Algorithm.DELAY_MS = 0
Expand Down Expand Up @@ -376,30 +377,22 @@ def plot_params(
AlgorithmParameters.ui: 0,
RlAlgorithmParameters.training_stats: False,
RlAlgorithmParameters.action_type: ReinforcementLearningActionType.discrete,
RlAlgorithmParameters.model: BaseModelType.PPO,
RlAlgorithmParameters.custom_neural_networks: {"net_arch": [256, 256]},
RlAlgorithmParameters.model: BaseModelType.PPO

}
# best_avellaneda_param_dict = {
# AvellanedaStoikovParameters.risk_aversion: 0.079665431,
# AvellanedaStoikovParameters.midprice_period_window: 60,
# AvellanedaStoikovParameters.seconds_change_k: 60
# }


algorithm_info_dqn = 'ppo_pytest'

avellaneda_dqn = AlphaAvellanedaStoikov(
algorithm_info=algorithm_info_dqn, parameters=parameters_default_dqn
)
# avellaneda_dqn.set_parameters(
# best_avellaneda_param_dict
# ) # same optimization as benchmark
alpha_avellaneda = AlphaAvellanedaStoikov(
algorithm_info=algorithm_info_dqn, parameters=parameters_default_dqn)

# print('Starting training')
output_train = avellaneda_dqn.train(
print('Starting training')
output_train = alpha_avellaneda.train(
instrument_pk=instrument_pk,
start_date=datetime.datetime(year=2023, day=13, month=11, hour=7),
end_date=datetime.datetime(year=2023, day=13, month=11, hour=15),
iterations=10,
iterations=3,
simultaneous_algos=1,
clean_initial_experience=True,
plot_training=True,
Expand All @@ -409,39 +402,17 @@ def plot_params(
)

print('Starting testing')

results = []
scores = []
# avellaneda_dqn.clean_model(output_path=BACKTEST_OUTPUT_PATH)
iterations = 0
explore_prob = 1.0

parameters = avellaneda_dqn.get_parameters(explore_prob=explore_prob)
avellaneda_dqn.set_parameters(parameters)

output_test = avellaneda_dqn.test(
output_test = alpha_avellaneda.test(
instrument_pk=instrument_pk,
start_date=datetime.datetime(year=2023, day=13, month=11, hour=10),
end_date=datetime.datetime(year=2023, day=13, month=11, hour=12),
start_date=datetime.datetime(year=2023, day=13, month=11, hour=15),
end_date=datetime.datetime(year=2023, day=13, month=11, hour=20),
clean_experience=False,
)

name_output = avellaneda_dqn.get_test_name(name=avellaneda_dqn.NAME)
print(rf"output_test.keys() = {output_test.keys()}")
name_output = alpha_avellaneda.get_test_name(name=alpha_avellaneda.NAME)
backtest_df = output_test[name_output]

score = get_score(
backtest_df=backtest_df,
score_enum=ScoreEnum.realized_pnl,
equity_column_score=ScoreEnum.realized_pnl,
)
alpha_avellaneda.plot_trade_results(raw_trade_pnl_df=backtest_df)
import matplotlib.pyplot as plt
plt.show()


plt.figure()
fig, df = avellaneda_dqn.plot_trade_results(
raw_trade_pnl_df=output_test[name_output], title='test %d' % iterations
)
# fig.savefig(rf"{name_output}_test.png")
# plt.show()
#
# avellaneda_dqn.plot_params(raw_trade_pnl_df=output_test[name_output])
# plt.show()
94 changes: 20 additions & 74 deletions python/trading_algorithms/market_making/alpha_constant_spread.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,81 +224,27 @@ def plot_params(

alpha_constant_spread.set_parameters(parameters=parameters_base_pt)

# print('Starting training')
# output_train = avellaneda_dqn.train(
# instrument_pk='btcusdt_binance',
# start_date=datetime.datetime(year=2020, day=8, month=12, hour=10),
# end_date=datetime.datetime(year=2020, day=8, month=12, hour=14),
# iterations=3,
# # algos_per_iteration=1,
# # simultaneous_algos=1,
# )

# name_output = avellaneda_dqn.NAME + '_' + avellaneda_dqn.algorithm_info + '_0'

# backtest_result_train = output_train[0][name_output]
# # memory_replay_file = r'E:\Usuario\Coding\Python\market_making_fw\python_lambda\output\memoryReplay_AvellanedaDQN_test_main_dqn_0.csv'
# # memory_replay_df=avellaneda_dqn.get_memory_replay_df(memory_replay_file=memory_replay_file)
#
# avellaneda_dqn.plot_trade_results(raw_trade_pnl_df=backtest_result_train,title='train initial')
#
# backtest_result_train = output_train[-1][name_output]
# avellaneda_dqn.plot_trade_results(raw_trade_pnl_df=backtest_result_train,title='train final')

print('Starting training')
output_train = alpha_constant_spread.train(
instrument_pk='btcusdt_binance',
start_date=datetime.datetime(year=2023, day=13, month=11, hour=7),
end_date=datetime.datetime(year=2023, day=13, month=11, hour=15),
iterations=3,
# algos_per_iteration=1,
# simultaneous_algos=1,
)
print('Starting testing')
output_test = alpha_constant_spread.test(
instrument_pk='btcusdt_kraken',
start_date=datetime.datetime(year=2023, day=13, month=11, hour=15),
end_date=datetime.datetime(year=2023, day=13, month=11, hour=20),
clean_experience=False,
)

results = []
scores = []
alpha_constant_spread.clean_model(output_path=LAMBDA_OUTPUT_PATH)
iterations = 0
explore_prob = 1.0
while True:

parameters = alpha_constant_spread.get_parameters(explore_prob=explore_prob)
alpha_constant_spread.set_parameters(parameters)
if iterations == 0:
clean_experience = True
else:
clean_experience = False
print(
rf"starting training with explore_prob = {alpha_constant_spread.parameters['epsilon']}"
)
output_test = alpha_constant_spread.test(
instrument_pk='btcusdt_kraken',
start_date=datetime.datetime(year=2023, day=13, month=11, hour=7),
end_date=datetime.datetime(year=2023, day=13, month=11, hour=15),
trainingPredictIterationPeriod=IterationsPeriodTime.END_OF_SESSION,
trainingTargetIterationPeriod=IterationsPeriodTime.END_OF_SESSION,
clean_experience=clean_experience,
)
# name_output = avellaneda_dqn.NAME + '_' + avellaneda_dqn.algorithm_info + '_0'
name_output = alpha_constant_spread.get_test_name(
name=alpha_constant_spread.NAME, algorithm_number=0
)
backtest_df = output_test[name_output]

score = get_score(
backtest_df=backtest_df,
score_enum=ScoreEnum.realized_pnl,
equity_column_score=ScoreEnum.realized_pnl,
)

results.append(backtest_df)
scores.append(score)

import matplotlib.pyplot as plt

alpha_constant_spread.plot_trade_results(
raw_trade_pnl_df=output_test[name_output], title='test %d' % iterations
)
plt.show()
name_output = alpha_constant_spread.get_test_name(name=alpha_constant_spread.NAME)
backtest_df = output_test[name_output]
alpha_constant_spread.plot_trade_results(raw_trade_pnl_df=backtest_df)
import matplotlib.pyplot as plt
plt.show()

alpha_constant_spread.plot_params(raw_trade_pnl_df=output_test[name_output])
plt.show()

pd.Series(scores).plot()
plt.title(f'scores evolution {explore_prob} {iterations} ')
plt.show()
iterations += 1
explore_prob -= 0.05
explore_prob = max(explore_prob, 0.05)

0 comments on commit 9edfa38

Please sign in to comment.