Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Welcome to Cryzen.
- # This file is a minimal example to get you started.
- # If you have any questions, you can always ask us on Telegram: t.me/cryzen
- # Or check out Catalyst's documentation: https://enigma.co/catalyst/
- # Happy trading!
- # Imports
- import pandas as pd
- import numpy as np
- from cryzen import plot
- # Exchange, symbol to trade, and starting capital
- EXCHANGE_NAME = 'binance'
- SYMBOL_NAME = 'eth_usdt'
- CAPITAL_BASE = 1000 # Units in `QUOTE_CURRENCY` below
- QUOTE_CURRENCY = 'usdt'
- # Trading frequency, and time range to backtest over
- DATA_FREQUENCY = 'daily' # choices: 'minute', or 'daily'
- START_TIME = pd.to_datetime('2018-01-01', utc=True)
- END_TIME = pd.to_datetime('2018-12-31', utc=True)
- BOUGHT = False
- BOUGHT_PRICE = 1
- # `initialize` runs once before the start of a strategy
- # (for both backtests and live deployments).
- #
- # `context` is a shared variable between the various
- # functions in this script.
- def initialize(context):
- # `context.asset` can be accessed from other
- # functions involved in this strategy such as `handle_data`
- # Feel free to define `context.anything = whatever`
- context.asset = symbol(SYMBOL_NAME)
- # `handle_data` runs every time there is a new complete candle
- # (contained in the `data` argument).
- #
- # If `DATA_FREQUENCY` above is set to 'minute', `handle_data` will
- # get called at the end of every minute; if it is set to 'daily'
- # it will get called at the end of every day (UTC time).
- def handle_data(context, data):
- global BOUGHT, BOUGHT_PRICE
- # Get historical data:
- # `data.history(...)` returns a pandas dataframe.
- # Each row in this dataframe represents one candle.
- # Each column corresponds to one of the specified fields:
- # price, open, high, low, close, & volume.
- # Data is sorted from oldest data at the top of the
- # dataframe to newest data at the bottom of the dataframe.
- # The latest candle can be found at the bottom of this dataframe.
- historical_data = data.history(
- context.asset,
- bar_count=7,
- fields=['price', 'open', 'high', 'low', 'close', 'volume'],
- frequency='1D') # frequency can be '1D' for daily, or
- # '1T' for minute
- current_close = historical_data['close'][-1]
- earliest_close = historical_data['close'][0]
- net_change = current_close - earliest_close
- plot(context, net_change=net_change)
- potential_profit = (current_close - BOUGHT_PRICE)/BOUGHT_PRICE
- if (net_change > 0) and (not BOUGHT):
- order_target_percent(context.asset, 1)
- # This is approximate, not exact:
- BOUGHT_PRICE = current_close
- BOUGHT = True
- elif (net_change < 0) and BOUGHT:
- order_target_percent(context.asset, 0)
- BOUGHT = False
- # Do something with the data above and trade using: `order_target_percent`
- #
- # For example,
- # order_target_percent(context.asset, .75)
- # will convert 75% of your current portfolio into the given
- # asset at market price (this may entail either a buy or a sell
- # depending on how much of the given asset is presently in your
- # portfolio)
- #
- # Limit orders to be supported soon.
- # `analyze_live` runs on every iteration of a
- # live algorithm (does not run for backtests).
- def analyze_live(context, results):
- pass
- # `analyze` runs at the end of a job
- def analyze(context, results):
- print(results)
- print(results.columns)
- print("Runs after job ends")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement