Skip to content

Instantly share code, notes, and snippets.

@MarkusHackspacher
Last active June 30, 2025 08:35
Show Gist options
  • Select an option

  • Save MarkusHackspacher/956dccd95951f18e16d87325473c8dc1 to your computer and use it in GitHub Desktop.

Select an option

Save MarkusHackspacher/956dccd95951f18e16d87325473c8dc1 to your computer and use it in GitHub Desktop.
trade-risk
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
trade-risk
Copyright (C) <2025> Markus Hackspacher
This file is part of trade-risk.
trade-risk is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
trade-risk is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU General Public License
along with trade. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import sys
import yfinance as yf
MAXLOST = 25.0
MAXRISK = 0.01
locale.setlocale(locale.LC_ALL, "")
def Show():
print('Berechnung von meinem Risiko')
print(f'Eingestellter maximaler Verlust {locale.currency(MAXLOST)} '
f'und maximales Risiko {locale.format_string("%.2f", MAXRISK*100)} % ')
def Risk(buy = 4.90, stop = 4.85):
locale.currency(buy)
print(f'# Kurs {locale.currency(buy)}, Stopp {locale.currency(stop)}, '
f'Differenz {locale.currency(buy-stop)}')
risk = buy / stop - 1
size = MAXLOST / (buy - stop)
print (f'# Positionsgröße {locale.currency(size * buy)}, entspricht {locale.format_string("%.2f", size)} Stück')
print (f'- Risiko {locale.format_string("%.2f", risk*100)} %, Stopp {locale.currency(size * stop)} ')
if risk > MAXRISK:
print (f'- MAXRISK {locale.format_string("%.2f", MAXRISK*100)} %: Stopp auf {locale.currency(buy - (buy * MAXRISK))}'
f', entspricht {locale.currency((size * buy) - (size * buy * MAXRISK))}')
return
def money(kurs, numberOfShares):
return (f'{locale.currency(kurs * numberOfShares)}, Kurs {locale.currency(kurs)}, '
f'{locale.format_string("%.2f", numberOfShares)} Stück')
def sell(buy=4.9, sell=5.1, stop=4.8, numberOfShares=40):
print(f'Gekauft bei Kurs {locale.currency(buy)}, Stopp {locale.currency(stop)}, Verkauf {locale.currency(sell)}')
print(f'Kaufwert bei {locale.currency(buy*numberOfShares)}, Stopp {locale.currency(stop*numberOfShares)}, '
f'Verkauf {locale.currency(sell*numberOfShares)}')
print(f'Gewinnverhältnis {locale.format_string("%.2f", 100*(sell/buy-1))}, '
f'Risiko {locale.format_string("%.2f", 100*(buy/stop-1))}')
if buy < sell:
print(f'Gewinn: {locale.currency((sell-buy)*numberOfShares)}')
else:
print(f'Verlust: {locale.currency((sell-buy)*numberOfShares)}')
def TickersData():
# yf.enable_debug_mode()
data = yf.download(['MSFT', 'AAPL', 'GOOG'],
period='5d',
interval='1h',
prepost=True)
print(data)
print(data['Close']) # This will show the adjusted close prices
return
"""
import matplotlib.pyplot as plt
plt.figure(figsize=(12, 6))
plt.plot(data['Open']['GOOG'], label='GOOG Open Price')
plt.plot(data['Low']['GOOG'], label='GOOG Low Price')
plt.xlabel('Datetime')
plt.ylabel('Close Price')
plt.title('Comparison of GOOG and MSFT Close Prices')
plt.legend()
plt.show()
last_datetime = [None, None, None]
for datetime, goog_Open in data['Low']['GOOG'].items():
if last_datetime[0] is None:
last_datetime[0] = datetime
continue
if last_datetime[1] is None:
last_datetime[1] = last_datetime[0]
continue
if last_datetime[2] is None:
last_datetime[2] = last_datetime[1]
continue
goog_Low = min(data['Low']['GOOG'].loc[last_datetime[0]],data['Low']['GOOG'].loc[last_datetime[1]],data['Low']['GOOG'].loc[last_datetime[2]])
if goog_Open < goog_Low:
print(f"{datetime}: GOOG open ({goog_Open:.2f}) was lower than low ({goog_Low:.2f},{last_datetime})")
elif goog_Low < goog_Open:
print(f"{datetime}: GOOG Low ({goog_Low:.2f}) ")
else:
print(f"{datetime}: GOOG Open und Low prices were the same ({goog_Open:.2f})")
last_datetime[2] = last_datetime[1]
last_datetime[1] = last_datetime[0]
last_datetime[0] = datetime
"""
if __name__ == "__main__":
Show()
# TickersData()
Risk()
print('Verkauf: ', money(0, 250))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment