开了一个文件夹专门放策略类
This commit is contained in:
parent
d46cc5eca0
commit
49761fccd2
30
main.py
30
main.py
@ -7,38 +7,16 @@ import sys # To find out the script name (in argv[0])
|
||||
|
||||
# Import the backtrader platform
|
||||
import backtrader as bt
|
||||
# Create a Stratey
|
||||
class TestStrategy(bt.Strategy):
|
||||
|
||||
def log(self, txt, dt=None):
|
||||
''' Logging function for this strategy'''
|
||||
if dt is None:
|
||||
dt = self.datas[0].datetime.date(0) if len(self) > 0 else 'No Data'
|
||||
print('%s, %s' % (dt, txt))
|
||||
# Import the TestStrategy from the new file
|
||||
from strategies.lowbuyhighsell_strategy import LowBuyHighSellStrategy
|
||||
|
||||
def __init__(self):
|
||||
# Keep a reference to the "close" line in the data[0] dataseries
|
||||
self.dataclose = self.datas[0].close
|
||||
self.log('Strategy initialized', dt='Initialization')
|
||||
|
||||
def next(self):
|
||||
# Log the current date and close price
|
||||
self.log('Date: %s, Close: %.2f' % (self.datas[0].datetime.date(0), self.dataclose[0]))
|
||||
self.log('High: %.2f, Low: %.2f' % (self.datas[0].high[0], self.datas[0].low[0]))
|
||||
|
||||
# Simple trading logic
|
||||
if self.dataclose[0] < 30000:
|
||||
self.log('BUY CREATE, Date: %s, Price: %.2f, Position: %d' % (self.datas[0].datetime.date(0), self.dataclose[0], self.position.size))
|
||||
self.buy()
|
||||
elif self.dataclose[0] > 30000 and self.position.size > 0:
|
||||
self.log('SELL CREATE, Date: %s, Price: %.2f, Position: %d' % (self.datas[0].datetime.date(0), self.dataclose[0], self.position.size))
|
||||
self.sell()
|
||||
if __name__ == '__main__':
|
||||
# Create a cerebro entity
|
||||
cerebro = bt.Cerebro()
|
||||
|
||||
# Add a strategy
|
||||
cerebro.addstrategy(TestStrategy)
|
||||
cerebro.addstrategy(LowBuyHighSellStrategy)
|
||||
|
||||
|
||||
# Datas are in a subfolder of the samples. Need to find where the script is
|
||||
@ -50,7 +28,7 @@ datapath = os.path.join(modpath, 'candle_test_asc.csv')
|
||||
data = bt.feeds.GenericCSVData(
|
||||
dataname=datapath,
|
||||
fromdate=datetime.datetime(2022, 1, 1),
|
||||
todate=datetime.datetime(2022, 5, 31),
|
||||
todate=datetime.datetime(2022, 12, 31),
|
||||
dtformat=('%Y-%m-%d %H:%M:%S'),
|
||||
timeframe=bt.TimeFrame.Days,
|
||||
compression=1,
|
||||
|
31
strategies/lowbuyhighsell_strategy.py
Normal file
31
strategies/lowbuyhighsell_strategy.py
Normal file
@ -0,0 +1,31 @@
|
||||
from __future__ import (absolute_import, division, print_function,
|
||||
unicode_literals)
|
||||
# 低买高卖策略
|
||||
import backtrader as bt
|
||||
|
||||
class LowBuyHighSellStrategy(bt.Strategy):
|
||||
|
||||
def log(self, txt, dt=None):
|
||||
''' Logging function for this strategy'''
|
||||
if dt is None:
|
||||
dt = self.datas[0].datetime.date(0) if len(self) > 0 else 'No Data'
|
||||
print('%s, %s' % (dt, txt))
|
||||
|
||||
def __init__(self):
|
||||
# Keep a reference to the "close" line in the data[0] dataseries
|
||||
self.dataclose = self.datas[0].close
|
||||
self.log('Strategy initialized', dt='Initialization')
|
||||
|
||||
def next(self):
|
||||
# Log the current date and close price
|
||||
self.log('Date: %s, Close: %.2f' % (self.datas[0].datetime.date(0), self.dataclose[0]))
|
||||
self.log('High: %.2f, Low: %.2f' % (self.datas[0].high[0], self.datas[0].low[0]))
|
||||
|
||||
# Simple trading logic
|
||||
if self.dataclose[0] < 17500:
|
||||
self.log('BUY CREATE, Date: %s, Price: %.2f, Position: %d' % (self.datas[0].datetime.date(0), self.dataclose[0], self.position.size))
|
||||
self.buy()
|
||||
elif self.dataclose[0] > 47500 and self.position.size > 0:
|
||||
self.log('SELL CREATE, Date: %s, Price: %.2f, Position: %d' % (self.datas[0].datetime.date(0), self.dataclose[0], self.position.size))
|
||||
self.sell()
|
||||
|
Loading…
x
Reference in New Issue
Block a user