blankly-finance / blankly

🚀 💸 Easily build, backtest and deploy your algo in just a few lines of code. Trade stocks, cryptos, and forex across exchanges w/ one package.
https://package.blankly.finance
GNU Lesser General Public License v3.0
2.09k stars 265 forks source link

Shorting stock not developed? #73

Closed bkunsberg closed 2 years ago

bkunsberg commented 2 years ago

Hi,

Thank you for the very useful package! I'm trying to backtest a strategy that involves shorting a stock/crypto. This means I make a call like: interface.market_order('MSFT', 'sell', 1) in a price_event function.

This results in an error message such as: *** blankly.utils.exceptions.InvalidOrder: Not enough base currency. Available: 0.0. hold: 0. requested: 1.

I'd expect to be able to short on Alpaca, as doing a 'sell' market order (e.g. through their web console) with 0 position is successful.

Note: I've gotten the same results with

alpaca = Alpaca()
s = Strategy(alpaca)

and coinbase = CoinbasePro() crypto = Strategy(coinbase)

Is this intentional? Thanks so much!

EmersonDove commented 2 years ago

Yep you are absolutely right. We haven't implemented the shorting for alpaca in the backtesting framework - this is the most significant feature that we're missing in a full alpaca integration.

We are working on this, I will attach this issue to a pull.

bkunsberg commented 2 years ago

Thanks for the quick reply! Looking forward to the full integration.

sjhermanek commented 2 years ago

@EmersonDove glad you're working on this! Do you plan to do the same for Crypto (e.g. binance API)?

EmersonDove commented 2 years ago

@sjhermanek This is a really good question. I suppose I'm not sure of the answer because I'm not really a binance user.

As I understand it, the margin API calls are through different endpoints which would make that integration significantly more complex but I'm not super sure. I think because of this we are going to wait for that more advanced stuff.

EmersonDove commented 2 years ago

This feature is now active on the dev branch.

bfan1256 commented 2 years ago

@bkunsberg , this is now live! Let us know what you think 💭

bkunsberg commented 2 years ago

Thank you for adding this feature so fast! I think it will be really useful for me (and others). Looking forward to trying it out.

On Sun, Jan 16, 2022 at 9:05 AM Brandon Fan @.***> wrote:

@bkunsberg https://github.com/bkunsberg , this is now live! Let us know what you think 💭

— Reply to this email directly, view it on GitHub https://github.com/Blankly-Finance/Blankly/issues/73#issuecomment-1013913517, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKB4TQDFVYQ22NI33JWTOOLUWL3F3ANCNFSM5HTGU3TQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you were mentioned.Message ID: @.***>

femtotrader commented 1 year ago

Shorting on Binance and with KeylessExchange seems to also be impossible. Any idea if it will be possible in a next release ?

EmersonDove commented 1 year ago

The blankly.Binance() exchange is spot, so shorting is disabled. KeylessExchange is also made to mimic spot so I didn't add an option for shorting but I could see an argument for adding it.

femtotrader commented 1 year ago

Supporting Binance cross margin will be great also.