Browse Source

fix: do not buy thx that you can't

pull/19/head
QuentinN42 1 year ago
parent
commit
8d1336d119
Signed by: number42 GPG Key ID: 2CD7D563712B3A50
  1. 22
      auto_trading/strat/prop.py
  2. 23
      tests/strat/test_prop.py

22
auto_trading/strat/prop.py

@ -45,17 +45,25 @@ class Prop(Strategy):
"""
orders: List[Union[Long, Short]] = []
conversion_rate = data.loc[data.index[-1][0]].close.to_dict()
# Desired state
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
)
stock_to_sell = []
# Remove all action that the indicator place as negative.
for stock in results[results < 0].index:
orders.append(Short(stock, state.stocks[stock], conversion_rate[stock]))
order = Short(stock, state.stocks[stock], conversion_rate[stock])
if self.filter_order(order):
orders.append(order)
stock_to_sell.append(stock)
total_money = state.balance + sum(
conversion_rate[stock_name] * amount
for stock_name, amount in state.stocks.items()
if results[stock] > 0 or stock_name in stock_to_sell
)
# Desired state
desired_state_precent = results[results > 0] / results[results > 0].sum()
desired_state_dolards = total_money * desired_state_precent
# Create the new buy orders from with the delta between the actual and the desired state.
for stock, amount in desired_state_dolards.items():

23
tests/strat/test_prop.py

@ -28,6 +28,29 @@ date = datetime.strptime("2015-03-31", "%Y-%m-%d")
PTFState(5, {"GOOG": 0, "GOOGL": 0}),
[],
),
(
1,
DataFrame({"close": {(date, "GOOG"): 10, (date, "GOOGL"): 10}}),
Series({"GOOG": 1, "GOOGL": -1}),
PTFState(0, {"GOOG": 0, "GOOGL": 1}),
[Short("GOOGL", price=10, amount=1), Long("GOOG", price=10, amount=1)],
),
(
20,
DataFrame(
{
"close": {
(date, "A"): 10,
(date, "B"): 10,
(date, "C"): 10,
(date, "D"): 10,
}
}
),
Series({"A": 1, "B": -1, "C": -1, "D": -1}),
PTFState(0, {"A": 0, "B": 1, "C": 1, "D": 1}),
[],
),
],
)
def test_prop(max_delta, data, indicators_results, state, results):

Loading…
Cancel
Save