Mike Fishy and Automation System Programming (Bots)


#304

That’s right. It’s only approximate and we can fine tune the calculation moving forward.

Pleased to say the $3 MCU has now been running for almost 24 hours and 15,000 iterations. This is only the basic “ICX” bot for now and it remains to be seen if it can cope with the load of the final bot script.

These are the MCU’s I use:

https://wiki.wemos.cc/products:d1:d1_mini


#305

Awesome!!! Love it how you wrote that!.. I find it amusing. Good effort… excellent group of people in this thread…

Crash…


#306

Firstly, many thank to @Mike_Fishy for giving up his own time to start this project. I have never programmed anything before, but even i have managed to follow through these steps and successfully run the bots.

I have thoroughly enjoyed learning how to do this and I have now spent many hours reading this thread and trying to learn as much as I can. That jolt of satisfaction when the bot started running the first time was like magic!

@Blynker many thanks for the revised code, I too was on -200 odd after a few hours, but since the change the bot hasn’t made a single trade:

====================================
Iteration = 6242
Running Time: 576.47 mins
Current running percentage = 0

As it was running i also thought i’d play around with a couple of settings, I tried to run one very aggressively and the other very conservative.

For the aggressive bot I changed
$strend = 30;
$mtrend = 50;

Which is now running at:

Iteration = 4109
Running Time: 376.55 mins
Current running percentage = -9.378

and the conservative bot i changed to

$strend = 120;
$mtrend = 200;

Which is now running at:

Iteration = 4073
Running Time: 373.08 mins
Current running percentage = -5.384

I am not 100% certain if these changes would do as i thought but i’ll continue to read up and play around.

Thanks again to everyone here, look forward to contributing where I can.


#307

Really big thanks to @Mike_Fishy regarding his huge efforts for the community :muscle::+1:

I’m quite a :nerd_face: so i will bring in some testing on different backends and code testing :eyes:

Have to keep up with last feedback in here first and will start joining you asap :muscle:


#308

@MancunianSoul I would be very surprised if the bot hasn’t traded in the last 576 minutes.
I have 1 trade in the last 135 minutes. Think you might have put the revision in the wrong place.
Can you post your code with 3 backticks on the line above and below your code.


#309

Nice one!

I think i will start running some virtual machines On-Prem or Azure based, but have some free RPI equipment too.

Nice work mate :+1:


#310

Thanks @Blynker, here’s the code I used below, I also used this with the 2 small tweaks on the strend and mtrend which are trading, which does seem a little odd.

<?php
require 'vendor/autoload.php';

// This bot will trade USDT to the other pairs and back to make more USDT
// It shall use all the trading pairs to make more UDST except the ones we tell it not to use

$strend = 60;             // Short Term Trend - must be less than $mtrend
$mtrend = 100;            // Medium Term Trend - must be less than $ltrend
$ltrend = 240;            // Long Term Trend
$tradefile = "newustcor.txt";  // The Trade Logging file name
$minspread = 1;           // The minimum spread percentage needed for a trade
$minrsi = 48;             // Relative Strength must be below this number to buy
$sellbuffer = 1.02;       // Create a buffer to hold CDA if sell is not profitable (2% profit threshold)
$stoplossbuffer = 0.99;   // Stop Loss buffer to sell CDA if it is forming a loss (1% Stop Loss threshold)

// Do not change any of the flags, we use this to signal the bot what to do and when
$buyready = 0;            // This flag signals the bot that the pair meets rules to buy
$buyprep = 1;             // This flag signals the bot to prepare to buy
$buyord = 2;              // This flag signals the bot to place an order
$sellok = 3;              // This flag signals the bot that the order was completed
$sellready = 4;           // This flag signals the bot to sell
$selldone = 5;            // This flag signals the bot the trade completed
$dontbuy = 6;             // This flag signals the bot we dont want to trade BCASH :P

// Standard variables and arrays we use
$i = 0;
$binance_prices = array();
$time_start = time();
$time_end = 0;
$run_time = 0;
$rpc = 0;
$tpc = 0;
$q = 0;

// API call to fetch pricing data from Binance
function getprices()
{
	$api = new Binance\API("<api key>","<secret>");
	$mp = $api->prices();
	return $mp;
}

