Skip to content

Instantly share code, notes, and snippets.

@fellowgeek
Created October 22, 2018 02:11
Show Gist options
  • Save fellowgeek/9f763c82aaaa316ac201d4a8af26898e to your computer and use it in GitHub Desktop.
Save fellowgeek/9f763c82aaaa316ac201d4a8af26898e to your computer and use it in GitHub Desktop.
Simple Trade Simulator
<style>
.win {
color: #00cf00;
}
.loose {
color: #cf0000;
}
</style>
<h1>Trade simulator</h1>
<pre>
<?php
$numberOfTrades = 1000;
$maxGain = 150;
$maxLoss = 300;
$startingBalance = 10000;
$chanceOfSuccess = 70;
for($i = 0; $i < $numberOfTrades; $i++) {
$random = rand(1,100);
$isTradeSuccessfull = false;
if($random <= $chanceOfSuccess) {
$isTradeSuccessfull = true;
}
print("\n" . ($isTradeSuccessfull ? 'Success' : 'Failed '));
if($isTradeSuccessfull == true) {
print(" +<span class=\"win\">" . $maxGain . "</span>");
$startingBalance = $startingBalance + $maxGain;
}
if($isTradeSuccessfull == false) {
print(" -<span class=\"loose\">" . $maxLoss . "</span>");
$startingBalance = $startingBalance - $maxLoss;
}
print(" | Balance: " . $startingBalance);
if($startingBalance <= 0) {
print("\nAccount balance is zero or lower: " . $startingBalance);
break;
}
}
print("\nBalance: " . $startingBalance);
?>
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment