Skip to content

Instantly share code, notes, and snippets.

@Rakademi
Last active January 8, 2023 14:12
Show Gist options
  • Save Rakademi/a56facfaa6fd081f206010eb8c638e14 to your computer and use it in GitHub Desktop.
Save Rakademi/a56facfaa6fd081f206010eb8c638e14 to your computer and use it in GitHub Desktop.
Simulasi Strategi EMA Crossover
import streamlit as st
import util
if __name__ == '__main__':
ticker_symbol = st.sidebar.text_input(
"Please enter the stock symbol", 'MSFT'
)
data_period = st.sidebar.text_input('Period', '10d')
data_interval = st.sidebar.radio('Interval', ['15m','30m','1h','1d'])
ema1 = st.sidebar.text_input('EMA 1', 20)
ema2 = st.sidebar.text_input('EMA 2', 50)
share_amount = st.sidebar.text_input('Number of Shares', 1)
initial_capital = st.sidebar.text_input('Initial Capital (USD)', 10000)
st.header("Backtest trading strategy **EMA crossover** :rocket:")
st.write("*Warning: This is just a programming guide from a guy on YouTube, not financial advice!* :sunglasses:")
st.write("""
- cross up :point_right: BUY
- cross down :point_right: SELL
---
""")
ticker_data = util.get_ticker_data(ticker_symbol, data_period, data_interval)
if len(ticker_data) != 0:
ticker_data = util.get_ema(ticker_data, int(ema1))
ticker_data = util.get_ema(ticker_data, int(ema2))
candle_fig = util.get_candle_chart(ticker_data)
candle_fig = util.add_ema_trace(candle_fig, ticker_data.index, ticker_data['ema_' + ema1], 'EMA ' + ema1, "#ffeb3b")
candle_fig = util.add_ema_trace(candle_fig, ticker_data.index, ticker_data['ema_' + ema2], 'EMA ' + ema2, "#2962ff")
trades = util.create_ema_trade_list(ticker_data, 'ema_' + ema1, 'ema_' + ema2)
ticker_data = util.join_trades_to_ticker_data(trades, ticker_data)
candle_fig = util.add_trades_trace(candle_fig, ticker_data)
simulation_results = util.simulate_ema_cross_trading(trades)
win_rate, sim_results_df, sim_fig, accumulative_fig = util.get_sim_summary(simulation_results, int(share_amount), float(initial_capital))
win_rate_str = str(win_rate.round(1))
st.write("### Simulation Results - ", ticker_symbol)
st.write("#### Win Rate: ", win_rate_str, "%")
st.write(sim_results_df.describe())
st.write("#### Share Price Change Per Trade")
st.write(sim_fig)
st.write("#### Account Value Change")
st.write(accumulative_fig)
st.write("#### Trade List")
trades
st.write(candle_fig)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment