chrisleekr / binance-trading-bot

Automated Binance trading bot - Trade multiple cryptocurrencies. Buy low/sell high with Grid Trading. Integrated with TradingView technical analysis
MIT License
5k stars 1.09k forks source link

Need help on how the bot work #230

Closed thangphamnd123 closed 3 years ago

thangphamnd123 commented 3 years ago

Hi all,

I am struggling to understand how the bot work. I quote the guide here and add my comment. Please help me to get through the ideas. I appreciate your help.

Buy Scenario Let say, if the buy configurations are set as below: • Maximum purchase amount: $50 • Trigger percentage: 1.005 (0.5%) • Stop price percentage: 1.01 (1.0%) • Limit price percentage: 1.011 (1.1%) And the market is as below: • Current price: $101 • Lowest price: $100 • Trigger price: $100.5 (How to calculate this price? If Trigger = Current * Trigger Percentage then it must be 101.505) Then the bot will not place an order because the trigger price ($100.5) is less than the current price ($101). In the next tick, the market changes as below: • Current price: $100 • Lowest price: $100 • Trigger price: $100.5 The bot will place new STOP-LOSS-LIMIT order for buying because the current price ($100) is less than the trigger price ($100.5). For the simple calculation, I do not take an account for the commission. In real trading, the quantity may be different. The new buy order will be placed as below: • Stop price: $100 1.01 = $101 • Limit price: $100 1.011 = $101.1 • Quantity: 0.49 (How to calculate this quantity here?)* In the next tick, the market changes as below: • Current price: $99 • Current limit price: $99 1.011 = 100.089 • Open order stop price: $101 As the open order's stop price ($101) is higher than the current limit price ($100.089), the bot will cancel the open order and place new STOP-LOSS-LIMIT order as below: (Why in here it uses previous stop price to compare with current limit price instead of trigger price?)* • Stop price: $99 1.01 = $99.99 • Limit price: $99 * 1.011 = $100.089 • Quantity: 0.49 If the price continuously falls, then the new buy order will be placed with the new price. And if the market changes as below in the next tick: • Current price: $100 Then the current price reaches the stop price ($99.99); hence, the order will be executed with the limit price ($100.089).

SELL: Let say, if the sell configurations are set as below: • Trigger percentage: 1.05 (5.0%) • Stop price percentage: 0.98 (-2.0%) • Limit price percentage: 0.979 (-2.1%) And the market is as below: • Coin owned: 0.5 • Current price: $100 • Last buy price: $100 • Trigger price: $100 1.05 = $105 Then the bot will not place an order because the trigger price ($105) is higher than the current price ($100). If the price is continuously falling, then the bot will keep monitoring until the price reaches the trigger price. In the next tick, the market changes as below: • Current price: $105 • Trigger price: $105 The bot will place new STOP-LOSS-LIMIT order for selling because the current price ($105) is higher or equal than the trigger price ($105). For the simple calculation, I do not take an account for the commission. In real trading, the quantity may be different. The new sell order will be placed as below: • Stop price: $105 0.98 = $102.9 • Limit price: $105 0.979 = $102.795 • Quantity: 0.5 In the next tick, the market changes as below: • Current price: $106 • Current limit price: $103.774 • Open order stop price: $102.29 Why the limit price is 103.774 and stop price is 102.29? Where is the value come from? As the open order's stop price ($102.29) is less than the current limit price ($103.774), the bot will cancel the open order and place new STOP-LOSS-LIMIT order as below: • Stop price: $106 0.98 = $103.88 • Limit price: $106 * 0.979 = $103.774 • Quantity: 0.5 If the price continuously rises, then the new sell order will be placed with the new price. And if the market changes as below in the next tick: • Current price: $103 The current price reaches the stop price ($103.88); hence, the order will be executed with the limit price ($103.774).

chrisleekr commented 3 years ago

Hi @thangphamnd123

I am struggling to understand how the bot work. I quote the guide here and add my comment. Please help me to get through the ideas. I appreciate your help.

Hmmm, where to start. I think you better do some calculation to find out how it works. Or try with a small amount like $15-$20.

The best way is to read the code.

• Trigger price: $100.5 (How to calculate this price? If Trigger = Current * Trigger Percentage then it must be 101.505)

Trigger price is based on your setting. As the trigger percentage is 1.005(0.5%), so if the lowest price is $100, the buy order will be placed after the current price reaching $100 * 1.005 = $100.5.

• Quantity: 0.49 (How to calculate this quantity here?)

The setting for maximum purchase amount is $50. As a result, if the coin is purchased, it would be about 0.49.

(Why in here it uses previous stop price to compare with current limit price instead of trigger price?)

Once the buy order is placed, then the bot will use the current limit price to determine whether the current price is falling or rising.

Why the limit price is 103.774 and stop price is 102.29? Where is the value come from?

The current limit price is calculated with the current price. The current price changes, then the current limit price is also changing.

I tried my best to explain, once you try, then you will get lots of slack messages with the calculated values are.

thangphamnd123 commented 3 years ago

Thank you @chrisleekr for your response.

I took a look at 2 file you mentioned but I still can not find the logic that we're discussing about.

  1. Can I confirm this formula on buy scenario : trigger price = lowest price trigger percent ? And on sell side trigger is calculated the same or it equal highest price trigger percent ?
  2. Trigger price is only use once time to trigger the 1st buy/sell limit order? After 1st buy/sell order is made then if current limit price < last stop price then bot place new order ( buy side ) and reverse on sell side: current limit price > last stop price then place new order. Is this correct?
chrisleekr commented 3 years ago

@thangphamnd123

Can I confirm this formula on buy scenario : trigger price = lowest price * trigger percent Yes

And on sell side trigger is calculated the same or it equal highest price trigger percent ? Sell trigger price is based on the last buy price sell trigger percentage

Trigger price is only use once time to trigger the 1st buy/sell limit order?

The bot does not mean to execute multiple orders. It cancels previous order and place new order.

After 1st buy/sell order is made then if current limit price < last stop price then bot place new order ( buy side ) and reverse on sell side: current limit price > last stop price then place new order. Is this correct?

Code for handling open orders is here: