-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery refactored master branch #161
base: master
Are you sure you want to change the base?
Conversation
indicator_calculator = StockDataFrame(df) | ||
sma_100 = indicator_calculator["close_100_sma"] | ||
if df.empty or df.close[-1] < sma_100[-1]: | ||
return False | ||
|
||
return ( | ||
self.portfolio.loc[self.portfolio.symbol == symbol].volatility | ||
>= 1 - self.volatility_threshold | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Trend.apply_filters_symbol
refactored with the following changes:
- Remove unreachable code (
remove-unreachable-code
)
filter = future.result() | ||
if filter: | ||
if filter := future.result(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Trend.apply_filters_for_short
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
if ( | ||
self.portfolio.loc[ | ||
self.portfolio.symbol == symbol | ||
].volatility.values | ||
< 1 - self.volatility_threshold | ||
): | ||
return False | ||
|
||
return True | ||
return ( | ||
self.portfolio.loc[self.portfolio.symbol == symbol].volatility.values | ||
>= 1 - self.volatility_threshold | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Trend.apply_filters_symbol
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Simplify boolean if expression (
boolean-if-exp-identity
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
if ( | ||
self.portfolio.loc[ | ||
self.portfolio.symbol == symbol | ||
].volatility.values | ||
< 1 - self.volatility_threshold | ||
): | ||
return False | ||
|
||
return True | ||
return ( | ||
self.portfolio.loc[self.portfolio.symbol == symbol].volatility.values | ||
>= 1 - self.volatility_threshold | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Trend.apply_filters_symbol_for_short
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Simplify boolean if expression (
boolean-if-exp-identity
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
filter = future.result() | ||
if filter: | ||
if filter := future.result(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Trend.apply_filters
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
)
std = {} | ||
for symbol in symbols: | ||
if symbol in _daiy_data: | ||
std[symbol] = statistics.pstdev( | ||
_daiy_data[symbol]["low"] | ||
) | ||
|
||
unsorted = [ | ||
std = { | ||
symbol: statistics.pstdev(_daiy_data[symbol]["low"]) | ||
for symbol in symbols | ||
if symbol in _daiy_data | ||
} | ||
if unsorted := [ | ||
x | ||
for x in unsorted | ||
if x.ticker in symbols | ||
and x.day["o"] < (x.prevDay["l"] - std[x.ticker]) | ||
] | ||
|
||
if len(unsorted) > 0: | ||
]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function GapDown.run
refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression
) - Simplify sequence length comparison (
simplify-len-comparison
) - Remove redundant pass statement (
remove-redundant-pass
) - Convert for loop into dictionary comprehension (
dict-comprehension
)
df["ema_short"] = ta.EMA({"close": df.close}, timeperiod=int(16)) | ||
df["ema_long"] = ta.EMA({"close": df.close}, timeperiod=int(50)) | ||
df["ema_short"] = ta.EMA({"close": df.close}, timeperiod=16) | ||
df["ema_long"] = ta.EMA({"close": df.close}, timeperiod=50) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function pre_calculated
refactored with the following changes:
- Remove unnecessary casts to int, str, float or bool [×2] (
remove-unnecessary-cast
)
rc_dict: Dict[str, float] = {} | ||
for _, row in new_df.iterrows(): | ||
rc_dict[row.symbol] = float(row.qty) | ||
|
||
rc_dict: Dict[str, float] = { | ||
row.symbol: float(row.qty) for _, row in new_df.iterrows() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function TrendFollow.load_symbol_position
refactored with the following changes:
- Convert for loop into dictionary comprehension (
dict-comprehension
)
return ( | ||
True | ||
if time(hour=14, minute=30) | ||
>= now.time() | ||
>= time(hour=9, minute=30) | ||
else False | ||
) | ||
return time(hour=14, minute=30) >= now.time() >= time(hour=9, minute=30) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function Gold.is_buy_time
refactored with the following changes:
- Simplify boolean if expression (
boolean-if-exp-identity
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
if trading_api: | ||
retry = 3 | ||
while retry > 0: | ||
try: | ||
portfolio_value = float( | ||
trading_api.get_account().portfolio_value | ||
) | ||
break | ||
except ConnectionError as e: | ||
tlog( | ||
f"[{symbol}][{now}[Error] get_account() failed w/ {e}, retrying {retry} more times" | ||
) | ||
await asyncio.sleep(0) | ||
retry -= 1 | ||
|
||
if not portfolio_value: | ||
tlog( | ||
"f[{symbol}][{now}[Error] failed to get portfolio_value" | ||
) | ||
return False, {} | ||
else: | ||
if not trading_api: | ||
raise Exception( | ||
f"{self.name}: both portfolio_value and trading_api can't be None" | ||
) | ||
|
||
retry = 3 | ||
while retry > 0: | ||
try: | ||
portfolio_value = float( | ||
trading_api.get_account().portfolio_value | ||
) | ||
break | ||
except ConnectionError as e: | ||
tlog( | ||
f"[{symbol}][{now}[Error] get_account() failed w/ {e}, retrying {retry} more times" | ||
) | ||
await asyncio.sleep(0) | ||
retry -= 1 | ||
|
||
if not portfolio_value: | ||
tlog( | ||
"f[{symbol}][{now}[Error] failed to get portfolio_value" | ||
) | ||
return False, {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function ShortTrapBuster.run
refactored with the following changes:
- Swap positions of nested conditionals [×2] (
swap-nested-ifs
) - Hoist nested repeated code outside conditional statements [×2] (
hoist-similar-statement-from-if
) - Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
if trading_api: | ||
retry = 3 | ||
while retry > 0: | ||
try: | ||
portfolio_value = float( | ||
trading_api.get_account().portfolio_value | ||
) | ||
break | ||
except ConnectionError as e: | ||
tlog( | ||
f"[{symbol}][{now}[Error] get_account() failed w/ {e}, retrying {retry} more times" | ||
) | ||
await asyncio.sleep(0) | ||
retry -= 1 | ||
|
||
if not portfolio_value: | ||
tlog( | ||
"f[{symbol}][{now}[Error] failed to get portfolio_value" | ||
) | ||
return False, {} | ||
else: | ||
if not trading_api: | ||
raise Exception( | ||
f"{self.name}: both portfolio_value and trading_api can't be None" | ||
) | ||
|
||
retry = 3 | ||
while retry > 0: | ||
try: | ||
portfolio_value = float( | ||
trading_api.get_account().portfolio_value | ||
) | ||
break | ||
except ConnectionError as e: | ||
tlog( | ||
f"[{symbol}][{now}[Error] get_account() failed w/ {e}, retrying {retry} more times" | ||
) | ||
await asyncio.sleep(0) | ||
retry -= 1 | ||
|
||
if not portfolio_value: | ||
tlog( | ||
"f[{symbol}][{now}[Error] failed to get portfolio_value" | ||
) | ||
return False, {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function ShortTrapBusterV2.run
refactored with the following changes:
- Swap positions of nested conditionals [×2] (
swap-nested-ifs
) - Hoist nested repeated code outside conditional statements [×2] (
hoist-similar-statement-from-if
) - Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
if trading_api: | ||
retry = 3 | ||
while retry > 0: | ||
try: | ||
portfolio_value = float( | ||
trading_api.get_account().portfolio_value | ||
) | ||
break | ||
except ConnectionError as e: | ||
tlog( | ||
f"[{symbol}][{now}[Error] get_account() failed w/ {e}, retrying {retry} more times" | ||
) | ||
await asyncio.sleep(0) | ||
retry -= 1 | ||
|
||
if not portfolio_value: | ||
tlog( | ||
"f[{symbol}][{now}[Error] failed to get portfolio_value" | ||
) | ||
return False, {} | ||
else: | ||
if not trading_api: | ||
raise Exception( | ||
f"{self.name}: both portfolio_value and trading_api can't be None" | ||
) | ||
|
||
retry = 3 | ||
while retry > 0: | ||
try: | ||
portfolio_value = float( | ||
trading_api.get_account().portfolio_value | ||
) | ||
break | ||
except ConnectionError as e: | ||
tlog( | ||
f"[{symbol}][{now}[Error] get_account() failed w/ {e}, retrying {retry} more times" | ||
) | ||
await asyncio.sleep(0) | ||
retry -= 1 | ||
|
||
if not portfolio_value: | ||
tlog( | ||
"f[{symbol}][{now}[Error] failed to get portfolio_value" | ||
) | ||
return False, {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function SwingMamaFama.run
refactored with the following changes:
- Swap positions of nested conditionals [×2] (
swap-nested-ifs
) - Hoist nested repeated code outside conditional statements [×2] (
hoist-similar-statement-from-if
) - Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
# scale-out | ||
# sell if not picking up. | ||
pass | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function VWAPScalp.run
refactored with the following changes:
- Remove redundant pass statement (
remove-redundant-pass
)
This removes the following comments ( why? ):
# sell if not picking up.
# scale-out
Branch
master
refactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
master
branch, then run:Help us improve this pull request!