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

Add api trade simulation scripts #5093

Merged
merged 41 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
5538914
Add line break
ghubstan Jan 16, 2021
ef4acc0
Make opt description generic
ghubstan Jan 16, 2021
629f408
Add method help docs
ghubstan Jan 16, 2021
03f6c2a
Add method help docs
ghubstan Jan 16, 2021
d14fc40
Fix help text typos
ghubstan Jan 16, 2021
68092a0
Add --memo opt to help text
ghubstan Jan 16, 2021
cde9a6d
Add method help docs
ghubstan Jan 16, 2021
dab65e7
Fix typo
ghubstan Jan 16, 2021
a133b0d
Add method help doc
ghubstan Jan 16, 2021
9efeee8
Explain example
ghubstan Jan 16, 2021
13d0d3e
Add method help doc
ghubstan Jan 16, 2021
44c1192
Add method help docs
ghubstan Jan 17, 2021
04657d9
Explain example
ghubstan Jan 17, 2021
8907d6b
Keep CLI method enum members in alphabetical order
ghubstan Jan 17, 2021
fbc31a5
Merge branch 'master' into 01-add-method-help-docs
ghubstan Jan 19, 2021
873c661
Add api trade simulation scripts
ghubstan Jan 19, 2021
93c3735
Fix bash bugs & style problems for codacy
ghubstan Jan 19, 2021
48a326b
Add env requirement and usage comments
ghubstan Jan 19, 2021
cf419d2
Make price request frequency configurable
ghubstan Jan 19, 2021
4eabb98
Fix indentation
ghubstan Jan 20, 2021
71ffa0f
Explain that paid tx fees are forfeited
ghubstan Jan 20, 2021
8675841
Fix punctuation
ghubstan Jan 20, 2021
fce3aab
Improve description
ghubstan Jan 20, 2021
031b18e
Fix typo
ghubstan Jan 20, 2021
11096d5
Fix tx-fee-rate opt description
ghubstan Jan 20, 2021
72731e5
Add parens around (sats/byte)
ghubstan Jan 20, 2021
67eed01
Fix tx-fee-rate opt description
ghubstan Jan 20, 2021
73c6b3d
Fix typo, group mutually exclusive opts around '||'
ghubstan Jan 20, 2021
6c322d4
Put $N variable references in double quotes
ghubstan Jan 20, 2021
0c06abd
Replace shortform '. with longform 'source'
ghubstan Jan 20, 2021
8b4938a
Do not export vars from lowest level child script
ghubstan Jan 20, 2021
cb96f27
Remove uneeded ${curlies}
ghubstan Jan 20, 2021
078f010
Remove api dev/test log statement
ghubstan Jan 20, 2021
3244db1
Print current market price just before offer is created
ghubstan Jan 20, 2021
ced422e
Add new api CLI method 'getbtcprice'
ghubstan Jan 20, 2021
20e1649
Get current mkt price from server, not directly from feed
ghubstan Jan 20, 2021
7fa61c9
Remove deprecated python script
ghubstan Jan 20, 2021
78d0024
Re-add exports to make codacy happy
ghubstan Jan 20, 2021
184ffd1
Enclose $vars in double quotes
ghubstan Jan 20, 2021
d18b2d5
Run an async price feed request when CLI needs market price
ghubstan Jan 20, 2021
b2d8faf
Log price feed request warnings, do not throw to CLI
ghubstan Jan 20, 2021
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
28 changes: 28 additions & 0 deletions apitest/scripts/editf2faccountform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sys, os, json

# Writes a Bisq json F2F payment account form for the given country_code to the current working directory.

if len(sys.argv) < 2:
print("usage: editf2faccountform.py country_code")
exit(1)

country_code = str(sys.argv[1]).upper()
acct_form = {
"_COMMENTS_": [
"Do not manually edit the paymentMethodId field.",
"Edit the salt field only if you are recreating a payment account on a new installation and wish to preserve the account age."
],
"paymentMethodId": "F2F",
"accountName": "Face to Face Payment Account",
"city": "Anytown",
"contact": "Me",
"country": country_code,
"extraInfo": "",
"salt": ""
}
target=os.path.dirname(os.path.realpath(__file__)) + '/' + 'f2f-acct.json'
with open (target, 'w') as outfile:
json.dump(acct_form, outfile, indent=2)
outfile.write('\n')

exit(0)
159 changes: 159 additions & 0 deletions apitest/scripts/limit-order-simulation.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#! /bin/bash

# Demonstrates a way to create a limit order (offer) using the API CLI with a local regtest bitcoin node.
#
# A country code argument is used to create a country based face to face payment account for the simulated offer.
#
# Prerequisites:
#
# - Linux or OSX with bash, Java 10, or Java 11-12 (JDK language compatibility 10), and bitcoin-core (v0.19, v0.20).
#
# - Bisq must be fully built with apitest dao setup files installed.
# Build command: `./gradlew clean build :apitest:installDaoSetup`
#
# - All supporting nodes must be run locally, in dev/dao/regtest mode:
# bitcoind, seednode, arbdaemon, alicedaemon, bobdaemon
#
# These should be run using the apitest harness. From the root project dir, run:
# `$ ./bisq-apitest --apiPassword=xyz --supportingApps=bitcoind,seednode,arbdaemon,alicedaemon,bobdaemon --shutdownAfterTests=false`
#
# - Only regtest btc can be bought or sold with the test payment account.
#
# Usage:
#
# This script must be run from the root of the project, e.g.:
#
# `$ apitest/scripts/limit-order-simulation.sh -l 40000 -d buy -c fr -m 3.00 -a 0.125`
#
# Script options: -l <limit-price> -d <direction> -c <country-code> (-m <mkt-price-margin(%)> || -f <fixed-price>) -a <amount(btc)> [-w <price-poll-interval(s)>]
#
# Example:
#
# Create a sell/eur offer to sell 0.125 btc at a fixed-price of 38,000 euros, using a France face to face
# payment account, when the BTC market price rises to or above 40,000 EUR:
#
# `$ apitest/scripts/limit-order-simulation.sh -l 40000 -d sell -c fr -m 0.00 -a 0.125`

APP_HOME=$(pwd -P)
APITEST_SCRIPTS_HOME="${APP_HOME}/apitest/scripts"

source "${APITEST_SCRIPTS_HOME}/trade-simulation-env.sh"
source "${APITEST_SCRIPTS_HOME}/trade-simulation-utils.sh"

checksetup
parselimitorderopts "$@"

printdate "Started ${APP_BASE_NAME} with parameters:"
printscriptparams
printbreak

editpaymentaccountform "$COUNTRY_CODE"
exitoncommandalert $?
cat "${APITEST_SCRIPTS_HOME}/${F2F_ACCT_FORM}"
printbreak

# Create F2F payment accounts for $COUNTRY_CODE, and get the $CURRENCY_CODE.
printdate "Creating Alice's face to face ${COUNTRY_CODE} payment account."
CMD="${CLI_BASE} --port=${ALICE_PORT} createpaymentacct --payment-account-form=${APITEST_SCRIPTS_HOME}/${F2F_ACCT_FORM}"
printdate "ALICE CLI: ${CMD}"
CMD_OUTPUT=$(createpaymentacct "${CMD}")
exitoncommandalert $?
echo "${CMD_OUTPUT}"
ALICE_ACCT_ID=$(getnewpaymentacctid "${CMD_OUTPUT}")
exitoncommandalert $?
CURRENCY_CODE=$(getnewpaymentacctcurrency "${CMD_OUTPUT}")
exitoncommandalert $?
printdate "ALICE F2F payment-account-id = ${ALICE_ACCT_ID}, currency-code = ${CURRENCY_CODE}."
printbreak

