Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Apr 17, 2024
1 parent bfe89fe commit d4b83e0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
4 changes: 2 additions & 2 deletions portfolio/_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ async def position_risk(self, query: GetPositionRisk):

if query.type == PositionSizeType.Kelly:
kelly = await self.state.get_kelly(symbol, timeframe, strategy)
return equity * kelly if kelly and kelly > 0 else equity * risk_per_trade
return equity * kelly if kelly > 0 else equity * risk_per_trade

if query.type == PositionSizeType.Optimalf:
optimalf = await self.state.get_optimalf(symbol, timeframe, strategy)
return equity * optimalf if optimalf else equity * risk_per_trade
return optimalf * equity if optimalf > 0 else equity * risk_per_trade

return equity * risk_per_trade

Expand Down
26 changes: 23 additions & 3 deletions position/risk/break_even.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def next(
) -> float:
ohlcvs = ohlcvs[:]
lookback = 14
factor = 2.0

if len(ohlcvs) < lookback:
return stop_loss_price, take_profit_price
Expand All @@ -40,24 +41,35 @@ def next(
high = min(ohlcvs[-lookback:], key=lambda x: abs(x.high - price)).high
low = min(ohlcvs[-lookback:], key=lambda x: abs(x.low - price)).low

upper_bb, lower_bb, middle_bb = self._bb(ohlcvs, lookback, factor)
bbw = (upper_bb - lower_bb) / middle_bb

squeeze = bbw <= np.min(bbw[-lookback:])

next_stop_loss = stop_loss_price

if side == PositionSide.LONG:
next_take_profit = max(entry_price + risk_value, high + tp_threshold)
if squeeze[-1]:
next_take_profit = max(entry_price + risk_value, upper_bb[-1])
else:
next_take_profit = max(entry_price + risk_value, high + tp_threshold)

if curr_dist > dist and price > entry_price:
next_stop_loss = max(entry_price - risk_value, low - sl_threshold)

elif side == PositionSide.SHORT:
next_take_profit = min(entry_price - risk_value, low - tp_threshold)
if squeeze[-1]:
next_take_profit = min(entry_price - risk_value, lower_bb[-1])
else:
next_take_profit = min(entry_price - risk_value, low - tp_threshold)

if curr_dist > dist and price < entry_price:
next_stop_loss = min(entry_price + risk_value, high + sl_threshold)

return next_stop_loss, next_take_profit

@staticmethod
def _atr(ohlcvs: List[OHLCV], period: int) -> float:
def _atr(ohlcvs: List[OHLCV], period: int) -> List[float]:
highs, lows, closes = (
np.array([ohlcv.high for ohlcv in ohlcvs]),
np.array([ohlcv.low for ohlcv in ohlcvs]),
Expand All @@ -81,3 +93,11 @@ def _atr(ohlcvs: List[OHLCV], period: int) -> float:
@staticmethod
def _price(ohlcvs: List[OHLCV]) -> float:
return (ohlcvs[-1].high + ohlcvs[-1].low + ohlcvs[-1].close) / 3.0

@staticmethod
def _bb(ohlcvs: List[OHLCV], period: int, factor: float) -> Tuple[List[float], List[float], List[float]]:
closes = np.array([ohlcv.close for ohlcv in ohlcvs])
rolling_mean = np.convolve(closes, np.ones(period) / period, mode='valid')
rolling_std = factor * np.std([closes[i:i+period] for i in range(len(closes) - period + 1)], axis=1)

return rolling_mean + rolling_std, rolling_mean - rolling_std, rolling_mean
4 changes: 2 additions & 2 deletions position/size/optimal_f.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ async def calculate(
GetPositionRisk(signal, PositionSizeType.Optimalf)
)

risk_amount = await self.query(GetPositionRisk(signal, PositionSizeType.Fixed))

if stop_loss_price is not None and entry_price is not None:
price_difference = abs(entry_price - stop_loss_price)
else:
Expand All @@ -34,4 +32,6 @@ async def calculate(

position_size = risk_amount / price_difference

print(f"Risk {risk_amount}, Size {position_size}")

return position_size
4 changes: 2 additions & 2 deletions quant.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from portfolio import Portfolio
from position import PositionActorFactory, PositionFactory
from position.risk.break_even import PositionRiskBreakEvenStrategy
from position.size.optimal_f import PositionOptimalFSizeStrategy
from position.size.fixed import PositionFixedSizeStrategy
from position.take_profit.risk_reward import PositionRiskRewardTakeProfitStrategy
from risk import RiskActorFactory
from service import EnvironmentSecretService, SignalService, WasmFileService
Expand Down Expand Up @@ -68,7 +68,7 @@ async def main():
SmartRouter(exchange_factory, config_service)

position_factory = PositionFactory(
PositionOptimalFSizeStrategy(),
PositionFixedSizeStrategy(),
PositionRiskBreakEvenStrategy(config_service),
PositionRiskRewardTakeProfitStrategy(config_service),
)
Expand Down
2 changes: 1 addition & 1 deletion strategy/generator/baseline/ma.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
class MaBaseLine(BaseLine):
type: BaseLineType = BaseLineType.Ma
ma: Parameter = CategoricalParameter(MovingAverageType)
period: Parameter = StaticParameter(24.0)
period: Parameter = StaticParameter(14.0)
2 changes: 1 addition & 1 deletion strategy/generator/signal/ma/ma_cross.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
class MaCrossSignal(Signal):
type: SignalType = SignalType.MaCross
ma: Parameter = CategoricalParameter(MovingAverageType)
period: Parameter = RandomParameter(150.0, 200.0, 10.0)
period: Parameter = RandomParameter(100.0, 150.0, 10.0)

0 comments on commit d4b83e0

Please sign in to comment.