-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreshold
80 lines (73 loc) · 3.5 KB
/
Threshold
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
74
75
76
77
78
79
80
from __future__ import division
import os
import pytz
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
from catalyst.api import order,record, symbols, order_target_percent,schedule_function,date_rules,time_rules
from catalyst.utils.run_algo import run_algorithm
from catalyst.exchange.utils.stats_utils import extract_transactions
def initialize(context):
# Portfolio assetss list
context.assets = symbols('btc_usd', 'eth_usd', 'ltc_usd', 'xrp_usd',
'xmr_usd')
context.bought = False
risk_level = 5
risk_based_allocation = {0: (0.2,0.2,0.2,0.2,0.2),
1: (0.2,0.2,0.2,0.2,0.2),
2: (0.2,0.2,0.2,0.2,0.2),
3: (0.2,0.2,0.2,0.2,0.2),
4: (0.2,0.2,0.2,0.2,0.2),
5: (0.198,0.198,0.198,0.198,0.198),
6: (0.2,0.2,0.2,0.2,0.2),
7: (0.2,0.2,0.2,0.2,0.2),
8: (0.2,0.2,0.2,0.2,0.2),
9: (0.2,0.2,0.2,0.2,0.2),
10: (0.2,0.2,0.2,0.2,0.2)}
#Saves the weights to easily access during rebalance
context.target_allocation = dict(zip(context.assets, risk_based_allocation[risk_level]))
schedule_function(rebalanceInit, date_rules.every_day(),time_rules.every_minute())
#Same as periodic threshold
#schedule_function(rebalanceInit, date_rules.week_start(days_offset=3),time_rules.market_open(minutes=480))
context.i=0
context.base_price = None
def rebalanceInit(context, data):
#total value of portfolio
value = context.portfolio.portfolio_value
for asset in context.assets:
if (context.target_allocation[asset] == 0):
continue
current_holdings = data.current(asset,'price') * context.portfolio.positions[asset].amount
weight = current_holdings/value
growth = float(weight) / float(context.target_allocation[asset])
#if weights of any position exceed threshold, trigger rebalance
if (growth >= 1.05 or growth <= 0.95):
rebalance(context, data)
break
print("No need to rebalance!")
def rebalance(context, data):
for asset in context.assets:
current_weight = (data.current(asset, 'price') * context.portfolio.positions[asset].amount) / context.portfolio.portfolio_value
target_weight = context.target_allocation[asset]
distance = current_weight - target_weight
if (distance > 0):
amount = -1 * (distance * context.portfolio.portfolio_value) / data.current(asset,'price')
if (amount == 0):
continue
print("Selling " + str(amount) + " shares of " + str(asset))
order(asset, amount)
for asset in context.assets:
current_weight = (data.current(asset, 'price') * context.portfolio.positions[asset].amount) / context.portfolio.portfolio_value
target_weight = context.target_allocation[asset]
distance = current_weight - target_weight
if (distance < 0):
amount = -1 * (distance * context.portfolio.portfolio_value) / data.current(asset,'price')
if (amount == 0):
continue
print("Buying " + str(amount) + " shares of " + str(asset))
order(asset, amount)
'''
for asset in context.assets:
order_target_percent(asset, context.target_allocation[asset])
'''