Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Customise the last buy price removing threshold #206

Merged
merged 19 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ data
log

.env
.vs
24 changes: 14 additions & 10 deletions app/cronjob/trailingTrade/step/determine-action.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,17 @@ const canSell = data => {
symbolInfo: {
filterMinNotional: { minNotional }
},
symbolConfiguration: {
buy: { lastBuyPriceRemoveThreshold }
},
baseAssetBalance: { total: baseAssetTotalBalance },
sell: { currentPrice: sellCurrentPrice, lastBuyPrice }
} = data;

return (
lastBuyPrice > 0 &&
baseAssetTotalBalance * sellCurrentPrice > parseFloat(minNotional)
baseAssetTotalBalance * sellCurrentPrice > parseFloat(minNotional) &&
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested manually, this line does not work. You gotta remove the below line because the bot is now relying on the lastBuyPriceRemoveThreshold

baseAssetTotalBalance * sellCurrentPrice > parseFloat(minNotional) &&

You can see from the below screenshot, the bot didn't remove the price, it still determines the action as sell-wait.

image

baseAssetTotalBalance * sellCurrentPrice > lastBuyPriceRemoveThreshold
);
};

Expand Down Expand Up @@ -176,9 +180,9 @@ const execute = async (logger, rawData) => {
data,
'wait',
`The current price reached the trigger price. ` +
`But you have enough ${baseAsset} to sell. ` +
`Set the last buy price to start selling. ` +
`Do not process buy.`
`But you have enough ${baseAsset} to sell. ` +
`Set the last buy price to start selling. ` +
`Do not process buy.`
);
}

Expand All @@ -193,8 +197,8 @@ const execute = async (logger, rawData) => {
data,
'buy-temporary-disabled',
'The current price reached the trigger price. ' +
`However, the action is temporarily disabled by ${checkDisable.disabledBy}. ` +
`Resume buy process after ${checkDisable.ttl}s.`
`However, the action is temporarily disabled by ${checkDisable.disabledBy}. ` +
`Resume buy process after ${checkDisable.ttl}s.`
);
}

Expand Down Expand Up @@ -223,8 +227,8 @@ const execute = async (logger, rawData) => {
data,
'sell-temporary-disabled',
'The current price is reached the sell trigger price. ' +
`However, the action is temporarily disabled by ${checkDisable.disabledBy}. ` +
`Resume sell process after ${checkDisable.ttl}s.`
`However, the action is temporarily disabled by ${checkDisable.disabledBy}. ` +
`Resume sell process after ${checkDisable.ttl}s.`
);
}
// Then sell
Expand All @@ -247,8 +251,8 @@ const execute = async (logger, rawData) => {
data,
'sell-temporary-disabled',
'The current price is reached the stop-loss price. ' +
`However, the action is temporarily disabled by ${checkDisable.disabledBy}. ` +
`Resume sell process after ${checkDisable.ttl}s.`
`However, the action is temporarily disabled by ${checkDisable.disabledBy}. ` +
`Resume sell process after ${checkDisable.ttl}s.`
);
}
// Then sell market order
Expand Down
19 changes: 11 additions & 8 deletions app/cronjob/trailingTrade/step/remove-last-buy-price.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ const {
getAPILimit,
isActionDisabled
} = require('../../trailingTradeHelper/common');

/**
* Retrieve last buy order from cache
*
* @param {*} logger
* @param {*} symbol
* @returns
*/

const getLastBuyOrder = async (logger, symbol) => {
const cachedLastBuyOrder =
JSON.parse(await cache.get(`${symbol}-last-buy-order`)) || {};
Expand Down Expand Up @@ -45,12 +45,12 @@ const removeLastBuyPrice = async (
`${symbol} Action (${moment().format(
'HH:mm:ss.SSS'
)}): Removed last buy price\n` +
`- Message: ${processMessage}\n\`\`\`${JSON.stringify(
extraMessages,
undefined,
2
)}\`\`\`\n` +
`- Current API Usage: ${getAPILimit(logger)}`
`- Message: ${processMessage}\n\`\`\`${JSON.stringify(
extraMessages,
undefined,
2
)}\`\`\`\n` +
`- Current API Usage: ${getAPILimit(logger)}`
);
};

