Guest User

Untitled

a guest
Apr 5th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. from deap import creator, base, tools, algorithms, gp
  2. import operator
  3. import re
  4. from random import randint
  5. from zipline.api import order_target, record, symbol
  6. from zipline import run_algorithm
  7. import matplotlib.pyplot as plt
  8. import pandas as pd
  9.  
  10. def protectedDiv(left, right):
  11.     try:
  12.         return left / right
  13.     except ZeroDivisionError:
  14.         return 1
  15.  
  16.  
  17. pset = gp.PrimitiveSetTyped("MAIN", [], bool)
  18.  
  19. # arithmetic operators
  20. pset.addPrimitive(operator.add, [float, float], float)
  21. pset.addPrimitive(operator.sub, [float, float], float)
  22. pset.addPrimitive(protectedDiv, [float, float], float)
  23. pset.addPrimitive(operator.mul, [float, float], float)
  24.  
  25.  
  26. def and_(a, b):
  27.     return (a and b)
  28.  
  29.  
  30. def or_(a, b):
  31.     return (a or b)
  32.  
  33.  
  34. def not_(a):
  35.     return not a
  36.  
  37.  
  38. # boolean operators
  39. pset.addPrimitive(and_, [bool, bool], bool)
  40. pset.addPrimitive(or_, [bool, bool], bool)
  41. pset.addPrimitive(not_, [bool], bool)
  42.  
  43. # relational operators
  44. pset.addPrimitive(operator.lt, [float, float], bool)
  45. pset.addPrimitive(operator.gt, [float, float], bool)
  46.  
  47.  
  48. def if_then_else(input, output1, output2):
  49.     if input:
  50.         return output1
  51.     else:
  52.         return output2
  53.  
  54.  
  55. pset.addPrimitive(if_then_else, [bool, bool, bool], bool)
  56.  
  57.  
  58. def average(string, days):
  59.     if days > 300:
  60.         days = 300
  61.     average = data.history(context.asset, string, bar_count=int(
  62.         days), frequency="1d").mean()
  63.  
  64.     return average
  65.  
  66.  
  67. def maximum(type, days):
  68.     return 100
  69.  
  70.  
  71. def minimum(type, days):
  72.     return 100
  73.  
  74.  
  75. def norm(left, right):
  76.     return abs(left - right)
  77.  
  78.  
  79. def idem(string):
  80.     return string
  81.  
  82.  
  83. pset.addPrimitive(idem, [str], str)
  84.  
  85. pset.addPrimitive(norm, [float, float], float)
  86. pset.addPrimitive(average, [str, float], float)
  87. pset.addPrimitive(maximum, [str, float], float)
  88. pset.addPrimitive(average, [str, float], float)
  89. pset.addTerminal("price", str)
  90. pset.addTerminal("volume", str)
  91. pset.addTerminal(False, bool)
  92. pset.addTerminal(True, bool)
  93. pset.addEphemeralConstant('a', lambda: float(randint(0, 300)), float)
  94. expr = gp.genFull(pset, min_=1, max_=4)
  95.  
  96. tree = gp.PrimitiveTree(expr)
  97. treeString = str(tree)
  98. treeString = re.sub(r"idem\([^']*", '', treeString)
  99. treeString = re.sub(r"(?<='price')[^,]*", '', treeString)
  100. treeString = re.sub(r"(?<='vol')[^,]*", '', treeString)
  101. tree2 = gp.PrimitiveTree.from_string(treeString, pset)
  102. print(tree2)
  103.  
  104. function = gp.compile(tree2, pset)
  105.  
  106.  
  107. def initialize(context):
  108.     context.i = 0
  109.     context.asset = symbol('AAPL')
  110.  
  111.  
  112. def handle_data(context, data):
  113.     # Skip first 300 days to get full windows
  114.     context.i += 1
  115.     if context.i < 300:
  116.         return
  117.  
  118.     print(average('price', 100))
  119.  
  120.     # Trading lo
  121.     # gic
  122.     if function:
  123.         # order_target orders as many shares as needed to
  124.         # achieve the desired number of shares.
  125.         order_target(context.asset, 100)
  126.  
  127.  
  128. def analyze(context, perf):
  129.     fig = plt.figure()
  130.     ax1 = fig.add_subplot(211)
  131.     perf.portfolio_value.plot(ax=ax1)
  132.     ax1.set_ylabel('portfolio value in $')
  133.  
  134.     plt.legend(loc=0)
  135.     plt.show()
  136.  
  137.  
  138. start = pd.Timestamp('2014-01-01', tz='utc')
  139. end = pd.Timestamp('2018-01-01', tz='utc')
  140.  
  141. run_algorithm(start=start, end=end, initialize=initialize,
  142.                   capital_base=100000, handle_data=handle_data,
  143.                   bundle='quantopian-quandl', analyze=analyze)
Advertisement
Add Comment
Please, Sign In to add comment