// Start of the Loop - can run for months - press CTRL-C to stop
for($i = 0; $i <= 2000000; $i++)
{
	$time_end = time();
	$run_time = round((($time_end - $time_start)/60),2);
	print "====================================\n";
	print "Iteration = $i \n";
	print "Running Time: $run_time mins \n";
	print "Current running percentage = $rpc \n";
	print "====================================\n";

	// Fetch current prices from Binance
	$binance_prices = getprices();

	// Loop through the price data as key and value pairs
	foreach($binance_prices as $key => $value)
	{

		// Only process pairs with USDT
		if(strpos($key, "USDT"))
		{

			// Convert the pair name to lower case in varibale $tick
			// for example the name "BTCUSDT" will become "btcusdt"

			$tick = strtolower($key);

			// For the first iteration, create arrays and varibales for the bot
			if($i == 0)
			{

				// Use the lower case name to form the leading part of varibales and arrays
				// for exmaple, using "btcusdt" and adding "st" for the short term array
				// will initialise an array called "btcusdtst"
				// as we loop thorugh the pairs, each one gets created for each pair
				// for exmaple "NEOUSDT" will become "neousdtst"

				${$tick . "st"} = array();         // Short Term Array
				${$tick . "mt"} = array();         // Medium Term Array
				${$tick . "lt"} = array();         // Long Term Array
				${$tick . "stavg"} = 0;            // Short Term Moving Average
				${$tick . "mtavg"} = 0;            // Medium Term Moving Average
				${$tick . "ltavg"} = 0;            // Long Term Moving Average
				${$tick . "strend"} = 0;           // Short Term Moving Trend
				${$tick . "mtrend"} = 0;           // Medium Term Moving Trend
				${$tick . "ltrend"} = 0;           // Long Term Moving Trend
				${$tick . "lspread"} = 0;          // Long Term Spread
				${$tick . "rsi"} = 0;              // Relative Strength Indicator for this pair
				${$tick . "buyflag"} = $buyready;  // Set this pair to buyready
				${$tick . "buyvalue"} = 0;         // record what we buy for on this pair
				${$tick . "sellvalue"} = 0;        // record what we sell for on this pair
				${$tick . "lasttrade"} = 0;        // record we have had one trade done
				${$tick . "lasttpc"} = 0;          // record what percentage last the trade was
				${$tick . "isset"} = 1;            // used to signal we are initialised for this pair
			}

			// We are not on the first loop anymore, we proceed with processing the data
			else
			{

				// Exclude List - these ones we do not trade

				if($key == "BCHABCUSDT") ${$tick . "buyflag"} = $dontbuy;
				if($key == "BCHSVUSDT") ${$tick . "buyflag"} = $dontbuy;
				if($key == "BCCUSDT") ${$tick . "buyflag"} = $dontbuy;
				if($key == "TUSDUSDT") ${$tick . "buyflag"} = $dontbuy;
				if($key == "VENUSDT") ${$tick . "buyflag"} = $dontbuy;
				if($key == "PAXUSDT") ${$tick . "buyflag"} = $dontbuy;

				// Check if the trading pair has been initialised
				// this covers if Binance add a new trading pair on USDT while we are running
				// if Binance adds new trading pairs while bot is running, we shall
				// ignore them and only use the ones since the bot was started and initialised

				if(isset(${$tick . "isset"}))
				{

					// Push data into arrays and shift arrays once we have enough data
					array_push(${$tick . "st"}, $value);
					array_push(${$tick . "mt"}, $value);
					array_push(${$tick . "lt"}, $value);
					if($i > $strend) array_shift(${$tick . "st"});
					if($i > $mtrend) array_shift(${$tick . "mt"});
					if($i > $ltrend) array_shift(${$tick . "lt"});

					// Wait until we have all the arrays populated with data
					if($i <= $ltrend)
					{
						printf("%-9s",$key);
						print "\t:Loading Arrays with data\n";
					}

					// Arrays are populated, so on with the processing
					else
					{

						// Calculate the Moving Average for the 3 arrays
						${$tick . "stavg"} = round((array_sum(${$tick . "st"})/$strend),8);
						${$tick . "mtavg"} = round((array_sum(${$tick . "mt"})/$mtrend),8);
						${$tick . "ltavg"} = round((array_sum(${$tick . "lt"})/$ltrend),8);

						// Check if the Short Term Trend is trending down, flat or up
						// We use the current price to see if it is above or below the short term moving average
						// We use "1" to signal it is trending down, "2" for flat, "3" for trending up
						if($value < ${$tick . "stavg"}) ${$tick . "strend"} = 1;
						if($value == ${$tick . "stavg"}) ${$tick . "strend"} = 2;
						if($value > ${$tick . "stavg"}) ${$tick . "strend"} = 3;

						// Check if the Medium Term Trend is trending down, flat or up
						// We use the short term moving average to see if it is above or below the medium term moving average
						// We use "1" to signal it is trending down, "2" for flat, "3" for trending up
						if(${$tick . "stavg"} < ${$tick . "mtavg"}) ${$tick . "mtrend"} = 1;
						if(${$tick . "stavg"} == ${$tick . "mtavg"}) ${$tick . "mtrend"} = 2;
						if(${$tick . "stavg"} > ${$tick . "mtavg"}) ${$tick . "mtrend"} = 3;

						// Check if the Long Term Trend is trending down, flat or up
						// We use the medium term moving average to see if it is above or below the long term moving average
						// We use "1" to signal it is trending down, "2" for flat, "3" for trending up
						if(${$tick . "mtavg"} < ${$tick . "ltavg"}) ${$tick . "ltrend"} = 1;
						if(${$tick . "mtavg"} == ${$tick . "ltavg"}) ${$tick . "ltrend"} = 2;
						if(${$tick . "mtavg"} > ${$tick . "ltavg"}) ${$tick . "ltrend"} = 3;

						// Calculate the Medium Term spread, which is the percentage difference between
						// the highest recorded price and the lowest recorded price in the Medium Term Array
						$mlow = min(${$tick . "lt"});
						$mhigh = max(${$tick . "lt"});
						${$tick . "lspread"} = round(((1-($mlow/$mhigh))*100),3);
						
						// Calculate the Relative Strength Indicator on the Long Term Array
						// A Low RSI indicates a buy opportunity
						$rsitck = 0;
						${$tick . "gain"} = array();
						${$tick . "loss"} = array();
						foreach(${$tick . "lt"} as $cdaval)
						{
							if($rsitck == 0)
							{
								$cdagain = 0;
								$cdaloss = 0;
							}
							else
							{
								if($cdaval == $cdaprev)
								{
									$cdagain = 0;
									$cdaloss = 0;
								}
								elseif($cdaval > $cdaprev)
								{
									$cdacalc = $cdaval - $cdaprev;
									$cdagain = number_format($cdacalc,8);
									$cdaloss = 0;
								}
								else
								{
									$cdacalc = $cdaprev - $cdaval;
									$cdaloss = number_format($cdacalc,8);
									$cdagain = 0;
								}
							}
							array_push(${$tick . "gain"}, $cdagain);
							array_push(${$tick . "loss"}, $cdaloss);
							$cdaprev = $cdaval;
							$rsitck++;
						}
						$cdarsgain = (array_sum(${$tick . "gain"})) / $ltrend;
						$cdarsloss = (array_sum(${$tick . "loss"})) / $ltrend;
						if($cdarsloss > 0)
						{
							${$tick . "rsi"} = round(100-(100/(1+($cdarsgain/$cdarsloss))),3);
						}
						else
						{
							${$tick . "rsi"} = 100;
						}

						// Print out what we have so far so we can see what is going on
						printf("%-9s",$key);
						print "\tV:";
						printf("%-14.8F",$value);
						print "\t  ST:";
						printf("%-14.8F",${$tick . "stavg"});
						if(${$tick . "strend"} == 1) printf("%-5s",":DOWN");
						if(${$tick . "strend"} == 2) printf("%-5s",":FLAT");
						if(${$tick . "strend"} == 3) printf("%-5s",":UP");
						print "\t  MT:";
						printf("%-14.8F",${$tick . "mtavg"});
						if(${$tick . "mtrend"} == 1) printf("%-5s",":DOWN");
						if(${$tick . "mtrend"} == 2) printf("%-5s",":FLAT");
						if(${$tick . "mtrend"} == 3) printf("%-5s",":UP");
						print "\t  LT:";
						printf("%-14.8F",${$tick . "ltavg"});
						if(${$tick . "ltrend"} == 1) printf("%-5s",":DOWN");
						if(${$tick . "ltrend"} == 2) printf("%-5s",":FLAT");
						if(${$tick . "ltrend"} == 3) printf("%-5s",":UP");
						print "\t  SPREAD:";
						printf("%-03.3F",${$tick . "lspread"}); 
						print "%\t  RSI:";
						printf("%-06.3F",${$tick . "rsi"});
						if(${$tick . "buyflag"} == $buyready) $cdastatus = "Buy Ready";
						if(${$tick . "buyflag"} == $buyprep) $cdastatus = "Buy Prep";
						if(${$tick . "buyflag"} == $buyord) $cdastatus = "Buy Order";
						if(${$tick . "buyflag"} == $sellok) $cdastatus = "Sell OK";
						if(${$tick . "buyflag"} == $sellready) $cdastatus = "Sell Ready";
						if(${$tick . "buyflag"} == $selldone) $cdastatus = "Sell Done";
						if(${$tick . "buyflag"} == $dontbuy) $cdastatus = "Dont Trade";
						if(${$tick . "buyflag"} == $sellready)
						{
							$ctp = round(((($value - ${$tick . "buyvalue"})/${$tick . "buyvalue"})*100),3);
							print "\t S:$cdastatus \tBV:${$tick . "buyvalue"} CTP:$ctp";
						}
						else
						{
							print "\tS:$cdastatus";
						}
						if(${$tick . "lasttrade"} == 1)
						{
							print "   LastTPC:${$tick . "lasttpc"}";
						}
						print "\n";

						// Trading rules start here
						// ========================

						// CDA is trending up so set to buyprep
						if(${$tick . "buyflag"} == $buyready AND ${$tick . "strend"}==3 AND ${$tick . "mtrend"}==3 AND ${$tick . "ltrend"}==3)
						{
							printf("%-9s",$key);
							print "Was Buyready, now Buyprep V:$value\n";
							${$tick . "buyflag"} = $buyprep;
						}

						// CDA was buyprep, now trending down, set to buyord if reasonable spread
						if(${$tick . "buyflag"} == $buyprep AND ${$tick . "strend"}==1 AND ${$tick . "mtrend"}==1 AND ${$tick . "ltrend"}==1 AND ${$tick . "lspread"} >= $minspread)
						{
							printf("%-9s",$key);
							print "Was Buyprep, now Buyord V:$value\n";
							${$tick . "buyflag"} = $buyord;
						}

						// CDA stopped trending down and is ready to buy
						if(${$tick . "buyflag"} == $buyord AND ${$tick . "strend"}!=1 AND ${$tick . "mtrend"}!=1)
						{
							if(${$tick . "rsi"} <= $minrsi)
							{
								printf("%-9s",$key);
								print "Was Buyord, now Buy V:$value\n";
								// Assume we buy at the current value
								${$tick . "buyvalue"} = $value;
								${$tick . "buyflag"} = $sellok;
								$fh = fopen($tradefile, "a") or die("Cannot open file");
								fwrite($fh, "========================== \n");
								fwrite($fh, "Runtime $run_time \n");
								fwrite($fh, "Buy on $key BV:${$tick . "buyvalue"} \n");
								fwrite($fh, "========================== \n");
								fclose($fh);
							}
							else
							{
								printf("%-9s",$key);
								print "RSI Check not meeting Minimum, resetting back to Buy Prep\n";
								${$tick . "buyflag"} = $buyprep;
							}
						}

						// Buy Order on CDA placed, do order tracking here to make sure order completes
						if(${$tick . "buyflag"} == $sellok)
						{
							// Since we are not placing an order, we just assume it completed
							${$tick . "buyflag"} = $sellready;
						}

						// CDA is sellready and is no longer trending upwards - time to sell
						if(${$tick . "buyflag"} == $sellready AND ${$tick . "strend"}!=3 AND ${$tick . "mtrend"}!=3)
						{
							// Assume we sell at the current value and if not meeting our sell buffer, we HODL
							$cdabuff = ${$tick . "buyvalue"} * $sellbuffer;
							if($value > $cdabuff)
							{
								${$tick . "sellvalue"} = $value;
								${$tick . "buyflag"} = $selldone;
							}
							else
							{
								printf("%-9s",$key);
								print "Did not meet minimum sell buffer, so HODL!\n";
							}
						}
						// Stop Loss
						$cdastoploss = ${$tick . "buyvalue"} * $stoplossbuffer;
						if(${$tick . "buyflag"} == $sellready AND ($value < $cdastoploss))
						{
							printf("%-9s",$key);
							print "hit the stop loss buffer, so we are selling out\n";
							${$tick . "sellvalue"} = $value;
							${$tick . "buyflag"} = $selldone;							
						}
						// CDA is selldone
						if(${$tick . "buyflag"} == $selldone)
						{
							// Sell Order on CDA placed, do order tracking here to make sure order completes
							// Since we are not placing an order, we just assume it completed
							$q = round((((${$tick . "sellvalue"} - ${$tick . "buyvalue"})/${$tick . "buyvalue"})*100),3);
							$tpc = $q - 0.2;
							$rpc = $rpc + $tpc;
							${$tick . "lasttrade"} = 1;
							${$tick . "lasttpc"} = $tpc;
							${$tick . "buyflag"} = $buyready;
							printf("%-9s",$key);
							print "Sell Done BV:${$tick . "buyvalue"} SV:${$tick . "sellvalue"} TPC:$tpc \n";
							$fh = fopen($tradefile, "a") or die("Cannot open file");
							fwrite($fh, "========================== \n");
							fwrite($fh, "Runtime $run_time \n");
							fwrite($fh, "Sell Done on $key BV:${$tick . "buyvalue"} SV:${$tick . "sellvalue"} TPC:$tpc RPC:$rpc \n");
							fwrite($fh, "========================== \n");
							fclose($fh);
						}
					}
				}
			}
		}
	}
	sleep(5);
}
?>

