stav / kingbot

Deno CLI trading robot
https://stav.github.io/kingbot/
2 stars 0 forks source link

Break-even working #20

Closed stav closed 2 years ago

stav commented 2 years ago

Happy Monday, here's what happened when the tip came in today.

stav commented 2 years ago

Log from Telethon:

Got event at 1866" 2022-05-09 13:36:07+00:00

from 1238116885 Balam "XAUUSD SELL ENTRADA: 1868 SL: 1878... <more data trauncated>
stav commented 2 years ago

Log from the bot's Telegram server:

2022-05-09 13:36:08 UTC INFO [tserver] Received payload {
  cid: 1699473616,
  fid: "PeerUser(user_id=1238116885)",
  date: "2022-05-09 13:36:07+00:00",
  eindex: 1,
  msg: "XAUUSD SELL\nENTRADA: 1868\n\nSL: 1878\nTP1: 1866\nTP2: 1864\nTP3: 1861\nTP4: 1858\nTP5: 1853"
}
stav commented 2 years ago

Log from the bot's signal parser:

2022-05-09 13:36:08 UTC INFO [tparser] Parser [Function: GoldParser] "Parsed" 
"Signal" {
  sl: 1878,
  tps: [ 1866, 1864, 1861, 1858, 1853 ],
  type: "SELL",
  entry: 1868,
  volume: 0.01,
  symbol: "GOLD"
}
stav commented 2 years ago

Bot sends in first order:

2022-05-09 13:36:10 UTC INFO [default] Sending {
  command: "tradeTransaction",
  arguments: {
    tradeTransInfo: {
      cmd: 1,
      customComment: "Kingbot Telegram Signal",
      expiration: 1683639370115,
      offset: 0,
      order: 0,
      symbol: "GOLD",
      price: 1868,
      sl: 1878,
      tp: 1866,
      type: 0,
      volume: 0.01
    }
  },
  customTag: "0.4876847247925544 Order 1 of 5"
}
stav commented 2 years ago

First order "accepted" by XTB:

2022-05-09 13:36:10 UTC  Telegram Signal "GOLD" "SELL" "@" 1868 "=" 1866 "order" 384044503 "ACCEPTED" ""
stav commented 2 years ago

Second order accepted:

2022-05-09 13:36:11 UTC  Telegram Signal "GOLD" "SELL" "@" 1868 "=" 1864 "order" 384044519 "ACCEPTED" ""
stav commented 2 years ago

Third order accepted:

2022-05-09 13:36:11 UTC  Telegram Signal "GOLD" "SELL" "@" 1868 "=" 1861 "order" 384044526 "ACCEPTED" ""
stav commented 2 years ago

Fourth order accepted:

2022-05-09 13:36:12 UTC  Telegram Signal "GOLD" "SELL" "@" 1868 "=" 1858 "order" 384044534 "ACCEPTED" ""
stav commented 2 years ago

Fifth order rejected because of not enough margin:

2022-05-09 13:36:13 UTC  Telegram Signal "GOLD" "SELL" "@" 1868 "=" 1853 "order" 384044539 "REJECTED" "Not enough money"
stav commented 2 years ago

One minute later the first take-profit gets closed:

position:    384044429
open time:   09-05-2022 09:36:10
open price:  1866.93
close time:  09-05-2022 09:37:14
close price: 1856.96
volume:      0.01
profit:      4.30

The bot did not register this transaction because I was at my terminal and closed the bot just two (2) seconds before:

09:37:12  I closed the bot
09:37:14  The T/P trade happened
09:37:16  I started the bot back up

What amazing (bad) timing!

stav commented 2 years ago

Four (4) minutes later another take-profit:

2022-05-09 13:40:57 UTC INFO [default] TAKE PROFIT {
  cmd: 1,
  order: 90277327,
  digits: 2,
  offset: 0,
  order2: 384044521,
  position: 384044432,
  symbol: "GOLD",
  comment: "[T/P]",
  customComment: "Kingbot Telegram Signal",
  commission: 0,
  storage: 0,
  margin_rate: 0,
  close_price: 1863.84,
  open_price: 1866.91,
  nominalValue: null,
  profit: 13.62,
  volume: 0.01,
  sl: 1878,
  tp: 1864,
  closed: true,
  type: 2,
  open_time: 1652103370842,
  close_time: 1652103613365,
  expiration: null,
  state: "Modified",
  open_length: "0h4m2s",
  open_time_str: 2022-05-09T13:36:10.842Z,
  close_time_str: 2022-05-09T13:40:13.365Z,
  cmd_field: "SELL",
  type_field: "CLOSE",
  price_diff: 3.07
}
stav commented 2 years ago