Expand All @@ -67,6 +67,9 @@ const execute = async (logger, rawData) => {
isLocked,
action,
symbol,
symbolConfiguration: {
buy: { lastBuyPriceRemoveThreshold }
},
symbolInfo: {
filterLotSize: { stepSize, minQty },
filterMinNotional: { minNotional }
Expand Down Expand Up @@ -169,7 +172,7 @@ const execute = async (logger, rawData) => {
return data;
}

if (baseAssetQuantity * currentPrice < parseFloat(minNotional)) {
if (baseAssetQuantity * currentPrice < lastBuyPriceRemoveThreshold) {
// Final check for open orders
refreshedOpenOrders = await getAndCacheOpenOrdersForSymbol(logger, symbol);
if (refreshedOpenOrders.length > 0) {
Expand Down
84 changes: 82 additions & 2 deletions app/cronjob/trailingTradeHelper/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const getMaxPurchaseAmount = async (

let newBuyMaxPurchaseAmount = -1;

// If old max purchase maount is -1, then should calculate maximum purchase amount based on the notional amount.
// If old max purchase amount is -1, then should calculate last buy remove threshold based on the notional amount.
const cachedSymbolInfo =
JSON.parse(
await cache.hget('trailing-trade-symbols', `${symbol}-symbol-info`)
Expand All @@ -174,7 +174,7 @@ const getMaxPurchaseAmount = async (

logger.info(
{ quoteAsset, newBuyMaxPurchaseAmount },
'Retreived max purchase amount from global configuration'
'Retrieved max purchase amount from global configuration'
);

if (newBuyMaxPurchaseAmount === -1) {
Expand All @@ -194,6 +194,74 @@ const getMaxPurchaseAmount = async (

pedrohusky marked this conversation as resolved.
Show resolved Hide resolved
return newBuyMaxPurchaseAmount;
};

const getLastBuyPriceRemoveThreshold = async (
logger,
symbol,
globalConfiguration,
symbolConfiguration
) => {
const symbolBuyLastBuyPriceRemoveThreshold = _.get(
symbolConfiguration,
'buy.lastBuyPriceRemoveThreshold',
-1
);

if (symbolBuyLastBuyPriceRemoveThreshold !== -1) {
logger.info(
{ symbolBuyLastBuyPriceRemoveThreshold },
'Last buy threshold is found from symbol configuration.'
);
return symbolBuyLastBuyPriceRemoveThreshold;
}

logger.info(
{ symbolBuyLastBuyPriceRemoveThreshold },
'Last Buy Price Remove Threshold is set as -1. Need to calculate and override it'
);

let newBuyLastBuyPriceRemoveThreshold = -1;

// If old last buy price remove threshold is -1, then should calculate last buy price remove threshold based on the notional amount.
const cachedSymbolInfo =
JSON.parse(
await cache.hget('trailing-trade-symbols', `${symbol}-symbol-info`)
) || {};

if (_.isEmpty(cachedSymbolInfo) === false) {
const {
quoteAsset,
filterMinNotional: { minNotional }
} = cachedSymbolInfo;

newBuyLastBuyPriceRemoveThreshold = _.get(
globalConfiguration,
`buy.lastBuyPriceRemoveThresholds.${quoteAsset}`,
-1
);

logger.info(
{ quoteAsset, newBuyLastBuyPriceRemoveThreshold },
'Retrieved last buy price remove threshold from global configuration'
);

if (newBuyLastBuyPriceRemoveThreshold === -1) {
newBuyLastBuyPriceRemoveThreshold = parseFloat(minNotional);

logger.info(
{ newBuyLastBuyPriceRemoveThreshold, minNotional },
'Could not get last buy price remove threshold from global configuration. Use minimum notional from symbol info'
);
}
} else {
logger.info(
{ cachedSymbolInfo },
'Could not find symbol info, wait to be cached.'
);
}

return newBuyLastBuyPriceRemoveThreshold;
};
/**
* Get global/symbol configuration
*
Expand Down Expand Up @@ -222,8 +290,20 @@ const getConfiguration = async (logger, symbol = null) => {
)
);

_.set(
mergedConfigValue,
'buy.lastBuyPriceRemoveThreshold',
await getLastBuyPriceRemoveThreshold(
logger,
symbol,
globalConfigValue,
symbolConfigValue
)
);

// For symbol configuration, remove maxPurchaseAmounts
_.unset(mergedConfigValue, 'buy.maxPurchaseAmounts');
_.unset(mergedConfigValue, 'buy.lastBuyPriceRemoveThresholds');
}

// Merge global and symbol configuration
Expand Down
1 change: 1 addition & 0 deletions app/frontend/websocket/handlers/setting-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const handleSettingUpdate = async (logger, ws, payload) => {
// Set max purchase amount to be -1, which mean max purchase amount
// will be automatically calculate based on the notional amount.
mergedConfiguration.buy.maxPurchaseAmount = -1;
mergedConfiguration.buy.lastBuyPriceRemoveThreshold = -1;

logger.info({ mergedConfiguration }, 'New merged configuration');

Expand Down
2 changes: 2 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
},
"buy": {
"enabled": true,
"lastBuyPriceRemoveThreshold": -1,
"lastBuyPriceRemoveThresholds": {},
"maxPurchaseAmount": -1,
"maxPurchaseAmounts": {},
"triggerPercentage": 1.0,
Expand Down
4 changes: 4 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@
type="text/babel"
src="./js/SettingIconMaxPurchaseAmount.js"
></script>
<script
type="text/babel"
src="./js/SettingIconLastBuyPriceRemoveThreshold.js"
></script>
<script type="text/babel" src="./js/SettingIcon.js"></script>
<script type="text/babel" src="./js/AccountWrapperAsset.js"></script>
<script type="text/babel" src="./js/AccountWrapper.js"></script>
Expand Down
12 changes: 9 additions & 3 deletions public/js/CoinWrapperSetting.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ class CoinWrapperSetting extends React.Component {
className='btn btn-sm btn-link p-0 ml-1'
onClick={this.toggleCollapse}>
<i
className={`fa ${
collapsed ? 'fa-arrow-right' : 'fa-arrow-down'
}`}></i>
className={`fa ${collapsed ? 'fa-arrow-right' : 'fa-arrow-down'
}`}></i>
</button>
</div>
<div
Expand Down Expand Up @@ -74,6 +73,13 @@ class CoinWrapperSetting extends React.Component {
</span>
</div>

<div className='coin-info-column coin-info-column-order'>
<span className='coin-info-label'>Last Buy Price Threshold:</span>
<HightlightChange className='coin-info-value'>
{symbolConfiguration.buy.lastBuyPriceRemoveThreshold} {quoteAsset}
</HightlightChange>
</div>

<div className='coin-info-column coin-info-column-order'>
<span className='coin-info-label'>Max purchase amount:</span>
<HightlightChange className='coin-info-value'>
Expand Down
Loading