Did i put the change in the wrong place?


#311

Hi all,

Sorry about the logic error on the stoploss, but looks like you guys figured it out.

However, for those watching the trades, you probably noticed that after it bought a CDA, it did trend up, but never reached the 2% goal.

So, instead of trying to reach the goal, try setting the sellbuffer to 1.003, which after fees would be a 0.1% gain. You might have noticed that on a regular basis it does reach more than 0.3% uptrend, but rarely would reach a 2% uptrend, especially when the market is moving downwards and we are trying to use short time frames. So if the signal is there to sell, we should just take the money and run.

See how you go.

Updated code, removed stoploss to see how far it can loose by, also changed buyflag to tradeflag and made it a bit more aggressive.

<?php
require 'vendor/autoload.php';

// This bot will trade USDT to the other pairs and back to make more USDT
// It shall use all the trading pairs to make more UDST except the ones we tell it not to use

$strend = 50;             // Short Term Trend - must be less than $mtrend
$mtrend = 90;             // Medium Term Trend - must be less than $ltrend
$ltrend = 120;            // Long Term Trend
$tradefile = "USDT.txt";  // The Trade Logging file name
$minspread = 1;           // The minimum spread percentage needed for a trade
$minrsi = 46;             // Relative Strength must be below this number to buy
$sellbuffer = 1.003;      // Create a buffer to hold CDA if sell is not profitable

