Skip to content

Commit

Permalink
ADJ timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
ondra-novak committed Jan 23, 2021
1 parent 4742974 commit 251bce9
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
9 changes: 9 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
mmbot (2:21.1.1) unstable; urgency=medium

* hotfix some exchanges doesn't update trade history immediately (new option introduced ADJ timeou)
* improvement: dynamic reduce changed
* improvement: reduction

-- Ondra Novak <[email protected]> Sun, 24 Jan 2021 00:00:00 +0200


mmbot (2:21.1) unstable; urgency=medium

* fdc5bad fix url in datasource, last dir in leveraged strategy can report alert
Expand Down
2 changes: 1 addition & 1 deletion src/brokers/bitfinex/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ IStockApi::TradesSync Interface::syncTrades(json::Value lastId, const std::strin
bool wasAnchor = data[ln][0] == anchor;
out.lastId = {data[ln][0], data[ln][2].getUIntLong()+(wasAnchor?1:0)};
} else {
out.lastId = lastId;
out.lastId = {lastId[0],lastId[1].getUIntLong()+1};
}
std::string spair(pair);
auto fiter = fees.find(spair);
Expand Down
18 changes: 12 additions & 6 deletions src/main/mtrader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void MTrader_Config::loadConfig(json::Value data, bool force_dry_run) {
dynmult_mode = strDynmult_mode[data["dynmult_mode"].getValueOrDefault("half_alternate")];

accept_loss = data["accept_loss"].getValueOrDefault(1);
adj_timeout = data["adj_timeout"].getValueOrDefault(60);
max_leverage = data["max_leverage"].getValueOrDefault(0);
external_balance= data["ext_bal"].getValueOrDefault(0.0);

Expand Down Expand Up @@ -116,7 +117,6 @@ MTrader::MTrader(IStockSelector &stock_selector,
uid = rnd();
}

need_live_balance = strategy.needLiveBalance();

}

Expand Down Expand Up @@ -231,18 +231,18 @@ void MTrader::perform(bool manually) {
buy_alert.reset();
}

