示例#1
0
def historic_run():
    length = 400
    price = price_history.get_prices()[-length:]
    quick_ma_f = decisions.sma_gen(9)
    slow_ma_f = decisions.sma_gen(24)
    open_percent = 0.8
    close_percent = 0.5
    t, money, position, close_t, close_profit_loss_rate = simulate(price, quick_ma=quick_ma_f, slow_ma=slow_ma_f, open_threshold_ratio=open_percent/100, close_threshold_ratio=close_percent/100)

    quick_ma = moving_average_values(price, quick_ma_f)
    slow_ma = moving_average_values(price, slow_ma_f)

    matplotlib.style.use('ggplot')
    fig, (p1, p2) = plt.subplots(2, 1, sharex=True)
    p1.plot(t, price, 'k-', linewidth=2)
    p1.plot(t, quick_ma, 'r-', linewidth=1)
    p1.plot(t, slow_ma, 'g-', linewidth=1)

    p1.add_collection(MpCollections.BrokenBarHCollection.span_where(t, ymin=0, ymax=10000, where=position > 0, facecolor='green', alpha=0.1))
    p1.add_collection(MpCollections.BrokenBarHCollection.span_where(t, ymin=0, ymax=10000, where=position < 0, facecolor='red', alpha=0.1))

    # p2_1 = p2.twinx()
    p2.plot(close_t, money, 'b-')
    money_prev = [1] + money[:-1]
    p2.bar(
        close_t,
        (np.array(close_profit_loss_rate) - 1) * money_prev,
        bottom=money_prev,
        width=10,
        color=['r' if pl < 1 else 'g' for pl in close_profit_loss_rate],
        alpha=1)

    plt.show()
示例#2
0
def tune_run():
    length = 400
    y_range = x_range = np.linspace(0.01, 1.5, 10)

    prices = price_history.get_prices()[-length:]
    x, y, gains, losses = zip(
        *[
            _tuple(
                x,
                y,
                *simulate_money(
                    prices,
                    quick_ma=decisions.sma_gen(quick_sma),
                    slow_ma=decisions.sma_gen(slow_sma),
                    open_threshold_ratio=open_percent / 100,
                    close_threshold_ratio=close_percent / 100,
                )
            )
            for x in x_range
            for y in y_range
            for open_percent in [x]
            for close_percent in [y]
            for quick_sma in [9]
            for slow_sma in [24]
            if quick_sma < slow_sma
            if close_percent <= open_percent
        ]
    )

    matplotlib.style.use("ggplot")
    fig, (p1) = plt.subplots(1, 1, sharex=True)

    for x_, y_, z_ in zip(x, y, gains):
        p1.scatter([x_], [y_], s=(z_ * 1000))
    for x_, y_, z_ in zip(x, y, losses):
        p1.scatter([x_], [y_], s=(z_ * 1000), c="r", alpha=0.5)
    for x_, y_, gain, loss in zip(x, y, gains, losses):
        p1.annotate(
            # '+{}%\n-{}%\n={}%'.format(int(gain * 100), int(loss * 100), int((gain - loss) * 100)),
            "*{}".format(round(gain * loss, 1)),
            (x_, y_),
            color="w",
            horizontalalignment="center",
            verticalalignment="center",
            fontweight="bold",
        )

    plt.show()