Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python backtest

df = pd.DataFrame([[0.0, 1.0],
                   [0.0, 1.2],
                   [0.0, 1.50],
                   [0.0, 1],
                   [1.0, 1.2], # We entered a LONG at the open of this timestep, which is the same as the close of the previous ($1.0)
                   [1.0, 1.3],
                   [1.0, 2.0], # We exit at close of this timestep, so $2
                   [0.0, 1.7],
                   [0.0, 2],
                   [-1.0, 1.798], # We entered a SHORT at the open of this timestep, which is the same as the close of the previous ($2.0) 
                   [-1.0, 0.50],
                   [-1.0, 1.3],
                   [-1.0, 1], # We exit at close of this timestep, so $1.0
                   [0.0, 1.5]],
                   columns=['position_signal','close'])

df['log_returns'] = np.log(df.close) - np.log(df.close.shift(1))
df['strategy_returns'] = df.log_returns * df.position_signal
df['cumulative_returns'] = df.strategy_returns.cumsum().apply(np.exp)
print(df)
 
PREVIOUS NEXT
Tagged: #python #backtest
ADD COMMENT
Topic
Name
5+5 =