if (!anytrades && cfg.enabled
if (!anytrades && cfg.adj_timeout && cfg.enabled
&& std::abs(status.assetBalance - *asset_balance)
/std::abs(status.assetBalance + *asset_balance) > 0.01) {
logNote("Need adjust $1 => $2, stage: $3", *asset_balance, status.assetBalance, adj_wait);
if (adj_wait>59) { //still issue on binance - wait aprox 1 hour before adjst
double last_price = trades.empty()?status.curPrice:trades.back().eff_price;
dorovnani(status, *asset_balance, last_price);
if (adj_wait>cfg.adj_timeout) {
dorovnani(status, *asset_balance, adj_wait_price);
anytrades = processTrades(status);
logNote("Adjust added: result - $1", *asset_balance);
adj_wait = 0;
} else {
if (adj_wait == 0) adj_wait_price = status.curPrice;
adj_wait++;
logNote("Need adjust $1 => $2, stage: $3/$4 price: $5", *asset_balance, status.assetBalance, adj_wait ,cfg.adj_timeout, adj_wait_price);
}
} else {
adj_wait = 0;
Expand Down Expand Up @@ -933,6 +933,8 @@ void MTrader::loadState() {
achieve_mode = state["achieve_mode"].getBool();
need_initial_reset = state["need_initial_reset"].getBool();
swapped = state["swapped"].getBool();
adj_wait = state["adj_wait"].getUInt();
adj_wait_price = state["adj_wait_price"].getNumber();
}
auto chartSect = st["chart"];
if (chartSect.defined()) {
Expand Down Expand Up @@ -1008,6 +1010,8 @@ void MTrader::saveState() {
if (achieve_mode) st.set("achieve_mode", achieve_mode);
if (cfg.swap_symbols) st.set("swapped", cfg.swap_symbols);
if (need_initial_reset) st.set("need_initial_reset", need_initial_reset);
st.set("adj_wait",adj_wait);
if (adj_wait) st.set("adj_wait_price", adj_wait_price);
}
{
auto ch = obj.array("chart");
Expand Down Expand Up @@ -1122,6 +1126,8 @@ void MTrader::clearStats() {
init();
trades.clear();
asset_balance.reset();
adj_wait = 0;
adj_wait_price = 0;
saveState();
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/mtrader.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ struct MTrader_Config {
Dynmult_mode dynmult_mode;

unsigned int accept_loss;
unsigned int adj_timeout;

double force_spread;
double report_position_offset;
Expand Down Expand Up @@ -271,12 +272,12 @@ class MTrader {
bool first_cycle = true;
bool achieve_mode = false;
bool need_initial_reset = true;
int adj_wait = 0;
unsigned int adj_wait = 0;
double adj_wait_price = 0;
double lastPriceOffset = 0;
json::Value test_backup;
json::Value lastTradeId = nullptr;
std::optional<double> sell_alert, buy_alert;
bool need_live_balance;

using TradeItem = IStockApi::Trade;
using TWBItem = IStatSvc::TradeRecord;
Expand All @@ -286,7 +287,6 @@ class MTrader {

std::optional<double> asset_balance;
std::optional<double> currency_balance;
// std::optional<double> currency_unadjusted_balance;

size_t magic = 0;
size_t uid = 0;
Expand Down
3 changes: 3 additions & 0 deletions www/admin/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ App.prototype.fillForm = function (src, trg) {
data.cstep = 0;
data.acum_factor = 0;
data.external_assets = 0;
data.adj_timeout=60
data.kv_valinc = 0;
data.kv_halfhalf=false;
data.st_power={"value":1.7};
Expand Down Expand Up @@ -634,6 +635,7 @@ App.prototype.fillForm = function (src, trg) {
data.zigzag = filledval(src.zigzag,false);
data.max_leverage = filledval(src.max_leverage,10);
data.ext_bal = filledval(src.ext_bal,0);
data.adj_timeout = filledval(src.adj_timeout,60);
data.emul_leverage = filledval(src.emulate_leveraged,0);


Expand Down Expand Up @@ -799,6 +801,7 @@ App.prototype.saveForm = function(form, src) {
trader.force_spread = Math.log(data.force_spread/100+1);
trader.ext_bal = data.ext_bal;
trader.emulate_leveraged = data.emul_leverage;
trader.adj_timeout = data.adj_timeout;
if (isFinite(data.min_balance)) trader.min_balance = data.min_balance;
if (isFinite(data.max_balance)) trader.max_balance = data.max_balance;
return trader;
Expand Down
1 change: 1 addition & 0 deletions www/admin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@
the same order, they are ordered by its ID - this is the default ordering. Modifying this number you can change the default ordering.
</span></span><input type="number" step="any" data-name="report_order"></label>
<label class="adv"><span>Force spread [%](0=disabled)<span class="tooltip">Specifies exact spread in % width and disables the internal spread detection</span></span><input type="number" step="any" data-name="force_spread"></label>
<label class="adv"><span>ADJ trade timeout [min] (0=disabled)<span class="tooltip">Specifies timeout after which a fake (ADJ) trade is put to trade history if there is a difference between calculated position and actual position reported by the broker. This allows to detect deposits and withdraws and report them as a trade, however, on some markets, trading data may be delayed, which causes puting wrong ADJ trades before the updated list of trades is being populated from the market. The value is in minutes, default value is set to 60 = aprx 1 hour. If you have experienced such wrong ADJ trades, you can increase the timeout or disable it by setting this option to 0. Note: The timer is reset after each trade, so setting a large value can cause, that ADJ trade also never appear.</span></span><input type="number" step="any" data-name="adj_timeout"></label>
</x-section>
<div></div>
</x-form>
Expand Down

0 comments on commit 251bce9

Please sign in to comment.