Skip to content

Commit

Permalink
updating docs
Browse files Browse the repository at this point in the history
  • Loading branch information
quantfreedom committed Dec 13, 2023
1 parent 4f35162 commit 7e0dcb7
Show file tree
Hide file tree
Showing 15 changed files with 735 additions and 17,977 deletions.
4 changes: 1 addition & 3 deletions qf_docstring.mustache
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{{! QF Docstring Template }}
Summary
-------
{{summaryPlaceholder}}

[Explainer Video]()
[Video]()

{{#parametersExist}}
Parameters
Expand Down
44 changes: 18 additions & 26 deletions quantfreedom/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class CandleBodyTypeT(NamedTuple):

class IncreasePositionTypeT(NamedTuple):
"""
Summary
-------
The different ways you can increase your position
Parameters
Expand Down Expand Up @@ -56,8 +54,6 @@ class LeverageModeTypeT(NamedTuple):

class LeverageStrategyTypeT(NamedTuple):
"""
Summary
-------
Choosing which leverage strategy you would like to use.
Parameters
Expand Down Expand Up @@ -136,8 +132,6 @@ class StopLossStrategyTypeT(NamedTuple):

class TakeProfitStrategyTypeT(NamedTuple):
"""
Summary
-------
How you want to process the take profit
Parameters
Expand Down Expand Up @@ -195,27 +189,25 @@ class AccountState(NamedTuple):

class BacktestSettings(NamedTuple):
"""
Summary
-------
Settings for filtering the results of your backtest. The main purpose of this is to save on memory and also there is sometimes no point in wanting to see strategies that are negative gains or below a specific qf score because they are useless.
Parameters
----------
array_size : int = 10000
_description_
gains_pct_filter : float = -np.inf
Will not record any strategies whos gains % result is below gains_pct_filter
total_trade_filter : int = -1
Will not record any strategies whos total trades result is below total trades filter.
qf_filter : float = -np.inf
Will not record any strategies whos qf score result is below the qf filter. qf_score is between -1 to 1,
total_trade_filter : int = -1
Will not record any strategies whos total trades result is below total trades filter.
record_size : int = 10000
The amount of records you want to to save. Keep this number as low as possible to save memory.
"""

array_size: int = 10000
gains_pct_filter: float = -np.inf
total_trade_filter: int = -1
qf_filter: float = -np.inf
total_trade_filter: int = -1
record_size: int = 10000


class DynamicOrderSettingsArrays(NamedTuple):
Expand Down Expand Up @@ -249,18 +241,18 @@ class DynamicOrderSettings(NamedTuple):


class ExchangeSettings(NamedTuple):
limit_fee_pct: float = None
max_leverage: float = None
market_fee_pct: float = None
mmr_pct: float = None
min_leverage: float = None
max_asset_size: float = None
min_asset_size: float = None
asset_tick_step: int = None
position_mode: int = None
leverage_mode: int = None
price_tick_step: int = None
leverage_tick_step: int = None
asset_tick_step: int
leverage_mode: int
leverage_tick_step: int
limit_fee_pct: float
market_fee_pct: float
max_asset_size: float
max_leverage: float
min_asset_size: float
min_leverage: float
mmr_pct: float
position_mode: int
price_tick_step: int


class OrderResult(NamedTuple):
Expand Down
4 changes: 0 additions & 4 deletions quantfreedom/helper_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ def dl_ex_candles(
candles_to_dl: int = None,
):
"""
Summary
-------
Download candles from the exchange of your choice
Parameters
Expand Down Expand Up @@ -103,8 +101,6 @@ def candles_to_df(
candles: np.array,
) -> pd.DataFrame:
"""
Summary
-------
Converts your numpy array candles to a pandas dataframe
Parameters
Expand Down
30 changes: 1 addition & 29 deletions quantfreedom/indicators/tv_indicators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ def wma_tv(
length: int,
) -> np.array:
"""
Summary
-------
[Weighted Moving Average From Tradingview](https://www.tradingview.com/pine-script-reference/v5/#fun_ta.wma)
[Explainer Video](https://youtu.be/eHlHoWC4W8k)
Expand Down Expand Up @@ -75,9 +73,7 @@ def ema_tv(
length: int,
) -> np.array:
"""
Summary
-------
Exponential Moving average from tradingview https://www.tradingview.com/pine-script-reference/v5/#fun_ta.ema
[Exponential Moving average from tradingview](https://www.tradingview.com/pine-script-reference/v5/#fun_ta.ema)
Parameters
----------
Expand Down Expand Up @@ -109,8 +105,6 @@ def rma_tv(
length: int,
) -> np.array:
"""
Summary
-------
[Relative strength index Moving average from tradingview](https://www.tradingview.com/pine-script-reference/v5/#fun_ta.rma)
Parameters
Expand Down Expand Up @@ -144,8 +138,6 @@ def rma_tv_2(
length: int,
):
"""
Summary
-------
[Relative strength index Moving average from tradingview](https://www.tradingview.com/pine-script-reference/v5/#fun_ta.rma)
Parameters
Expand Down Expand Up @@ -184,8 +176,6 @@ def stdev_tv(
length: int,
) -> np.array:
"""
Summary
-------
[Standard deviation from tradingview](https://www.tradingview.com/pine-script-reference/v5/#fun_ta.stdev)
[Explainer Video](https://youtu.be/Hejf_bzLfL4)
Expand Down Expand Up @@ -234,8 +224,6 @@ def macd_tv(
signal_ma_type: Callable = ema_tv,
) -> tuple[np.array, np.array, np.array]:
"""
Summary
-------
[Moving average convergence divergence from tradingview](https://www.tradingview.com/pine-script-reference/v5/#fun_ta.macd)
Parameters
Expand Down Expand Up @@ -273,8 +261,6 @@ def bb_tv(
basis_ma_type: Callable = sma_tv,
) -> tuple[np.array, np.array, np.array]:
"""
Summary
-------
[Bollinger bands from tradingview](https://www.tradingview.com/pine-script-reference/v5/#fun_ta.bb)
Parameters
Expand Down Expand Up @@ -304,8 +290,6 @@ def true_range_tv(
candles: np.array,
) -> np.array:
"""
Summary
-------
[True Range from tradingview](https://www.tradingview.com/pine-script-reference/v5/#fun_ta.tr)
Parameters
Expand Down Expand Up @@ -338,8 +322,6 @@ def atr_tv(
smoothing_type: Callable = rma_tv,
) -> np.array:
"""
Summary
-------
[Average true range smoothing from tradingview](https://www.tradingview.com/pine-script-reference/v5/#fun_ta.atr)
Parameters
Expand All @@ -366,8 +348,6 @@ def rsi_tv(
length: int,
) -> np.array:
"""
Summary
-------
[Relative strength index](https://www.tradingview.com/pine-script-reference/v5/#fun_ta.rsi)
Parameters
Expand Down Expand Up @@ -409,8 +389,6 @@ def supertrend_tv(
factor: int,
) -> tuple[np.array, np.array]:
"""
Summary
-------
[Super Trend](https://www.tradingview.com/pine-script-reference/v5/#fun_ta.supertrend)
Parameters
Expand Down Expand Up @@ -477,8 +455,6 @@ def vwap_tv(
candles: np.array,
) -> np.array:
"""
Summary
-------
[Volume Weighted Average Price](https://blog.quantinsti.com/vwap-strategy/)
Parameters
Expand Down Expand Up @@ -532,8 +508,6 @@ def squeeze_momentum_lazybear_tv(
multi_kc: int,
) -> tuple[np.array, np.array, np.array]:
"""
Summary
-------
[LazyBear Pinescript](https://www.tradingview.com/script/nqQ1DT5a-Squeeze-Momentum-Indicator-LazyBear/)
Parameters
Expand Down Expand Up @@ -601,8 +575,6 @@ def linear_regression_candles_ugurvu_tv(
smoothing_type: Callable = sma_tv,
) -> tuple[np.array, np.array]:
"""
Summary
-------
[Linear regression indicator](https://www.tradingview.com/script/hMaQO1FX-Linear-Regression-Candles/)
Parameters
Expand Down
45 changes: 23 additions & 22 deletions quantfreedom/live_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,34 @@
RejectedOrder,
StaticOrderSettings,
)

from quantfreedom.order_handler.order import OrderHandler
from quantfreedom.strategies.strategy import Strategy
from quantfreedom.exchanges.live_exchange import LiveExchange
from datetime import datetime, timedelta
from dash_bootstrap_templates import load_figure_template
from jupyter_dash import JupyterDash
from dash import Dash
from IPython import get_ipython
import dash_bootstrap_components as dbc
import plotly.graph_objects as go
import pandas as pd

from quantfreedom.order_handler.order import OrderHandler
from quantfreedom.strategies.strategy import Strategy

load_figure_template("darkly")
dbc_css = "https://cdn.jsdelivr.net/gh/AnnMarieW/dash-bootstrap-templates/dbc.min.css"
try:
shell = str(get_ipython())
if "ZMQInteractiveShell" in shell:
app = JupyterDash(__name__, external_stylesheets=[dbc.themes.DARKLY, dbc_css])
elif shell == "TerminalInteractiveShell":
app = JupyterDash(__name__, external_stylesheets=[dbc.themes.DARKLY, dbc_css])
else:
app = Dash(__name__, external_stylesheets=[dbc.themes.DARKLY, dbc_css])
except NameError:
app = Dash(__name__, external_stylesheets=[dbc.themes.DARKLY, dbc_css])

bg_color = "#0b0b18"
# from dash_bootstrap_templates import load_figure_template
# from jupyter_dash import JupyterDash
# from dash import Dash
# from IPython import get_ipython
# import dash_bootstrap_components as dbc

# load_figure_template("darkly")
# dbc_css = "https://cdn.jsdelivr.net/gh/AnnMarieW/dash-bootstrap-templates/dbc.min.css"
# try:
# shell = str(get_ipython())
# if "ZMQInteractiveShell" in shell:
# app = JupyterDash(__name__, external_stylesheets=[dbc.themes.DARKLY, dbc_css])
# elif shell == "TerminalInteractiveShell":
# app = JupyterDash(__name__, external_stylesheets=[dbc.themes.DARKLY, dbc_css])
# else:
# app = Dash(__name__, external_stylesheets=[dbc.themes.DARKLY, dbc_css])
# except NameError:
# app = Dash(__name__, external_stylesheets=[dbc.themes.DARKLY, dbc_css])

# bg_color = "#0b0b18"

logger = getLogger("info")
trade_logger = getLogger("trades")
Expand Down
7 changes: 6 additions & 1 deletion quantfreedom/order_handler/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,13 @@ def check_liq_hit(self, current_candle: np.array):
liq_price=self.liq_price,
)

def check_take_profit_hit(self, current_candle: np.array):
def check_take_profit_hit(
self,
current_candle: np.array,
exit_price: float,
):
self.obj_take_profit.checker_tp_hit(
exit_price=exit_price,
current_candle=current_candle,
tp_price=self.tp_price,
)
Expand Down
42 changes: 41 additions & 1 deletion quantfreedom/order_handler/take_profit.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def __init__(
if tp_strategy_type == TakeProfitStrategyType.RiskReward:
self.tp_calculator = self.tp_rr
self.checker_tp_hit = self.c_tp_hit_regular
if tp_strategy_type == TakeProfitStrategyType.Provided:
self.tp_calculator = self.tp_provided
self.checker_tp_hit = self.c_tp_hit_provided

def short_tp_price(
self,
Expand Down Expand Up @@ -108,12 +111,32 @@ def tp_rr(
tp_pct,
)

def tp_provided(
self,
average_entry: float,
position_size_usd: float,
possible_loss: float,
):
can_move_sl_to_be = True
tp_price = np.nan
tp_pct = np.nan

return (
can_move_sl_to_be,
tp_price,
tp_pct,
)

def c_tp_hit_regular(
self,
current_candle: np.array,
exit_price: float,
tp_price: float,
):
if self.get_check_tp_candle_price(current_candle=current_candle, tp_price=tp_price):
if self.get_check_tp_candle_price(
current_candle=current_candle,
tp_price=tp_price,
):
logger.debug(f"TP Hit tp_price= {tp_price}")
raise DecreasePosition(
exit_fee_pct=self.tp_fee_pct,
Expand All @@ -123,3 +146,20 @@ def c_tp_hit_regular(
else:
logger.debug("No Tp Hit")
pass

def c_tp_hit_provided(
self,
current_candle: np.array,
exit_price: float,
tp_price: float,
):
if not np.isnan(exit_price):
logger.debug(f"TP Hit tp_price= {exit_price}")
raise DecreasePosition(
exit_fee_pct=self.tp_fee_pct,
exit_price=exit_price,
order_status=OrderStatus.TakeProfitFilled,
)
else:
logger.debug("No Tp Hit")
pass
Loading

0 comments on commit 7e0dcb7

Please sign in to comment.