Browse Source

Petits Fix

- Correction du calcul de la confiance
- Ajout d'un booleen pour analyser ou non le dataset
master
Barthélemy Paléologue 7 months ago
parent
commit
b2e1a01cb5
  1. 6
      auto_trading/indicators/ema2.py
  2. 6
      auto_trading/indicators/sma2.py
  3. 3
      auto_trading/interfaces.py
  4. 25
      auto_trading/strat/buyupselldown.py
  5. 30
      main.py

6
auto_trading/indicators/ema2.py

@ -1,4 +1,4 @@
"""SMA indicator, not ready yet."""
"""EMA indicator, not ready yet."""
from calendar import c
from re import sub
import pandas as pd # type: ignore
@ -47,9 +47,7 @@ class EMA(Indicator):
# )
# TODO: ne pas hardcoder
trust = 1 + (coeff / 5)
trust = min(trust, 1.0)
trust = max(trust, 0.0)
trust = coeff / 5
res[column] = trust

6
auto_trading/indicators/sma2.py

@ -1,7 +1,7 @@
"""SMA indicator, not ready yet."""
from calendar import c
from re import sub
import pandas as pd # type: ignore
import pandas as pd
from ..interfaces import Indicator
@ -47,9 +47,7 @@ class SMA(Indicator):
# )
# TODO: ne pas hardcoder
trust = 1 + (coeff / 5)
trust = min(trust, 1.0)
trust = max(trust, 0.0)
trust = coeff / 5
res[column] = trust

3
auto_trading/interfaces.py

@ -19,6 +19,9 @@ class PTFState:
balance: float
stocks: Dict[str, float]
# TODO: stocker le conversion rate à l'instant t pour facilement calculer le total balance
# conversion_rate: Dict[str, float]
def total_balance(self, conversion_rate: Dict[str, float]) -> float:
return self.balance + sum(
conversion_rate[stock_name] * amount

25
auto_trading/strat/buyupselldown.py

@ -1,3 +1,4 @@
from re import S
from typing import Dict, List
import pandas as pd # type: ignore
@ -41,17 +42,14 @@ class BuyUpSellDown(Strategy):
### compute trust
trust = 0
for (index, row) in indicators_results.iterrows():
if row[stock_name] > 0:
trust += 1
elif row[stock_name] < 0:
trust -= 1
# print(trust)
trust += row[stock_name]
print(trust)
if market_price is None:
# retry later
continue
if trust > 0:
if trust > 1.0:
if (balance := state.balance) > 0:
amount = balance / (market_price * nb_stock_type)
@ -61,14 +59,15 @@ class BuyUpSellDown(Strategy):
)
else:
print("A PU DE THUNE")
elif trust < 0:
elif trust < 1.0:
# and I sell it all at market price
orders.append(
Short(
stock=stock_name,
amount=state.stocks[stock_name],
price=market_price,
if state.stocks.get(stock_name) is not None:
orders.append(
Short(
stock=stock_name,
amount=state.stocks.get(stock_name),
price=market_price,
)
)
)
# print(state.stocks)
return orders

30
main.py

@ -15,7 +15,7 @@ from auto_trading.bot import Bot
pd.options.plotting.backend = "plotly"
# logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.INFO)
if __name__ == "__main__":
@ -30,24 +30,28 @@ if __name__ == "__main__":
bot.run()
###### Visualisation ######
###### Visualisation du Dataset ######
data = bt.data.unstack()
ANALYSE_DATASET = False
if ANALYSE_DATASET:
data = bt.data.unstack()
# select high prices for each action
closeData = data["close"]
# select high prices for each action
closeData = data["close"]
sma = closeData.rolling(window=5).mean()
ema = closeData.ewm(alpha=0.6).mean()
sma = closeData.rolling(window=5).mean()
ema = closeData.ewm(alpha=0.6).mean()
plt.plot(closeData, label="real", marker="x", linewidth=0)
plt.plot(sma, color="red", label="SMA")
plt.plot(ema, color="green", label="EMA")
plt.legend()
plt.plot(closeData, label="real", marker="x", linewidth=0)
plt.plot(sma, color="red", label="SMA")
plt.plot(ema, color="green", label="EMA")
plt.legend()
print(bot.ptf.total_balance(bot.broker.current_change))
plt.show()
print(bot.ptf.balance)
plt.show()
print(bot.ptf.total_balance(bot.broker.current_change))
ch_history: pd.DataFrame = bot.broker.change_rate_history # type: ignore
st_history = pd.DataFrame(

Loading…
Cancel
Save