Alpaca卖出订单在Perl中

huangapple go评论59阅读模式
英文:

Alpaca sell order in Perl

问题

I have read the LWP documentation, the Alpaca API documentation for its primitives, and numerous examples in other languages. I can't get POSTs to work in Perl. GETs work fine. POSTs work fine in curl or Python, but I can't find the right syntax for Perl.

I always get {"code":40010000,"message":"request body format is invalid"} Always.

Working in the sandbox, of course. Here are some subroutines I have tried.

use LWP::UserAgent;

my vars...

while ($line = <POSITIONS>) {
    ($ticker, $date8601, $epoch, $side, $qty0, $entryprice, $unrealized) = split(",", $line);
    sellorder5($url, $API, $secret, $ticker, $qty0);
}

sub sellorder5 {
    $req = HTTP::Request->new(POST=>$url);
    $req->header('APCA-API-KEY-ID' => $API, 'APCA-API-SECRET-KEY' => $secret, 'content-type' => 'application/json');
    $req->content('{"symbol":"$ticker","qty":"$qty0","side":"sell","type":"market","time_in_force":"gtc"}');
    $resp = $browser->request($req);
    $raw_text = $resp->decoded_content;
}

sub sellorder4 {
    $req = HTTP::Request->new(POST => $url);      # Create a request
    $req->header('APCA-API-KEY-ID' => $API, 'APCA-API-SECRET-KEY' => $secret, 'content-type' => 'application/json');
    $req->content('{"symbol":"$ticker", "qty":"$qty0", "side":"sell", "type":"market", "time_in_force":"gtc"}');
    $resp = $browser->request($req);
    $raw_text = $resp->content;
}

sub sellorder3 {
    $req = HTTP::Request->new(POST => $url);
    $req->header('APCA-API-KEY-ID' => $API, 'APCA-API-SECRET-KEY' => $secret, 'content-type' => 'application/json');
    $post_data = '{"symbol":"$ticker", "qty":"$qty0", "side":"sell", "type":"market", "time_in_force":"gtc" }';
    $req->content($post_data);
    $resp = $browser->request($req);
    $raw_text = $resp->decoded_content;
}

sub sellorder2 {
    my $req = HTTP::Request->new(POST => $url);
    $req->header('APCA-API-KEY-ID' => $API, 'APCA-API-SECRET-KEY' => $secret);
    $post_data = '{"symbol":"$ticker", "qty":$qty0, "side":"sell", "type":"market", "time_in_force":"gtc" }';
    $req->content($post_data);
    $response = $browser->request($req);
    $raw_text = $response->decoded_content;
}

sub sellorder {
    $response = $browser->post($url, 'APCA-API-KEY-ID' , $API, 'APCA-API-SECRET-KEY' , $secret, [ 'symbol'  => '$ticker', 'qty' => '$qty0', 'side' => 'sell', 'type' => 'market', 'time_in_force' => 'gtc' ] );
    $raw_text = $response->content;
    $raw_text =~ s/[\x00-\x1f]//g;      #remove stray characters, and make into one line
}

I would appreciate a working example if someone has one. Thanks.

英文:

I have read the LWP documentation, the Alpaca API documentation for its primitives, and numerous examples in other languages. I can't get POSTs to work in Perl. GETS work fine. POSTs work fine in curl or Python, but I can't find the right syntax for Perl.

I always get {"code":40010000,"message":"request body format is invalid"} Always.

Working in the sandbox, of course. Here are some subroutines I have tried.

