Coder Social home page Coder Social logo

bfx-hf-strategy's Introduction

Bitfinex Honey Framework Trading Strategy Library for Node.JS

Build Status

This repo serves as a framework for creating trading bots/strategies on the Bitfinex platform. It consists of a set of order methods and an architecture compatible with bfx-hf-data-server and bfx-hf-backtest for backtests on historical candle/trade data, which can be transitioned seamlessly to trading on the live markets.

Strategies written using this framework must define a set of update methods, called on each tick (with either a trade or a candle), along with a set of indicators which are automatically updated on each tick. The indicators are made available to the strategy methods, and can be queried to direct trading behavior.

Features

  • Event-driven design approach allowing strategies to react to market updates in real-time
  • Compatibility with bfx-hf-backtest for backtest execution
  • Compatibility with bfx-hf-strategy-exec for execution on live markets

Installation

npm i --save bfx-hf-strategy

Quickstart & Example

Using bfx-hf-stratey implies writing a custom strategy utilizing the methods provided by the library. The following is an example of a valid strategy as defined within examples/macd_cross:

const { MACD } = require('bfx-hf-indicators')
const { SYMBOLS, TIME_FRAMES } = require('bfx-hf-util')
const HFS = require('bfx-hf-strategy')

module.exports = ({
  symbol = SYMBOLS.BTC_USD,
  tf = TIME_FRAMES.ONE_HOUR
} = {}) => HFS.define({
  id: 'quickstart_example',
  name: 'quickstart_example',
  symbol,
  tf,

  indicators: {
    macd: new MACD([10, 26, 9])
  },

  // This quickstart example immediately opens a long position, and then no
  // longer reacts to future market updates
  onPriceUpdate: async (state = {}, update = {}) => {
    const position = HFS.getPosition(state)

    if (position) {
      return state
    }

    return HFS.openLongPositionMarket(state, {
      mtsCreate: mts,
      amount: 1,
      price
    })
  }
})

Docs

Refer to docs/usage.md for an overview of the strategy system and methods available at runtime, and docs/api.md for JSDoc-generated API documentation.

Ready to run examples can be found in the examples/ folder

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Note

This package will be maintained only via github, please use latest relases from github instead of npm.

Example on how to install specific version from github:

npm i --save-prod https://github.com/bitfinexcom/bfx-hf-strategy.git#v1.1.1

Example on how to install it latest version from github:

npm i --save-prod https://github.com/bitfinexcom/bfx-hf-strategy.git

bfx-hf-strategy's People

Contributors

avsek477 avatar f3rno avatar jacobplaster avatar lecss avatar prdn avatar robertkowalski avatar tarcisiozf avatar tekwani avatar vigan-abd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bfx-hf-strategy's Issues

Fix: Take fee currency into account

Fees for trades are blindly summed together to track total fees over a strategy's lifetime without taking into account the fee currency. In preparation for supporting multiple markets & symbols, the fee's currency needs to be accounted for and the fee value converted to a user-specified fee currency (maybe default to USD).

This requires fetching price data, likely something that will be provided by the new multi-market system.

Examples of fee usage:

Feature: Add Tests

The test folder currently contains a stub so npm test passes; this needs to be filled in/expanded.

Feature: Extract fee definition from internals

The architecture needs to be refactored to support custom fees for internal calculations. The responsibility of acquiring an own user's maker/taker fees & passing them to bfx-hf-strategy should be deferred to the library consumer.

State positions are emptied - onCandleUpdate returns the initial state instead of the updated state

Issue type

  • bug
  • missing functionality
  • performance
  • feature request

Brief description

This bug can be seen on using live data stream, with the bfx-hf-strategy-exec. When a new candle is received, upon processing the candle update message and a trade is executed, the state position object is overridden by the previous empty positions state;

Steps to reproduce

  • run with bfx-hf-strategy-exec, place a trade from either onUpdate or onEnter. Position state is updated temporarily, then on next candle_update it is reverted to empty object;
Additional Notes:

Cannot submit order with client id = nonce()

Issue type

  • bug
  • missing functionality
  • performance
  • feature request

Brief description

Steps to reproduce

Additional Notes:
  • submit_order.js:
    invalid submit order when nonce() is using as order.cid
    If order.cid=Date.now() then order will be successfuly submitted

bfx-hf-strategy-2.1.1/examples/exec_ema_cross.js broken?

Issue type

  • [x ] bug
  • missing functionality
  • performance
  • feature request

Brief description

It seems to break on its own example.

My-iMac:backtest-with-bitfinex-honey me$ node ~/src/bfx-hf-strategy-2.1.1/examples/exec_ema_cross.js
/Users/me/src/bfx-hf-strategy-2.1.1/lib/orders/simulate_order_fill.js:22
const authorizationError = perfManager.canOpenOrder(amount, price || priceFeed.price)
^

TypeError: Cannot read properties of undefined (reading 'canOpenOrder')
at module.exports (/Users/me/src/bfx-hf-strategy-2.1.1/lib/orders/simulate_order_fill.js:22:42)
at module.exports (/Users/me/src/bfx-hf-strategy-2.1.1/lib/orders/submit_trade.js:33:13)
at openPositionWithOrder (/Users/me/src/bfx-hf-strategy-2.1.1/lib/position/open_position_with_order.js:43:34)
at openPosition (/Users/me/src/bfx-hf-strategy-2.1.1/lib/position/open_position.js:14:10)
at openPositionMarket (/Users/me/src/bfx-hf-strategy-2.1.1/lib/position/open_position_market.js:16:10)
at Object.openLongPositionMarket (/Users/me/src/bfx-hf-strategy-2.1.1/lib/position/open_long_position_market.js:13:10)
at module.exports (/Users/me/src/bfx-hf-strategy-2.1.1/examples/ema_cross/on_enter.js:15:18)
at /Users/me/src/bfx-hf-strategy-2.1.1/lib/updates/on_price_update.js:46:23
at module.exports (/Users/me/src/bfx-hf-strategy-2.1.1/lib/position/with_no_position.js:18:12)
at onPriceUpdate (/Users/me/src/bfx-hf-strategy-2.1.1/lib/updates/on_price_update.js:45:9)
at Object.onCandle (/Users/me/src/bfx-hf-strategy-2.1.1/lib/updates/on_candle.js:32:23)
at run (/Users/me/src/bfx-hf-strategy-2.1.1/examples/exec_ema_cross.js:32:28)

Steps to reproduce

node ~/src/bfx-hf-strategy-2.1.1/examples/exec_ema_cross.js

Additional Notes:

I believe I am running the latest versions of the various bfx-hf node.js stuff, as available in gitlab.

Alway throw error order not fully filled, refusing to create trade (${amount}/${amountOrig})

Issue type

  • bug
  • missing functionality
  • performance
  • feature request

Brief description

in Bitfinex new order documentation:
https://docs.bitfinex.com/reference/ws-auth-input-order-new

amount always !== 0
image

but in code trade_for_order.js

  if (amount !== 0) {
    throw new Error(
      `order not fully filled, refusing to create trade (${amount}/${amountOrig})`
    )
  }

that lead to bfx-hf-strategy always throw error

Steps to reproduce

Additional Notes:

How to create OCO oder for backtesting?

Hey,
i'm new to automated trading as well as node.js.

I folled the implementation from hackernoon.com which manually fetches candles and passes each onto HFS.onCandle(strategyState, candles[i]).

I managed to create a market order using HFS.openLongPositionMarket(). Additionally to this opened position i also want to create a OCO order. But this should of course only be filled if either the limit price was reached & filled or the stop has triggered a market oder.

I tried doing this right after creating the market order with

const positionOCO = await HFS.closePositionLimit(positionShortState, {
     mtsCreate: update.candle.mts,
     oco: true,
     price: priceTarget,
     priceAuxLimit: priceStop,
 });

but this immeditately fills the order at the same time.

Am i doing a conceptual mistake and misunderstand how HF should be used, or do i need to call some other method?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.