-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtlock_strategy.py
More file actions
73 lines (52 loc) · 1.9 KB
/
tlock_strategy.py
File metadata and controls
73 lines (52 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from datetime import datetime # For datetime objects
import os.path # To manage paths
import sys # To find out the script name (in argv[0])
import math
#check seasonality
#recessionary
#certainty
# Import the backtrader platform
import backtrader as bt
from backtrader.indicators import EMA, SMA
import backtrader.feeds as feed
# Create a Stratey
class MyStrategy(bt.Strategy):
def log(self, txt, dt=None):
''' Logging function not this strategy'''
dt = dt or self.datas[0].datetime.date(0)
print('%s, %s' % (dt.isoformat(), txt))
def __init__(self):
self.dataclose = self.datas[0].close
self.ema = EMA(self.dataclose, period=30)
#self.crossover = bt.ind.CrossOver(self.ema, self.dataclose)
def next(self):
if (self.ema[0] - self.dataclose[0]) > 0.6:
# self.log('BUY CREATE, %.2f' % self.dataclose[0])
# self.log('Close, %.2f' % self.dataclose[0])
# self.log('EMA, %.2f' % self.ema[0])
self.log('Difference, %.2f' % (self.ema[0] - self.dataclose[0]))
order = self.buy()
if __name__ == '__main__':
cerebro = bt.Cerebro(stdstats=False)
cerebro.addstrategy(MyStrategy)
data = feed.GenericCSVData(
dataname = 'C:/Users/ME/Documents/Project I/treasury.csv',
# fromdate = datetime(2007, 1, 1),
# todate = datetime(2012, 1, 1),
nullvalue = float('NaN'),
dtformat = ('%m/%d/%Y'),
datetime = 0,
time = -1,
high = -1,
low = -1,
volume = -1,
openinterest = -1,
close = 5,
open = 2,
)
cerebro.adddata(data)
cerebro.broker.setcash(100000.0)
cerebro.run()
#cerebro.plot(volume=False)