StockSwingApp / utils /calculator2.py
netflypsb's picture
Create calculator2.py
744162c verified
import pandas as pd
def simulate_all_in_trading(signals_data):
"""
Simulates trading by going all-in on the first buy signal and selling all on the first sell signal,
then repeating the process with the new amount of cash including profit or loss.
Parameters:
- signals_data (pd.DataFrame): DataFrame with 'Close', 'Buy_Signal', and 'Sell_Signal'.
Returns:
- float: The final amount of cash after applying the trading strategy.
- int: The number of trades executed.
"""
cash = 1000 # Initial cash
holding = False
trades_executed = 0
for index, row in signals_data.iterrows():
if not holding and row['Buy_Signal']:
# Buy all you can with the available cash
shares = cash / row['Close']
holding = True
trades_executed += 1
elif holding and row['Sell_Signal']:
# Sell all shares
cash = shares * row['Close']
holding = False
trades_executed += 1
return cash, trades_executed
if __name__ == "__main__":
# Example DataFrame with close prices and buy/sell signals
signals_data = pd.DataFrame({
'Close': [100, 105, 103, 108, 107, 110, 108, 112], # Example close prices
'Buy_Signal': [False, True, False, False, True, False, False, False],
'Sell_Signal': [False, False, True, False, False, True, False, False]
})
final_cash, trades = simulate_all_in_trading(signals_data)
print(f"Final Cash: ${final_cash:.2f}")
print(f"Trades Executed: {trades}")