
    'ib              
       >   d dl mZmZmZmZ d dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZ eej                  j                   ej                  j"                  ej                  j$                  ej                  j&                  ej                  j(                  ej                  j*                  ej                  j*                  ej                  j,                        Z	 ej                  j0                  ed<   	 ej                  j4                  fed<   	 ej                  j6                  fed<    eej8                  j:                  ej8                  j<                  ej8                  j>                  ej8                  j@                  ej8                  jB                  ej8                  jD                  ej8                  jF                        Z$ddZ%d	 Z&d
 Z'ddZ(ddZ)ddZ*d Z+d Z,ddZ-d Z.ddZ/e0dk(  r e%        yy# e$ r Y w xY w# e$ r Y w xY w# e$ r Y w xY w)    )absolute_importdivisionprint_functionunicode_literalsN)btcsv	vchartcsvvcfile	sierracsvmt4csvyahoocsvyahoocsv_unreversedyahoovcdataibdata	oandadata)microsecondssecondsminutesdaysweeksmonthsyearsc                 

   t        |       }|j                  rdd l}|j                   }|j                  }t        d|z   dz         }d|vr|j                  |       t        j                  di |}|j                  |j                  |j                  |j                  j                  d      }n'|j                  |j                  j                  d      }t              dk(  s|d   dk(  r|d   d}	}n|\  }}	t        |	      }	t        j                  |d       }t!        |      D ]U  }
|j                  |j#                  |
		       $|j                  |j%                  |
		       E|j'                  |
       W t)        |j*                  t        j,                  t        j*                  d
      }|D ]E  \  }}}t/        t        j0                  d|j3                         z         } |j4                  ||fi | G t)        |j6                  t        j8                  t        j6                        }|D ]  \  }} |j:                  |fi |  t)        |j<                  t        j,                  t        j<                        }|D ]  \  }} |j>                  |fi |  t)        |j@                  t        jB                  t        j@                        }|D ]  \  }} |jD                  |fi |  t)        |jF                  t        jH                  t        jF                        }|D ]  \  }} |jJ                  |fi |  tM        ||       |jN                  xs g D ]4  }t        d|z   dz         } |jP                  t        jR                  fi | 6 tU        |jV                  t        j                        }|D ]  \  }} ||fi |  |jY                         }|d   }|jZ                  s|j\                  r|jF                  rt_        d       t_        d       t_        d       |jF                  ja                         D ]`  \  }}|jZ                  r|j_                          #|j\                  s0t_        d       t_        |       t_        d       |jc                          b |jd                  rYtg        d      }|jd                  d
ur,t        d|jd                  z   dz         } |j                  |         |jd                  di | y y )Nr   dict()stdstats)r   :    )	timeframecompressionT)issignalSIGNAL_z====================z== Analyzersz
##########bar)style )4
parse_argsflushbacktrader.utils.flushfile
nostdstatscerebroevalupdatebtCerebroresamplereplaysplitlenint
TIMEFRAMESgetgetdatasresampledata
replaydataadddata
getobjectssignals	Indicatorgetattrsignalupper
add_signal
strategiesStrategyaddstrategy
indicatorsaddindicator	observersObserveraddobserver	analyzersAnalyzeraddanalyzer	setbrokerwriters	addwriter
WriterFilegetfunctionshooksrun
pranalyzerppranalyzerprintgetitemspprintplotdict)!pargsargs
backtraderr   cer_kwargs_str
cer_kwargsr+   tfcptfcpdatar<   sigkwargssigtypestyperB   stratindsindobsobansanwrkwargs_strwrkwargshookrunstsrunstnameanalyzerpkwargsekwargss!                                    T/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/btrun/btrun.pybtrunry   I   sR   eDzz)??"H\\Ng.45J#8,jj&:&G}} DKK$;==$==&&s+D[[$;;$$S)D t9>T!W]!WaBFBW^^B% "==$   D[[$trrBOOD!" r||RZZ$OG ' 1VW		9w}}#>?5#001
 DOOR[["--HJ# -vE,V,- door||R]]CD ,VS+F+, T^^R[[",,
?C *
FB)&)* T^^R[[",,
?C *
FB)&)* dG* 5,.45"--4845 tzz2::
.C  fW [[]F1IE$**??().!()"'//":":"< &h??NN$%%,'$K,'OO%& yyU#99D 7TYY.45GNN7# 	w     c                    |j                         }| j                  |j                  | j                         t               }| j                  | j                  |d<   | j
                  | j
                  |d<   | j                  | j                  |d<   | j                  | j                  |d<   | j                  | j                  |d<   |r |j                  di | | j                  I|j                  j                  | j                  | j                  | j                   | j                         y | j                   I|j                  j#                  | j                   | j                  | j                   | j                         y y )N
commissionmarginmultinterestinterest_long)	slip_open
slip_matchslip_outr&   )	getbrokercashsetcashrZ   r|   r}   r~   r   r   setcommission	slip_percbrokerset_slippage_percr   no_slip_matchr   
slip_fixedset_slippage_fixed)r\   r+   r   
commkwargss       rx   rM   rM      sN    Fyytyy!J"#'??
< {{#{{
8yy!YY
6}} !%
:%&*&8&8
?#*z*~~!((37>>8<8J8J4J26-- 	) 	A 
	$))$//48NN9=9K9K5K37== 	* 	B 
%rz   c                     t         | j                     }t               }| j                  dk(  rd|d<   d}| j                  r]| j                  j	                  d      }t        |      dkD  r|dz  }t        j                  j                  | j                  |      }||d<   d}| j                  r]| j                  j	                  d      }t        |      dkD  r|dz  }t        j                  j                  | j                  |      }||d	<   | j                  t        | j                     |d
<   | j                  | j                  |d<   t               }| j                  D ]   }||d<    |di |}	|j                  |	       " |S )Nyahoo_unreversedTreversez%Y-%m-%dTr   z	T%H:%M:%Sfromdatetodater    r!   datanamer&   )DATAFORMATSformatrZ   r   r2   r3   datetimestrptimer   r    r5   r!   listrc   append)
r\   dfclsdfkwargsfmtstrdtsplitr   r   datasdnamerc   s
             rx   r7   r7      se   $E vH{{(("F}}--%%c*w<!k!F$$--dmmVD'F{{++##C(w<!k!F""++DKK@#~~! *4>> :#"&"2"2FE $ x T
 Lrz   c                     t        j                  | t         j                        }t               }|D ]?  \  }}t	        ||      s|r||k(  s|j                  |        |S |j                  |       A |S N)inspect
getmembersisclassr   
issubclassr   )modclstypeclsname
clsmembersclslistrt   clss          rx   getmodclassesr      st    ##C9JfG 	 	c#w'$s# N NN3	  Nrz   c                    t        j                  | t         j                        t        j                  | t         j                        z   }t	               }|D ]2  \  }}|r||k(  s|j                  |        |S |j                  |       4 |S r   )r   r   
isfunctionismethodr   r   )r   funcnamemembersfunclistrt   members         rx   getmodfunctionsr     s      g&8&893 0 012G vH $fx' O OOF#$ Orz   c                 ^   | j                  d      s| dz  } |sDt        j                  t        j                  z   dj	                  fdt        d      D              }t        j                  d   t        j                  d   f}|dk  rt        | |      \  }}||fS t        | |      \  }}||fS )Nz.pyr   c              3   H   K   | ]  }t        j                          y wr   )randomchoice).0_charss     rx   	<genexpr>zloadmodule.<locals>.<genexpr>  s     B1&--.Bs   "
   r   r   )   r   )
endswithstringascii_uppercasedigitsjoinrangesysversion_infoloadmodule2loadmodule3)modpathmodnameversionr   er   s        @rx   
loadmoduler     s     E"5&&6''Bb	BB"C$4$4Q$78GWg.Q 6M Wg.Q6Mrz   c                 l    dd l }	 |j                  ||       }|d fS # t        $ r}d |fcY d }~S d }~ww xY wNr   )impload_source	Exception)r   r   r   r   r   s        rx   r   r   +  sB    oogw/ ;  ays    	3.33c                     dd l }	 |j                  j                  ||       }|j                         }|d fS # t        $ r}d |fcY d }~S d }~ww xY wr   )importlib.machinery	machinerySourceFileLoaderload_moduler   )r   r   	importlibloaderr   r   s         rx   r   r   6  sX    $$55gwG  " ;  ays   ,6 	AAAAc                    t               }| xs g D ]^  }|r(|j                  dd      }t        |      dk(  rd}n|\  }}|j                  dd      }t        |      dk(  r|d   }	d}
t               }nK|\  }	}
|
j                  dd      }t        |      dk(  rt               }n|d   }
d|d   z   dz   }t	        |      }|	r@t        |	      \  }}|s2t        d       t        d	|	z  |       t        j                  d       n|}t        |||

      }|s/t        dt        |
      d|	       t        j                  d       |r|j                  |d   |f       I|j                  |d   |f       a |S )N+r   	longshortr   r   r   r   r   Failed to load module %s:)r   r   r   z	No class 
 / module )r   r2   r3   rZ   r,   r   rV   r   exitr   strr   )iterableclsbasemodbaser"   
retobjectsitem	sigtokensrf   tokensr   rt   re   kwtokenskwtextr   r   loadeds                    rx   r;   r;   B  sp   JB ,3

3*I9~"% )C#v;!QiGDVF"MGTzz#q)H8}!{ 8A;.4f(FCb	1G;Q?C3Fs4y'BCHHQKvay&':;vay&12Y,3\ rz   c                 ^   t               }| xs g D ]  }|j                  dd      }t        |      dk(  r|d   }d}t               }nK|\  }}|j                  dd      }t        |      dk(  rt               }n|d   }d|d   z   dz   }	t	        |	      }|r@t        |      \  }
}|
s2t        d       t        d|z  |       t        j                  d       n|}
t        |
|      }|s/t        d	t        |      d
|       t        j                  d       |j                  |d   |f        |S )Nr   r   r   r   r   r   r   )r   r   zNo function r   )r   r2   r3   rZ   r,   r   rV   r   r   r   r   r   )r   r   retfunctionsr   r   r   rt   re   r   r   r   r   r   s                rx   rQ   rQ   u  s%   6LB "1C#v;!QiGDVF"MGTzz#q)H8}!{ 8A;.4f(FCb	1G;Q?C S48#d)WEFHHQKVAY/0E"1H rz   c                 F   t        j                  dt         j                        }|j                  d      }|j	                  ddddd	
       |j                  d      }|j	                  dddddddd       |j	                  ddd       t        t              }|j	                  ddddd|d       |j	                  dddd d !       |j	                  d"d#dd d$!       |j	                  d%d&dd't        j                         d$       |j	                  d(d)dd*t        d$+       |j                  d,      }|j	                  d-d.dd d/!       |j	                  d0d1dd d2!       |j	                  d3d4ddd5d67       |j                  d8      }|j	                  d9d:d;ddd<d=7       |j                  d>      }|j	                  d?d@dAdddBdC7       |j                  dD      }|j	                  dEdFdGddd<dH7       |j                  dI      }|j	                  dJdKdLddd<dM7       |j                  d,      }|j	                  dNdOdddPQ       |j	                  dRdSdddTQ       |j                  dU      }|j	                  dVdWdXd<dddMY       |j                  dZ      }|j	                  d[d\d]dddddd^_	       |j                  d`      }|j	                  dadbdt        dcd       |j	                  dedfdt        dgd       |j	                  dhdidt        djd       |j	                  dkdldt        dmd       |j	                  dndt        d dop       |j	                  dqddd drs       |j	                  dtdd t        du+       |j	                  dvdd t        dw+       |j	                  dxdddyQ       |j	                  dzddd{Q       |j	                  d|ddd}Q       |j	                  d~dddQ       |j	                  dddddddd       | r|j                  |       S |j                         S )NzBacktrader Run Script)descriptionformatter_classzData options)titlez--dataz-dr   Tz$Data files to be added to the system)actionrequiredhelpzCerebro optionsz	--cerebroz-cerre   Fr   ?a  The argument can be specified with the following form:

  - kwargs

    Example: "preload=True" which set its to True

