Skip to content

Commit 7bd44a6

Browse files
new version
1 parent 94fbfe1 commit 7bd44a6

2 files changed

Lines changed: 54 additions & 75 deletions

File tree

entry/entry_fibo.py

Lines changed: 53 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -23,73 +23,41 @@ def __init__(self):
2323
self.row_rel_extreme = 0
2424
self.largest_time = 0 #extension in time
2525
self.index_name = 'index'
26-
2726

2827
def ent_fibo(self,curr_row,buy_signal=False,sell_signal=False):
2928
"""This the main method that uses Fibonnacci strategies to enter the market
3029
30+
First, the method finds local minimum and maximum in the current trend with function `self.local_extremum_()`.
31+
Then using `self.largest_extension()`, it finds the largest setback which is stored in the attribute
32+
`self.largest_extension_`. This function also store the largest setback in term of time in `self.largest_time`.
33+
Then in method `self.try_entry()`, the system will try to enter the market..
34+
3135
Trying to enter the market with Fibonacci retracement and extension. 3 types:
3236
Retracement from the last wave
3337
Retracement from beginning of the trend
3438
Extension from the current trend (largest one in the last trend)
3539
36-
At the moment, it is possible to enter the market only with extensions from the current wave.
40+
At the moment, it is possible to enter the market only with extensions from the current trend.
3741
3842
Parameters
3943
----------
4044
`self.buy_signal` and `self.sell_signal` : bool
41-
45+
The module `r_aquare_ty.py` (or package `trading_rules`) tells us if there a buy or sell signal. Then,
46+
just not to have the code twice, we set some variable depending on if `self.buy_signal` or
47+
`self.sell_signal` is `True`.
4248
4349
Notes
4450
-----
45-
4651
No slippage included in `self.try_entry()`. If the price reached the desired level, we just exit at either the
47-
current price or the next desired price
52+
current price or the next desired price
4853
4954
The system doesn't check on a shorter time frame if it reaches an entry point and a stop at the same time
50-
or even an exit point and stop at the same time (in case of high volatility) in `self.try_entry()`
51-
Taking into account the system, those are really rare cases. However it could be tested by using a
52-
shorter time every time an entry or exit signal
53-
We first get an entry confirmation with the function `self.ent_fibo()` in `entry_fibo.py`. Then we run through
54-
the remaining data (according to the determined data range). We have an profit, stop loss and the method
55-
even tighten the stop under certain circumstances.
56-
57-
58-
Then it tries to exit the market using Fibonacci retracement and extension. 1 type at the moment:
59-
1- Largest extension `self.largest_extension_` from the current trend. The `self.largest_extension_` is set
60-
in `entry_fibo.py`. It is in fact the largest setback in the current trend. This method uses
61-
`self.profit_ext` to calculate the profit level and `self.stop_ext` for the stop level
62-
63-
There is no slippage included in `try_exit()`. If the price reached the desired level, we just exit at
64-
either the current price or the next desired price
65-
66-
Parameters
67-
----------
68-
`self.profit_ext` : float
69-
% of the largest extension from previous trend that the system uses to exit the market to take profit
70-
Default value is 2.618. Possible values are 1.618, 2 , 2.618, 3.382, 4.236.
71-
`self.stop_ext` : float
72-
% of the largest extension from previous trend that the system uses as a stop loss.
73-
Default value is 1.618. Possible values are 1, 1.382, 1.618, 2.
74-
`self.is_entry` : bool
75-
The value comes from `entry_fibo.py`. It says if we have a position.
76-
77-
Notes
78-
-----
79-
The stops may be tightened (see "stop tightening" in `initialize.py`)
80-
81-
The system doesn't check on a shorter time frame if it reaches an exit point and a stop in `try_exit()`
82-
in case of high volatility. Really rare cases
83-
84-
85-
55+
or even an exit point and stop at the same time (in case of high volatility) in `self.try_entry()`
56+
Taking into account how the system works, those are really rare cases. However it could be tested by using a
57+
shorter time every time there is an entry or exit signal
8658
8759
"""
8860

89-
#ENTRY TRACKER
90-
#----------------
91-
# Entry level, tells if the system has a position in the market, buy or sell signal
92-
9361
self.curr_row=curr_row
9462
self.buy_signal=buy_signal
9563
self.sell_signal=sell_signal
@@ -151,8 +119,8 @@ def ent_fibo(self,curr_row,buy_signal=False,sell_signal=False):
151119
self.try_entry()
152120

153121
def largest_extension(self):
154-
"""
155-
Find largest extension (setback) from current trend (Fibonacci) in size + largest in time
122+
""" Find largest extension (setback) from current trend (Fibonacci) in size which is stored in
123+
`self.largest_extension_` and largest in term of time stored in `self.largest_time`.
156124
"""
157125

158126
if self.buy_signal:
@@ -248,22 +216,17 @@ def largest_extension(self):
248216

249217
def set_extremum(self):
250218
"""
251-
Set the global max and min for the given range (from first_data to curr_row).
252-
253-
"""
219+
Set the global max and min for the given range (from first_data to curr_row)."""
254220

255221
data_range = self.series.loc[self.first_data:self.curr_row,self.default_data]
256222
self.extreme = {self.high : data_range.max(),
257223
self.low : data_range.min(),
258224
self.high_idx : data_range.idxmax(),
259225
self.low_idx : data_range.idxmin()
260226
}
261-
262227

263228
def set_value(self):
264-
"""
265-
Method to set some values that are used in class and sublcass
266-
"""
229+
"""Method to set some values that are used in this class and sublcass """
267230

268231
# extension level if condition in initialize.py is True
269232
if self.exit_dict[self.exit_name][self.exit_ext_bool]:
@@ -273,10 +236,10 @@ def set_value(self):
273236

274237
if self.extension_lost < 0:
275238
raise Exception(
276-
f"Houston, we've got a problem, Extension lost in enter_fibo.py is {self.extension_lost} "
239+
f"Houston, we've got a problem, `self.extension_lost` in enter_fibo.py is {self.extension_lost} "
277240
f"and should not be negative")
278241
if self.extension_profit < 0:
279-
raise Exception(f"Houston, we've got a problem, Extension profit in enter_fibo.py is "
242+
raise Exception(f"Houston, we've got a problem, `self.extension_profit` in enter_fibo.py is "
280243
f"{self.extension_profit} and should not be negative")
281244

282245

@@ -286,36 +249,52 @@ def try_entry(self):
286249
287250
Function that will try to enter in the market :
288251
Until the system hit the desired extension and/or retracement. At the moment, only using extension (the
289-
largest), which is `self.largest_extension_` set in function
290-
`self.largest_extension()`. We can decide the proportion of the largest extension we want the system
291-
to use in module `initialize.py` within dictionary `self.enter_dict{}` and variable `self.enter_ext`
292-
(default value is 1)
293-
Stop trying to enter in the market when a condition is met.
252+
largest), which is `self.largest_extension_`. We can decide the proportion of the largest
253+
extension we want the system to use in module `initialize.py` within dictionary `self.enter_dict{}`
254+
and variable `self.enter_ext` (default value is 1)
255+
It can also enter in the market only if the market retraces (or setback) above a certain amount of time
256+
`self.time_ext` (set in `initialize.py`) the largest setback in term of time from the current
257+
trend `self.largest_time`
258+
259+
Possibility to stop trying to enter in the market when a condition is met.
294260
At the moment, the only condition is when the price during a setback hits a
295261
percentage `self.fst_cdt_ext` (0.618 by default) of the largest extension `self.largest_extension_`
296262
(low for a buy signal and high for a sell signal which is `self.entry`)
297263
AND hits the minimum retracement in the other direction `self.sec_cdt_ext` (.882 by default)
298-
Set true with this `self.bol_st_ext` in `initialize.py.
264+
Set to `True` with `self.bol_st_ext` in `initialize.py to have this condition.
299265
266+
Parameters
267+
----------
268+
`self.largest_extension_` : float
269+
the largest extension or setback (in point) from the the current trend
270+
`self.enter_ext` : float
271+
This is the % of largest extension at which the system enters the market. Can be .882 or .764 or 1
272+
`self.largest_time` :
273+
the largest setback from the current trend
274+
`self.time_ext` : float
275+
Proportion in term in time needed that the current setback most do compared to the largest extension
276+
in therm of time in the current trend to enter the market. It can be .5, .618 .884,1. Set in `initialize.py`
277+
`self.bol_st_ext` : bool
278+
Tells the system if it has to stop trying to enter the market using Fibonacci extension techniques.
279+
Can be optimized to `True` or `False`. Set in `initialize.py`
280+
`self.fst_cdt_ext` : float
281+
% of the largest extension that if the market reaches, the system stops trying to enter the market.
282+
Possible values are .618, .764 or .882. Set in `initialize.py`
283+
`self.sec_cdt_ext` : float
284+
if the market triggers the first condition, then if it reaches this level in the opposite direction, the
285+
# system stops trying to enter in the market. Can be set to .618, .764, 1 or 1.382. Set in `initialize.py`
300286
301-
NOTES
287+
Notes
302288
-----
303289
Note that the system will priorise an entry over a new high or new low (to be more conservative). To solve
304290
this issue (rare cases, only with high volatility) :
305-
Check simulateneously if a new high or low is reached & (if a buy/sell level is trigerred |
306-
market hits minimum required extension (if this condition is tested))
307-
Then, on a shorter timeframe, check if an entry | minimum required extension is reached before the
291+
Check simulateneously if a new high or low is reached & (if a buy/sell level is trigerred or
292+
if the market hits minimum required extension (if this condition is tested))
293+
Then, on a shorter timeframe, check if an entry or minimum required extension is reached before the
308294
market makes new low or high, vice versa
309295
310-
The entry signal are based on extension at the moment. We could check if it true or false
311-
for different entry type
312-
313296
If the price of the current row on which the signal is trigerred is below the buying level or above the
314-
selling level, the system just don't execute it and end it. CHECK THIS... Maybe the system could enter unless
315-
the price goes below the stop loss (buy) or above the stop loss (sell)
316-
317-
At the moment, the system uses ONLY the extension to try to enter in the market. We may have to change a bit
318-
of the code if we want the flexibility of using other stuff
297+
selling level, the system just don't execute it and end it.
319298
"""
320299

321300
data_test = len(self.series) - self.curr_row - 1

initialize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def __call__(self):
196196
Telling if we use this technic or not. Can be set to `True` or `False`
197197
`self.time_ext` : float
198198
Proportion in term in time needed that the current setback most do compared to the largest extension
199-
in the current trend to enter the market. It can be .5, .618 .884,1
199+
in therm of time in the current trend to enter the market. It can be .5, .618 .884,1
200200
Exit
201201
Extension
202202
`self.exit_dict` : dict

0 commit comments

Comments
 (0)