Чрезмерное неожиданное кредитное плечо Zipline - PullRequest
0 голосов
/ 02 ноября 2018

Игра с Zipline и моделирование очень простой стратегии. То, что я не могу понять, это то, почему, когда я строю кредитное плечо, я получаю кредитное плечо больше 1.

from zipline.api import order, record, symbol, set_benchmark, 
order_target, order_target_percent, schedule_function, date_rules, 
time_rules, set_max_leverage, get_open_orders, cancel_order
import zipline
import matplotlib.pyplot as plt
from datetime import datetime
import logging


def initialize(context):
    set_benchmark(symbol("ETH"))
    context.security = symbol('ETH')
    schedule_function(create_bels, date_rules.week_start(),time_rules.market_open(hours=1))
    schedule_function(run_model, date_rules.every_day(), time_rules.market_open(hours=1))




def run_model(context,data):

    average_price = data[context.security].mavg(28)
    current_price = data[context.security].price

    oo = get_open_orders()  
    for o in oo:  
        print(o)   
        cancel_order(o)  

    cash = context.portfolio.cash

    if current_price > 1.01*average_price and cash > current_price:
        order_target(context.security, 0)
    elif current_price < average_price:
        order_target(context.security, 0)


def handle_data(context, data):

    record(stock_price= data[context.security].price)
    record(leverage = context.account.leverage)




perf = zipline.run_algorithm(start=datetime(2017, 1, 5, 0, 0, 0, 0, pytz.utc),
                  end=datetime(2018, 3, 1, 0, 0, 0, 0, pytz.utc),
                  initialize=initialize,
                  capital_base=1000,
                  handle_data=handle_data,
                  data=panel)

perf.leverage.plot()

plt.show()

На этом графике показаны несколько периодов с плечом выше 1, но из этого кода я бы ожидал, что плечо переместится между 0 (нет позиции) и 1 (полная позиция). Есть идеи, почему это происходит?

...