Skip to content

Commit

Permalink
renaming file
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmeier committed Jun 14, 2021
1 parent 596390f commit dca1795
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 169 deletions.
2 changes: 1 addition & 1 deletion fast_trade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
from .transformers_map import transformers_map
from .validate_backtest import validate_backtest
from .update_symbol_data import update_symbol_data, load_archive_to_df
from .check_missing_dates import check_missing_dates
from .calculate_perc_missing import calculate_perc_missing
4 changes: 4 additions & 0 deletions fast_trade/build_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import pandas as pd
from .calculate_perc_missing import calculate_perc_missing


def build_summary(df, performance_start_time):
Expand Down Expand Up @@ -58,6 +59,8 @@ def build_summary(df, performance_start_time):

performance_stop_time = datetime.datetime.utcnow()

perc_missing = calculate_perc_missing(df)

summary = {
"return_perc": return_perc,
"sharpe_ratio": sharpe_ratio, # BETA
Expand All @@ -84,6 +87,7 @@ def build_summary(df, performance_start_time):
"first_tic": start_date.strftime("%Y-%m-%d %H:%M:%S"),
"last_tic": end_date.strftime("%Y-%m-%d %H:%M:%S"),
"total_tics": len(df.index),
"perc_missing": perc_missing,
"test_duration": round(
(performance_stop_time - performance_start_time).total_seconds(), 3
),
Expand Down
22 changes: 22 additions & 0 deletions fast_trade/calculate_perc_missing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def calculate_perc_missing(df):
"""
Parameters
----------
df, pandas dataframe check
Returns
-------
perc_missing, float, a float of the percentage of rows with the missing closing price
"""

total_missing = df.close.isna().sum()

perc_missing = 0.0
if total_missing:
total_tics = len(df.index)
perc_missing = (total_missing / total_tics) * 100

perc_missing = round(perc_missing, 2)

return perc_missing, total_missing
49 changes: 0 additions & 49 deletions fast_trade/check_missing_dates.py

This file was deleted.

4 changes: 2 additions & 2 deletions fast_trade/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ def main():
return

if command == "validate":
print("args: ",args)
print("args: ", args)
backtest = open_strat_file(args["backtest"])
if not backtest:
print("backtest not found! ")
return
print("backtest: ",backtest)
print("backtest: ", backtest)
backtest = {**backtest, **args}

res = validate_backtest(backtest)
Expand Down
2 changes: 0 additions & 2 deletions fast_trade/run_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def apply_logic_to_df(df: pd.DataFrame, backtest: dict):
adj_account_value = new_account_value + convert_aux_to_base(aux, close)

aux_list.append(aux)
# base_list.append(new_base)
account_value_list.append(new_account_value)
in_trade_list.append(in_trade)
fee_list.append(fee)
Expand All @@ -99,7 +98,6 @@ def apply_logic_to_df(df: pd.DataFrame, backtest: dict):
adj_account_value_list.append(adj_account_value)

df["aux"] = aux_list
# df["base"] = base_list
df["account_value"] = account_value_list
df["adj_account_value"] = adj_account_value_list
df["in_trade"] = in_trade_list
Expand Down
1 change: 0 additions & 1 deletion fast_trade/run_backtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def run_backtest(
"summary": summary,
"df": df,
"trade_df": trade_log,
# "missing_data": gaps,
"backtest": new_backtest,
}

Expand Down
105 changes: 49 additions & 56 deletions run_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from fast_trade.build_data_frame import prepare_df
from fast_trade import run_backtest
from fast_trade.validate_backtest import validate_backtest
from fast_trade.check_missing_dates import check_missing_dates
from fast_trade.calculate_perc_missing import calculate_perc_missing
import datetime
import json
import pandas as pd
Expand All @@ -27,56 +27,55 @@
# case 1: sell

# case 2: buy
backtest = {
"any_enter": [],
"any_exit": [],
"chart_period": "1H",
"comission": 0.00,
"base_balance": 100000,
"lot_size": 0.6,
"datapoints": [{"args": [], "name": "rsi_dp", "transformer": "rsi"}],
"enter": [
[
"rsi_dp",
"<",
30,
]
],
"exit": [
[
"rsi_dp",
">",
70,
]
],
"exit_on_end": False,
"max_lot_size": 0
# "start": "2019-12-01 15:29:00",
# "stop": "2021-03-08 15:29:00",
# "trailing_stop_loss": 0.05, # 5% stoploss
}

# backtest = {
# "any_enter": [],
# "any_exit": [],
# # "chart_period": "37Min",
# "chart_period": "11Min",
# "chart_period": "15Min",
# "comission": 0.01,
# "datapoints": [
# {"args": [14], "name": "er", "transformer": "er"},
# {"args": [14], "name": "zlema_1", "transformer": "zlema"},
# {"args": [66], "name": "zlema_2", "transformer": "zlema"},
# "base_balance": 100000,
# "lot_size": 0.6,
# "datapoints": [{"args": [], "name": "rsi_dp", "transformer": "rsi"}],
# "enter": [
# [
# "rsi_dp",
# "<",
# 30,
# ]
# ],
# "exit": [
# [
# "rsi_dp",
# ">",
# 70,
# ]
# ],
# "enter": [["zlema_1", ">", "close", 1]],
# "exit": [["zlema_2", "<", "close", 1]],
# "exit_on_end": False,
# # "start": "2021-01-01 22:30:00",
# # "stop": "2021-03-11 23:30:59",
# "trailing_stop_loss": 0.05,
# # "max_lot_size": 1000,
# "lot_size": 1,
# "base_balance": 500,
# "max_lot_size": 0
# # "start": "2019-12-01 15:29:00",
# # "stop": "2021-03-08 15:29:00",
# # "trailing_stop_loss": 0.05, # 5% stoploss
# }

backtest = {
"any_enter": [],
"any_exit": [],
# "chart_period": "37Min",
"chart_period": "1H",
"comission": 0.01,
"datapoints": [
{"args": [11], "name": "zlema_1", "transformer": "zlema"},
{"args": [88], "name": "zlema_2", "transformer": "zlema"},
],
"enter": [["zlema_1", ">", "close", 4]],
"exit": [["zlema_2", "<", "close", 1]],
"exit_on_end": False,
# "start": "2021-01-01 22:30:00",
# "stop": "2021-03-11 23:30:59",
# "trailing_stop_loss": 0.05,
# "max_lot_size": 1000,
"lot_size": 1,
"base_balance": 500,
}
# backtest = {
# "any_enter": [],
# "any_exit": [],
Expand All @@ -93,25 +92,19 @@
if __name__ == "__main__":
# datafile = "./BTCUSDT.csv"
# datafile = "./archive/BTCUSDT_2021.csv"
datafile = "/Users/jedmeier/Desktop/BTCUSDT_ALL/BTCUSDT_2021.csv"
datafile = "/Users/jedmeier/Desktop/BTCUSDT_ALL/BTCUSDT_2020.csv"
# datafile = "/Users/jedmeier/Desktop/BTCUSDT_2021_06_12.csv"
# datafile = "./archive/BCHUSDT_2021.csv"

# df = pd.read_csv(datafile)

# df.index = pd.to_datetime(df.date, unit="s")
# print(df)
# check_missing_dates(df)
# with open("./example_backtest.json", "r") as backtest_file:
# backtest = json.load(backtest_file)
# print(backtest)
# backtest["start"] = "2021-03-01"
# backtest["stop"] = "2021-05-01"

# backtest["chart_period"] = "1Min"
test = run_backtest(backtest, datafile, summary=True)
# print(test)
df = test["trade_df"]
df = test["df"]

perc_missing = calculate_perc_missing(df)

print(df)
print(json.dumps(test["summary"], indent=2))
print("perc_missing: ", perc_missing)
# print(json.dumps(test["summary"], indent=2))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="fast-trade",
version="0.2.3",
version="0.3.0",
description="About low code backtesting library utilizing pandas and technical analysis indicators",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
35 changes: 35 additions & 0 deletions test/test_calculate_perc_missing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from datetime import datetime
import pandas as pd
import numpy as np
from pandas.core.base import DataError
from fast_trade.calculate_perc_missing import calculate_perc_missing


def test_calculate_perc_missing_none_missing():
mock_df = pd.read_csv("./test/ohlcv_data.csv.txt", parse_dates=True).set_index(
"date"
)

[perc_missinng, total_missing] = calculate_perc_missing(mock_df)

assert perc_missinng == 0.0
assert total_missing == 0


def test_calculate_perc_missing_some_missing():
mock_df = pd.read_csv("./test/ohlcv_data.csv.txt", parse_dates=True)
# mock_df.index = pd.to_datetime(mock_df.date, unit="s")
# print(mock_df.iloc[0].name)
print(mock_df.iloc[0].date)
# wtf = mock_df.index.get_loc(pd.to_datetime(1523938263, unit="s"))

# print(mock_df.iloc[wtf])
mock_df.close = [np.nan, np.nan, 2, 3, 4, 5, 6, 7, 8]
# print(mock_df["1523937784"])
# mock_df[1523937784].close = np.nan
[perc_missinng, total_missing] = calculate_perc_missing(mock_df)
#
assert perc_missinng == 22.22
assert total_missing == 2
# print(perc_missinng)
# assert True is False
57 changes: 0 additions & 57 deletions test/test_check_missing_dates.py

This file was deleted.

0 comments on commit dca1795

Please sign in to comment.