Node-RED node for Alpaca

Node-RED is a graphical programming language based on NodeJS. I love Node-RED, it puts so many useful tasks right at your fingertips using a beautiful web-based GUI. Surprisingly there wasn’t a node already existing for Alpaca so I wrapped the official SDK into the required format for Node-RED and it worked! easy as pie. This is a work in progress; I plan on including all features and applying the appropriate polish so stay tuned!

Download version 0.1.0 here: https://github.com/mdkrieg/node-red-contrib-alpaca

1 Like

Hey There,

I checked out your node red and all looks great! I am however getting an error with your submit order node
{“code”:40010000,“message”:“request body format is invalid”}

Hi,

I tried alpaca node with “last quote” function then got following error.

TypeError: alpaca_conn[topic] is not a function

Hello,

I tried just now myself and it seems to work for me, if you’re using the msg.topic to select the function make sure it is “lastQuote”, case sensitive, no spaces. My demo flow looks like this:

both injects provide this output:

{
	"status": "success",
	"symbol": "AAPL",
	"last": {
		"askprice": 166.37,
		"asksize": 4,
		"askexchange": 15,
		"bidprice": 165.15,
		"bidsize": 1,
		"bidexchange": 15,
		"timestamp": 1638817528099150800
	}
}

Perhaps you don’t have the latest version of the node? Open “Manage Palette” and if an update to 2.2.1 is available then click update and restart nodered

Regards,
-Matt

{“code”:40010000,“message”:“request body format is invalid”}

Sorry I missed this, this one is most likely as it says, something in the request is misspelled or misconfigured. This is why in newer version of the node I have a template available to copy/paste from the node configuration pane. For example the template for submit order is this:

{
    "symbol": string, // any valid ticker symbol
    "qty": number,
    "side": 'buy' | 'sell',
    "type": 'market' | 'limit' | 'stop' | 'stop_limit' | 'trailing_stop',
    "time_in_force": 'day' | 'gtc' | 'opg' | 'ioc',
    "limit_price": number, // optional,
    "stop_price": number, // optional,
    "client_order_id": string, // optional,
    "extended_hours": boolean, // optional,
    "order_class": string, // optional,
    "take_profit": object, // optional,
    "stop_loss": object, // optional,
    "trail_price": string, // optional,
    "trail_percent": string // optional,
}

of course, some of this is pseudo-code and will need to be pared down to just the options you are selecting.

My alpaca is 2.2.1 and version of Node-Red is 2.1.4. I still get the error.


inject
alpaca

Sorry, I am new to Node-Red.

Thanks for your help!

EDIT - see latest post, I think I solved it.

Everything for me is the same except I’m not getting the error. That is odd.

I think this function was introduced somewhat recently (ie, in v2), maybe your nodered is somehow importing a different version of @alpacahq/alpaca-trade-api. Have you installed that package globally perhaps? Version 2.3.0 is the latest tested version, you should be able to see it in your node-red home directory (if you’re unsure, look at userDir which is printed in the logs at start of nodered)

also, you can see what global packages you have by running npm list -g

Ok, here’s a method to confirm the version of the dependency,

Take the bottom path and follow it all the way to alpaca-trade-api and look at the package.json in that folder. if it’s < 2.3.0 then that may be your issue.

ok, so I was able to reproduce your error when using alpaca-trade-api v2.10.0

So, the short-term fix is to force my package to require v2.3.0 which it was developed with. I’ve bumped my node version to 2.2.2 with this change.

Update through the palette manager (if it doesn’t give the option may need to restart nodered), then update to 2.2.2 and restart nodered.***

***Or, you can run this command from the user directory:

npm install node-red-contrib-alpaca@latest

and restart nodered

Sorry for all the noise, (leaving it up for education purposes), but I was able to reproduce and subsequently fix the error so I’m pretty confident this is the solution.

Thank your help, I will update to node verson 2.2.2.

According your info, I check my package.json.

In alpaca-trade-api folder, the package.json begains with
{
“_from”: “@alpacahq/alpaca-trade-api@^2.3.0”,
“_id”: ": “@alpacahq/alpaca-trade-api@^2.10.1”
…,
“raw”: “@alpacahq/alpaca-trade-api@^2.3.0”,
“name”: “@alpacahq/alpaca-trade-api”,

}

This may indicate the problem with my older version of alpaca-trade-api.

Thanks!

Hi Matt,
Thanks for node-red-contrib-alpaca update to solve my issue.
I have other issue to install node-red-contrib-alphacate. Following is the log.

2021-12-07T19:57:17.633Z Install : node-red-contrib-alphacate 1.1.0

2021-12-07T19:57:16.991Z npm install --no-audit --no-update-notifier --no-fund --save --save-prefix=~ --production --engine-strict node-red-contrib-alphacate@1.1.0
2021-12-07T19:57:53.694Z [err] npm
2021-12-07T19:57:53.697Z [err]
2021-12-07T19:57:53.702Z [err] ERR! code ENOENT
2021-12-07T19:57:53.705Z [err] npm
2021-12-07T19:57:53.712Z [err] ERR! syscall spawn git
2021-12-07T19:57:53.712Z [err] npm ERR! path git
2021-12-07T19:57:53.714Z [err] npm
2021-12-07T19:57:53.716Z [err]
2021-12-07T19:57:53.722Z [err] ERR! errno -2
2021-12-07T19:57:53.884Z [err] npm
2021-12-07T19:57:53.886Z [err]
2021-12-07T19:57:53.890Z [err] ERR! enoent Error while executing:
2021-12-07T19:57:53.890Z [err] npm ERR! enoent
2021-12-07T19:57:53.892Z [err] undefined ls-remote -h -t ssh://git@github.com/mdkrieg/alphacate.git
2021-12-07T19:57:53.902Z [err] npm ERR! enoent
2021-12-07T19:57:53.902Z [err] npm ERR! enoent
2021-12-07T19:57:53.902Z [err] npm ERR! enoent spawn git ENOENT
2021-12-07T19:57:53.902Z [err] npm ERR! enoent This is related to npm not being able to find a file.
2021-12-07T19:57:53.902Z [err] npm ERR! enoent
2021-12-07T19:57:54.483Z [err]
2021-12-07T19:57:54.486Z [err] npm
2021-12-07T19:57:54.492Z [err] ERR! A complete log of this run can be found in:
2021-12-07T19:57:54.492Z [err] npm ERR! /home/pi/.npm/_logs/2021-12-07T19_57_53_917Z-debug.log
2021-12-07T19:57:54.543Z rc=254

Can you help me to solve this issue?

Thank you!

Looks like you don’t have git installed, this one requires a dependency that is just on github, try running:

sudo apt-get install git

and then try installing again.

Thank you!
It works.

Hi Matt,

  I wish you can add "STOCHRSI" indicator in node-red-contrib-alphacate. 

Thank you so much!

1 Like