back-trader-python 1.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- back_trader_python-1.4.0.dist-info/METADATA +1491 -0
- back_trader_python-1.4.0.dist-info/RECORD +465 -0
- back_trader_python-1.4.0.dist-info/WHEEL +5 -0
- back_trader_python-1.4.0.dist-info/licenses/LICENSE +674 -0
- back_trader_python-1.4.0.dist-info/top_level.txt +1 -0
- backtrader/__init__.py +148 -0
- backtrader/_cerebro/__init__.py +5 -0
- backtrader/_cerebro/channel.py +382 -0
- backtrader/_cerebro/execution.py +377 -0
- backtrader/_cerebro/lifecycle.py +143 -0
- backtrader/_cerebro/notifications.py +150 -0
- backtrader/_cerebro/presentation.py +230 -0
- backtrader/_cerebro/registry.py +593 -0
- backtrader/_cerebro/runnext.py +551 -0
- backtrader/_cerebro/runonce.py +142 -0
- backtrader/analyzer.py +594 -0
- backtrader/analyzers/__init__.py +50 -0
- backtrader/analyzers/annualreturn.py +226 -0
- backtrader/analyzers/calmar.py +165 -0
- backtrader/analyzers/drawdown.py +287 -0
- backtrader/analyzers/leverage.py +112 -0
- backtrader/analyzers/logreturnsrolling.py +190 -0
- backtrader/analyzers/periodstats.py +153 -0
- backtrader/analyzers/positions.py +119 -0
- backtrader/analyzers/pyfolio.py +470 -0
- backtrader/analyzers/returns.py +192 -0
- backtrader/analyzers/sharpe.py +307 -0
- backtrader/analyzers/sharpe_ratio_stats.py +534 -0
- backtrader/analyzers/sqn.py +112 -0
- backtrader/analyzers/timereturn.py +192 -0
- backtrader/analyzers/total_value.py +75 -0
- backtrader/analyzers/tradeanalyzer.py +278 -0
- backtrader/analyzers/transactions.py +141 -0
- backtrader/analyzers/vwr.py +245 -0
- backtrader/bokeh/__init__.py +155 -0
- backtrader/bokeh/analyzers/__init__.py +13 -0
- backtrader/bokeh/analyzers/plot.py +192 -0
- backtrader/bokeh/analyzers/recorder.py +181 -0
- backtrader/bokeh/app.py +1094 -0
- backtrader/bokeh/live/__init__.py +11 -0
- backtrader/bokeh/live/client.py +352 -0
- backtrader/bokeh/live/datahandler.py +346 -0
- backtrader/bokeh/plot_adapter.py +200 -0
- backtrader/bokeh/schemes/__init__.py +14 -0
- backtrader/bokeh/schemes/blackly.py +76 -0
- backtrader/bokeh/schemes/scheme.py +150 -0
- backtrader/bokeh/schemes/tradimo.py +82 -0
- backtrader/bokeh/tab.py +125 -0
- backtrader/bokeh/tabs/__init__.py +30 -0
- backtrader/bokeh/tabs/analyzer.py +120 -0
- backtrader/bokeh/tabs/config.py +154 -0
- backtrader/bokeh/tabs/live.py +109 -0
- backtrader/bokeh/tabs/log.py +185 -0
- backtrader/bokeh/tabs/metadata.py +182 -0
- backtrader/bokeh/tabs/performance.py +359 -0
- backtrader/bokeh/tabs/source.py +70 -0
- backtrader/bokeh/utils/__init__.py +8 -0
- backtrader/bokeh/utils/helpers.py +167 -0
- backtrader/bokeh/webapp.py +164 -0
- backtrader/broker.py +478 -0
- backtrader/brokers/__init__.py +36 -0
- backtrader/brokers/bbroker.py +2576 -0
- backtrader/brokers/btapibroker.py +8227 -0
- backtrader/brokers/hft/__init__.py +89 -0
- backtrader/brokers/hft/binance_bbo.py +625 -0
- backtrader/brokers/hft/binance_bbo_compare.py +1398 -0
- backtrader/brokers/hft/examples.py +1228 -0
- backtrader/brokers/hft/exchange.py +380 -0
- backtrader/brokers/hft/latency.py +309 -0
- backtrader/brokers/hft/matching_core.py +572 -0
- backtrader/brokers/hft/queue.py +238 -0
- backtrader/brokers/hft/recorder.py +88 -0
- backtrader/brokers/hft/state.py +138 -0
- backtrader/brokers/impact_models.py +118 -0
- backtrader/brokers/mixbroker.py +895 -0
- backtrader/brokers/tickbroker.py +1991 -0
- backtrader/btrun/__init__.py +12 -0
- backtrader/btrun/btrun.py +1218 -0
- backtrader/cerebro.py +828 -0
- backtrader/channel.py +682 -0
- backtrader/channels/__init__.py +23 -0
- backtrader/channels/bridge.py +186 -0
- backtrader/channels/funding.py +248 -0
- backtrader/channels/live_queue.py +216 -0
- backtrader/channels/live_validator.py +294 -0
- backtrader/channels/orderbook.py +257 -0
- backtrader/channels/tick.py +202 -0
- backtrader/comminfo.py +665 -0
- backtrader/commissions/__init__.py +106 -0
- backtrader/commissions/ctpoption.py +993 -0
- backtrader/configs/account_config_example.yaml +8 -0
- backtrader/dataseries.py +379 -0
- backtrader/errors.py +106 -0
- backtrader/events.py +980 -0
- backtrader/feed.py +1523 -0
- backtrader/feeds/__init__.py +75 -0
- backtrader/feeds/barrier.py +2006 -0
- backtrader/feeds/blaze.py +118 -0
- backtrader/feeds/btapifeed.py +1538 -0
- backtrader/feeds/btcsv.py +203 -0
- backtrader/feeds/chainer.py +114 -0
- backtrader/feeds/cryptohftdata.py +164 -0
- backtrader/feeds/csvgeneric.py +1205 -0
- backtrader/feeds/ctpcohort.py +1051 -0
- backtrader/feeds/influxfeed.py +158 -0
- backtrader/feeds/livefeed.py +71 -0
- backtrader/feeds/mixed_channel.py +108 -0
- backtrader/feeds/mt4csv.py +42 -0
- backtrader/feeds/pandafeed.py +381 -0
- backtrader/feeds/quandl.py +256 -0
- backtrader/feeds/rollover.py +229 -0
- backtrader/feeds/sierrachart.py +30 -0
- backtrader/feeds/vchart.py +162 -0
- backtrader/feeds/vchartcsv.py +84 -0
- backtrader/feeds/vchartfile.py +153 -0
- backtrader/feeds/yahoo.py +399 -0
- backtrader/fillers.py +148 -0
- backtrader/filters/__init__.py +34 -0
- backtrader/filters/bsplitter.py +127 -0
- backtrader/filters/calendardays.py +121 -0
- backtrader/filters/datafiller.py +192 -0
- backtrader/filters/datafilter.py +74 -0
- backtrader/filters/daysteps.py +96 -0
- backtrader/filters/heikinashi.py +63 -0
- backtrader/filters/renko.py +164 -0
- backtrader/filters/session.py +289 -0
- backtrader/flt.py +80 -0
- backtrader/functions.py +960 -0
- backtrader/indicator.py +449 -0
- backtrader/indicators/__init__.py +148 -0
- backtrader/indicators/accdecoscillator.py +110 -0
- backtrader/indicators/aroon.py +300 -0
- backtrader/indicators/atr.py +315 -0
- backtrader/indicators/awesomeoscillator.py +122 -0
- backtrader/indicators/basicops.py +834 -0
- backtrader/indicators/bollinger.py +223 -0
- backtrader/indicators/cci.py +89 -0
- backtrader/indicators/channels_ext.py +83 -0
- backtrader/indicators/contrib/__init__.py +228 -0
- backtrader/indicators/contrib/absolutely_no_lag_lwma.py +28 -0
- backtrader/indicators/contrib/absolutely_no_lag_lwma_color.py +44 -0
- backtrader/indicators/contrib/accumulation_distribution_line.py +92 -0
- backtrader/indicators/contrib/adx_cross_hull_style_indicator.py +249 -0
- backtrader/indicators/contrib/adxdmi.py +34 -0
- backtrader/indicators/contrib/ai_acceleration_deceleration_oscillator.py +34 -0
- backtrader/indicators/contrib/altr_trend_signal_v22.py +85 -0
- backtrader/indicators/contrib/anchored_momentum_line.py +115 -0
- backtrader/indicators/contrib/any_range_cld_tail_indicator.py +82 -0
- backtrader/indicators/contrib/aroon_horn_sign_indicator.py +96 -0
- backtrader/indicators/contrib/aroon_oscillator_sign_alert.py +50 -0
- backtrader/indicators/contrib/arrows_curves_indicator.py +112 -0
- backtrader/indicators/contrib/as_ctrend_indicator.py +143 -0
- backtrader/indicators/contrib/asimmetric_stoch_nr_indicator.py +187 -0
- backtrader/indicators/contrib/atr_normalize_histogram.py +118 -0
- backtrader/indicators/contrib/average_change_candle.py +165 -0
- backtrader/indicators/contrib/bb_squeeze_indicator.py +60 -0
- backtrader/indicators/contrib/bezier_st_dev_indicator.py +135 -0
- backtrader/indicators/contrib/binary_wave_indicator.py +233 -0
- backtrader/indicators/contrib/blau_c_momentum_indicator.py +123 -0
- backtrader/indicators/contrib/blau_cmi_indicator.py +141 -0
- backtrader/indicators/contrib/blau_csi.py +76 -0
- backtrader/indicators/contrib/blau_ergodic.py +53 -0
- backtrader/indicators/contrib/blau_t_stoch_i.py +72 -0
- backtrader/indicators/contrib/blau_ts_stochastic.py +85 -0
- backtrader/indicators/contrib/blau_tvi.py +55 -0
- backtrader/indicators/contrib/brain_trend2_indicator.py +128 -0
- backtrader/indicators/contrib/brain_trend_signal_proxy.py +47 -0
- backtrader/indicators/contrib/brake_parb_indicator.py +85 -0
- backtrader/indicators/contrib/breakout_bars_trend_v2.py +121 -0
- backtrader/indicators/contrib/bsi_indicator.py +87 -0
- backtrader/indicators/contrib/bulls_bears_eyes.py +67 -0
- backtrader/indicators/contrib/bulls_power.py +56 -0
- backtrader/indicators/contrib/bw_wise_man1_signal.py +102 -0
- backtrader/indicators/contrib/bykov_trend_indicator.py +85 -0
- backtrader/indicators/contrib/candle_stop_color.py +46 -0
- backtrader/indicators/contrib/candles_x_smoothed_indicator.py +69 -0
- backtrader/indicators/contrib/candlesticks_bw.py +45 -0
- backtrader/indicators/contrib/caudate_x_period_candle_color.py +56 -0
- backtrader/indicators/contrib/cci_histogram_indicator.py +53 -0
- backtrader/indicators/contrib/cci_woodies_indicator.py +80 -0
- backtrader/indicators/contrib/center_of_gravity_candle_indicator.py +83 -0
- backtrader/indicators/contrib/center_of_gravity_indicator.py +70 -0
- backtrader/indicators/contrib/cg_oscillator.py +40 -0
- backtrader/indicators/contrib/close_line_cci.py +38 -0
- backtrader/indicators/contrib/close_price_fractals.py +47 -0
- backtrader/indicators/contrib/color3rd_gen_xma_indicator.py +122 -0
- backtrader/indicators/contrib/color_bb_candles_indicator.py +108 -0
- backtrader/indicators/contrib/color_coppock_indicator.py +157 -0
- backtrader/indicators/contrib/color_hma.py +71 -0
- backtrader/indicators/contrib/color_j_variation_indicator.py +53 -0
- backtrader/indicators/contrib/color_metro_de_marker_indicator.py +78 -0
- backtrader/indicators/contrib/color_metro_stochastic_indicator.py +93 -0
- backtrader/indicators/contrib/color_metro_wpr_indicator.py +85 -0
- backtrader/indicators/contrib/color_schaff_de_marker_trend_cycle.py +92 -0
- backtrader/indicators/contrib/color_schaff_trend_cycle_indicator.py +203 -0
- backtrader/indicators/contrib/color_step_xccx_indicator.py +193 -0
- backtrader/indicators/contrib/color_x2_ma.py +49 -0
- backtrader/indicators/contrib/color_x_derivative.py +63 -0
- backtrader/indicators/contrib/color_zerolag_de_marker.py +84 -0
- backtrader/indicators/contrib/corrected_average_indicator.py +127 -0
- backtrader/indicators/contrib/darvas_boxes_system.py +73 -0
- backtrader/indicators/contrib/dema_range_channel_color.py +42 -0
- backtrader/indicators/contrib/derivative_indicator.py +95 -0
- backtrader/indicators/contrib/digital_ft01_indicator.py +112 -0
- backtrader/indicators/contrib/digital_macd.py +200 -0
- backtrader/indicators/contrib/donchian_channels_system.py +45 -0
- backtrader/indicators/contrib/dots_indicator.py +93 -0
- backtrader/indicators/contrib/ef_distance_indicator.py +82 -0
- backtrader/indicators/contrib/ema_rsi_va.py +80 -0
- backtrader/indicators/contrib/envelopes_jp_alonso.py +32 -0
- backtrader/indicators/contrib/f2a_ao_indicator.py +120 -0
- backtrader/indicators/contrib/fatl_filter.py +179 -0
- backtrader/indicators/contrib/fibo_candles_indicator.py +78 -0
- backtrader/indicators/contrib/fine_tuning_ma.py +100 -0
- backtrader/indicators/contrib/fisher_org_v1.py +102 -0
- backtrader/indicators/contrib/fisher_org_v1_sign.py +118 -0
- backtrader/indicators/contrib/force_index_ema.py +96 -0
- backtrader/indicators/contrib/force_index_ema_2.py +27 -0
- backtrader/indicators/contrib/forecast_oscilator.py +145 -0
- backtrader/indicators/contrib/fractal_amambk.py +81 -0
- backtrader/indicators/contrib/frama_series.py +84 -0
- backtrader/indicators/contrib/frasm_av2_indicator.py +104 -0
- backtrader/indicators/contrib/go_indicator.py +93 -0
- backtrader/indicators/contrib/hlr_indicator.py +95 -0
- backtrader/indicators/contrib/hma.py +50 -0
- backtrader/indicators/contrib/i4_drfv2.py +34 -0
- backtrader/indicators/contrib/i4_drfv3.py +38 -0
- backtrader/indicators/contrib/i_anch_mom_indicator.py +72 -0
- backtrader/indicators/contrib/i_de_marker_sign_indicator.py +64 -0
- backtrader/indicators/contrib/i_gap_indicator.py +45 -0
- backtrader/indicators/contrib/i_stoch_komposter_indicator.py +77 -0
- backtrader/indicators/contrib/i_trend_indicator.py +125 -0
- backtrader/indicators/contrib/iamma_indicator.py +39 -0
- backtrader/indicators/contrib/indexed_moving_average.py +33 -0
- backtrader/indicators/contrib/instantaneous_trend_filter_indicator.py +51 -0
- backtrader/indicators/contrib/inverse_reaction_indicator.py +41 -0
- backtrader/indicators/contrib/irsi_sign_indicator.py +95 -0
- backtrader/indicators/contrib/iwpr_sign_indicator.py +59 -0
- backtrader/indicators/contrib/j_brain_trend1_sig_indicator.py +233 -0
- backtrader/indicators/contrib/j_tpo_proxy.py +32 -0
- backtrader/indicators/contrib/jma_slope_indicator.py +73 -0
- backtrader/indicators/contrib/kalman_filter_indicator.py +119 -0
- backtrader/indicators/contrib/kalman_filter_line.py +127 -0
- backtrader/indicators/contrib/kama_indicator.py +150 -0
- backtrader/indicators/contrib/karacatica_indicator.py +99 -0
- backtrader/indicators/contrib/kdj_indicator.py +59 -0
- backtrader/indicators/contrib/kwan_ccc_indicator.py +195 -0
- backtrader/indicators/contrib/kwan_nrp_indicator.py +113 -0
- backtrader/indicators/contrib/kwan_rdp_indicator.py +192 -0
- backtrader/indicators/contrib/laguerre_adx_indicator.py +85 -0
- backtrader/indicators/contrib/laguerre_filter_indicator.py +66 -0
- backtrader/indicators/contrib/laguerre_plus_di_proxy.py +57 -0
- backtrader/indicators/contrib/laguerre_roc_indicator.py +81 -0
- backtrader/indicators/contrib/le_man_signal_indicator.py +63 -0
- backtrader/indicators/contrib/linear_reg_slope_v2_indicator.py +136 -0
- backtrader/indicators/contrib/loco_indicator.py +88 -0
- backtrader/indicators/contrib/lrma_indicator.py +185 -0
- backtrader/indicators/contrib/lsma_angle_indicator.py +106 -0
- backtrader/indicators/contrib/ma_rounding_channel_indicator.py +149 -0
- backtrader/indicators/contrib/macd2_indicator.py +61 -0
- backtrader/indicators/contrib/macd_candle_indicator.py +80 -0
- backtrader/indicators/contrib/malr_indicator.py +77 -0
- backtrader/indicators/contrib/momentum_candle_sign_indicator.py +51 -0
- backtrader/indicators/contrib/moving_average_fn_indicator.py +139 -0
- backtrader/indicators/contrib/mt5_stochastic_close_close.py +57 -0
- backtrader/indicators/contrib/muv_nor_diff_cloud_indicator.py +107 -0
- backtrader/indicators/contrib/non_lag_dot_indicator.py +124 -0
- backtrader/indicators/contrib/nrtr_extr_indicator.py +95 -0
- backtrader/indicators/contrib/nrtr_indicator.py +95 -0
- backtrader/indicators/contrib/p_channel_system.py +40 -0
- backtrader/indicators/contrib/percent_envelope.py +37 -0
- backtrader/indicators/contrib/percentage_crossover_channel.py +47 -0
- backtrader/indicators/contrib/pivot_zig_zag_proxy.py +47 -0
- backtrader/indicators/contrib/price_channel_stop_indicator.py +104 -0
- backtrader/indicators/contrib/price_extreme_channel.py +35 -0
- backtrader/indicators/contrib/qqe_cloud_indicator.py +129 -0
- backtrader/indicators/contrib/ravi_indicator.py +40 -0
- backtrader/indicators/contrib/raw_close_close_stochastic.py +74 -0
- backtrader/indicators/contrib/rd_trend_trigger_indicator.py +51 -0
- backtrader/indicators/contrib/renko_level.py +85 -0
- backtrader/indicators/contrib/renko_line_break.py +91 -0
- backtrader/indicators/contrib/rftl_indicator.py +41 -0
- backtrader/indicators/contrib/rkd_indicator.py +53 -0
- backtrader/indicators/contrib/roc2_vg_indicator.py +68 -0
- backtrader/indicators/contrib/rsi_histogram_indicator.py +43 -0
- backtrader/indicators/contrib/rsi_slowdown.py +57 -0
- backtrader/indicators/contrib/rsioma_v2.py +41 -0
- backtrader/indicators/contrib/rvi_histogram_indicator.py +107 -0
- backtrader/indicators/contrib/safe_adx.py +89 -0
- backtrader/indicators/contrib/shared_strategy_indicators.py +1651 -0
- backtrader/indicators/contrib/sidus_indicator.py +105 -0
- backtrader/indicators/contrib/silver_trend_indicator.py +79 -0
- backtrader/indicators/contrib/sliding_range_color.py +56 -0
- backtrader/indicators/contrib/slow_stoch.py +42 -0
- backtrader/indicators/contrib/smoothed_adx_indicator.py +86 -0
- backtrader/indicators/contrib/smoothed_rsi.py +31 -0
- backtrader/indicators/contrib/spearman_rank_correlation_histogram.py +60 -0
- backtrader/indicators/contrib/stalin_indicator.py +152 -0
- backtrader/indicators/contrib/starter_laguerre_filter.py +62 -0
- backtrader/indicators/contrib/step_manrtr_indicator.py +137 -0
- backtrader/indicators/contrib/stochastic_histogram_indicator.py +143 -0
- backtrader/indicators/contrib/t3_alarm_indicator.py +125 -0
- backtrader/indicators/contrib/t3_average.py +76 -0
- backtrader/indicators/contrib/t3_indicator.py +40 -0
- backtrader/indicators/contrib/the20s_v020_signal.py +93 -0
- backtrader/indicators/contrib/three_candles_indicator.py +70 -0
- backtrader/indicators/contrib/three_line_break_indicator.py +64 -0
- backtrader/indicators/contrib/time_line.py +57 -0
- backtrader/indicators/contrib/trading_channel_index_proxy.py +48 -0
- backtrader/indicators/contrib/trend_arrows_indicator.py +109 -0
- backtrader/indicators/contrib/trend_continuation_indicator.py +127 -0
- backtrader/indicators/contrib/trend_intensity_index_proxy.py +51 -0
- backtrader/indicators/contrib/trend_manager_indicator.py +39 -0
- backtrader/indicators/contrib/tri_x_candle_indicator.py +51 -0
- backtrader/indicators/contrib/trigger_line.py +66 -0
- backtrader/indicators/contrib/triple_ema_rate.py +34 -0
- backtrader/indicators/contrib/trvi_indicator.py +194 -0
- backtrader/indicators/contrib/two_pb_ideal_xosma_indicator.py +127 -0
- backtrader/indicators/contrib/ultra_absolutely_no_lag_lwma_color.py +92 -0
- backtrader/indicators/contrib/ultra_wpr_indicator.py +173 -0
- backtrader/indicators/contrib/up_down_candle_strength.py +68 -0
- backtrader/indicators/contrib/vinin_i_trend_indicator.py +139 -0
- backtrader/indicators/contrib/volume_weighted_ma_indicator.py +78 -0
- backtrader/indicators/contrib/volume_weighted_ma_st_dev_indicator.py +111 -0
- backtrader/indicators/contrib/vwap_close_indicator.py +65 -0
- backtrader/indicators/contrib/vwma_candle.py +57 -0
- backtrader/indicators/contrib/vwma_digit_system.py +70 -0
- backtrader/indicators/contrib/wami.py +43 -0
- backtrader/indicators/contrib/wprsi_signal_indicator.py +105 -0
- backtrader/indicators/contrib/x_de_marker_histogram_vol_direct_indicator.py +145 -0
- backtrader/indicators/contrib/x_fisher_indicator.py +64 -0
- backtrader/indicators/contrib/xcci_histogram_vol_direct_indicator.py +56 -0
- backtrader/indicators/contrib/xcci_histogram_vol_indicator.py +85 -0
- backtrader/indicators/contrib/xma_ichimoku.py +163 -0
- backtrader/indicators/contrib/xma_ishimoku_channel_indicator.py +65 -0
- backtrader/indicators/contrib/xma_ishimoku_line.py +68 -0
- backtrader/indicators/contrib/xma_range_bands_indicator.py +107 -0
- backtrader/indicators/contrib/xmacd_indicator.py +70 -0
- backtrader/indicators/contrib/xrsi_de_marker_histogram.py +67 -0
- backtrader/indicators/contrib/xrsi_histogram_vol_direct_indicator.py +52 -0
- backtrader/indicators/contrib/xrsi_histogram_vol_indicator.py +81 -0
- backtrader/indicators/contrib/xrvi_indicator.py +130 -0
- backtrader/indicators/contrib/zero_lag_macd.py +36 -0
- backtrader/indicators/contrib/zig_zag_recent_pivot_signal.py +90 -0
- backtrader/indicators/contrib/zpf_indicator.py +115 -0
- backtrader/indicators/crossover.py +337 -0
- backtrader/indicators/dema.py +175 -0
- backtrader/indicators/demarker.py +270 -0
- backtrader/indicators/deviation.py +284 -0
- backtrader/indicators/directionalmove.py +1071 -0
- backtrader/indicators/dma.py +112 -0
- backtrader/indicators/dpo.py +96 -0
- backtrader/indicators/dv2.py +56 -0
- backtrader/indicators/ema.py +145 -0
- backtrader/indicators/envelope.py +475 -0
- backtrader/indicators/hadelta.py +198 -0
- backtrader/indicators/heikinashi.py +153 -0
- backtrader/indicators/hma.py +153 -0
- backtrader/indicators/hurst.py +151 -0
- backtrader/indicators/ichimoku.py +267 -0
- backtrader/indicators/kama.py +181 -0
- backtrader/indicators/kst.py +159 -0
- backtrader/indicators/lrsi.py +125 -0
- backtrader/indicators/mabase.py +147 -0
- backtrader/indicators/macd.py +322 -0
- backtrader/indicators/momentum.py +267 -0
- backtrader/indicators/moneyflow.py +237 -0
- backtrader/indicators/mt5atr.py +124 -0
- backtrader/indicators/myind.py +179 -0
- backtrader/indicators/obv.py +94 -0
- backtrader/indicators/ols.py +265 -0
- backtrader/indicators/oscillator.py +161 -0
- backtrader/indicators/percentchange.py +83 -0
- backtrader/indicators/percentrank.py +46 -0
- backtrader/indicators/pivotpoint.py +469 -0
- backtrader/indicators/prettygoodoscillator.py +113 -0
- backtrader/indicators/priceops_ext.py +123 -0
- backtrader/indicators/priceoscillator.py +262 -0
- backtrader/indicators/psar.py +212 -0
- backtrader/indicators/rmi.py +69 -0
- backtrader/indicators/rsi.py +440 -0
- backtrader/indicators/sma.py +141 -0
- backtrader/indicators/smma.py +116 -0
- backtrader/indicators/spread.py +54 -0
- backtrader/indicators/stochastic.py +263 -0
- backtrader/indicators/supertrend.py +436 -0
- backtrader/indicators/trend_ext.py +105 -0
- backtrader/indicators/trix.py +202 -0
- backtrader/indicators/tsi.py +155 -0
- backtrader/indicators/ultimateoscillator.py +158 -0
- backtrader/indicators/vortex.py +62 -0
- backtrader/indicators/williams.py +194 -0
- backtrader/indicators/wma.py +103 -0
- backtrader/indicators/zlema.py +135 -0
- backtrader/indicators/zlind.py +104 -0
- backtrader/linebuffer.py +3155 -0
- backtrader/lineiterator.py +2911 -0
- backtrader/lineroot.py +1106 -0
- backtrader/lineseries.py +2559 -0
- backtrader/live_trading/__init__.py +31 -0
- backtrader/live_trading/interface.py +404 -0
- backtrader/mathsupport.py +94 -0
- backtrader/metabase.py +1804 -0
- backtrader/mixins/__init__.py +21 -0
- backtrader/mixins/singleton.py +118 -0
- backtrader/observer.py +106 -0
- backtrader/observers/__init__.py +45 -0
- backtrader/observers/benchmark.py +126 -0
- backtrader/observers/broker.py +184 -0
- backtrader/observers/buysell.py +144 -0
- backtrader/observers/drawdown.py +161 -0
- backtrader/observers/logreturns.py +113 -0
- backtrader/observers/timereturn.py +86 -0
- backtrader/observers/trade_logger.py +2972 -0
- backtrader/observers/tradelogger.py +6 -0
- backtrader/observers/trades.py +258 -0
- backtrader/order.py +1114 -0
- backtrader/parameters.py +2345 -0
- backtrader/plot/__init__.py +54 -0
- backtrader/plot/finance.py +1022 -0
- backtrader/plot/formatters.py +200 -0
- backtrader/plot/locator.py +353 -0
- backtrader/plot/multicursor.py +495 -0
- backtrader/plot/plot.py +2500 -0
- backtrader/plot/plot_plotly.py +1351 -0
- backtrader/plot/scheme.py +253 -0
- backtrader/plot/utils.py +104 -0
- backtrader/position.py +290 -0
- backtrader/position_modes.py +132 -0
- backtrader/profiles.py +254 -0
- backtrader/reports/__init__.py +39 -0
- backtrader/reports/charts.py +371 -0
- backtrader/reports/performance.py +620 -0
- backtrader/reports/reporter.py +660 -0
- backtrader/resamplerfilter.py +1001 -0
- backtrader/signal.py +118 -0
- backtrader/signals/__init__.py +17 -0
- backtrader/sizer.py +114 -0
- backtrader/sizers/__init__.py +26 -0
- backtrader/sizers/fixedsize.py +161 -0
- backtrader/sizers/percents_sizer.py +119 -0
- backtrader/store.py +221 -0
- backtrader/stores/__init__.py +33 -0
- backtrader/stores/btapistore.py +15506 -0
- backtrader/stores/livestore.py +137 -0
- backtrader/stores/vchartfile.py +96 -0
- backtrader/strategy.py +3655 -0
- backtrader/talib.py +280 -0
- backtrader/test_helpers.py +96 -0
- backtrader/timer.py +358 -0
- backtrader/trade.py +442 -0
- backtrader/tradingcal.py +361 -0
- backtrader/utils/__init__.py +68 -0
- backtrader/utils/autodict.py +251 -0
- backtrader/utils/date.py +71 -0
- backtrader/utils/dateintern.py +509 -0
- backtrader/utils/flushfile.py +94 -0
- backtrader/utils/fractal.py +101 -0
- backtrader/utils/get_metrics.py +101 -0
- backtrader/utils/load_data.py +209 -0
- backtrader/utils/log_message.py +998 -0
- backtrader/utils/ordereddefaultdict.py +75 -0
- backtrader/utils/py3.py +296 -0
- backtrader/version.py +21 -0
- backtrader/writer.py +372 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
"""Plot Scheme Module - Plotting configuration and styling.
|
|
3
|
+
|
|
4
|
+
This module provides the PlotScheme class and color schemes for
|
|
5
|
+
customizing the appearance of backtrader plots.
|
|
6
|
+
|
|
7
|
+
Variables:
|
|
8
|
+
tableau20: 20-color palette for plots.
|
|
9
|
+
tableau10: 10-color palette for plots.
|
|
10
|
+
tableau10_light: 10 light-color palette.
|
|
11
|
+
tab10_index: Color index mapping.
|
|
12
|
+
|
|
13
|
+
Classes:
|
|
14
|
+
PlotScheme: Configuration class for plot appearance.
|
|
15
|
+
|
|
16
|
+
Example:
|
|
17
|
+
Using PlotScheme:
|
|
18
|
+
>>> scheme = PlotScheme()
|
|
19
|
+
>>> scheme.ytight = True
|
|
20
|
+
>>> cerebro.plot(scheme=scheme)
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
tableau20 = [
|
|
24
|
+
"steelblue", # 0
|
|
25
|
+
"lightsteelblue", # 1
|
|
26
|
+
"darkorange", # 2
|
|
27
|
+
"peachpuff", # 3
|
|
28
|
+
"green", # 4
|
|
29
|
+
"lightgreen", # 5
|
|
30
|
+
"crimson", # 6
|
|
31
|
+
"lightcoral", # 7
|
|
32
|
+
"mediumpurple", # 8
|
|
33
|
+
"thistle", # 9
|
|
34
|
+
"saddlebrown", # 10
|
|
35
|
+
"rosybrown", # 11
|
|
36
|
+
"orchid", # 12
|
|
37
|
+
"lightpink", # 13
|
|
38
|
+
"gray", # 14
|
|
39
|
+
"lightgray", # 15
|
|
40
|
+
"olive", # 16
|
|
41
|
+
"palegoldenrod", # 17
|
|
42
|
+
"mediumturquoise", # 18
|
|
43
|
+
"paleturquoise", # 19
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
tableau10 = [
|
|
47
|
+
"blue", # 'steelblue', # 0
|
|
48
|
+
"darkorange", # 1
|
|
49
|
+
"green", # 2
|
|
50
|
+
"crimson", # 3
|
|
51
|
+
"mediumpurple", # 4
|
|
52
|
+
"saddlebrown", # 5
|
|
53
|
+
"orchid", # 6
|
|
54
|
+
"gray", # 7
|
|
55
|
+
"olive", # 8
|
|
56
|
+
"mediumturquoise", # 9
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
tableau10_light = [
|
|
60
|
+
"lightsteelblue", # 0
|
|
61
|
+
"peachpuff", # 1
|
|
62
|
+
"lightgreen", # 2
|
|
63
|
+
"lightcoral", # 3
|
|
64
|
+
"thistle", # 4
|
|
65
|
+
"rosybrown", # 5
|
|
66
|
+
"lightpink", # 6
|
|
67
|
+
"lightgray", # 7
|
|
68
|
+
"palegoldenrod", # 8
|
|
69
|
+
"paleturquoise", # 9
|
|
70
|
+
]
|
|
71
|
+
|
|
72
|
+
tab10_index = [3, 0, 2, 1, 2, 4, 5, 6, 7, 8, 9]
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class PlotScheme:
|
|
76
|
+
"""Configuration class for plot appearance and styling.
|
|
77
|
+
|
|
78
|
+
This class defines the visual appearance of backtrader plots, including
|
|
79
|
+
colors, fonts, margins, and other styling parameters. It can be customized
|
|
80
|
+
and passed to cerebro.plot() to change the look of generated charts.
|
|
81
|
+
|
|
82
|
+
Attributes:
|
|
83
|
+
ytight (bool): Whether to use tight packing on y-axis.
|
|
84
|
+
yadjust (float): Y-margin (top/bottom) for subcharts.
|
|
85
|
+
zdown (bool): Whether new lines are painted below previous ones.
|
|
86
|
+
tickrotation (int): Rotation of date labels on x-axis.
|
|
87
|
+
rowsmajor (int): Subparts for major charts (data feeds).
|
|
88
|
+
rowsminor (int): Subparts for minor charts (indicators/observers).
|
|
89
|
+
plotdist (float): Distance between subcharts.
|
|
90
|
+
grid (bool): Whether to show grid in charts.
|
|
91
|
+
style (str): Plot style for OHLC bars ('line', 'bar', or 'candle').
|
|
92
|
+
loc (str): Color for line-on-close plots.
|
|
93
|
+
barup (str): Color for bullish bars/candles.
|
|
94
|
+
barupfill (bool): Whether bullish candles are filled.
|
|
95
|
+
baralpha (float): Opacity for filled candlesticks.
|
|
96
|
+
fillalpha (float): Alpha blending for fill areas.
|
|
97
|
+
volume (bool): Whether to plot volume.
|
|
98
|
+
voloverlay (bool): Whether to overlay volume on data chart.
|
|
99
|
+
volscaling (float): Scaling factor for volume when overlaying.
|
|
100
|
+
volpushup (float): Vertical offset for overlaid volume.
|
|
101
|
+
volup (str): Color for bullish volume.
|
|
102
|
+
voldown (str): Color for bearish volume.
|
|
103
|
+
voltrans (float): Transparency for volume when overlaying.
|
|
104
|
+
subtxttrans (float): Transparency for text labels.
|
|
105
|
+
subtxtsize (int): Font size for labels on chart.
|
|
106
|
+
legendind (bool): Whether indicators have legend display.
|
|
107
|
+
legendindloc (str): Location of legend for indicators.
|
|
108
|
+
legenddataloc (str): Location of legend for data feeds.
|
|
109
|
+
linevalues (bool): Whether to plot last value after object name.
|
|
110
|
+
valuetags (bool): Whether to plot tags with last values.
|
|
111
|
+
hlinescolor (str): Default color for horizontal lines.
|
|
112
|
+
hlinesstyle (str): Default style for horizontal lines.
|
|
113
|
+
hlineswidth (float): Default width for horizontal lines.
|
|
114
|
+
lcolors (list): Default color scheme for lines.
|
|
115
|
+
fmt_x_ticks (str): strftime format for x-axis ticks.
|
|
116
|
+
fmt_x_data (str): strftime format for data point values.
|
|
117
|
+
|
|
118
|
+
Example:
|
|
119
|
+
>>> scheme = PlotScheme()
|
|
120
|
+
>>> scheme.ytight = True
|
|
121
|
+
>>> scheme.style = 'candle'
|
|
122
|
+
>>> cerebro.plot(scheme=scheme)
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
def __init__(self):
|
|
126
|
+
"""Initialize PlotScheme with default styling values.
|
|
127
|
+
|
|
128
|
+
Sets all plotting parameters to their default values, which can be
|
|
129
|
+
customized after initialization to achieve the desired chart appearance.
|
|
130
|
+
"""
|
|
131
|
+
self.ytight = False
|
|
132
|
+
|
|
133
|
+
# y-margin (top/bottom) for the subcharts. This will not overrule the
|
|
134
|
+
# option plotinfo.plotymargin
|
|
135
|
+
self.yadjust = 0.0
|
|
136
|
+
# Each new line is in z-order below the previous one. change it False
|
|
137
|
+
# to have lines painted above the previous line
|
|
138
|
+
self.zdown = True
|
|
139
|
+
# Rotation of the date labes on the x-axis
|
|
140
|
+
self.tickrotation = 15
|
|
141
|
+
|
|
142
|
+
# How many "subparts" takes a major chart (datas) in the overall chart
|
|
143
|
+
# This is proportional to the total number of subcharts
|
|
144
|
+
self.rowsmajor = 5
|
|
145
|
+
|
|
146
|
+
# How many "subparts" takes a minor chart (indicators/observers) in the
|
|
147
|
+
# overall chart. This is proportional to the total number of subcharts
|
|
148
|
+
# Together with rowsmajor, this defines a proportion ratio between data
|
|
149
|
+
# charts and indicators/observers charts
|
|
150
|
+
self.rowsminor = 1
|
|
151
|
+
|
|
152
|
+
# Distance in between subcharts
|
|
153
|
+
self.plotdist = 0.0
|
|
154
|
+
|
|
155
|
+
# Have a grid in the background of all charts
|
|
156
|
+
self.grid = True
|
|
157
|
+
|
|
158
|
+
# Default plotstyle for the OHLC bars which (line -> line on close)
|
|
159
|
+
# Other options: 'bar' and 'candle'
|
|
160
|
+
self.style = "line"
|
|
161
|
+
|
|
162
|
+
# Default color for the 'line on close' plot
|
|
163
|
+
self.loc = "black"
|
|
164
|
+
# Default color for a bullish bar/candle (0.75 -> intensity of gray)
|
|
165
|
+
self.barup = "0.75"
|
|
166
|
+
# Default color for a bearish bar/candle
|
|
167
|
+
self.bardown = "red"
|
|
168
|
+
# Level of transparency to apply to bars/cancles (NOT USED)
|
|
169
|
+
self.bartrans = 1.0
|
|
170
|
+
|
|
171
|
+
# Whether the candlesticks have to be filled or be transparent
|
|
172
|
+
self.barupfill = True
|
|
173
|
+
self.bardownfill = True
|
|
174
|
+
|
|
175
|
+
# Opacity for the filled candlesticks (1.0 opaque - 0.0 transparent)
|
|
176
|
+
self.baralpha = 1.0
|
|
177
|
+
|
|
178
|
+
# Alpha blending for fill areas between lines (_fill_gt and _fill_lt)
|
|
179
|
+
self.fillalpha = 0.20
|
|
180
|
+
|
|
181
|
+
# Whether to plot volume or not.Note: if the data in question has no
|
|
182
|
+
# volume values, volume plotting will be skipped even if this is True
|
|
183
|
+
self.volume = True
|
|
184
|
+
|
|
185
|
+
# Whether to overlay the volume on the data or use a separate subchart
|
|
186
|
+
self.voloverlay = True
|
|
187
|
+
# Scaling of the volume to the data when plotting as overlay
|
|
188
|
+
self.volscaling = 0.33
|
|
189
|
+
# Pushing overlay volume up for better visibility.
|
|
190
|
+
# Experimentation
|
|
191
|
+
# needed if the volume and data overlap too much
|
|
192
|
+
self.volpushup = 0.00
|
|
193
|
+
|
|
194
|
+
# Default color for the volume of a bullish day
|
|
195
|
+
self.volup = "#aaaaaa" # 0.66 of gray
|
|
196
|
+
# Default color for the volume of a bearish day
|
|
197
|
+
self.voldown = "#cc6073" # (204, 96, 115)
|
|
198
|
+
# Transparency to apply to the volume when overlaying
|
|
199
|
+
self.voltrans = 0.50
|
|
200
|
+
|
|
201
|
+
# Transparency for text labels (NOT USED CURRENTLY)
|
|
202
|
+
self.subtxttrans = 0.66
|
|
203
|
+
# Default font text size for labels on the chart
|
|
204
|
+
self.subtxtsize = 9
|
|
205
|
+
|
|
206
|
+
# Transparency for the legend (NOT USED CURRENTLY)
|
|
207
|
+
self.legendtrans = 0.25
|
|
208
|
+
# Whether indicators have a legend display in their charts
|
|
209
|
+
self.legendind = True
|
|
210
|
+
# Location of the legend for indicators (see matplotlib)
|
|
211
|
+
self.legendindloc = "upper left"
|
|
212
|
+
|
|
213
|
+
# Location of the legend for datafeeds (see matplotlib)
|
|
214
|
+
self.legenddataloc = "upper left"
|
|
215
|
+
|
|
216
|
+
# Plot the last value of a line after the Object name
|
|
217
|
+
self.linevalues = True
|
|
218
|
+
|
|
219
|
+
# Plot a tag at the end of each line with the last value
|
|
220
|
+
self.valuetags = True
|
|
221
|
+
|
|
222
|
+
# Default color for horizontal lines (see plotinfo.plothlines)
|
|
223
|
+
self.hlinescolor = "0.66" # shade of gray
|
|
224
|
+
# Default style for horizontal lines
|
|
225
|
+
self.hlinesstyle = "--"
|
|
226
|
+
# Default width for horizontal lines
|
|
227
|
+
self.hlineswidth = 1.0
|
|
228
|
+
|
|
229
|
+
# Default color scheme: Tableau 10
|
|
230
|
+
self.lcolors = tableau10
|
|
231
|
+
|
|
232
|
+
# strftime Format string for the display of ticks on the x-axis
|
|
233
|
+
self.fmt_x_ticks = "%Y-%m-%d %H:%M"
|
|
234
|
+
|
|
235
|
+
# strftime Format string for the display of data points values
|
|
236
|
+
self.fmt_x_data = None
|
|
237
|
+
|
|
238
|
+
def color(self, idx):
|
|
239
|
+
"""Get color from color scheme for given index.
|
|
240
|
+
|
|
241
|
+
Args:
|
|
242
|
+
idx (int): Color index to retrieve.
|
|
243
|
+
|
|
244
|
+
Returns:
|
|
245
|
+
str: Color name or hex string from the color scheme.
|
|
246
|
+
|
|
247
|
+
Example:
|
|
248
|
+
>>> scheme = PlotScheme()
|
|
249
|
+
>>> scheme.color(0) # Returns first color in scheme
|
|
250
|
+
'blue'
|
|
251
|
+
"""
|
|
252
|
+
colidx = tab10_index[idx % len(tab10_index)]
|
|
253
|
+
return self.lcolors[colidx]
|
backtrader/plot/utils.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
"""Plot Utils Module - Plotting utility functions.
|
|
3
|
+
|
|
4
|
+
This module provides utility functions for plotting, including
|
|
5
|
+
box styling and color manipulation.
|
|
6
|
+
|
|
7
|
+
Functions:
|
|
8
|
+
tag_box_style: Create a box path with a tab/pointer.
|
|
9
|
+
color_by_alpha: Modify color alpha channel.
|
|
10
|
+
clip_obj: Clip object to visible area.
|
|
11
|
+
|
|
12
|
+
Example:
|
|
13
|
+
Creating a box style:
|
|
14
|
+
>>> from backtrader.plot.utils import tag_box_style
|
|
15
|
+
>>> path = tag_box_style(0, 0, 1, 1, 0.1)
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from colorsys import hls_to_rgb as hls2rgb
|
|
19
|
+
from colorsys import rgb_to_hls as rgb2hls
|
|
20
|
+
|
|
21
|
+
import matplotlib.colors as mplcolors
|
|
22
|
+
import matplotlib.path as mplpath
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def tag_box_style(x0, y0, width, height, mutation_size, mutation_aspect=1):
|
|
26
|
+
"""Create a box path with a tab/pointer.
|
|
27
|
+
|
|
28
|
+
Given the location and size of the box, return the path of
|
|
29
|
+
the box around it.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
x0: X coordinate of box origin.
|
|
33
|
+
y0: Y coordinate of box origin.
|
|
34
|
+
width: Width of the box.
|
|
35
|
+
height: Height of the box.
|
|
36
|
+
mutation_size: Reference scale for the mutation.
|
|
37
|
+
mutation_aspect: Aspect ratio for the mutation.
|
|
38
|
+
|
|
39
|
+
Returns:
|
|
40
|
+
A matplotlib Path object representing the box.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
# Note that we are ignoring mutation_aspect. This is okay in general.
|
|
44
|
+
mypad = 0.2
|
|
45
|
+
pad = mutation_size * mypad
|
|
46
|
+
|
|
47
|
+
# width and height with padding added.
|
|
48
|
+
width, height = (
|
|
49
|
+
width + 2.0 * pad,
|
|
50
|
+
height + 2.0 * pad,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
# boundary of the padded box
|
|
54
|
+
x0, y0 = (
|
|
55
|
+
x0 - pad,
|
|
56
|
+
y0 - pad,
|
|
57
|
+
)
|
|
58
|
+
x1, y1 = x0 + width, y0 + height
|
|
59
|
+
|
|
60
|
+
cp = [(x0, y0), (x1, y0), (x1, y1), (x0, y1), (x0 - pad, (y0 + y1) / 2.0), (x0, y0), (x0, y0)]
|
|
61
|
+
|
|
62
|
+
com = [
|
|
63
|
+
mplpath.Path.MOVETO,
|
|
64
|
+
mplpath.Path.LINETO,
|
|
65
|
+
mplpath.Path.LINETO,
|
|
66
|
+
mplpath.Path.LINETO,
|
|
67
|
+
mplpath.Path.LINETO,
|
|
68
|
+
mplpath.Path.LINETO,
|
|
69
|
+
mplpath.Path.CLOSEPOLY,
|
|
70
|
+
]
|
|
71
|
+
|
|
72
|
+
path = mplpath.Path(cp, com)
|
|
73
|
+
|
|
74
|
+
return path
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def shade_color(color, percent):
|
|
78
|
+
"""Shade Color
|
|
79
|
+
This color utility function allows the user to easily darken or
|
|
80
|
+
lighten a color for plotting purposes.
|
|
81
|
+
Parameters
|
|
82
|
+
----------
|
|
83
|
+
color : string, list, hexvalue
|
|
84
|
+
Any acceptable Matplotlib color value, such as
|
|
85
|
+
'red', 'slategrey', '#FFEE11', (1,0,0)
|
|
86
|
+
percent : the amount by which to brighten or darken the color.
|
|
87
|
+
Returns
|
|
88
|
+
-------
|
|
89
|
+
color : tuple of floats
|
|
90
|
+
tuple representing converted rgb values
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
rgb = mplcolors.colorConverter.to_rgb(color)
|
|
94
|
+
|
|
95
|
+
h, lightness, s = rgb2hls(*rgb)
|
|
96
|
+
|
|
97
|
+
lightness *= 1 + float(percent) / 100
|
|
98
|
+
|
|
99
|
+
lightness = min(1, lightness)
|
|
100
|
+
lightness = max(0, lightness)
|
|
101
|
+
|
|
102
|
+
r, g, b = hls2rgb(h, lightness, s)
|
|
103
|
+
|
|
104
|
+
return r, g, b
|
backtrader/position.py
ADDED
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
"""Position Module - Position tracking and management.
|
|
3
|
+
|
|
4
|
+
This module provides the Position class for tracking the size and price
|
|
5
|
+
of trading positions. It maintains position state including opening
|
|
6
|
+
and closing amounts.
|
|
7
|
+
|
|
8
|
+
Classes:
|
|
9
|
+
Position: Tracks position size, price, and related attributes.
|
|
10
|
+
|
|
11
|
+
Example:
|
|
12
|
+
Getting position from broker:
|
|
13
|
+
>>> position = broker.getposition(data)
|
|
14
|
+
>>> print(f"Size: {position.size}, Price: {position.price}")
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# Position class, keeps and updates position size and price, has no relationship with any other assets, only keeps size and price
|
|
19
|
+
class Position:
|
|
20
|
+
"""Keeps and updates the size and price of a position.
|
|
21
|
+
|
|
22
|
+
The Position object has no relationship to any specific asset. It only
|
|
23
|
+
keeps size and price information.
|
|
24
|
+
|
|
25
|
+
Attributes:
|
|
26
|
+
size: Current position size (positive for long, negative for short).
|
|
27
|
+
price: Current price of the position.
|
|
28
|
+
price_orig: Original price when position was opened.
|
|
29
|
+
upopened: Amount of position opened in last update.
|
|
30
|
+
upclosed: Amount of position closed in last update.
|
|
31
|
+
adjbase: Adjustment base for position calculations.
|
|
32
|
+
|
|
33
|
+
Example:
|
|
34
|
+
>>> position = Position(size=100, price=50.0)
|
|
35
|
+
>>> print(len(position)) # Returns True if size != 0
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
# Information displayed when printing position
|
|
39
|
+
def __str__(self):
|
|
40
|
+
return (
|
|
41
|
+
"--- Position Begin\n"
|
|
42
|
+
f"- Size: {self.size}\n"
|
|
43
|
+
f"- Price: {self.price}\n"
|
|
44
|
+
f"- Price orig: {self.price_orig}\n"
|
|
45
|
+
f"- Closed: {self.upclosed}\n"
|
|
46
|
+
f"- Opened: {self.upopened}\n"
|
|
47
|
+
f"- Adjbase: {self.adjbase}\n"
|
|
48
|
+
"--- Position End"
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
# Initialize based on different size and price
|
|
52
|
+
def __init__(self, size=0, price=0.0):
|
|
53
|
+
"""Initialize a Position instance.
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
size: Initial position size (default: 0).
|
|
57
|
+
price: Initial price for the position (default: 0.0).
|
|
58
|
+
"""
|
|
59
|
+
self.datetime = None
|
|
60
|
+
self.size = size
|
|
61
|
+
if size:
|
|
62
|
+
self.price = self.price_orig = price
|
|
63
|
+
else:
|
|
64
|
+
self.price = 0.0
|
|
65
|
+
|
|
66
|
+
self.adjbase = None
|
|
67
|
+
|
|
68
|
+
self.upopened = 0
|
|
69
|
+
self.upclosed = 0
|
|
70
|
+
self.set(size, price)
|
|
71
|
+
|
|
72
|
+
self.updt = None
|
|
73
|
+
|
|
74
|
+
# Modify position's size and price
|
|
75
|
+
def fix(self, size, price):
|
|
76
|
+
"""Fix the position to a specific size and price.
|
|
77
|
+
|
|
78
|
+
Args:
|
|
79
|
+
size: The new position size.
|
|
80
|
+
price: The new position price.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
bool: True if size equals oldsize, False otherwise.
|
|
84
|
+
"""
|
|
85
|
+
oldsize = self.size
|
|
86
|
+
self.size = size
|
|
87
|
+
self.price = price
|
|
88
|
+
return self.size == oldsize
|
|
89
|
+
|
|
90
|
+
# Set position's size and price
|
|
91
|
+
def set(self, size, price):
|
|
92
|
+
"""Set position's size and price, calculating opening and closing amounts.
|
|
93
|
+
|
|
94
|
+
Args:
|
|
95
|
+
size: The new position size.
|
|
96
|
+
price: The new position price.
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
tuple: (size, price, upopened, upclosed) calculated values.
|
|
100
|
+
"""
|
|
101
|
+
# If current position > 0 and theoretical size > current size, means new position opening;
|
|
102
|
+
# If theoretical size <= current position, opening amount is minimum of 0 and theoretical size;
|
|
103
|
+
# Closing amount equals minimum of current position and current position minus theoretical position
|
|
104
|
+
if self.size > 0:
|
|
105
|
+
if size > self.size:
|
|
106
|
+
self.upopened = size - self.size # new 10 - old 5 -> 5
|
|
107
|
+
self.upclosed = 0
|
|
108
|
+
else:
|
|
109
|
+
# same side min(0, 3) -> 0 / reversal min(0, -3) -> -3
|
|
110
|
+
self.upopened = min(0, size)
|
|
111
|
+
# same side min(10, 10 - 5) -> 5
|
|
112
|
+
# reversal min(10, 10 - -5) -> min(10, 15) -> 10
|
|
113
|
+
self.upclosed = min(self.size, self.size - size)
|
|
114
|
+
# Similar effect when current position < 0
|
|
115
|
+
elif self.size < 0:
|
|
116
|
+
if size < self.size:
|
|
117
|
+
self.upopened = size - self.size # ex: -5 - -3 -> -2
|
|
118
|
+
self.upclosed = 0
|
|
119
|
+
else:
|
|
120
|
+
# same side max(0, -5) -> 0 / reversal max(0, 5) -> 5
|
|
121
|
+
self.upopened = max(0, size)
|
|
122
|
+
# same side max(-10, -10 - -5) -> max(-10, -5) -> -5
|
|
123
|
+
# reversal max(-10, -10 - 5) -> max(-10, -15) -> -10
|
|
124
|
+
self.upclosed = max(self.size, self.size - size)
|
|
125
|
+
# If current position equals 0, opening equals the new size being set
|
|
126
|
+
else: # self.size == 0
|
|
127
|
+
self.upopened = size
|
|
128
|
+
self.upclosed = 0
|
|
129
|
+
# Actual position size
|
|
130
|
+
self.size = size
|
|
131
|
+
# Original price
|
|
132
|
+
self.price_orig = self.price
|
|
133
|
+
# If position size > 0, current price equals price, otherwise current price equals 0
|
|
134
|
+
if size:
|
|
135
|
+
self.price = price
|
|
136
|
+
else:
|
|
137
|
+
self.price = 0.0
|
|
138
|
+
|
|
139
|
+
return self.size, self.price, self.upopened, self.upclosed
|
|
140
|
+
|
|
141
|
+
def __repr__(self):
|
|
142
|
+
return f"Position(size={self.size}, price={self.price})"
|
|
143
|
+
|
|
144
|
+
def __eq__(self, other):
|
|
145
|
+
if not isinstance(other, Position):
|
|
146
|
+
return NotImplemented
|
|
147
|
+
return self.size == other.size and self.price == other.price
|
|
148
|
+
|
|
149
|
+
# Explicit: Position is mutable, so it must not be hashable
|
|
150
|
+
__hash__ = None
|
|
151
|
+
|
|
152
|
+
# When calling len(position), return absolute value of position
|
|
153
|
+
def __len__(self):
|
|
154
|
+
return abs(self.size)
|
|
155
|
+
|
|
156
|
+
# When calling bool(position), check if current size equals 0
|
|
157
|
+
def __bool__(self):
|
|
158
|
+
return bool(self.size != 0)
|
|
159
|
+
|
|
160
|
+
__nonzero__ = __bool__
|
|
161
|
+
|
|
162
|
+
# Clone position information
|
|
163
|
+
def clone(self):
|
|
164
|
+
"""Create a copy of the current position.
|
|
165
|
+
|
|
166
|
+
Returns:
|
|
167
|
+
Position: A new Position instance with the same size and price,
|
|
168
|
+
including all internal state fields.
|
|
169
|
+
"""
|
|
170
|
+
pos = Position(size=self.size, price=self.price)
|
|
171
|
+
pos.adjbase = self.adjbase
|
|
172
|
+
pos.datetime = self.datetime
|
|
173
|
+
pos.updt = self.updt
|
|
174
|
+
pos.upopened = self.upopened
|
|
175
|
+
pos.upclosed = self.upclosed
|
|
176
|
+
pos.price_orig = self.price_orig
|
|
177
|
+
return pos
|
|
178
|
+
|
|
179
|
+
# Create a position instance, then update size and price
|
|
180
|
+
def pseudoupdate(self, size, price):
|
|
181
|
+
"""Create a pseudo-update by cloning and updating.
|
|
182
|
+
|
|
183
|
+
Args:
|
|
184
|
+
size: The size to add to the position.
|
|
185
|
+
price: The price for the update.
|
|
186
|
+
|
|
187
|
+
Returns:
|
|
188
|
+
Position: A new Position instance after update.
|
|
189
|
+
"""
|
|
190
|
+
return Position(self.size, self.price).update(size, price)
|
|
191
|
+
|
|
192
|
+
# Update size and price
|
|
193
|
+
def update(self, size, price, dt=None):
|
|
194
|
+
"""
|
|
195
|
+
Updates the current position and returns the updated size, price and
|
|
196
|
+
units used to open/close a position
|
|
197
|
+
# Update current position and return updated size, price and position size needed to open/close
|
|
198
|
+
|
|
199
|
+
Args:
|
|
200
|
+
size (int): amount to update the position's size
|
|
201
|
+
if size < 0: A sell operation has taken place
|
|
202
|
+
if size > 0: A buy operation has taken place
|
|
203
|
+
# Amount to update position size, if size < 0, a sell operation will be executed, if size > 0, a buy operation will be executed
|
|
204
|
+
price (float):
|
|
205
|
+
Must always be positive to ensure consistency
|
|
206
|
+
# Price, must always be positive to maintain consistency
|
|
207
|
+
dt (datetime.datetime): record datetime update (datetime.datetime)
|
|
208
|
+
|
|
209
|
+
Returns:
|
|
210
|
+
|
|
211
|
+
A tuple (non-named) contaning
|
|
212
|
+
size - new position size
|
|
213
|
+
Simply the sum of the existing size plus the "size" argument
|
|
214
|
+
price - new position price
|
|
215
|
+
If a position is increased, the new average price will be
|
|
216
|
+
returned
|
|
217
|
+
If a position is reduced, the price of the remaining size
|
|
218
|
+
does not change
|
|
219
|
+
If a position is closed, the price is nullified
|
|
220
|
+
If a position is reversed, the price is the price given as
|
|
221
|
+
argument
|
|
222
|
+
opened - amount of contracts from argument "size" that were used
|
|
223
|
+
to open/increase a position.
|
|
224
|
+
A position can be opened from 0 or can be a reversal.
|
|
225
|
+
If a reversal is performed, then opened is less than "size",
|
|
226
|
+
because part of "size" will have been used to close the
|
|
227
|
+
existing position
|
|
228
|
+
closed - the amount of units from arguments "size" that were used to
|
|
229
|
+
close/reduce a position
|
|
230
|
+
|
|
231
|
+
Both opened and closed carry the same sign as the "size" argument
|
|
232
|
+
because they refer to a part of the "size" argument
|
|
233
|
+
# Result will return a tuple containing the following data:
|
|
234
|
+
# size represents new position size, simply existing position size plus new position increment
|
|
235
|
+
# price represents new position price, returns different price based on different position
|
|
236
|
+
# opened represents position size to be newly opened
|
|
237
|
+
# closed represents position size to be closed
|
|
238
|
+
"""
|
|
239
|
+
# Update position time
|
|
240
|
+
self.datetime = dt # record datetime update (datetime.datetime)
|
|
241
|
+
# Original price
|
|
242
|
+
self.price_orig = self.price
|
|
243
|
+
# Old position size
|
|
244
|
+
oldsize = self.size
|
|
245
|
+
# New position size
|
|
246
|
+
self.size += size
|
|
247
|
+
# If size is 0
|
|
248
|
+
if not self.size:
|
|
249
|
+
# Update closed existing position
|
|
250
|
+
# Update opening, closing and price
|
|
251
|
+
opened, closed = 0, size
|
|
252
|
+
self.price = 0.0
|
|
253
|
+
# If position size is 0, need to open size amount
|
|
254
|
+
elif not oldsize:
|
|
255
|
+
# Update opened a position from 0
|
|
256
|
+
opened, closed = size, 0
|
|
257
|
+
self.price = price
|
|
258
|
+
# If original position size is 0
|
|
259
|
+
elif oldsize > 0: # existing "long" position updated
|
|
260
|
+
# If increased position > 0, need new opening, and calculate average position price
|
|
261
|
+
if size > 0: # increased position
|
|
262
|
+
opened, closed = size, 0
|
|
263
|
+
self.price = (self.price * oldsize + size * price) / self.size
|
|
264
|
+
# If after closing size, position still > 0, close size
|
|
265
|
+
elif self.size > 0: # reduced position
|
|
266
|
+
opened, closed = 0, size
|
|
267
|
+
# self.price = self.price
|
|
268
|
+
# In other cases, need to open self.size, close -oldsize
|
|
269
|
+
else: # self.size < 0 # reversed position form plus to minus
|
|
270
|
+
opened, closed = self.size, -oldsize
|
|
271
|
+
self.price = price
|
|
272
|
+
# Original position is negative
|
|
273
|
+
else: # oldsize < 0 - existing short position updated
|
|
274
|
+
# If newly added position is also negative, open size
|
|
275
|
+
if size < 0: # increased position
|
|
276
|
+
opened, closed = size, 0
|
|
277
|
+
self.price = (self.price * oldsize + size * price) / self.size
|
|
278
|
+
# If current self.size < 0, close size
|
|
279
|
+
elif self.size < 0: # reduced position
|
|
280
|
+
opened, closed = 0, size
|
|
281
|
+
# self.price = self.price
|
|
282
|
+
# In other cases, need to open self.size, close -oldsize
|
|
283
|
+
else: # self.size > 0 - reversed position from minus to plus
|
|
284
|
+
opened, closed = self.size, -oldsize
|
|
285
|
+
self.price = price
|
|
286
|
+
# Opening and closing amounts
|
|
287
|
+
self.upopened = opened
|
|
288
|
+
self.upclosed = closed
|
|
289
|
+
|
|
290
|
+
return self.size, self.price, opened, closed
|