Last active
April 28, 2024 05:15
-
-
Save ammarfaizi2/265b2e306bdcb637aecc8d3839c86995 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function gen_grid(float $total_value, float $total_amount, | |
int $nr_sell_grid, float $sell_percent_interval, | |
float $sell_percent_init_margin, | |
float $total_prep_value, int $nr_buy_grid, | |
float $buy_percent_interval, float $buy_percent_init_margin) | |
{ | |
$sell_lines = []; | |
$avg_price = $total_value / $total_amount; | |
if ($total_value > 0 && $total_amount > 0 && $nr_sell_grid > 0) { | |
$pos_amt = $total_amount; | |
$out_acc = 0; | |
$tmp_total_amount = $total_amount; | |
$sell_interval = $sell_percent_interval / 100; | |
$sell_amount_per_line = round($total_amount / $nr_sell_grid); | |
$price_to_sell = $avg_price + ($avg_price * ($sell_percent_init_margin / 100)); | |
for ($i = 0; $i < $nr_sell_grid; $i++) { | |
$amount_to_sell = min($sell_amount_per_line, $tmp_total_amount); | |
$out_val = $price_to_sell * $amount_to_sell; | |
$tmp_total_amount -= $amount_to_sell; | |
$out_acc += $out_val; | |
$pos_amt -= $amount_to_sell; | |
$sell_lines[] = [ | |
"price" => $price_to_sell, | |
"amount" => $amount_to_sell, | |
"pos_amt" => $pos_amt, | |
"out_val" => $out_val, | |
"out_acc" => $out_acc, | |
"per_favg" => (($price_to_sell - $avg_price) / $avg_price) * 100 | |
]; | |
$price_to_sell += $price_to_sell * $sell_interval; | |
if ($tmp_total_amount <= 0) | |
break; | |
} | |
} | |
$buy_lines = []; | |
if ($total_prep_value > 0 && $nr_buy_grid > 0) { | |
$amt_acc = 0; | |
$spend_acc = 0; | |
$buy_interval = $buy_percent_interval / 100; | |
$spend_per_line = $total_prep_value / $nr_buy_grid; | |
$price_to_buy = $avg_price - ($avg_price * ($buy_percent_init_margin / 100)); | |
for ($i = 0; $i < $nr_buy_grid; $i++) { | |
$amount_to_buy = $spend_per_line / $price_to_buy; | |
$spend_acc += $spend_per_line; | |
$amt_acc += $amount_to_buy; | |
$buy_lines[] = [ | |
"price" => $price_to_buy, | |
"amount" => $amount_to_buy, | |
"spend" => $spend_per_line, | |
"spend_acc" => $spend_acc, | |
"amt_acc" => $amt_acc, | |
"per_favg" => (($price_to_buy - $avg_price) / $avg_price) * 100, | |
]; | |
$price_to_buy -= $price_to_buy * $buy_interval; | |
} | |
} | |
return [ | |
"sell_lines" => $sell_lines, | |
"buy_lines" => $buy_lines | |
]; | |
} | |
$total_usdt = 398; | |
$total_mew = 74127; | |
$nr_sell_grid = 12; | |
$sell_percent_interval = 1; | |
$sell_percent_init_margin = 10; | |
$total_prep_usdt = 100; | |
$nr_buy_grid = 10; | |
$buy_percent_interval = 1; | |
$buy_percent_init_margin = 10; | |
$ret = gen_grid($total_usdt, $total_mew, | |
$nr_sell_grid, $sell_percent_interval, | |
$sell_percent_init_margin, | |
$total_prep_usdt, $nr_buy_grid, | |
$buy_percent_interval, | |
$buy_percent_init_margin); | |
foreach ($ret["sell_lines"] as $key => $grid) { | |
printf("p = %.6f; amt = %.2f; out = %.2f; out_acc = %.2f; per_favg = %.2f; pos_amt = %.2f;\n", | |
$grid["price"], $grid["amount"], $grid["out_val"], | |
$grid["out_acc"], $grid["per_favg"], $grid["pos_amt"]); | |
} | |
printf("\n================================================================================\n\n"); | |
foreach ($ret["buy_lines"] as $key => $grid) { | |
printf("p = %.6f; amt = %.2f; spend = %.2f; spend_acc = %.2f; per_favg = %.2f; amt_acc = %.2f;\n", | |
$grid["price"], $grid["amount"], $grid["spend"], | |
$grid["spend_acc"], $grid["per_favg"], $grid["amt_acc"]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment