Search
 
SCRIPT & CODE EXAMPLE
 

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)
Comment

PREVIOUS NEXT
Code Example
Python :: python literation (looping) 
Python :: numpy.floor_divide() in Python 
Python :: operator overloading in python 
Python :: awesome python 
Python :: python enum key string get 
Python :: Sampling data in different ways 
Python :: pytrend 
Python :: airflow get execution context dictionary kubernetes pod name 
Python :: qr code scanner on django 
Python :: needle in haystack 
Python :: linear plot 1D vector for x python 
Python :: stackoverflow Django ForeignKey 
Python :: python check anangram 
Python :: python Find Hash 
Python :: how to square in python 
Python :: india states django choices 
Python :: Convert torch.nn.Embedding layer to numpy array 
Python :: machine learning project outline 
Python :: what is a console in pythonanywhere 
Python :: adjusted price in crsp pandas 
Python :: python find multiple matches in string 
Python :: vvm 2020 exam date 
Python :: python site-packages pyspark 
Python :: custom dense layer 
Python :: python multiple imports 
Python :: Python Raw string using r prefix 
Python :: Convert Time object to String in python 
Python :: iterate over the dataset and compute the mean vector. 
Python :: how to access clipboard with python 
Python :: myPYmenu 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =