Skip to content

Instantly share code, notes, and snippets.

@darkerego
Last active October 26, 2017 02:47
Show Gist options
  • Save darkerego/4f4475c2bb9ae1f34f64db04f6985b79 to your computer and use it in GitHub Desktop.
Save darkerego/4f4475c2bb9ae1f34f64db04f6985b79 to your computer and use it in GitHub Desktop.
Totally random, nonsensical, entropy-driven trade simulator
#!/bin/bash
#########################################################
# ZenSim ... in posix ~ Darkerego 2017
#########################################################
#
# Runs simulations using randomly generated
# variables, preselected for each included strategy. Run
# over and over until you find a desirable result, and than
# you can train the bot to trade using whatever paramaters the
# program generated. Commands are saved to a file called
# `cmd.log`
#
genMarkup(){
awk -v "seed=$[(RANDOM & 32767) + 32768 * (RANDOM & 32767)]" 'BEGIN { srand(seed); printf("%.5f\n", rand() * 4.0) }'
}
sim(){
strats='cci_srsi macd rsi sar speed srsi_macd ta_ema ta_macd trend_ema trust_distrust'
for s in $strats; do
case $s in
cci_srsi)
echo zenbot sim --strategy=cci_srsi --period="$(( ( RANDOM % 28 ) + 1 ))"m --cci_periods="$(( ( RANDOM % 28 ) + 1 ))" \
--rsi_periods=="$(( ( RANDOM % 28 ) + 1 ))" --period="$(( ( RANDOM % 90 ) + 1 ))"m --min_periods="$(( ( RANDOM % 30 ) + 1 ))" \
--srsi_k="$(( ( RANDOM % 3 ) + 1 ))" --srsi_d="$(( ( RANDOM % 3 ) + 1 ))" --oversold_rsi="$(( ( RANDOM % 18 ) + 1 ))" \
--overbought_rsi="$(( ( RANDOM % 85 ) + 1 ))" --oversold_cci=-"$(( ( RANDOM % 90 ) + 1 ))" --overbought_cci=-"$(( ( RANDOM % 140 ) + 1 ))" \
--constant=0.0"$(( ( RANDOM % 15 ) / 1 ))" --markup_pct=`genMarkup` --enable_profit_stop_pct=$stop_pct --max_sell_loss_pct="$(( ( RANDOM % 5 ) + 1 ))" \
--profit_stop_pct=$prof_stop_pct "$selector" > cmd
cat cmd >> cmd.log
bash cmd
echo -n "Running: $(cat cmd)";echo
>cmd
;;
macd)
echo zenbot sim --strategy=$s --buy_pct 99 --sell_pct 99 --markup_pct=`genMarkup` --period="$(( ( RANDOM % 90 ) + 1 ))"m \
--enable_profit_stop_pct=$stop_pct --max_sell_loss_pct="$(( ( RANDOM % 5 ) + 1 ))" --profit_stop_pct=$prof_stop_pct --ema_short_period=="$(( ( RANDOM % 20 ) + 1 ))" --ema_long_period="$(( ( RANDOM % 29 ) + 1 ))" \
--signal_period=="$(( ( RANDOM % 12 ) + 1 ))" --overbought_rsi_periods="$(( ( RANDOM % 30 ) + 1 ))" --overbought_rsi="$(( ( RANDOM % 90 ) + 1 ))" "$selector" > cmd
cat cmd >> cmd.log
bash cmd
echo -n "Running: $(cat cmd)";echo
>cmd
;;
rsi)
echo zenbot sim --strategy=$s --buy_pct 99 --sell_pct 99 --markup_pct=`genMarkup` --period="$(( ( RANDOM % 10 ) + 1 ))"m --min_periods="$(( ( RANDOM % 52 ) + 1 ))" --rsi_periods="$(( ( RANDOM % 26 ) + 1 ))" --oversold_rsi="$(( ( RANDOM % 30 ) + 1 ))" --overbought_rsi="$(( ( RANDOM % 85 ) + 1 ))" --rsi_recover="$(( ( RANDOM % 5 ) + 1 ))" --rsi_drop="$(( ( RANDOM % 3 ) / 1 ))" --rsi_divisor="$(( ( RANDOM % 3 ) / 1 ))" --max_sell_loss_pct="$(( ( RANDOM % 5 ) + 1 ))" --enable_profit_stop_pct=$stop_pct --profit_stop_pct=$prof_stop_pct "$selector" >cmd
cat cmd >> cmd.log
bash cmd
echo -n "Running: $(cat cmd)";echo
>cmd
;;
sar)
echo zenbot sim --strategy=$s --buy_pct 99 --sell_pct 99 --markup_pct=`genMarkup` --period="$(( ( RANDOM % 6 ) + 1 ))"m \
--enable_profit_stop_pct=$stop_pct --profit_stop_pct=$prof_stop_pct --max_sell_loss_pct="$(( ( RANDOM % 5 ) + 1 ))" --sar_af=0.0"$(( ( RANDOM % 17 ) / 1 ))" --sar_max_af=0."$(( ( RANDOM % 5 ) + 1 ))" "$selector" >cmd
cat cmd >> cmd.log
bash cmd
echo -n "Running: $(cat cmd)";echo
>cmd
;;
speed)
echo zenbot sim --strategy=$s --buy_pct 99 --sell_pct 99 --markup_pct=`genMarkup` --period="$(( ( RANDOM % 4 ) + 1 ))"m \
--enable_profit_stop_pct=$stop_pct --max_sell_loss_pct="$(( ( RANDOM % 5 ) + 1 ))" --profit_stop_pct=$prof_stop_pct --baseline_periods="$(( ( RANDOM % 3500 ) / 1 ))" --trigger_factor=1."$(( ( RANDOM % 5 ) + 1 ))" "$selector" >cmd
cat cmd >> cmd.log
bash cmd
echo -n "Running: $(cat cmd)";echo
>cmd
;;
srsi_macd)
echo zenbot sim --strategy=$s --buy_pct 99 --sell_pct 99 --markup_pct=`genMarkup` --period="$(( ( RANDOM % 40 ) + 1 ))"m \
--enable_profit_stop_pct=$stop_pct --profit_stop_pct=$prof_stop_pct --max_sell_loss_pct="$(( ( RANDOM % 5 ) + 1 ))" --rsi_periods="$(( ( RANDOM % 14 ) + 1 ))" --srsi_periods="$(( ( RANDOM % 9 ) + 1 ))"\
--srsi_k="$(( ( RANDOM % 4 ) + 1 ))" --srsi_d="$(( ( RANDOM % 5 ) + 1 ))" --oversold_rsi="$(( ( RANDOM % 26 ) + 1 ))" \
--overbought_rsi="$(( ( RANDOM % 100 ) + 1 ))" --ema_short_period="$(( ( RANDOM % 25 ) + 1 ))" --ema_long_period="$(( ( RANDOM % 27 ) + 1 ))"\
--signal_period="$(( ( RANDOM % 9 ) + 1 ))" --up_trend_threshold="$(( ( RANDOM % 2 ) / 1 ))" \
--down_trend_threshold="$(( ( RANDOM % 2 ) / 1 ))" "$selector" >cmd
cat cmd >> cmd.log
bash cmd
echo -n "Running: $(cat cmd)";echo
>cmd
;;
ta_ema)
echo zenbot sim --strategy=$s --buy_pct 99 --sell_pct 99 --markup_pct=`genMarkup` --period="$(( ( RANDOM % 60 ) + 1 ))"m \
--enable_profit_stop_pct=$stop_pct --profit_stop_pct=$prof_stop_pct --max_sell_loss_pct="$(( ( RANDOM % 5 ) + 1 ))" --neutral_rate=auto --trend_ema="$(( ( RANDOM % 23 ) + 1 ))" \
--oversold_rsi="$(( ( RANDOM % 35 ) + 1 ))" --oversold_rsi_periods="$(( ( RANDOM % 23 ) + 1 ))" "$selector" >cmd
cat cmd >> cmd.log
bash cmd
echo -n "Running: $(cat cmd)";echo
>cmd
;;
ta_macd)
echo zenbot sim --strategy=$s --buy_pct 99 --sell_pct 99 --markup_pct=`genMarkup` --period="$(( ( RANDOM % 120 ) + 1 ))"m \
--min_periods="$(( ( RANDOM % 52 ) + 1 ))" --ema_short_period="$(( ( RANDOM % 12 ) + 1 ))" --ema_long_period="$(( ( RANDOM % 26 ) + 1 ))" \
--signal_period="$(( ( RANDOM % 9 ) + 1 ))" --up_trend_threshold="$(( ( RANDOM % 2 ) / 1 ))" --down_trend_threshold="$(( ( RANDOM % 2 ) / 1 ))" \
--overbought_rsi_periods="$(( ( RANDOM % 27 ) + 1 ))" --overbought_rsi="$(( ( RANDOM % 80 ) + 1 ))" --enable_profit_stop_pct=$stop_pct --profit_stop_pct=$prof_stop_pct \
--max_sell_loss_pct="$(( ( RANDOM % 5 ) + 1 ))" "$selector" >cmd
cat cmd >> cmd.log
bash cmd
echo -n "Running: $(cat cmd)";echo
>cmd
;;
trend_ema)
echo zenbot sim --strategy=$s --buy_pct 99 --sell_pct 99 --markup_pct=`genMarkup` --period="$(( ( RANDOM % 5 ) + 1 ))"m \
--min_periods="$(( ( RANDOM % 50 ) + 1 ))" --trend_ema="$(( ( RANDOM % 27 ) + 1 ))" --neutral_rate=auto \
--oversold_rsi_periods="$(( ( RANDOM % 17 ) + 1 ))" --oversold_rsi="$(( ( RANDOM % 10 ) + 1 ))" --enable_profit_stop_pct=$stop_pct --max_sell_loss_pct="$(( ( RANDOM % 5 ) + 1 ))" \
--profit_stop_pct=$prof_stop_pct "$selector" >cmd
cat cmd >> cmd.log
bash cmd
echo -n "Running: $(cat cmd)";echo
>cmd
;;
trust_distrust)
echo zenbot sim --strategy=$s --buy_pct 99 --sell_pct 99 --markup_pct=`genMarkup` --period="$(( ( RANDOM % 45 ) + 1 ))"m \
--min_periods="$(( ( RANDOM % 52 ) + 1 ))" --sell_threshold="$(( ( RANDOM % 2 ) + 1 ))" --sell_threshold_max=0 --max_sell_loss_pct="$(( ( RANDOM % 5 ) + 1 ))" \
--sell_min="$(( ( RANDOM % 3 ) + 1 ))" --buy_threshold="$(( ( RANDOM % 2 ) + 1 ))" --buy_threshold_max=0 --greed="$(( ( RANDOM % 4 ) / 1 ))" \
--enable_profit_stop_pct=$stop_pct --profit_stop_pct=$prof_stop_pct "$selector" >cmd
cat cmd >> cmd.log
bash cmd
echo -n "Running: $(cat cmd)";echo
>cmd
;;
*)
echo "Strategy: $s"
echo 'This is a catch all function, I left it here commented out for referance.'
#echo zenbot sim --strategy=$s --buy_pct 99 --sell_pct 99 --markup_pct=`genMarkup` --period="$(( ( RANDOM % 13 ) + 1 ))"m \
# --enable_profit_stop_pct=$stop_pct --profit_stop_pct=$prof_stop_pct "$selector" >cmd
#cat cmd >> cmd.log
#bash cmd
#echo -n "Running: $(cat cmd)";echo
#>cmd
;;
esac
done
}
usage(){
echo "USAGE: $0 --sim/-s <selector, default poloniex.BTC-USDT> <enable_profit_stop_pct=number, default=10> <profit_stop_pct=number, default=4>"
echo "For zenbot strategy full usage run with --help: ";echo
}
usage_zen(){
echo 'Stochastic CCI Strategy
description:
Stochastic CCI Strategy
options:
--period=<value> period length (default: 20m)
--min_periods=<value> min. number of history periods (default: 30)
--ema_acc=<value> sideways threshold (0.2-0.4) (default: 0.03)
--cci_periods=<value> number of RSI periods (default: 14)
--rsi_periods=<value> number of RSI periods (default: 14)
--srsi_periods=<value> number of RSI periods (default: 9)
--srsi_k=<value> %K line (default: 5)
--srsi_d=<value> %D line (default: 3)
--oversold_rsi=<value> buy when RSI reaches or drops below this value (default: 18)
--overbought_rsi=<value> sell when RSI reaches or goes above this value (default: 85)
--oversold_cci=<value> buy when CCI reaches or drops below this value (default: -90)
--overbought_cci=<value> sell when CCI reaches or goes above this value (default: 140)
--constant=<value> constant (default: 0.015)
If you have questions about this strategy, contact me... @talvasconcelos
forex_analytics
description:
Apply the trained forex analytics model.
options:
--modelfile=<value> modelfile (generated by running `train`), should be in models/ (default: none)
--period=<value> period length of a candlestick (default: 30m) (default: 30m)
--min_periods=<value> min. number of history periods (default: 100)
macd
description:
Buy when (MACD - Signal > 0) and sell when (MACD - Signal < 0).
options:
--period=<value> period length (default: 1h)
--min_periods=<value> min. number of history periods (default: 52)
--ema_short_period=<value> number of periods for the shorter EMA (default: 12)
--ema_long_period=<value> number of periods for the longer EMA (default: 26)
--signal_period=<value> number of periods for the signal EMA (default: 9)
--up_trend_threshold=<value> threshold to trigger a buy signal (default: 0)
--down_trend_threshold=<value> threshold to trigger a sold signal (default: 0)
--overbought_rsi_periods=<value> number of periods for overbought RSI (default: 25)
--overbought_rsi=<value> sold when RSI exceeds this value (default: 70)
rsi
description:
Attempts to buy low and sell high by tracking RSI high-water readings.
options:
--period=<value> period length (default: 2m)
--min_periods=<value> min. number of history periods (default: 52)
--rsi_periods=<value> number of RSI periods
--oversold_rsi=<value> buy when RSI reaches or drops below this value (default: 30)
--overbought_rsi=<value> sell when RSI reaches or goes above this value (default: 82)
--rsi_recover=<value> allow RSI to recover this many points before buying (default: 3)
--rsi_drop=<value> allow RSI to fall this many points before selling (default: 0)
--rsi_divisor=<value> sell when RSI reaches high-water reading divided by this value (default: 2)
sar
description:
Parabolic SAR
options:
--period=<value> period length (default: 2m)
--min_periods=<value> min. number of history periods (default: 52)
--sar_af=<value> acceleration factor for parabolic SAR (default: 0.015)
--sar_max_af=<value> max acceleration factor for parabolic SAR (default: 0.3)
speed
description:
Trade when % change from last two 1m periods is higher than average.
options:
--period=<value> period length (default: 1m)
--min_periods=<value> min. number of history periods (default: 3000)
--baseline_periods=<value> lookback periods for volatility baseline (default: 3000)
--trigger_factor=<value> multiply with volatility baseline EMA to get trigger value (default: 1.6)
srsi_macd
description:
Stochastic MACD Strategy
options:
--period=<value> period length (default: 30m)
--min_periods=<value> min. number of history periods (default: 200)
--rsi_periods=<value> number of RSI periods
--srsi_periods=<value> number of RSI periods (default: 9)
--srsi_k=<value> %D line (default: 5)
--srsi_d=<value> %D line (default: 3)
--oversold_rsi=<value> buy when RSI reaches or drops below this value (default: 20)
--overbought_rsi=<value> sell when RSI reaches or goes above this value (default: 80)
--ema_short_period=<value> number of periods for the shorter EMA (default: 24)
--ema_long_period=<value> number of periods for the longer EMA (default: 200)
--signal_period=<value> number of periods for the signal EMA (default: 9)
--up_trend_threshold=<value> threshold to trigger a buy signal (default: 0)
--down_trend_threshold=<value> threshold to trigger a sold signal (default: 0)
ta_ema
description:
Buy when (EMA - last(EMA) > 0) and sell when (EMA - last(EMA) < 0). Optional buy on low RSI.
options:
--period=<value> period length (default: 10m)
--min_periods=<value> min. number of history periods (default: 52)
--trend_ema=<value> number of periods for trend EMA (default: 20)
--neutral_rate=<value> avoid trades if abs(trend_ema) under this float (0 to disable, "auto" for a variable filter) (default: 0.06)
--oversold_rsi_periods=<value> number of periods for oversold RSI (default: 20)
--oversold_rsi=<value> buy when RSI reaches this value (default: 30)
ta_macd
description:
Buy when (MACD - Signal > 0) and sell when (MACD - Signal < 0).
options:
--period=<value> period length (default: 1h)
--min_periods=<value> min. number of history periods (default: 52)
--ema_short_period=<value> number of periods for the shorter EMA (default: 12)
--ema_long_period=<value> number of periods for the longer EMA (default: 26)
--signal_period=<value> number of periods for the signal EMA (default: 9)
--up_trend_threshold=<value> threshold to trigger a buy signal (default: 0)
--down_trend_threshold=<value> threshold to trigger a sold signal (default: 0)
--overbought_rsi_periods=<value> number of periods for overbought RSI (default: 25)
--overbought_rsi=<value> sold when RSI exceeds this value (default: 70)
trend_ema (default)
description:
Buy when (EMA - last(EMA) > 0) and sell when (EMA - last(EMA) < 0). Optional buy on low RSI.
options:
--period=<value> period length (default: 2m)
--min_periods=<value> min. number of history periods (default: 52)
--trend_ema=<value> number of periods for trend EMA (default: 26)
--neutral_rate=<value> avoid trades if abs(trend_ema) under this float (0 to disable, "auto" for a variable filter) (default: auto)
--oversold_rsi_periods=<value> number of periods for oversold RSI (default: 14)
--oversold_rsi=<value> buy when RSI reaches this value (default: 10)
trust_distrust
description:
Sell when price higher than $sell_min% and highest point - $sell_threshold% is reached. Buy when lowest price point + $buy_threshold% reached.
options:
--period=<value> period length (default: 30m)
--min_periods=<value> min. number of history periods (default: 52)
--sell_threshold=<value> sell when the top drops at least below this percentage (default: 2)
--sell_threshold_max=<value> sell when the top drops lower than this max, regardless of sell_min (panic sell, 0 to disable) (default: 0)
--sell_min=<value> do not act on anything unless the price is this percentage above the original price (default: 1)
--buy_threshold=<value> buy when the bottom increased at least above this percentage (default: 2)
--buy_threshold_max=<value> wait for multiple buy signals before buying (kill whipsaw, 0 to disable) (default: 0)
--greed=<value> sell if we reach this much profit (0 to be greedy and either win or lose) (default: 0)
'
echo
}
backfill(){
echo Backfilling...
zenbot backfill "$selector"
exit $?
}
case "$1" in
-h)
usage
;;
--help)
usage_zen
usage
;;
-s)
selector=${2:-"poloniex.BTC-USDT"}
stop_pct=${3:-"10"}
prof_stop_pct=${4:-"4"}
sim
;;
-b|--backfill)
selector=${2:-"poloniex.BTC-USDT"}
backfill "$selector"
exit
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment