Comments (7)
Hi @HarmJan1990
Can you provide at least the code that fail and the "error message", we've got only stack trace here.
from coinbase-pro-sdk.
Without seeing your code, I think the first thing to look at is getOrderByIdRaw() which would not find the order in question. It seems to me that the "local.ERROR NOT FOUND" is being brought up by the coinbase server which is not finding the order in question.
Do you have a try catch block in place to handle this kind of case?
from coinbase-pro-sdk.
Hi @HarmJan1990
$placeOrder = $coinbase->orders()->placeOrder($marketOrder);
if(isset($placeOrder)) { $marketOrder = $coinbase->orders()->getOrderById($placeOrder->getId());
First, but not really related, I think it is kind of duplicate here between placing the order and get it back a second time form server.
Not releated again, but please, use early return in your code for better reading and understanding, refacto example. Also I'll point out on error in your code (at the end, see Order::create) :
<?php
use MockingMagician\CoinbaseProSdk\CoinbaseFacade;
use MockingMagician\CoinbaseProSdk\Contracts\Connectivity\OrdersInterface;
use MockingMagician\CoinbaseProSdk\Functional\Build\MarketOrderToPlace;
$coinbase = CoinbaseFacade::createDefaultCoinbaseApi(
'https://api.exchange.coinbase.com',
$api_key,
$api_secret,
$api_passphrase
);
/** replace this code by the two lines behind
foreach ($accounts as $account) {
if($account->getCurrency() == 'EUR') {
$checkBalance = $account->getAvailableFunds();
}
}
*/
$eurAccount = $coinbase->accounts()->getAccount('EUR');
$balance = $eurAccount->getAvailableFunds();
/** first early return */
if($balance < $strategy->amount) {
return; //stop program or run exception or what you want
}
// like an implicit 'else' here, continue we have enough in balance, and it is according to strategy
foreach($markets as $market) {
$amount_used = $market->percentage * $percentagepoint;
$marketOrder = CoinbaseFacade::createMarketOrderToPlace(
MarketOrderToPlace::SIDE_BUY,
$market->market,
null,
$amount_used
);
try {
$placeOrder = $coinbase->orders()->placeOrder($marketOrder);
} catch (\MockingMagician\CoinbaseProSdk\Functional\Error\ApiError $exception) {
// here you should log why that failed
$logger->error('placing order has failed', [
'market' => $market->market,
'amount_used' => $amount_used,
//... etc ...
]);
continue; // continue for next market, else "place order" is OK no need for another check,
}
// except maybe checking if still pending...
while ($placeOrder->getStatus() === OrdersInterface::STATUS_PENDING) {
// may be you can wait here, like a sleep
usleep(250000);
// the update $placeOrder
try {
$placeOrder = $coinbase->orders()->getOrderById($placeOrder->getId());
} catch (\MockingMagician\CoinbaseProSdk\Functional\Error\ApiError $exception) {
// here you should log why that failed
$logger->error('fetching order with id ' . $placeOrder->getId() . ' has failed', [
'place_order' => $placeOrder->getId(),
// etc ...
]);
continue 2; // continue for next market, else place order is OK no need for another check,
}
}
$fills = $coinbase->fills()->listFills($placeOrder->getId());
/*
also, this deducing price method is not accurate
if first fill represent 0.1% of the volume at let say a price of 100 eur
and second fill represent 99.9% of the volume let say a price 200 eur
with your method, you calculate as a price of 150, but in reality it is almost 200
*/
$price = 0;
$pricecount = 0;
foreach($fills as $fill) {
$price += $fill->getPrice();
$pricecount++;
}
if($pricecount > 0) {
$price /= $pricecount;
}
Order::create([
'external_id' => $placeOrder->getId(),
'market' => $market->market, // Custom market of you ?
'side' => $marketOrder->getSide(),
'amount' => $placeOrder->getFilledSize(), // <= it was an error here
'price' => $price,
'amount_used' => $amount_used,
'exchange_name' => $exchange->exchange_name,
'exchange_id' => $strategy->exchange_id,
'user_id' => $strategy->user_id,
'strategy_id' => $strategy->id,
]);
}
from coinbase-pro-sdk.
I refactored most of my code but the part where you use
$coinbase->accounts()->getAccount('EUR');
Gives a BadRequest error.
Yeah, my bad, getAccount expect id like string, exemple 18ba201e-6241-4efb-9b89-ed2885954566, then you can get currency from account and chek if euro :) , you was using the good method to do it
Any ideas on how to track the price better?
You should use getSize information from $fill, example :
$price = 0;
$size = 0;
foreach($fills as $fill) {
$price += $fill->getPrice() * $fill->getSize();
$size += $fill->getSize();
}
$price /= $size;
from coinbase-pro-sdk.
Thank you for your help and your work on this!
from coinbase-pro-sdk.
Hi, i edited the original post. Here is the script.
$coinbase = CoinbaseFacade::createDefaultCoinbaseApi(
'https://api.exchange.coinbase.com',
$api_key,
$api_secret,
$api_passphrase
);
$accounts = $coinbase->accounts()->list();
foreach ($accounts as $account) {
if($account->getCurrency() == 'EUR') {
$checkBalance = $account->getAvailableFunds();
}
}
if(isset($checkBalance) && $checkBalance >= $strategy->amount) {
foreach($markets as $market) {
$amount_used = $market->percentage * $percentagepoint;
$marketOrder = CoinbaseFacade::createMarketOrderToPlace(
MarketOrderToPlace::SIDE_BUY,
$market->market,
null,
$amount_used
);
$placeOrder = $coinbase->orders()->placeOrder($marketOrder);
if(isset($placeOrder)) {
$marketOrder = $coinbase->orders()->getOrderById($placeOrder->getId());
$fills = $coinbase->fills()->listFills($placeOrder->getId());
$price = 0;
$pricecount = 0;
foreach($fills as $fill) {
$price += $fill->getPrice();
$pricecount++;
}
if($pricecount > 0) {
$price /= $pricecount;
}
Order::create([
'external_id' => $placeOrder->getId(),
'market' => $market->market,
'side' => $marketOrder->getSide(),
'amount' => $marketOrder->getFilledSize(),
'price' => $price,
'amount_used' => $amount_used,
'exchange_name' => $exchange->exchange_name,
'exchange_id' => $strategy->exchange_id,
'user_id' => $strategy->user_id,
'strategy_id' => $strategy->id,
]);
}
}
}
I'll try with a try catch block first. Thanks.
from coinbase-pro-sdk.
Hi @MockingMagician,
I refactored most of my code but the part where you use
$coinbase->accounts()->getAccount('EUR');
Gives a BadRequest error.
Thank you for your help and for noticing the errors in my code.
Any ideas on how to track the price better?
from coinbase-pro-sdk.
Related Issues (13)
- [Question] What about websockets to get real-time market data? HOT 3
- Change how the API responses to the server? HOT 4
- I cannot place a limit order HOT 13
- OrderBook level issue HOT 7
- getCurrencies() is returning an object HOT 2
- Need better install instructions. HOT 7
- Support Symfony 5.x HOT 2
- OrderData.php post_only must be of the type bool, null given HOT 4
- Unable to get currencies from API HOT 2
- Error while calling it from queue HOT 2
- MockingMagician\CoinbaseProSdk\Functional\Error\ApiError cURL error 3: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for /products HOT 1
- Products Api Not working its shows error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coinbase-pro-sdk.