The break-even logic now steps into action updating the remaining two (2) orders:

2022-05-09 13:40:58 UTC INFO [default] check1: 7 open trades in total
2022-05-09 13:40:58 UTC INFO [default] check2: 2 in family of GOLD
2022-05-09 13:40:58 UTC INFO [default] STOP LOSS: 1866.35 = 1866.91 + -0.5600729999999999
2022-05-09 13:40:58 UTC INFO [default] Updating stop loss to 1866.35 for 2 orders

Updating the first order:

2022-05-09 13:40:58 UTC INFO [default] Sending {
  command: "tradeTransaction",
  arguments: {
    tradeTransInfo: {
      price: 1867.11,
      cmd: 1,
      order: 384044434,
      offset: 0,
      symbol: "GOLD",
      volume: 0.01,
      sl: 1866.35,
      tp: 1861,
      type: 3
    }
  },
  customTag: "0.30949721957490706"
}

We get back a status true so we got no error, effectively proving the break-even is now working:

2022-05-09 13:40:58 UTC INFO [default] Message '{"status":true,"returnData":{"order":384044434},"customTag":"0.30949721957490706"}'

2022-05-09 13:40:58 UTC INFO [default] Trade { order: 384044434 } "Status" {
  order: 384044434,
  requestStatus: 3,
  message: null,
  customComment: "Kingbot Telegram Signal",
  ask: 1867.11,
  bid: 1867.11
}

Updating the second order:

2022-05-09 13:40:58 UTC INFO [default] Sending {
  command: "tradeTransaction",
  arguments: {
    tradeTransInfo: {
      price: 1866.97,
      cmd: 1,
      order: 384044437,
      offset: 0,
      symbol: "GOLD",
      volume: 0.01,
      sl: 1866.35,
      tp: 1858,
      type: 3
    }
  },
  customTag: "0.3462960954934997"
}

Also no error:

2022-05-09 13:40:58 UTC INFO [default] Message '{"status":true,"returnData":{"order":384044437},"customTag":"0.3462960954934997"}'

2022-05-09 13:40:59 UTC INFO [default] Trade { order: 384044437 } "Status" {
  order: 384044437,
  requestStatus: 3,
  message: null,
  customComment: "Kingbot Telegram Signal",
  ask: 1866.97,
  bid: 1866.97
}
stav commented 2 years ago

The last two (2) orders closed at the break-even ten (10) minutes later:

2022-05-09 13:51:37 UTC INFO [default] Stream message {
  cmd: 1,
  order: 90280274,
  digits: 2,
  offset: 0,
  order2: 384044527,
  position: 384044434,
  symbol: "GOLD",
  comment: "[S/L]",
  customComment: "Kingbot Telegram Signal",
  commission: 0,
  storage: 0,
  margin_rate: 0,
  close_price: 1866.37,
  open_price: 1867.11,
  nominalValue: null,
  profit: 3.29,
  volume: 0.01,
  sl: 1866.35,
  tp: 1861,
  closed: true,
  type: 2,
  open_time: 1652103371432,
  close_time: 1652104297738,
  expiration: null,
  state: "Modified"
}

2022-05-09 13:51:37 UTC INFO [default] Stream message {
  cmd: 1,
  order: 90280275,
  digits: 2,
  offset: 0,
  order2: 384044535,
  position: 384044437,
  symbol: "GOLD",
  comment: "[S/L]",
  customComment: "Kingbot Telegram Signal",
  commission: 0,
  storage: 0,
  margin_rate: 0,
  close_price: 1866.37,
  open_price: 1866.97,
  nominalValue: null,
  profit: 2.67,
  volume: 0.01,
  sl: 1866.35,
  tp: 1858,
  closed: true,
  type: 2,
  open_time: 1652103372045,
  close_time: 1652104297738,
  expiration: null,
  state: "Modified"
}