printdate "Creating Bob's face to face ${COUNTRY_CODE} payment account."
CMD="${CLI_BASE} --port=${BOB_PORT} createpaymentacct --payment-account-form=${APITEST_SCRIPTS_HOME}/${F2F_ACCT_FORM}"
printdate "BOB CLI: ${CMD}"
CMD_OUTPUT=$(createpaymentacct "${CMD}")
exitoncommandalert $?
echo "${CMD_OUTPUT}"
BOB_ACCT_ID=$(getnewpaymentacctid "${CMD_OUTPUT}")
exitoncommandalert $?
CURRENCY_CODE=$(getnewpaymentacctcurrency "${CMD_OUTPUT}")
exitoncommandalert $?
printdate "BOB F2F payment-account-id = ${BOB_ACCT_ID}, currency-code = ${CURRENCY_CODE}."
printbreak

# Bob & Alice now have matching payment accounts, now loop until the price limit is reached, then create an offer.
if [ "$DIRECTION" = "BUY" ]
then
printdate "Create a BUY / ${CURRENCY_CODE} offer when the market price falls to or below ${LIMIT_PRICE} ${CURRENCY_CODE}."
else
printdate "Create a SELL / ${CURRENCY_CODE} offer when the market price rises to or above ${LIMIT_PRICE} ${CURRENCY_CODE}."
fi

DONE=0
while : ; do
if [ "$DONE" -ne 0 ]; then
break
fi

CURRENT_PRICE=$(getcurrentprice "$ALICE_PORT" "$CURRENCY_CODE")
exitoncommandalert $?
printdate "Current Market Price: $CURRENT_PRICE"

if [ "$DIRECTION" = "BUY" ] && [ "$CURRENT_PRICE" -le "$LIMIT_PRICE" ]; then
printdate "Limit price reached."
DONE=1
break
fi

if [ "$DIRECTION" = "SELL" ] && [ "$CURRENT_PRICE" -ge "$LIMIT_PRICE" ]; then
printdate "Limit price reached."
DONE=1
break
fi

sleep "$WAIT"
done

printdate "ALICE: Creating ${DIRECTION} ${CURRENCY_CODE} offer with payment acct ${ALICE_ACCT_ID}."
CMD="$CLI_BASE --port=${ALICE_PORT} createoffer"
CMD+=" --payment-account=${ALICE_ACCT_ID}"
CMD+=" --direction=${DIRECTION}"
CMD+=" --currency-code=${CURRENCY_CODE}"
CMD+=" --amount=${AMOUNT}"
if [ -z "${MKT_PRICE_MARGIN}" ]; then
CMD+=" --fixed-price=${FIXED_PRICE}"
else
CMD+=" --market-price-margin=${MKT_PRICE_MARGIN}"
fi
CMD+=" --security-deposit=50.0"
CMD+=" --fee-currency=BSQ"
printdate "ALICE CLI: ${CMD}"
OFFER_ID=$(createoffer "${CMD}")
exitoncommandalert $?
printdate "ALICE: Created offer with id: ${OFFER_ID}."
printbreak
sleeptraced 3

# Show Alice's new offer.
printdate "ALICE: Looking at her new ${DIRECTION} ${CURRENCY_CODE} offer."
CMD="$CLI_BASE --port=${ALICE_PORT} getmyoffer --offer-id=${OFFER_ID}"
printdate "ALICE CLI: ${CMD}"
OFFER=$($CMD)
exitoncommandalert $?
echo "${OFFER}"
printbreak
sleeptraced 7

# Generate some btc blocks.
printdate "Generating btc blocks after publishing Alice's offer."
genbtcblocks 3 3
printbreak
sleeptraced 5

# Show Alice's offer in Bob's CLI.
printdate "BOB: Looking at ${DIRECTION} ${CURRENCY_CODE} offers."
CMD="$CLI_BASE --port=${BOB_PORT} getoffers --direction=${DIRECTION} --currency-code=${CURRENCY_CODE}"
printdate "BOB CLI: ${CMD}"
OFFERS=$($CMD)
exitoncommandalert $?
echo "${OFFERS}"

exit 0
Loading