robswc / tradingview-webhooks-bot

a framework 🏗 for trading with tradingview webhooks!
http://tvwb.robswc.me
GNU General Public License v3.0
587 stars 180 forks source link

Feature request: Different types of orders #13

Open JDJoe opened 4 years ago

JDJoe commented 4 years ago
  1. Order size based on % of available funds. Example: free funds € 1000, order LTC/EUR, size 10%: order €100 I personally like to buy round numbers and avoid buying "dust" so maybe we should have some type of rounding to closest 0.5 or something too.
  2. Cancel orders placed by the bot
  3. Margin trade (define margin level) with the order
  4. Stop loss
  5. Take profit
JDJoe commented 4 years ago
  1. Margin trade (define margin level) with the order

in actions.py, you change the order line to: order = exchange.create_order(data['symbol'], data['type'], data['side'], data['amount'], calc_price(data['price']), { 'leverage': data["leverage"] })

JDJoe commented 4 years ago
  1. Stop loss order This works and also with margin.

This is how I used it in "Tradingview alert message:

{"type": "stop-loss", "side": "sell", "amount": "100", "symbol": "BCH/EUR", "price": "321", "leverage": "2", "key": "yourkeyhere"}

LPX55 commented 4 years ago

@JDJoe https://github.com/Robswc/tradingview-webhooks-bot/tree/lda/webhook-bot

JDJoe commented 4 years ago

Interesting. BTW, I can barely read code (I am not a programmer) but do I understand it correctly, you have moved the stop loss calculation to the bot side?

Not sure, if this is a good idea because this information should come from the strategy running on TradingView and it can be different for trading pairs. For example, I use volatility to calculate the possible SL and it's not a % percentage from last price.

I personally hope this bot remains as dumb as possible - only facilitate outside commands.

Maybe, in the future, it should be able to check did your limit order get filled and if not, do something about it.

LPX55 commented 4 years ago

@JDJoe At the time I wrote it PineScript was still very limited in what info it can pass on (variables). If you look closely you'll see a file called talib.py which I never got around to. Had some personal issues to deal with the past 3 months. Ideally you'd want to calculate stop losses server-side, as even a few seconds can make a huge difference. The hard part is getting a constant stream of reliable data without any downtime, which TV is particularly good at. There's many ways to approach this I guess

robswc commented 4 years ago

Most of the requests here can be done within the bot's "send order" action (as it was intended) BUT I am working on a branch that will allow for "blueprints" of sorts. It's been awhile since I worked with this bot, so I might get it wrong but for the following:

Stop Loss & Take Profit:

Should be possible by setting it to "StopLimit" or "StopMarket". Take profit can be done with a limit order, set to POST only, or "ParticipateDoNotInitiate" in the custom parameters. It should be possible, something like this:

order = exchange.create_order(data['symbol'], data['type'], data['side'], data['amount'], calc_price(data['price']), params={'execInst': 'ParticipateDoNotInitiate'})

The bot is basically a ccxt trigger that runs when it gets a TV alert.

I could add the ability to cancel orders made by the bot, I would say this might not be totally efficient though, as it would have to grab the order ID from the exchange, then there would also have to be a way for the alert to know which order you're trying to cancel. It could be "last order" or "3rd from last" etc. Beyond that though would be a bit difficult.

You could do multiple orders even, if you want. Getting price:

exchange.fetch_ticker('XBTUSD')) for example. Then just run your stop/profit formula, and copy the "order = exchange..." to create 3 orders, essentially a bracket order.

Let me know how it works out!

JDJoe commented 4 years ago
  1. Take profit

You can do this right now if you use limit order. This will give you a little kickback on fees too. So, When going short, place limit buy order at what ever level below the current market and opposite for the long position - limit sell at something above the market.

To keep it simple: When entering a new order:

  1. Cancel all open orders for the symbol XYZ (get rid of your previously placed SL, TP etc)
  2. Place your new order for symbol XYZ (market, limit, stop)
  3. Place new take profit ((stop) limit sell or buy) for XYZ at some price level.

What is really important is to execute all this in one single TradingView message so it can be processed in correct order. Because the "key" is really long, TV wont be able to handle 3 x key + orders in one message.

robswc commented 4 years ago

Ah, I see. I'm currently working on a new version that allows for placing a take profit and stop with one alert, it might only work with market orders to start though.

In the mean time, try seeing if this works:

    order1 = exchange.create_order(data['symbol'], data['type'], data['side'], data['amount'], calc_price(data['price']))
    order2 = exchange.create_order(data['symbol'], 'stopLoss', ('sell' if data['side'] == 'buy' else 'buy'), data['amount'], data['takeProfitPrice']))
    order3 = exchange.create_order(data['symbol'], 'takeProfit', ('sell' if data['side'] == 'buy' else 'buy'), data['amount'], data['stopLossPrice']))

Off the top of my head, so it might not work.

Just add the "takeProfitPrice": "{{something}}"

In the tradingview alert to get that data to the bot.

JDJoe commented 4 years ago

I'm currently working on a new version that allows for placing a take profit and stop with one alert

Sounds good. I'll be waiting. I'll be more than happy to help you test it. Stop and take profit already works but there is no way to submit multiple orders at once.

JDJoe commented 4 years ago

How's the new version looking? :)

JDJoe commented 4 years ago

Any news about the new version?