Mike Fishy and Automation System Programming (Bots)


#243

I used Windows 10 originally just like the instructions above, but also got an error. The photo is from when I tried it on one of my Window 7 laptops (error type might be different). Might just learn how this stuff works first, instead of just copying and pasting :laughing:


#244

Awesome…now I know what github does I am all in… Making an account.

Starting to get the hang of php… just a new language to learn.

I was really quite surprised at the time() command that returns the number of seconds from Jan 1st 1970. Due to size limitation we run out of seconds in 2038.

Awesome … am I a nerd or what… kudos…


#245

@Mike_Fishy Great to keep our first trading bot which really makes trade up and running. Still a lot of work to do in the rules part I think.
Running the bot several times even with your suggested mtrend change leads to only negatieve results. I am looking forward to the next lessen and post. Have a nice weekend for now man! :slight_smile: :slight_smile:


#246

Maclovin.

In more detail to the personal message now I am at my PC, I was struggling to get things working until I opened up

Control Panel >User Accounts>User Account Control Settings

I have then changed the settings to never notify. as this then allows you to change things to your PC without notifications. When your PC notifies you it stops the code working.

Also ensure that you are logged into your PC with full Adminstration Rights.

Hopefully this gets you away. I am really excited about this project.

Cheers.

Crash


#247

It would help if we could at all/ more trading pairs. It takes hours now to get 1 trade happig. Would improve the testing. Thanks man! :slight_smile: :slight_smile:


#248

User93 have you signed up for github? Have a look at this great application? There is lots to do and lots to understand.

I like to understand how things work so I can fiddle with it. Currently I am still making notes to the first bot model. Everything I look up and understand makes me understand more.

I have 100% belief if I work hard enough at this, everything will be fine and I will get a decent bot working how I want it to.

My aim is to make it sufficiently good so my partner can keep an eye on it when I am at work. She is not very computer savy and the on button is about all she knows.

Also, have you figured out why it is reading a negative answer? That would be a good thing to work out.

Cheers

Crash


#249

For those trying to rapidly iterate, this version of the script may be useful for you…this version of the script will record all pricing to USDT_prices.txt file while $replay = 0;

Run it for a while to collect pricing data and then stop it and change $replay = 1;

In replay mode, it will serve up the prices from the recorded prices file until end of file is reached and will run very, very fast. Which means, you can toy with settings and quickly try new scenarios.

<?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 = 100;            // Medium Term Trend - must be less than $ltrend
$ltrend = 250;            // Long Term Trend
$tradefile = "USDT.txt";  // The Trade Logging file name
$minspread = 0.6;         // The minimum spread percentage needed for a trade
$minrsi = 46;             // Relative Strength must be below this number to buy

// 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
$pricefh = NULL;

// Standard variables and arrays we use
$replay = 0;
$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()
{
  global $replay;
  global $pricefh;
  
  if ($replay == 0) {
    $api = new Binance\API("<api key>","<secret>");
    $mp = $api->prices();

    $pricefh = fopen("USDT_prices.txt", "a") or die("Cannot open file");
    fwrite($pricefh, serialize($mp) . "\n");
    fclose($pricefh);
  }
  else
  {
    if (!$pricefh) {
      $pricefh = fopen("USDT_prices.txt", "r") or die("Cannot open replay file");
    }
    $mp = unserialize(fgets($pricefh));
    if ($mp == NULL) die("end of price data reached.\n");
  }
  return $mp;
}

if ($replay == 1) 
{
  $simulation_mode = "replaying"; 
}
else 
{
  $simulation_mode = "real-time";
  unlink("USDT_prices.txt");
}

// 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 ($simulation_mode) \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 . "mspread"} = 0;          // Medium 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 . "isset"} = 1;            // used to signal we are initialised for this pair
        ${$tick . "stoploss"} = 0; // MWLANG
      }

