Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. # Welcome to Cryzen.
  2. # This file is a minimal example to get you started.
  3. # If you have any questions, you can always ask us on Telegram: t.me/cryzen
  4. # Or check out Catalyst's documentation: https://enigma.co/catalyst/
  5. # Happy trading!
  6.  
  7. # Imports
  8. import pandas as pd
  9. import numpy as np
  10. from cryzen import plot
  11.  
  12.  
  13. # Exchange, symbol to trade, and starting capital
  14. EXCHANGE_NAME  = 'binance'
  15. SYMBOL_NAME    = 'eth_usdt'
  16. CAPITAL_BASE   = 1000 # Units in `QUOTE_CURRENCY` below
  17. QUOTE_CURRENCY = 'usdt'
  18.  
  19.  
  20. # Trading frequency, and time range to backtest over
  21. DATA_FREQUENCY = 'daily' # choices: 'minute', or 'daily'
  22. START_TIME     = pd.to_datetime('2018-01-01', utc=True)
  23. END_TIME       = pd.to_datetime('2018-12-31', utc=True)
  24.  
  25. BOUGHT = False
  26. BOUGHT_PRICE = 1
  27.  
  28. # `initialize` runs once before the start of a strategy
  29. # (for both backtests and live deployments).
  30. #
  31. # `context` is a shared variable between the various
  32. # functions in this script.
  33. def initialize(context):
  34.     # `context.asset` can be accessed from other
  35.     # functions involved in this strategy such as `handle_data`
  36.     # Feel free to define `context.anything = whatever`
  37.     context.asset = symbol(SYMBOL_NAME)
  38.  
  39.  
  40. # `handle_data` runs every time there is a new complete candle
  41. # (contained in the `data` argument).
  42. #
  43. # If `DATA_FREQUENCY` above is set to 'minute', `handle_data` will
  44. # get called at the end of every minute; if it is set to 'daily'
  45. # it will get called at the end of every day (UTC time).
  46. def handle_data(context, data):
  47.    
  48.     global BOUGHT, BOUGHT_PRICE
  49.    
  50.     # Get historical data:
  51.     # `data.history(...)` returns a pandas dataframe.
  52.     # Each row in this dataframe represents one candle.
  53.     # Each column corresponds to one of the specified fields:
  54.     # price, open, high, low, close, & volume.
  55.     # Data is sorted from oldest data at the top of the
  56.     # dataframe to newest data at the bottom of the dataframe.
  57.     # The latest candle can be found at the bottom of this dataframe.
  58.     historical_data = data.history(
  59.         context.asset,
  60.         bar_count=7,
  61.         fields=['price', 'open', 'high', 'low', 'close', 'volume'],
  62.         frequency='1D') # frequency can be '1D' for daily, or
  63.                         # '1T' for minute
  64.    
  65.     current_close  = historical_data['close'][-1]
  66.     earliest_close = historical_data['close'][0]
  67.    
  68.     net_change = current_close - earliest_close
  69.     plot(context, net_change=net_change)
  70.    
  71.     potential_profit = (current_close - BOUGHT_PRICE)/BOUGHT_PRICE
  72.    
  73.     if (net_change > 0) and (not BOUGHT):
  74.         order_target_percent(context.asset, 1)
  75.         # This is approximate, not exact:
  76.         BOUGHT_PRICE = current_close
  77.         BOUGHT = True
  78.        
  79.     elif (net_change < 0) and BOUGHT:
  80.         order_target_percent(context.asset, 0)
  81.         BOUGHT = False
  82.                
  83.     # Do something with the data above and trade using: `order_target_percent`
  84.     #
  85.     # For example,
  86.     # order_target_percent(context.asset, .75)
  87.     # will convert 75% of your current portfolio into the given
  88.     # asset at market price (this may entail either a buy or a sell
  89.     # depending on how much of the given asset is presently in your
  90.     # portfolio)
  91.     #
  92.     # Limit orders to be supported soon.
  93.  
  94.  
  95. # `analyze_live` runs on every iteration of a
  96. # live algorithm (does not run for backtests).
  97. def analyze_live(context, results):
  98.     pass
  99.  
  100.  
  101. # `analyze` runs at the end of a job
  102. def analyze(context, results):
  103.     print(results)
  104.     print(results.columns)
  105.     print("Runs after job ends")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement