Skip to content

Commit

Permalink
change order of arguments of simulation function and adjust its output
Browse files Browse the repository at this point in the history
  • Loading branch information
asavinov committed Nov 19, 2023
1 parent 21c164a commit 8bd22a6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
21 changes: 16 additions & 5 deletions common/signal_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def apply_rule_with_slope_thresholds(df, model, buy_score_column, sell_score_col
# Trade performance calculation
#

def simulated_trade_performance(df, sell_signal_column, buy_signal_column, price_column):
def simulated_trade_performance(df, buy_signal_column, sell_signal_column, price_column):
"""
The function simulates trades over the time by buying and selling the asset
according to the specified buy/sell signals and price. Essentially, it assumes
Expand Down Expand Up @@ -287,7 +287,7 @@ def simulated_trade_performance(df, sell_signal_column, buy_signal_column, price
short_transactions += 1
if profit > 0:
short_profitable += 1
longs.append((index, is_buy_mode, price, profit, profit_percent)) # Bought
shorts.append((index, previous_price, price, profit, profit_percent)) # Bought
is_buy_mode = False
else:
# Check if maximum price
Expand All @@ -300,22 +300,22 @@ def simulated_trade_performance(df, sell_signal_column, buy_signal_column, price
long_transactions += 1
if profit > 0:
long_profitable += 1
shorts.append((index, is_buy_mode, price, profit, profit_percent)) # Sold
longs.append((index, previous_price, price, profit, profit_percent)) # Sold
is_buy_mode = True

long_performance = dict( # Performance of buy at low price and sell at high price
profit=long_profit,
profit_percent=long_profit_percent,
transaction_no=long_transactions,
profitable=long_profitable / long_transactions if long_transactions else 0.0,
transactions=longs, # Buy signal list
transactions=longs, # Sell transactions
)
short_performance = dict( # Performance of sell at high price and buy at low price
profit=short_profit,
profit_percent=short_profit_percent,
transaction_no=short_transactions,
profitable=short_profitable / short_transactions if short_transactions else 0.0,
transactions=shorts, # Sell signal list
transactions=shorts, # Buy transactions
)

profit = long_profit + short_profit
Expand Down Expand Up @@ -545,4 +545,15 @@ def all_lower_fn(row, model):


if __name__ == '__main__':
from pathlib import Path
import numpy as np
import pandas as pd

df = pd.read_csv(r"C:\DATA_ITB\BTCUSDT\signals.csv", parse_dates=["timestamp"], nrows=100_000_000)

df = df.dropna(subset=["close"])

performance, long_performance, short_performance = \
simulated_trade_performance(df, 'sell_signal_column', 'buy_signal_column', 'close')

pass
2 changes: 1 addition & 1 deletion scripts/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def main(config_file):
signal_column_names = trade_model.get("signal_columns")

performance, long_performance, short_performance = \
simulated_trade_performance(df, signal_column_names[1], signal_column_names[0], 'close')
simulated_trade_performance(df, signal_column_names[0], signal_column_names[1], 'close')

#
# Convert to columns: longs, shorts, signal, profit (both short and long)
Expand Down
2 changes: 1 addition & 1 deletion scripts/train_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def main(config_file):
# Add a pair of two dicts: performance dict and model parameters dict
#
performance, long_performance, short_performance = \
simulated_trade_performance(df, 'sell_signal_column', 'buy_signal_column', 'close')
simulated_trade_performance(df, 'buy_signal_column', 'sell_signal_column', 'close')

# Remove some items. Remove lists of transactions which are not needed
long_performance.pop('transactions', None)
Expand Down

0 comments on commit 8bd22a6

Please sign in to comment.