Perl中的Alpaca卖单

umuewwlo  于 2023-08-06  发布在  Perl
关注(0)|答案(1)|浏览(219)

我阅读了LWP文档、API文档中的原语,以及其他语言的大量示例。我不能让POST在Perl中工作。一切正常。POST在curl或Python中工作正常,但我找不到Perl的正确语法。
我总是得到{“code”:40010000,“message”:“request body format is invalid”}总是。
当然是在沙盒里工作。下面是我尝试过的一些子程序。

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
}

字符串
如果有人有一个工作的例子,我会很感激。- 谢谢-谢谢

c3frrgcw

c3frrgcw1#

你的问题是这一行:

$req->content('{"symbol":"$ticker","qty":"$qty0","side":"sell","type":"market","time_in_force":"gtc"}');

字符串
单引号用于字符串 *,不带插值 *。所以$ticker将不是$ticker变量的内容,而是一个由美元符号和单词“ticker”组成的字符串。
双引号将插入变量,所以:

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


当然,那很难看。因此,您可以使用qq//,类似引号的插值运算符。

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


但更好的是,不要手写JSON。使用JSON::PP或类似的格式。

use JSON::PP qw( encode_json decode_json );

# ...

$req->content( encode_json( {
  symbol         => $ticker,
  qty            => $qty0,
  side           => 'sell',
  type           => 'market',
  time_in_force  => 'gtc',
} ) );

相关问题