Esempio n. 1
0
def load_from_cli(
    market_indicators,
    machine_learning_models,
    dataset,
    columns,
    data_indicators,
    json_file,
    python_script,
):
    strat = Strategy()

    if python_script is not None:
        strat = get_strat(python_script)

    columns = list(columns)

    for i in market_indicators:
        click.echo(f"Attaching indicator: {i.upper()}")
        strat.add_market_indicator(i.upper())

    for i in machine_learning_models:
        click.echo(f"Attaching ml model: {i.upper()}")
        strat.add_ml_models(i.upper())

    # currently assigns -i indicator to the column provided at the same index
    if dataset is not None:
        click.echo(f"Using dataset: {dataset}")
        strat.use_dataset(dataset, columns)
        for i, ind in enumerate(data_indicators):
            strat.add_data_indicator(dataset, ind.upper(), col=columns[i])

    if json_file is not None:
        click.echo(f"Loading from json file {json_file}")
        strat.load_json_file(json_file)

    click.secho(strat.serialize(), fg="white")
    return strat
Esempio n. 2
0
    log.info("buying position cheaper than cost basis {} < {}".format(
        context.price, context.cost_basis))
    order(
        asset=context.asset,
        amount=context.buy_increment,
        limit_price=context.price * (1 + context.SLIPPAGE_ALLOWED),
    )


@strat.sell_order
def sell(context):
    profit = (context.price * context.position.amount) - (
        context.cost_basis * context.position.amount)
    log.info("closing position, taking profit: {}".format(profit))
    order_target_percent(asset=context.asset,
                         target=0,
                         limit_price=context.price *
                         (1 - context.SLIPPAGE_ALLOWED))


@strat.analyze()
def analyze(context, results, pos):
    ending_cash = results.cash[-1]
    log.info("Ending cash: ${}".format(ending_cash))
    log.info("Completed for {} trading periods".format(context.i))


if __name__ == "__main__":
    log.info("Strategy Schema:\n{}".format(strat.serialize()))
    strat.run()
Esempio n. 3
0
# Note that minute data is not supported for external datasets
# strat.trading_info['CAPITAL_BASE'] = 10000
# strat.trading_info['DATA_FREQ'] = 'minute'
# strat.trading_info['HISTORY_FREQ'] = '1m'
# strat.trading_info['START'] = '2017-12-10'
# strat.trading_info['END'] = '2017-12-11'


@strat.init
def init(context):
    log.info('Algo is being initialzed, setting up context')
    context.i = 0


@strat.handle_data
def handle_data(context, data):
    log.debug('Processing new trading step')
    context.i += 1


@strat.analyze()
def analyze(context, results, pos):
    ending_cash = results.cash[-1]
    log.info('Ending cash: ${}'.format(ending_cash))
    log.info('Completed for {} trading periods'.format(context.i))


if __name__ == '__main__':
    log.info('Strategy Schema:\n{}'.format(strat.serialize()))
    strat.run()
Esempio n. 4
0
def signal_sell(context, data):
    return utils.cross_below(sma_fast.outputs.SMA_FAST,
                             sma_slow.outputs.SMA_SLOW)


@strat.signal_buy(override=True)
def signal_buy(context, data):
    return utils.cross_above(sma_fast.outputs.SMA_FAST,
                             sma_slow.outputs.SMA_SLOW)


@strat.analyze(num_plots=1)
def extra_plot(context, results, pos):
    viz.plot_column(results,
                    'SMA_FAST',
                    pos,
                    label='Fast',
                    y_label='Crossover')
    viz.plot_column(results,
                    'SMA_SLOW',
                    pos,
                    label='Slow',
                    y_label='Crossover')
    viz.plot_column(results, 'price', pos, y_label='price', linestyle="--")
    plt.legend()


if __name__ == '__main__':
    print('Strategy:\n{}'.format(strat.serialize()))
    strat.run()