Professional Documents
Culture Documents
import pandas as pd
import talib
import yfinance as yf
data['prev_high'] = data['High'].shift(1)
data['prev_low'] = data['Low'].shift(1)
# initialize variables
# long position
if position != 1:
entry_price = data['Open'][i+1]
exit_price = data['Close'][i+1]
position = 1
# short position
if position != -1:
entry_price = data['Open'][i+1]
exit_price = data['Close'][i+1]
position = -1
else:
# close position
if position == 1:
entry_price = data['Open'][i+1]
exit_price = data['Close'][i+1]
position = 0
entry_price = data['Open'][i+1]
exit_price = data['Close'][i+1]
position = 0
total_returns = sum(returns)
# plot returns
plt.plot(returns)
plt.xlabel('Trades')
plt.ylabel('Returns')
plt.show()