Browse Source

fix: patched Prop

pull/19/head
QuentinN42 8 months ago
parent
commit
47686b1c39
Signed by: number42 GPG Key ID: 2CD7D563712B3A50
  1. 22
      auto_trading/orders.py
  2. 14
      auto_trading/strat/prop.py
  3. 2
      tests/orders/test_orders.py
  4. 38
      tests/strat/test_prop.py

22
auto_trading/orders.py

@ -17,9 +17,18 @@ class Long(Order):
"""The amount in $"""
return self.amount * self.price
def __repr__(self) -> str:
def __str__(self) -> str:
return f"{self.__class__.__name__}({self.stock}: {self.amount_usd}$)"
def __eq__(self, other) -> bool:
if not isinstance(other, Long):
return False
return (
self.stock == other.stock
and self.amount == other.amount
and self.price == other.price
)
@dataclass
class Short(Order):
@ -34,5 +43,14 @@ class Short(Order):
"""The amount in $"""
return self.amount * self.price
def __repr__(self) -> str:
def __str__(self) -> str:
return f"{self.__class__.__name__}({self.stock}: {self.amount_usd}$)"
def __eq__(self, other) -> bool:
if not isinstance(other, Short):
return False
return (
self.stock == other.stock
and self.amount == other.amount
and self.price == other.price
)

14
auto_trading/strat/prop.py

@ -47,7 +47,7 @@ class Prop(Strategy):
conversion_rate = data.loc[data.index[-1][0]].close.to_dict()
# Desired state
results = indicators_results[self.indicator]
results = indicators_results.T[self.indicator]
desired_state_precent = results[results > 0] / results[results > 0].sum()
desired_state_dolards = (
state.total_balance(conversion_rate) * desired_state_precent
@ -61,11 +61,19 @@ class Prop(Strategy):
for stock, amount in desired_state_dolards.items():
if amount > state.stocks[stock]:
orders.append(
Long(stock, amount - state.stocks[stock], conversion_rate[stock])
Long(
stock,
amount / conversion_rate[stock] - state.stocks[stock],
conversion_rate[stock],
)
)
else:
orders.append(
Short(stock, state.stocks[stock] - amount, conversion_rate[stock])
Short(
stock,
state.stocks[stock] - amount / conversion_rate[stock],
conversion_rate[stock],
)
)
# Filter low orders

2
tests/orders/test_orders.py

@ -33,4 +33,4 @@ def test_order_usd(order: Union[Long, Short], usd: float):
)
def test_repr(order: Union[Long, Short], string: str):
"""Test repr of an order."""
assert repr(order) == string
assert str(order) == string

38
tests/strat/test_prop.py

@ -0,0 +1,38 @@
import pytest
from datetime import datetime
from pandas import DataFrame, Series # type: ignore
from auto_trading.strat.prop import Prop
from auto_trading.indicators.dumb import Dumb
from auto_trading.interfaces import PTFState
from auto_trading.orders import Long, Short
date = datetime.strptime("2015-03-31", "%Y-%m-%d")
@pytest.mark.parametrize(
"max_delta, data, indicators_results, state, results",
[
(
10,
DataFrame({"close": {(date, "GOOG"): 10, (date, "GOOGL"): 10}}),
Series({"GOOG": 1, "GOOGL": -1}),
PTFState(50, {"GOOG": 0, "GOOGL": 0}),
[Long("GOOG", price=10, amount=5)],
),
(
10,
DataFrame({"close": {(date, "GOOG"): 10, (date, "GOOGL"): 10}}),
Series({"GOOG": 1, "GOOGL": -1}),
PTFState(5, {"GOOG": 0, "GOOGL": 0}),
[],
),
],
)
def test_prop(max_delta, data, indicators_results, state, results):
strat = Prop(max_delta=max_delta, indicators={"ind": Dumb(indicators_results)})
res = strat.run(data, state)
assert len(res) == len(results)
for result in results:
assert result in res
Loading…
Cancel
Save