63 lines
1.7 KiB
Python
63 lines
1.7 KiB
Python
from __future__ import (absolute_import, division, print_function,
|
|
unicode_literals)
|
|
|
|
import datetime # For datetime objects
|
|
import os.path # To manage paths
|
|
import sys # To find out the script name (in argv[0])
|
|
|
|
# Import the backtrader platform
|
|
import backtrader as bt
|
|
|
|
# Import the TestStrategy from the new file
|
|
from strategies.lowbuyhighsell_strategy import LowBuyHighSellStrategy
|
|
|
|
if __name__ == '__main__':
|
|
# Create a cerebro entity
|
|
cerebro = bt.Cerebro()
|
|
|
|
# Add a strategy
|
|
cerebro.addstrategy(LowBuyHighSellStrategy)
|
|
|
|
|
|
# Datas are in a subfolder of the samples. Need to find where the script is
|
|
# because it could have been called from anywhere
|
|
modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
|
|
datapath = os.path.join(modpath, 'candle_test_asc.csv')
|
|
|
|
# Create a Data Feed
|
|
data = bt.feeds.GenericCSVData(
|
|
dataname=datapath,
|
|
fromdate=datetime.datetime(2022, 1, 1),
|
|
todate=datetime.datetime(2022, 12, 31),
|
|
dtformat=('%Y-%m-%d %H:%M:%S'),
|
|
timeframe=bt.TimeFrame.Days,
|
|
compression=1,
|
|
datetime=0,
|
|
open=1,
|
|
high=2,
|
|
low=3,
|
|
close=4,
|
|
volume=5,
|
|
openinterest=-1)
|
|
|
|
# Add the Data Feed to Cerebro
|
|
cerebro.adddata(data)
|
|
|
|
# Set our desired cash start
|
|
cerebro.broker.setcash(100000.0)
|
|
|
|
# Print out the starting conditions
|
|
print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
|
|
|
|
# Run over everything
|
|
cerebro.run()
|
|
|
|
# Print out the final result
|
|
final_value = cerebro.broker.getvalue()
|
|
cash = cerebro.broker.getcash()
|
|
portfolio_value = final_value - cash
|
|
|
|
print('Final Portfolio Value: %.2f' % final_value)
|
|
print('Cash: %.2f' % cash)
|
|
print('Portfolio Value (Positions): %.2f' % portfolio_value)
|