connect Matlab to Binance API

Hey everyone,
i am seeking for a solution to put an order with Binance API.
if anyone has experience in this field or has knowledge of posting a request please let me know.
here is my code
% Set up authentication:
APIKey = '12a2fff338b41101ed886dd1c0ef4f35e96fe0233d23616daed4f2dbd1389526';
APISecret = '3d52b16aa7a0c6cd12fd11e09a955967cfeb839fd729357a0ca272351da75873';
symbol = 'BTCUSDT';
type = 'limit'; % or 'market'
side = 'sell'; % or 'buy'
amount = 1.0;
price = 60540; % or None
options = weboptions('RequestMethod',apiMethod, 'MediaType','application/json');
Body = struct('symbol', symbol, ...
'type', type, ...
'side', side, ...
'amount', char(num2str(amount)), ...
'price', char(num2str(price)), ...
'APIKey', APIKey,...
'APISecret', APISecret);
webaddress = 'https://testnet.binance.vision/api/v3/order/';
response = webwrite(webaddress, Body, options);
kind regrads,
Abolfazl

2 commentaires

Turlough Hughes
Turlough Hughes le 8 Juil 2021
By the way, if those are active keys on an account with anything worth losing you need to deactivate them (IP restrictions are not enough to keep your account safe!).
Turlough Hughes
Turlough Hughes le 9 Juil 2021
Assuming you've deactivated your keys?

Connectez-vous pour commenter.

Réponses (2)

Turlough Hughes
Turlough Hughes le 8 Juil 2021

2 votes

Try my recent submission to the file exchange: MATLAB-Binance-API
Then placing a limit order is simply:
obj = spot.newOrder('limit');
set(obj,'sym','btcusdt','side','sell','quantity',0.001,'price',60000)
response = obj.send;
or with the syntax that came in R2020a (which I'm a big fan of) you could also write:
set(obj,sym='btcusdt',side='sell',quantity=0.001,price=60000)
response = obj.send;
Note: this order sells 0.001 btc from your spot account (not testnet) and assumes you have 0.001 BTC on your books.

4 commentaires

Abolfazl Nejatian
Abolfazl Nejatian le 9 Juil 2021
Thank you so much for your code sharing.
I saw the code and it was very well written.
I got my answer, actually, I have written a code to Trade in the Binance in Future section with leverage.
I will share it as soon as I beautiful the code.
and I hope it will be useful for other users.
kind regards,
Abolfazl Nejatian
Turlough Hughes
Turlough Hughes le 9 Juil 2021
Thanks, I'm glad you like the code @Abolfazl Nejatian. If you do end up using it consider leaving a review, it would be much appreciated. I look forward to seeing your Futures code... Are you going to post here as an answer or make an FEX submission?
Abolfazl Nejatian
Abolfazl Nejatian le 9 Juil 2021
absolutely, I will write a review ASAP.
I just think about implementation some usual strategy for risk management, it is called Martingale Strategy.
it is a kind of risk management strategy.
actually, the Martingale Strategy involves doubling the trade size every time a loss is faced. A classic scenario for the strategy is to try and trade an outcome with a 50% probability of it occurring.
it means if you have a failed buy/long position you should compensate the loss by ReEntering to another buy/long position with increasing the volume inorder to reach to your goal profit.
This algorithm always guarantees the profit in condition that the ReEntering points are not too bad. consecutive(5 or more) bad ReEntering points will lead to liquidation!
so the question is did you done some similarstategy in Matlab for the crypto ?
Turlough Hughes
Turlough Hughes le 9 Juil 2021
No, I haven't tried anything similar tbh. Most of my interest has been on arbitrage and backtesting other strategies.

Connectez-vous pour commenter.

Asghar Moeini
Asghar Moeini le 21 Juil 2022

1 vote

I would be grateful if you share your code for futures trade.
Regards,
Asghar

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by