use LWP::UserAgent;
my vars...
while ($line = &lt;POSITIONS&gt;) {
($ticker, $date8601, $epoch, $side, $qty0, $entryprice, $unrealized) = split(&quot;,&quot;, $line);
sellorder5($url, $API, $secret, $ticker, $qty0);
}
sub sellorder5 {
$req=HTTP::Request-&gt;new(POST=&gt;$url);
$req-&gt;header( &#39;APCA-API-KEY-ID&#39; =&gt; $API, &#39;APCA-API-SECRET-KEY&#39; =&gt; $secret, &#39;content-type&#39; =&gt; &#39;application/json&#39;);
$req-&gt;content(&#39;{&quot;symbol&quot;:&quot;$ticker&quot;,&quot;qty&quot;:&quot;$qty0&quot;,&quot;side&quot;:&quot;sell&quot;,&quot;type&quot;:&quot;market&quot;,&quot;time_in_force&quot;:&quot;gtc&quot;}&#39;);
$resp = $browser-&gt;request($req);
$raw_text = $resp-&gt;decoded_content;
}
sub sellorder4 {
$req = HTTP::Request-&gt;new(POST =&gt; $url);      # Create a request
$req-&gt;header( &#39;APCA-API-KEY-ID&#39; =&gt; $API, &#39;APCA-API-SECRET-KEY&#39; =&gt; $secret, &#39;content-type&#39; =&gt; &#39;application/json&#39;);
$req-&gt;content(&#39;{&quot;symbol&quot;:&quot;$ticker&quot;, &quot;qty&quot;:&quot;$qty0&quot;, &quot;side&quot;:&quot;sell&quot;, &quot;type&quot;:&quot;market&quot;, &quot;time_in_force&quot;:&quot;gtc&quot;}&#39;);
$resp = $browser-&gt;request($req);
$raw_text = $resp-&gt;content;
}
sub sellorder3 {
$req = HTTP::Request-&gt;new(POST =&gt; $url);
$req-&gt;header( &#39;APCA-API-KEY-ID&#39; =&gt; $API, &#39;APCA-API-SECRET-KEY&#39; =&gt; $secret, &#39;content-type&#39; =&gt; &#39;application/json&#39;);
$post_data = &#39;{ &quot;symbol&quot;:&quot;$ticker&quot;, &quot;qty&quot;:&quot;$qty0&quot;, &quot;side&quot;:&quot;sell&quot;, &quot;type&quot;:&quot;market&quot;, &quot;time_in_force&quot;:&quot;gtc&quot; }&#39;;
$req-&gt;content($post_data);
$resp = $browser-&gt;request($req);
$raw_text = $resp-&gt;decoded_content;
}
sub sellorder2 {
my $req = HTTP::Request-&gt;new(POST =&gt; $url);
$req-&gt;header( &#39;APCA-API-KEY-ID&#39; =&gt; $API, &#39;APCA-API-SECRET-KEY&#39; =&gt; $secret);
$post_data = &#39;{ &quot;symbol&quot;:&quot;$ticker&quot;,  &quot;qty&quot; : $qty0,  &quot;side&quot; : &quot;sell&quot;,  &quot;type&quot; : &quot;market&quot;,  &quot;time_in_force&quot; : &quot;gtc&quot; }&#39;;
$req-&gt;content($post_data);
$response = $browser-&gt;request($req);
$raw_text = $response-&gt;decoded_content;
}
sub sellorder {
$response = $browser-&gt;post($url, &#39;APCA-API-KEY-ID&#39; , $API, &#39;APCA-API-SECRET-KEY&#39; , $secret, [ &#39;symbol&#39;  =&gt; &#39;$ticker&#39;, &#39;qty&#39; =&gt; &#39;$qty0&#39;, &#39;side&#39; =&gt; &#39;sell&#39;, &#39;type&#39; =&gt; &#39;market&#39;, &#39;time_in_force&#39; =&gt; &#39;gtc&#39; ] );
$raw_text = $response-&gt;content;
$raw_text =~ s/[\x00-\x1f]//g;      #remove stray characters, and make into one line
}

I would appreciate a working example if someone has one.
Thanks.

答案1

得分: 3

以下是翻译好的内容:

你的问题在于这一行:

$req-&gt;content(&#39;{&quot;symbol&quot;:&quot;$ticker&quot;,&quot;qty&quot;:&quot;$qty0&quot;,&quot;side&quot;:&quot;sell&quot;,&quot;type&quot;:&quot;market&quot;,&quot;time_in_force&quot;:&quot;gtc&quot;}&#39;);

单引号用于不进行插值的字符串。因此,$ticker 不会是 $ticker 变量的内容,而是由美元符号后跟单词 "ticker" 组成的字符串。

双引号会插值变量,所以:

$req-&gt;content(&quot;{\&quot;symbol\&quot;:\&quot;$ticker\&quot;,\&quot;qty\&quot;:\&quot;$qty0\&quot;,\&quot;side\&quot;:\&quot;sell\&quot;,\&quot;type\&quot;:\&quot;market\&quot;,\&quot;time_in_force\&quot;:\&quot;gtc\&quot;}&quot;);

当然,这看起来很丑陋。所以你可以使用 qq//,这是插值引号操作符的替代方法。

$req-&gt;content(qq/{&quot;symbol&quot;:&quot;$ticker&quot;,&quot;qty&quot;:&quot;$qty0&quot;,&quot;side&quot;:&quot;sell&quot;,&quot;type&quot;:&quot;market&quot;,&quot;time_in_force&quot;:&quot;gtc&quot;}/);

但更好的做法是不要手写你的 JSON。而是使用 JSON::PP 或类似的工具。

use JSON::PP qw( encode_json decode_json );

# ...

$req-&gt;content( encode_json( {
  symbol         =&gt; $ticker,
  qty            =&gt; $qty0,
  side           =&gt; &#39;sell&#39;,
  type           =&gt; &#39;market&#39;,
  time_in_force  =&gt; &#39;gtc&#39;,
} ) );
英文:

Your problem is this line:

$req-&gt;content(&#39;{&quot;symbol&quot;:&quot;$ticker&quot;,&quot;qty&quot;:&quot;$qty0&quot;,&quot;side&quot;:&quot;sell&quot;,&quot;type&quot;:&quot;market&quot;,&quot;time_in_force&quot;:&quot;gtc&quot;}&#39;);

Single quotes are for strings without interpolation. So $ticker won't be the contents of the $ticker variable, but a string consisting of the dollar sign followed by the word "ticker".

Double quotes will interpolate variables, so:

$req-&gt;content(&quot;{\&quot;symbol\&quot;:\&quot;$ticker\&quot;,\&quot;qty\&quot;:\&quot;$qty0\&quot;,\&quot;side\&quot;:\&quot;sell\&quot;,\&quot;type\&quot;:\&quot;market\&quot;,\&quot;time_in_force\&quot;:\&quot;gtc\&quot;}&quot;);

Of course, that's ugly. So you could use qq// instead, the interpolating quote-like operator.

$req-&gt;content(qq/{&quot;symbol&quot;:&quot;$ticker&quot;,&quot;qty&quot;:&quot;$qty0&quot;,&quot;side&quot;:&quot;sell&quot;,&quot;type&quot;:&quot;market&quot;,&quot;time_in_force&quot;:&quot;gtc&quot;}/);

But better yet, don't hand-write your JSON. Instead use JSON::PP or similar.

use JSON::PP qw( encode_json decode_json );

# ...

$req-&gt;content( encode_json( {
  symbol         =&gt; $ticker,
  qty            =&gt; $qty0,
  side           =&gt; &#39;sell&#39;,
  type           =&gt; &#39;market&#39;,
  time_in_force  =&gt; &#39;gtc&#39;,
} ) );

huangapple
  • 本文由 发表于 2023年6月30日 04:03:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定