1 | should = require( "should" )
|
2 |
|
3 | Client = require '../src/client'
|
4 | key = require '../_key'
|
5 |
|
6 | describe '1Broker-client', ->
|
7 |
|
8 |
|
9 | client = null
|
10 |
|
11 |
|
12 |
|
13 | client = new Client( key )
|
14 |
|
15 | describe 'Calculate Stop Loss and Take Profit using Percentage', ->
|
16 |
|
17 | it 'calculate correct SL and TP for SHORT with 1x leverage', ->
|
18 |
|
19 | params = client.calculate "eurusd", 1, 1, "short", "-10%", '20%'
|
20 |
|
21 | params.leverage.should.equal 1
|
22 | params.stop_loss.should.equal 1.1
|
23 | params.take_profit.should.equal 0.8
|
24 |
|
25 | it 'calculate correct SL and TP for SHORT with 100x leverage', ->
|
26 |
|
27 | params = client.calculate "eurusd", 100, 1, "short", "-10%", '20%'
|
28 |
|
29 | params.leverage.should.equal 100
|
30 | params.stop_loss.should.equal 1.001
|
31 | params.take_profit.should.equal 0.998
|
32 |
|
33 | it 'calculate correct SL and TP for LONG with 1x leverage', ->
|
34 |
|
35 | params = client.calculate "eurusd", 1, 1, "long", "-10%", '20%'
|
36 |
|
37 | params.leverage.should.equal 1
|
38 | params.stop_loss.should.equal 0.9
|
39 | params.take_profit.should.equal 1.2
|
40 |
|
41 | it 'calculate correct SL and TP for LONG with 100x leverage', ->
|
42 |
|
43 | params = client.calculate "eurusd", 100, 1, "long", "-10%", '20%'
|
44 |
|
45 | params.leverage.should.equal 100
|
46 | params.stop_loss.should.equal 0.999
|
47 | params.take_profit.should.equal 1.002
|
48 |
|
49 |
|
50 | describe 'Calculate Stop Loss and Take Profit using Points', ->
|
51 |
|
52 | it 'calculate correct SL and TP for LONG', ->
|
53 |
|
54 | params = client.calculate "eurusd", 1, 1, "long", "-100", '+200'
|
55 |
|
56 | params.stop_loss.should.equal 0.999
|
57 | params.take_profit.should.equal 1.002
|
58 |
|
59 | it 'calculate correct SL and TP for SHORT', ->
|
60 |
|
61 | params = client.calculate "eurusd", 1, 1, "short", "-100", '+200'
|
62 |
|
63 | params.stop_loss.should.equal 1.001
|
64 | params.take_profit.should.equal 0.998
|
65 |
|
66 |
|
67 | describe 'Calculate Stop Loss and Take Profit using absolute values', ->
|
68 |
|
69 | it 'calculate correct SL and TP for LONG', ->
|
70 |
|
71 | params = client.calculate "eurusd", 40, 1, "long", "0.95", '1.2'
|
72 |
|
73 | params.stop_loss.should.equal 0.95
|
74 | params.take_profit.should.equal 1.2
|
75 |
|
76 | it 'calculate correct SL and TP for SHORT', ->
|
77 |
|
78 | params = client.calculate "eurusd", 123, 1, "short", "1.2", '0.9'
|
79 |
|
80 | params.stop_loss.should.equal 1.2
|
81 | params.take_profit.should.equal 0.9
|
82 |
|
83 | describe 'Calculate Stop Loss and Take Profit using mixed values', ->
|
84 |
|
85 | it 'calculate correct SL and TP for LONG using Points and Percentage', ->
|
86 |
|
87 | params = client.calculate "eurusd", 100, 1, "long", "-200", '20%'
|
88 |
|
89 | params.stop_loss.should.equal 0.998
|
90 | params.take_profit.should.equal 1.002
|
91 |
|
92 | it 'calculate correct SL and TP for SHORT using Points and Percentage', ->
|
93 |
|
94 | params = client.calculate "eurusd", 1, 1, "short", "-200", '20%'
|
95 |
|
96 | params.stop_loss.should.equal 1.002
|
97 | params.take_profit.should.equal 0.8
|
98 |
|
99 | it 'calculate correct SL and TP for LONG using Percentage and Points', ->
|
100 |
|
101 | params = client.calculate "eurusd", 100, 1, "long", "-30%", '+300'
|
102 |
|
103 | params.stop_loss.should.equal 0.997
|
104 | params.take_profit.should.equal 1.003
|
105 |
|
106 | it 'calculate correct SL and TP for SHORT using Percentage and Points', ->
|
107 |
|
108 | params = client.calculate "eurusd", 1, 1, "short", "-30%", '+300'
|
109 |
|
110 | params.stop_loss.should.equal 1.3
|
111 | params.take_profit.should.equal 0.997
|
112 |
|
113 | it 'calculate correct SL and TP for LONG using Percentage and Fixed value', ->
|
114 |
|
115 | params = client.calculate "eurusd", 100, 1, "long", "-30%", '0.999'
|
116 |
|
117 | params.stop_loss.should.equal 0.997
|
118 | params.take_profit.should.equal 0.999
|
119 |
|
120 | it 'calculate correct SL and TP for SHORT using Percentage and Fixed value', ->
|
121 |
|
122 | params = client.calculate "eurusd", 1, 1, "short", "-30%", '0.997'
|
123 |
|
124 | params.stop_loss.should.equal 1.3
|
125 | params.take_profit.should.equal 0.997
|
126 |
|
127 | it 'calculate correct SL and TP for LONG using Points and Fixed value', ->
|
128 |
|
129 | params = client.calculate "eurusd", 100, 1, "long", "-450", '0.999'
|
130 |
|
131 | params.stop_loss.should.equal 0.9955
|
132 | params.take_profit.should.equal 0.999
|
133 |
|
134 | it 'calculate correct SL and TP for SHORT using Points and Fixed value', ->
|
135 |
|
136 | params = client.calculate "eurusd", 1, 1, "short", "-450", '0.997'
|
137 |
|
138 | params.stop_loss.should.equal 1.0045
|
139 | params.take_profit.should.equal 0.997
|
140 |
|
141 | it 'calculate correct SL and TP for LONG using Fixed value and Percentage', ->
|
142 |
|
143 | params = client.calculate "eurusd", 100, 1, "long", "0.999", '10%'
|
144 |
|
145 | params.stop_loss.should.equal 0.999
|
146 | params.take_profit.should.equal 1.001
|
147 |
|
148 | it 'calculate correct SL and TP for SHORT using Fixed value and Percentage', ->
|
149 |
|
150 | params = client.calculate "eurusd", 1, 1, "short", "1.002", '30%'
|
151 |
|
152 | params.stop_loss.should.equal 1.002
|
153 | params.take_profit.should.equal 0.7
|
154 |
|
155 | it 'calculate correct SL and TP for LONG using Fixed value and Points', ->
|
156 |
|
157 | params = client.calculate "eurusd", 100, 1, "long", "0.999", '+200'
|
158 |
|
159 | params.stop_loss.should.equal 0.999
|
160 | params.take_profit.should.equal 1.002
|
161 |
|
162 | it 'calculate correct SL and TP for SHORT using Fixed value and Points', ->
|
163 |
|
164 | params = client.calculate "eurusd", 1, 1, "short", "1.002", '+200'
|
165 |
|
166 | params.stop_loss.should.equal 1.002
|
167 | params.take_profit.should.equal 0.998
|
168 |
|
169 | describe 'Calculate Leverage using string values', ->
|
170 |
|
171 | it 'calculate correct leverage using MAX', ->
|
172 |
|
173 | params = client.calculate "eurusd", 'max', 1, "long", "-200", '20%'
|
174 |
|
175 | params.leverage.should.equal 200
|
176 |
|
177 | it 'calculate correct leverage using HALF', ->
|
178 |
|
179 | params = client.calculate "eurusd", 'half', 1, "long", "-200", '20%'
|
180 |
|
181 | params.leverage.should.equal 100
|
182 |
|
183 | it 'calculate correct leverage using QUARTER', ->
|
184 |
|
185 | params = client.calculate "eurusd", 'quarter', 1, "long", "-200", '20%'
|
186 |
|
187 | params.leverage.should.equal 50
|
188 |
|
189 | it 'deal with null leverage', ->
|
190 |
|
191 | params = client.calculate "eurusd", null, 1, "long", "-200", '20%'
|
192 |
|
193 | params.leverage.should.equal 1
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | if not key
|
204 |
|
205 | console.warn "NOTE: Due to the lack of API KEY no API methods are being tested"
|
206 | return
|
207 |
|
208 | describe 'call free methods', ->
|
209 |
|
210 | it 'should get market quotes', ( done ) ->
|
211 |
|
212 | params = symbols: [ 'EURUSD', 'USDJPY' ]
|
213 |
|
214 | client.market.quotes params, ( error, result ) ->
|
215 |
|
216 | should( error ).be.null
|
217 |
|
218 | result.error.should.equal false
|
219 |
|
220 | result.response[0].symbol.should.be.ok
|
221 | result.response[0].bid.should.be.ok
|
222 | result.response[0].ask.should.be.ok
|
223 |
|
224 | result.response[1].symbol.should.be.ok
|
225 | result.response[1].bid.should.be.ok
|
226 | result.response[1].ask.should.be.ok
|
227 |
|
228 | done()
|
229 |
|
230 |
|
231 | it 'should get user overview', ( done ) ->
|
232 |
|
233 | client.user.overview ( error, result ) ->
|
234 |
|
235 | should( error ).be.null
|
236 |
|
237 | result.error.should.equal false
|
238 |
|
239 | result.response.username.should.be.ok
|
240 | result.response.email.should.be.ok
|
241 | result.response.balance.should.be.ok
|
242 | result.response.orders_worth.should.be.ok
|
243 | result.response.positions_worth.should.be.ok
|
244 | result.response.net_worth.should.be.ok
|
245 | result.response.orders_open.should.be.ok
|
246 | result.response.positions_open.should.be.ok
|
247 |
|
248 | done()
|
249 |
|
250 |
|
251 | describe 'call paid methods', ->
|
252 |
|
253 | it 'should long eurusd and get insuficient funds error', ( done ) ->
|
254 |
|
255 |
|
256 | @timeout 2000
|
257 |
|
258 | params =
|
259 | symbol : 'EURUSD'
|
260 | margin : 0.01
|
261 | direction : 'long'
|
262 | leverage : 1
|
263 | order_type: 'market'
|
264 |
|
265 | client.order.create params, ( error, result ) ->
|
266 |
|
267 | should( error ).be.null
|
268 |
|
269 |
|
270 | result.error.should.be.true
|
271 | result.error_code.should.equal 502
|
272 |
|
273 | done()
|
274 |
|
275 |
|