// 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 . "mt"});
            $mhigh = max(${$tick . "mt"});
            ${$tick . "mspread"} = 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);
              if(${$tick . "rsi"} == 0) ${$tick . "rsi"} = 0;
            }
            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 . "mspread"}); 
            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";
            print "\tS:$cdastatus \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 . "mspread"} >= $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 . "mtrend"}!=1 AND ${$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);
            }

// 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;
            }

// **********************            
// **** START CHANGES ***
// **********************            

// Track Selling Stop Loss
// $pl is unrealized profit/loss of current holding
// $new_sl is current value trailing by 0.5% half a percent (1 - 0.005, thus 0.995)
            if(${$tick . "buyvalue"} > 0) {
              
              $pl = round(((($value - ${$tick . "buyvalue"})/${$tick . "buyvalue"})*100),3);
              $new_sl = round($value * 0.995, 6);

              if(${$tick . "buyflag"} == $sellready AND $pl > 2.0 AND ${$tick . "stoploss"} < $new_sl)
              {
                ${$tick . "stoploss"} = ($value * 0.9985);
              }

              if (${$tick . "buyflag"} == $sellready && $value <= ${$tick . "stoploss"})
              {
                ${$tick . "sellvalue"} = $value;
                ${$tick . "buyflag"} = $selldone;
                ${$tick . "stoploss"} = 0;
              }
            }

// **********************            
// ***** END CHANGES ****
// **********************            

// 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
              ${$tick . "sellvalue"} = $value;
              ${$tick . "buyflag"} = $selldone;
              ${$tick . "stoploss"} = 0; // MWLANG
            }

// 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 . "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 \n");
              fwrite($fh, "========================== \n");
              fclose($fh);
            }
          }
        }
      }
    }
  }
  if ($replay == 0) 
  {
    sleep(5);
  }
}
?>

NOTE: I didn’t try to comment everywhere I made changes this time as I’m just toying around. Just look for $replay occurring in the code and you’ll find most all changes I made readily enough.


#250

@mwlang

Sublime works good… Thanks.


#251

Thank you for the Mac instructions @mwlang! I couldn’t get my Windows 10 to run the PHP commands, it just returned absolutely nothing as per screenshot below. @Mike_Fishy’s quote (in gray) shows the PHP 7.2.12 response, mine (in blue immediately below) returned nothing:

I wonder if I manually have to turn on a service or services in Windows.


#253

@Garzaro, I wish I could help you, but I’ve never attempted to run PHP in Windows before. Best bet is Mike or one of the others here working from Windows.

Even so, the fact that you get back a blank prompt seems to indicate to me a php.exe exists and did execute. Maybe uninstall, re-download, verify it’s an officially released and untampered file and then re-install with the new downloaded file.


#254

Hi there,

Please check that you are an Administrator on your PC.

I have installed it on Windows 10. If you sort out your PC to run without notifications this will help. Control Panel … security and maintenance… user account control settings… Set to never notify(not recommended is it’s description)… oximoron… it is recommended win10 as if it’s not on never notify the code will run correctly.

I would start at the very beginning of Mike fishy instructions. It does work.


#255

Hi @Garzaro,
I had the same issue. You’ll need to install this first.
https://www.microsoft.com/en-us/download/details.aspx?id=48145


#256

Yes I did the same a week ago. This indeed is the solution man! :slight_smile: :slight_smile:


#257

Look familiar?


Mike’s “ICX” code converted to C++ running on a $3 MCU with built-in WiFi and interfaced to an Android phone via a cloud server.
I did my first API trade with Binance yesterday.
Just need to make a decent set of rules for the bot now.


#258

image


#259

Change the Name? I think it looks amazing!!

I can think of

“THEBITCOIN.PUB MONEYMAKER”

it might be a little long though!!!


#260

thebitcoin.pub is about the maximum number of visible characters.


#261

Blynker, it looks well cool.

Will need to learn C++ next me thinks… I am having a very interesting time with php… smiles.

Crash


#262


#263

awesome… looks well cool… kudos.

Gosh… I tried to click on the buttons… dooh… it didnt work… not impressed with your bot mate ;p

Crash