Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit 253a748

Browse files
committed
added sell command.
1 parent a2ab6fc commit 253a748

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

internal/selllimit/selllimit.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//
2+
// Copyright 2015-2019 Pedro Salgado
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
package selllimit
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/shopspring/decimal"
23+
"github.com/steenzout/go-env"
24+
"github.com/toorop/go-bittrex"
25+
26+
cli "gopkg.in/urfave/cli.v1"
27+
)
28+
29+
const (
30+
// resolution number of digits after the decimal point.
31+
resolution = 8
32+
33+
// flags
34+
flagKey = "key"
35+
flagSecret = "secret"
36+
37+
// EnvBittrexKey environment variable containing the Bittrex key.
38+
EnvBittrexKey = "BITTREX_KEY"
39+
// EnvBittrexSecret environment variable containing the Bittrex secret.
40+
EnvBittrexSecret = "BITTREX_SECRET"
41+
)
42+
43+
var (
44+
// arguments
45+
key string
46+
market string
47+
quantity decimal.Decimal
48+
rate decimal.Decimal
49+
secret string
50+
)
51+
52+
// Key returns the Bittrex jey.
53+
func Key(c *cli.Context) string {
54+
v := c.String(flagKey)
55+
if v == "" {
56+
return env.GetString(EnvBittrexKey)
57+
}
58+
59+
return v
60+
}
61+
62+
// Secret returns the Bittrex secret.
63+
func Secret(c *cli.Context) string {
64+
v := c.String(flagSecret)
65+
if v == "" {
66+
return env.GetString(EnvBittrexSecret)
67+
}
68+
69+
return v
70+
}
71+
72+
// ValidateArg validate input arguments
73+
func ValidateArg(c *cli.Context) error {
74+
var err error
75+
76+
if c.NArg() != 3 {
77+
return fmt.Errorf("invalid number of arguments")
78+
}
79+
80+
market = c.Args().First()
81+
82+
quantity, err = decimal.NewFromString(c.Args().Get(1))
83+
if err != nil {
84+
return err
85+
}
86+
87+
rate, err = decimal.NewFromString(c.Args().Get(2))
88+
if err != nil {
89+
return err
90+
}
91+
92+
secret = Secret(c)
93+
94+
key = Key(c)
95+
96+
return nil
97+
}
98+
99+
// Run request a SELL order.
100+
func Run(cctx *cli.Context) error {
101+
api := bittrex.New(key, secret)
102+
103+
orderID, err := api.SellLimit(market, quantity, rate)
104+
if err != nil {
105+
return err
106+
}
107+
108+
fmt.Printf(
109+
"order to SELL %s on %s at %s has been submitted: %s\n",
110+
quantity.StringFixed(resolution),
111+
market,
112+
rate.StringFixed(resolution),
113+
orderID,
114+
)
115+
116+
return nil
117+
}

main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/CoinMintTech/go-bittrex-cli/internal/getmarketsummary"
2727
"github.com/CoinMintTech/go-bittrex-cli/internal/getticker"
2828
"github.com/CoinMintTech/go-bittrex-cli/internal/getticks"
29+
"github.com/CoinMintTech/go-bittrex-cli/internal/selllimit"
2930
"github.com/CoinMintTech/go-bittrex-cli/internal/subscribeexchangeupdate"
3031
"github.com/CoinMintTech/go-bittrex-cli/internal/version"
3132
)
@@ -81,6 +82,23 @@ func main() {
8182
},
8283
},
8384
},
85+
{
86+
Name: "sell",
87+
Usage: "sell <market> <quantity> <rate>",
88+
ArgsUsage: "<market> <quantity> <rate>",
89+
Before: selllimit.ValidateArg,
90+
Action: selllimit.Run,
91+
Flags: []cli.Flag{
92+
cli.StringFlag{
93+
Name: "bittrex-key",
94+
Usage: "Bittrex key (optional, BITTREX_KEY environment variable).",
95+
},
96+
cli.StringFlag{
97+
Name: "bittrex-secret",
98+
Usage: "Bittrex secret (optional, BITTREX_SECRET environment variable).",
99+
},
100+
},
101+
},
84102
{
85103
Name: "version",
86104
Usage: "version",

0 commit comments

Comments
 (0)