// Do not change any of the flags, we use this to signal the bot what to do and when
$buyready = 0;            // This flag signals the bot that the pair meets rules to buy
$buyprep = 1;             // This flag signals the bot to prepare to buy
$buyord = 2;              // This flag signals the bot to place an order
$sellok = 3;              // This flag signals the bot that the order was completed
$sellready = 4;           // This flag signals the bot to sell
$selldone = 5;            // This flag signals the bot the trade completed
$dontbuy = 6;             // This flag signals the bot we dont want to trade BCASH :P

// Standard variables and arrays we use
$i = 0;
$binance_prices = array();
$time_start = time();
$time_end = 0;
$run_time = 0;
$rpc = 0;
$tpc = 0;
$q = 0;

// API call to fetch pricing data from Binance
function getprices()
{
	$api = new Binance\API("<api key>","<secret>");
	$mp = $api->prices();
	return $mp;
}

// Start of the Loop - can run for months - press CTRL-C to stop
for($i = 0; $i <= 2000000; $i++)
{
	$time_end = time();
	$run_time = round((($time_end - $time_start)/60),2);
	print "====================================\n";
	print "Iteration = $i \n";
	print "Running Time: $run_time mins \n";
	print "Current running percentage = $rpc \n";
	print "====================================\n";

	// Fetch current prices from Binance
	$binance_prices = getprices();

	// Loop through the price data as key and value pairs
	foreach($binance_prices as $key => $value)
	{

		// Only process pairs with USDT
		if(strpos($key, "USDT"))
		{

			// Convert the pair name to lower case in varibale $tick
			// for example the name "BTCUSDT" will become "btcusdt"

			$tick = strtolower($key);

			// For the first iteration, create arrays and varibales for the bot
			if($i == 0)
			{

				// Use the lower case name to form the leading part of varibales and arrays
				// for exmaple, using "btcusdt" and adding "st" for the short term array
				// will initialise an array called "btcusdtst"
				// as we loop thorugh the pairs, each one gets created for each pair
				// for exmaple "NEOUSDT" will become "neousdtst"

				${$tick . "st"} = array();           // Short Term Array
				${$tick . "mt"} = array();           // Medium Term Array
				${$tick . "lt"} = array();           // Long Term Array
				${$tick . "stavg"} = 0;              // Short Term Moving Average
				${$tick . "mtavg"} = 0;              // Medium Term Moving Average
				${$tick . "ltavg"} = 0;              // Long Term Moving Average
				${$tick . "strend"} = 0;             // Short Term Moving Trend
				${$tick . "mtrend"} = 0;             // Medium Term Moving Trend
				${$tick . "ltrend"} = 0;             // Long Term Moving Trend
				${$tick . "lspread"} = 0;            // Long Term Spread
				${$tick . "rsi"} = 0;                // Relative Strength Indicator for this pair
				${$tick . "tradeflag"} = $buyready;  // Set this pair to buyready
				${$tick . "buyvalue"} = 0;           // record what we buy for on this pair
				${$tick . "sellvalue"} = 0;          // record what we sell for on this pair
				${$tick . "lasttrade"} = 0;          // record we have had one trade done
				${$tick . "lasttpc"} = 0;            // record what percentage last the trade was
				${$tick . "isset"} = 1;              // used to signal we are initialised for this pair
			}

			// We are not on the first loop anymore, we proceed with processing the data
			else
			{

				// Exclude List - these ones we do not trade

				if($key == "BCHABCUSDT") ${$tick . "tradeflag"} = $dontbuy;
				if($key == "BCHSVUSDT") ${$tick . "tradeflag"} = $dontbuy;
				if($key == "BCCUSDT") ${$tick . "tradeflag"} = $dontbuy;
				if($key == "TUSDUSDT") ${$tick . "tradeflag"} = $dontbuy;
				if($key == "VENUSDT") ${$tick . "tradeflag"} = $dontbuy;
				if($key == "PAXUSDT") ${$tick . "tradeflag"} = $dontbuy;

				// Check if the trading pair has been initialised
				// this covers if Binance add a new trading pair on USDT while we are running
				// if Binance adds new trading pairs while bot is running, we shall
				// ignore them and only use the ones since the bot was started and initialised

				if(isset(${$tick . "isset"}))
				{

					// Push data into arrays and shift arrays once we have enough data
					array_push(${$tick . "st"}, $value);
					array_push(${$tick . "mt"}, $value);
					array_push(${$tick . "lt"}, $value);
					if($i > $strend) array_shift(${$tick . "st"});
					if($i > $mtrend) array_shift(${$tick . "mt"});
					if($i > $ltrend) array_shift(${$tick . "lt"});

					// Wait until we have all the arrays populated with data
					if($i <= $ltrend)
					{
						printf("%-9s",$key);
						print "\t:Loading Arrays with data\n";
					}

					// Arrays are populated, so on with the processing
					else
					{

						// Calculate the Moving Average for the 3 arrays
						${$tick . "stavg"} = round((array_sum(${$tick . "st"})/$strend),8);
						${$tick . "mtavg"} = round((array_sum(${$tick . "mt"})/$mtrend),8);
						${$tick . "ltavg"} = round((array_sum(${$tick . "lt"})/$ltrend),8);

						// Check if the Short Term Trend is trending down, flat or up
						// We use the current price to see if it is above or below the short term moving average
						// We use "1" to signal it is trending down, "2" for flat, "3" for trending up
						if($value < ${$tick . "stavg"}) ${$tick . "strend"} = 1;
						if($value == ${$tick . "stavg"}) ${$tick . "strend"} = 2;
						if($value > ${$tick . "stavg"}) ${$tick . "strend"} = 3;

						// Check if the Medium Term Trend is trending down, flat or up
						// We use the short term moving average to see if it is above or below the medium term moving average
						// We use "1" to signal it is trending down, "2" for flat, "3" for trending up
						if(${$tick . "stavg"} < ${$tick . "mtavg"}) ${$tick . "mtrend"} = 1;
						if(${$tick . "stavg"} == ${$tick . "mtavg"}) ${$tick . "mtrend"} = 2;
						if(${$tick . "stavg"} > ${$tick . "mtavg"}) ${$tick . "mtrend"} = 3;

						// Check if the Long Term Trend is trending down, flat or up
						// We use the medium term moving average to see if it is above or below the long term moving average
						// We use "1" to signal it is trending down, "2" for flat, "3" for trending up
						if(${$tick . "mtavg"} < ${$tick . "ltavg"}) ${$tick . "ltrend"} = 1;
						if(${$tick . "mtavg"} == ${$tick . "ltavg"}) ${$tick . "ltrend"} = 2;
						if(${$tick . "mtavg"} > ${$tick . "ltavg"}) ${$tick . "ltrend"} = 3;

						// Calculate the Medium Term spread, which is the percentage difference between
						// the highest recorded price and the lowest recorded price in the Medium Term Array
						$mlow = min(${$tick . "lt"});
						$mhigh = max(${$tick . "lt"});
						${$tick . "lspread"} = round(((1-($mlow/$mhigh))*100),3);
						
						// Calculate the Relative Strength Indicator on the Long Term Array
						// A Low RSI indicates a buy opportunity
						$rsitck = 0;
						${$tick . "gain"} = array();
						${$tick . "loss"} = array();
						foreach(${$tick . "lt"} as $cdaval)
						{
							if($rsitck == 0)
							{
								$cdagain = 0;
								$cdaloss = 0;
							}
							else
							{
								if($cdaval == $cdaprev)
								{
									$cdagain = 0;
									$cdaloss = 0;
								}
								elseif($cdaval > $cdaprev)
								{
									$cdacalc = $cdaval - $cdaprev;
									$cdagain = number_format($cdacalc,8);
									$cdaloss = 0;
								}
								else
								{
									$cdacalc = $cdaprev - $cdaval;
									$cdaloss = number_format($cdacalc,8);
									$cdagain = 0;
								}
							}
							array_push(${$tick . "gain"}, $cdagain);
							array_push(${$tick . "loss"}, $cdaloss);
							$cdaprev = $cdaval;
							$rsitck++;
						}
						$cdarsgain = (array_sum(${$tick . "gain"})) / $ltrend;
						$cdarsloss = (array_sum(${$tick . "loss"})) / $ltrend;
						if($cdarsloss > 0)
						{
							${$tick . "rsi"} = round(100-(100/(1+($cdarsgain/$cdarsloss))),3);
						}
						else
						{
							${$tick . "rsi"} = 100;
						}

						// Print out what we have so far so we can see what is going on
						printf("%-9s",$key);
						print "\tV:";
						printf("%-14.8F",$value);
						print "\t  ST:";
						printf("%-14.8F",${$tick . "stavg"});
						if(${$tick . "strend"} == 1) printf("%-5s",":DOWN");
						if(${$tick . "strend"} == 2) printf("%-5s",":FLAT");
						if(${$tick . "strend"} == 3) printf("%-5s",":UP");
						print "\t  MT:";
						printf("%-14.8F",${$tick . "mtavg"});
						if(${$tick . "mtrend"} == 1) printf("%-5s",":DOWN");
						if(${$tick . "mtrend"} == 2) printf("%-5s",":FLAT");
						if(${$tick . "mtrend"} == 3) printf("%-5s",":UP");
						print "\t  LT:";
						printf("%-14.8F",${$tick . "ltavg"});
						if(${$tick . "ltrend"} == 1) printf("%-5s",":DOWN");
						if(${$tick . "ltrend"} == 2) printf("%-5s",":FLAT");
						if(${$tick . "ltrend"} == 3) printf("%-5s",":UP");
						print "\t  SPREAD:";
						printf("%-03.3F",${$tick . "lspread"}); 
						print "%\t  RSI:";
						printf("%-06.3F",${$tick . "rsi"});
						if(${$tick . "tradeflag"} == $buyready) $cdastatus = "Buy Ready";
						if(${$tick . "tradeflag"} == $buyprep) $cdastatus = "Buy Prep";
						if(${$tick . "tradeflag"} == $buyord) $cdastatus = "Buy Order";
						if(${$tick . "tradeflag"} == $sellok) $cdastatus = "Sell OK";
						if(${$tick . "tradeflag"} == $sellready) $cdastatus = "Sell Ready";
						if(${$tick . "tradeflag"} == $selldone) $cdastatus = "Sell Done";
						if(${$tick . "tradeflag"} == $dontbuy) $cdastatus = "Dont Trade";
						if(${$tick . "tradeflag"} == $sellready)
						{
							$ctp = round(((($value - ${$tick . "buyvalue"})/${$tick . "buyvalue"})*100),3);
							print "\t S:$cdastatus \tBV:${$tick . "buyvalue"} CTP:$ctp";
						}
						else
						{
							print "\tS:$cdastatus";
						}
						if(${$tick . "lasttrade"} == 1)
						{
							print "   LastTPC:${$tick . "lasttpc"}";
						}
						print "\n";

						// Trading rules start here
						// ========================

						// CDA is trending up so set to buyprep
						if(${$tick . "tradeflag"} == $buyready AND ${$tick . "strend"}==3 AND ${$tick . "mtrend"}==3 AND ${$tick . "ltrend"}==3)
						{
							printf("%-9s",$key);
							print "Was Buyready, now Buyprep V:$value\n";
							${$tick . "tradeflag"} = $buyprep;
						}

						// CDA was buyprep, now trending down, set to buyord if reasonable spread
						if(${$tick . "tradeflag"} == $buyprep AND ${$tick . "strend"}==1 AND ${$tick . "mtrend"}==1 AND ${$tick . "ltrend"}==1 AND ${$tick . "lspread"} >= $minspread)
						{
							printf("%-9s",$key);
							print "Was Buyprep, now Buyord V:$value\n";
							${$tick . "tradeflag"} = $buyord;
						}

						// CDA stopped trending down and is ready to buy
						if(${$tick . "tradeflag"} == $buyord AND ${$tick . "strend"}!=1 AND ${$tick . "mtrend"}!=1)
						{
							if(${$tick . "rsi"} <= $minrsi)
							{
								printf("%-9s",$key);
								print "Was Buyord, now Buy V:$value\n";
								// Assume we buy at the current value
								${$tick . "buyvalue"} = $value;
								${$tick . "tradeflag"} = $sellok;
								$fh = fopen($tradefile, "a") or die("Cannot open file");
								fwrite($fh, "========================== \n");
								fwrite($fh, "Runtime $run_time \n");
								fwrite($fh, "Buy on $key BV:${$tick . "buyvalue"} \n");
								fwrite($fh, "========================== \n");
								fclose($fh);
							}
							else
							{
								printf("%-9s",$key);
								print "RSI Check not meeting Minimum, resetting back to Buy Prep\n";
								${$tick . "tradeflag"} = $buyprep;
							}
						}

						// Buy Order on CDA placed, do order tracking here to make sure order completes
						if(${$tick . "tradeflag"} == $sellok)
						{
							// Since we are not placing an order, we just assume it completed
							${$tick . "tradeflag"} = $sellready;
						}

						// CDA is sellready and is no longer trending upwards - time to sell
						if(${$tick . "tradeflag"} == $sellready AND ${$tick . "strend"}!=3 AND ${$tick . "mtrend"}!=3)
						{
							// Assume we sell at the current value and not sell if not meeting a minimum
							$cdabuff = ${$tick . "buyvalue"} * $sellbuffer;
							if($value > $cdabuff)
							{
								${$tick . "sellvalue"} = $value;
								${$tick . "tradeflag"} = $selldone;
							}
							else
							{
								printf("%-9s",$key);
								print "Did not meet minimum sell amount\n";
							}
						}

						// CDA is selldone
						if(${$tick . "tradeflag"} == $selldone)
						{
							// Sell Order on CDA placed, do order tracking here to make sure order completes
							// Since we are not placing an order, we just assume it completed
							$q = round((((${$tick . "sellvalue"} - ${$tick . "buyvalue"})/${$tick . "buyvalue"})*100),3);
							$tpc = $q - 0.2;
							$rpc = $rpc + $tpc;
							${$tick . "lasttrade"} = 1;
							${$tick . "lasttpc"} = $tpc;
							${$tick . "tradeflag"} = $buyready;
							printf("%-9s",$key);
							print "Sell Done BV:${$tick . "buyvalue"} SV:${$tick . "sellvalue"} TPC:$tpc \n";
							$fh = fopen($tradefile, "a") or die("Cannot open file");
							fwrite($fh, "========================== \n");
							fwrite($fh, "Runtime $run_time \n");
							fwrite($fh, "Sell Done on $key BV:${$tick . "buyvalue"} SV:${$tick . "sellvalue"} TPC:$tpc RPC:$rpc \n");
							fwrite($fh, "========================== \n");
							fclose($fh);
						}
					}
				}
			}
		}
	}
	sleep(5);
}
?>

Cheers…

Mike Fishy


#312

Code looks ok and the markets are a lot calmer than they were earlier in the day. Do you have any BV entries showing in PowerShell as seen in the screenshot below?

@Mike_Fishy by tweaking the “CDA is sellready and is no longer trending upwards - time to sell” rule I just got my first gain 2.845% after fees for ADA.


#313

Presumably without a SL it can wipe you out, no?


#314

Sorry guys, noob here, very dumb question:
So we run the script in PowerShell, where do we put the username/password for it to access the Binance account to start the bot trade?


#315

The goal is to see if we can make some short term gains, otherwise just hold it and see how far it can loose by.

Remember, the markets are often like a rubber band, after the rubber band is stretched, it wants to go back where it was under normal circumstances. However, when the market is being manipulated, you want to see by how much, this helps in defining rules that will be used later, but we need to see it so we can formulate those rules.

Edit: with the right rules in place, your bot would do what mine did over the last 21hrs.

bot-rules

If I just gave you the code, you wouldn’t know how to maintain it and what many of the rules are for, this is why I am doing it this way, so you learn what they are all for, then you will know how to maintain it and even improve on it.

Stay Fishy


#316

LATER it will be 2 keys NOT username and pwd but for data collection nothing is needed.


#317

Are these the two keys that we need to start it?
$api = new Binance\API("","");

Thanks!


#318

Yes much later they will go in there. Do you have API keys enabled in your Binance account?


#319

No, I don’t have them enabled, I will take a look.
Another question, is there minimum USDT required to start the bot?
Thanks!


#320

@Mike_Fishy can you please incorporate the replay code in your future scripts as provided by @mwlang


#321

Nothing at all, just says Buy Prep in each one (apart from the don’t trade) Weird as the other 2 seemed to be running fine.


#322

For live trading yes but not at this stage. Each ticker has a modest min trade value. Looked to be about $40 when I did my first live trade the other day. So at 17 tickers maybe around $700 min.


#323

Is the 19.314% just a net sum of each trade rather than your capital increase i.e. capitial increase is approx 19.314 / 17 tickers?