The passed kwargs will be passed directly to the cerebro
instance created for the execution

The available kwargs to cerebro are:
  - preload (default: True)
  - runonce (default: True)
  - maxcpus (default: None)
  - stdstats (default: True)
  - live (default: False)
  - exactbars (default: False)
  - preload (default: True)
  - writer (default False)
  - oldbuysell (default False)
  - tradehistory (default False)
)metavarr   constdefaultnargsr   z--nostdstats
store_truez)Disable the standard statistics observers)r   r   z--formatz--csvformatz-cr   z
CSV Format)r   r   choicesr   z
--fromdatez-fz-Starting date in YYYY-MM-DD[THH:MM:SS] format)r   r   r   z--todatez-tz+Ending date in YYYY-MM-DD[THH:MM:SS] formatz--timeframez-tfr   z--compressionz-cpr   )r   r   typer   )r   z
--resamplez-rsz*resample with timeframe:compression valuesz--replayz-rpz(replay with timeframe:compression valuesz--hookrR   zmodule:hookfunction:kwargsa  This option can be specified multiple times.

The argument can be specified with the following form:

  - module:hookfunction:kwargs

    Example: mymod:myhook:a=1,b=2

kwargs is optional

If module is omitted then hookfunction will be sought
as the built-in cerebro method. Example:

  - :addtz:tz=America/St_Johns

If name is omitted, then the 1st function found in the
mod will be used. Such as in:

  - module or module::kwargs

The function specified will be called, with cerebro
instance passed as the first argument together with
kwargs, if any were specified. This allows to customize
cerebro, beyond options provided by this script

)destr   r   r   r   zStrategy optionsz
--strategyz-strB   zmodule:name:kwargsa  This option can be specified multiple times.

The argument can be specified with the following form:

  - module:classname:kwargs

    Example: mymod:myclass:a=1,b=2

kwargs is optional

If module is omitted then class name will be sought in
the built-in strategies module. Such as in:

  - :name:kwargs or :name

If name is omitted, then the 1st strategy found in the mod
will be used. Such as in:

  - module or module::kwargsSignalsz--signalz-sigr<   zmodule:signaltype:name:kwargsa  This option can be specified multiple times.

The argument can be specified with the following form:

  - signaltype:module:signaltype:classname:kwargs

    Example: longshort+mymod:myclass:a=1,b=2

signaltype may be ommited: longshort will be used

    Example: mymod:myclass:a=1,b=2

kwargs is optional

signaltype will be uppercased to match the defintions
fromt the backtrader.signal module

If module is omitted then class name will be sought in
the built-in signals module. Such as in:

  - LONGSHORT::name:kwargs or :name

If name is omitted, then the 1st signal found in the mod
will be used. Such as in:

  - module or module:::kwargszObservers and statisticsz
--observerz-obrG   a  This option can be specified multiple times.

The argument can be specified with the following form:

  - module:classname:kwargs

    Example: mymod:myclass:a=1,b=2

kwargs is optional

If module is omitted then class name will be sought in
the built-in observers module. Such as in:

  - :name:kwargs or :name

If name is omitted, then the 1st observer found in the
will be used. Such as in:

  - module or module::kwargs	Analyzersz
--analyzerz-anrJ   a  This option can be specified multiple times.

The argument can be specified with the following form:

  - module:classname:kwargs

    Example: mymod:myclass:a=1,b=2

kwargs is optional

If module is omitted then class name will be sought in
the built-in analyzers module. Such as in:

  - :name:kwargs or :name

If name is omitted, then the 1st analyzer found in the
will be used. Such as in:

  - module or module::kwargsz--pranalyzerz	-pralyzerzAutomatically print analyzers)r   r   r   z--ppranalyzerz
-ppralyzerz$Automatically PRETTY print analyzers
Indicatorsz--indicatorz-indrE   )r   r   r   r   r   Writersz--writerz-wrrN   a	  This option can be specified multiple times.

The argument can be specified with the following form:

  - kwargs

    Example: a=1,b=2

kwargs is optional

It creates a system wide writer which outputs run data

Please see the documentation for the available kwargs)r   r   r   r   r   r   r   zCash and Commission Scheme Argsz--cashz-cashzCash to set to the broker)r   r   r   z--commissionz-commzCommission value to setz--marginz-margzMargin type to setz--multz-mulzMultiplier to usez
--interestz$Credit Interest rate to apply (0.0x))r   r   r   r   z--interest_longz'Apply credit interest to long positions)r   r   r   r   z--slip_percz'Enable slippage with a percentage valuez--slip_fixedz(Enable slippage with a fixed point valuez--slip_openz0enable slippage for when matching opening pricesz--no-slip_matchzWDisable slip_match, ie: matching capped at 
high-low if slippage goes over those limitsz
--slip_outz/with slip_match enabled, match outside high-lowz--flushz-flush the output - useful under win32 systemsz--plotz-pzlPlot the read data applying any kwargs passed

For example:

  --plot style="candle" (to plot candlesticks)
)r   r   r   r   r   r   )argparseArgumentParserRawTextHelpFormatteradd_argument_groupadd_argumentr   r   r5   keysr4   add_mutually_exclusive_groupfloatr'   )r[   parsergroupdatakeyss       rx   r'   r'     s   $$+ 55F
 %%N%;E	xhB  D %%,=%>E	Vb"C3	  4 
~lG  I K H	z=$&(  * 
|TE4K  M 
z4%I  K 
}eeV)0I  K 
qI  K ///?E	|UUDH  J 
z55$F  H 
w%,D	  > %%,>%?E	e,%$-	  4 %%I%6E	F%/.	  B %%,F%GE	e+%$-	  2 %%K%8E	e+%$-	  4 ///?E	~{ %l<  ? 
 %lC  F
 %%L%9E	vL$%-	  4 %%I%6E	E%rF	  ( %%,M%NE	x5u7  9	~wU5  7	z7U0  2	x%e/  1 
|e%#B  D 
( %tE  G 
}ud!E  G 
~t!F  H 
}u\N  P 
(5K  M 
|eLM  O 
y5K  M $cTEA	  	   ''rz   __main__)r   r   )F)1
__future__r   r   r   r   r  r   r   	itertoolsr   r   r   r]   r.   rZ   feedsBacktraderCSVDataVChartCSVData
VChartFileSierraChartCSVData
MT4CSVDataYahooFinanceCSVDataYahooFinanceDatar   VCDataAttributeErrorIBData	OandaData	TimeFrameMicroSecondsSecondsMinutesDaysWeeksMonthsYearsr5   ry   rM   r7   r   r   r   r   r   r;   rQ   r'   __name__r&   rz   rx   <module>r&     s  ** *       
  
((
$
$hh$$88hh))88XX))44
((
#
#		HHOOK	HHOO,K	!xx112K
 **LL  LL  			
,,

<<
,,


_ DBB&R$ (	1f'TDN
 z	G m  		
  		
  		s6   G= 8H	 H =HH	HHHH