Browse Source

Finished testing strategy

master
Barthélemy Paléologue 7 months ago
parent
commit
70e03ef86c
  1. 3
      auto_trading/indicators/ema2.py
  2. 2
      auto_trading/indicators/sma2.py
  3. 7
      auto_trading/strat/buyupselldown.py
  4. 4
      auto_trading/strat/yoyo.py
  5. 37
      tests/strat/test_buyUpSellDown.py

3
auto_trading/indicators/ema2.py

@ -29,10 +29,9 @@ class EMA(Indicator):
# select high prices for each action
data3 = data2.get("close")
if data3 is None:
if data3 is None or len(data3) <= 1:
return pd.Series([])
logging.info(data3)
# apply ema

2
auto_trading/indicators/sma2.py

@ -28,7 +28,7 @@ class SMA(Indicator):
# select high prices for each action
data3 = data2.get("close")
if data3 is None or len(data3[0]) == 0:
if data3 is None or len(data3) <= 1:
return pd.Series([])
# apply sma

7
auto_trading/strat/buyupselldown.py

@ -50,13 +50,15 @@ class BuyUpSellDown(Strategy):
for (index, row) in indicators_results.iterrows():
trust += row[stock_name]
logging.info(trust)
#logging.info(indicators_results.get("AAPL"))
if market_price is None:
# retry later
continue
if trust > 1.0:
if trust > 0.1:
if (balance := state.balance) > 0:
amount = balance / (market_price * nb_stock_type)
@ -66,7 +68,7 @@ class BuyUpSellDown(Strategy):
)
else:
print("A PU DE THUNE")
elif trust < 1.0:
elif trust < -0.1:
# and I sell it all at market price
if state.stocks.get(stock_name) is not None:
orders.append(
@ -76,6 +78,5 @@ class BuyUpSellDown(Strategy):
price=market_price,
)
)
#logging.info(orders)
return orders

4
auto_trading/strat/yoyo.py

@ -1,12 +1,12 @@
"""Yoyo strategy."""
from auto_trading.interfaces import Strategy, Order, PTFState
from auto_trading.orders import Long, Short
from typing import List, Dict, cast, Callable
from typing import List, Dict, Optional
import pandas as pd # type: ignore
def is_positive(number: float | None) -> bool:
def is_positive(number: Optional[float]) -> bool:
"""Return True if the price is positive."""
return number is not None and number > 0

37
tests/strat/test_buyUpSellDown.py

@ -11,6 +11,7 @@ from auto_trading.interfaces import PTFState as State
from auto_trading.orders import Long, Short
date = datetime.strptime("2015-03-31", "%Y-%m-%d")
today = date.today()
@pytest.mark.parametrize(
"data, state, output",
@ -46,32 +47,38 @@ date = datetime.strptime("2015-03-31", "%Y-%m-%d")
DataFrame({
"close": {
(date, "AAPL"): 5,
(date, "TSLA"): 10,
(date, "TSLA"): 50,
(date.today() + timedelta(days=1), "AAPL"): 20,
(date.today() + timedelta(days=1), "TSLA"): 5,
(today + timedelta(days=1), "AAPL"): 7,
(today + timedelta(days=1), "TSLA"): 40,
(date.today() + timedelta(days=2), "AAPL"): 30,
(date.today() + timedelta(days=2), "TSLA"): 4,
(today + timedelta(days=2), "AAPL"): 10,
(today + timedelta(days=2), "TSLA"): 30,
(date.today() + timedelta(days=3), "AAPL"): 40,
(date.today() + timedelta(days=3), "TSLA"): 2,
(today + timedelta(days=3), "AAPL"): 15,
(today + timedelta(days=3), "TSLA"): 15,
(date.today() + timedelta(days=4), "AAPL"): 50,
(date.today() + timedelta(days=4), "TSLA"): 1,
(today + timedelta(days=4), "AAPL"): 20,
(today + timedelta(days=4), "TSLA"): 10,
(date.today() + timedelta(days=5), "AAPL"): 70,
(date.today() + timedelta(days=5), "TSLA"): 1,
(today + timedelta(days=5), "AAPL"): 30,
(today + timedelta(days=5), "TSLA"): 5,
(date.today() + timedelta(days=6), "AAPL"): 100,
(date.today() + timedelta(days=6), "TSLA"): 1,
(today + timedelta(days=6), "AAPL"): 40,
(today + timedelta(days=6), "TSLA"): 2,
(today + timedelta(days=7), "AAPL"): 60,
(today + timedelta(days=7), "TSLA"): 1,
(today + timedelta(days=8), "AAPL"): 80,
(today + timedelta(days=8), "TSLA"): 1,
}
}),
State(
balance=100,
stocks={},
stocks={"TSLA":20},
),
[],
[Long(stock="AAPL", amount=0.625, price=80), Short(stock="TSLA", amount=20, price=1)],
),
],
)

Loading…
Cancel
Save