Skip to content

Commit 6d60196

Browse files
authored
Merge pull request #93 from ChipaDevTeam/copilot/add-pending-order-cancellation
Add pending-order cancellation API and enable multi in-flight pending orders
2 parents fe93151 + 368dd2d commit 6d60196

10 files changed

Lines changed: 573 additions & 114 deletions

File tree

BinaryOptionsToolsV2/python/BinaryOptionsToolsV2/pocketoption/asynchronous.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,9 @@ async def get_pending_deals(self) -> List[Dict]:
491491
"""
492492
return json.loads(await self.client.get_pending_deals())
493493

494-
async def open_pending_order(
495-
self,
496-
open_type: int,
494+
async def open_pending_order(
495+
self,
496+
open_type: int,
497497
amount: float,
498498
asset: str,
499499
open_time: int,
@@ -518,10 +518,20 @@ async def open_pending_order(
518518
Returns:
519519
Dict: The created pending order details.
520520
"""
521-
order = await self.client.open_pending_order(
522-
open_type, amount, asset, open_time, open_price, timeframe, min_payout, command
523-
)
524-
return json.loads(order)
521+
order = await self.client.open_pending_order(
522+
open_type, amount, asset, open_time, open_price, timeframe, min_payout, command
523+
)
524+
return json.loads(order)
525+
526+
async def cancel_pending_order(self, ticket: str) -> Dict:
527+
"""Cancels a pending order by ticket UUID."""
528+
result = await self.client.cancel_pending_order(ticket)
529+
return json.loads(result)
530+
531+
async def cancel_pending_orders(self, tickets: List[str]) -> List[Dict]:
532+
"""Cancels multiple pending orders by ticket UUID."""
533+
result = await self.client.cancel_pending_orders(tickets)
534+
return json.loads(result)
525535

526536
async def closed_deals(self) -> List[Dict]:
527537
"""Retrieves a list of all closed/completed deals.

BinaryOptionsToolsV2/python/BinaryOptionsToolsV2/pocketoption/synchronous.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,9 @@ def get_pending_deals(self) -> List[Dict]:
391391
with self._lock:
392392
return self.loop.run_until_complete(self._client.get_pending_deals())
393393

394-
def open_pending_order(
395-
self,
396-
open_type: int,
394+
def open_pending_order(
395+
self,
396+
open_type: int,
397397
amount: float,
398398
asset: str,
399399
open_time: int,
@@ -420,12 +420,22 @@ def open_pending_order(
420420
"""
421421
with self._lock:
422422
return self.loop.run_until_complete(
423-
self._client.open_pending_order(
424-
open_type, amount, asset, open_time, open_price, timeframe, min_payout, command
425-
)
426-
)
427-
428-
def closed_deals(self) -> List[Dict]:
423+
self._client.open_pending_order(
424+
open_type, amount, asset, open_time, open_price, timeframe, min_payout, command
425+
)
426+
)
427+
428+
def cancel_pending_order(self, ticket: str) -> Dict:
429+
"""Cancels a pending order by ticket UUID."""
430+
with self._lock:
431+
return self.loop.run_until_complete(self._client.cancel_pending_order(ticket))
432+
433+
def cancel_pending_orders(self, tickets: List[str]) -> List[Dict]:
434+
"""Cancels multiple pending orders by ticket UUID."""
435+
with self._lock:
436+
return self.loop.run_until_complete(self._client.cancel_pending_orders(tickets))
437+
438+
def closed_deals(self) -> List[Dict]:
429439
"Returns a list of all the closed deals as dictionaries"
430440
with self._lock:
431441
return self.loop.run_until_complete(self._client.closed_deals())

BinaryOptionsToolsV2/rust/src/pocketoption.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,51 @@ impl RawPocketOption {
391391
})
392392
}
393393

394+
pub fn cancel_pending_order<'py>(
395+
&self,
396+
py: Python<'py>,
397+
ticket: String,
398+
) -> PyResult<Bound<'py, PyAny>> {
399+
let client = self.client.clone();
400+
let ticket = Uuid::parse_str(&ticket).map_err(|_| {
401+
BinaryErrorPy::InvalidParameter(format!("Invalid ticket UUID: {}", ticket))
402+
})?;
403+
404+
future_into_py(py, async move {
405+
let res = client
406+
.cancel_pending_order(ticket)
407+
.await
408+
.map_err(BinaryErrorPy::from)?;
409+
let result = serde_json::to_string(&res).map_err(BinaryErrorPy::from)?;
410+
Ok(result)
411+
})
412+
}
413+
414+
pub fn cancel_pending_orders<'py>(
415+
&self,
416+
py: Python<'py>,
417+
tickets: Vec<String>,
418+
) -> PyResult<Bound<'py, PyAny>> {
419+
let client = self.client.clone();
420+
let tickets = tickets
421+
.into_iter()
422+
.map(|ticket| {
423+
Uuid::parse_str(&ticket).map_err(|_| {
424+
BinaryErrorPy::InvalidParameter(format!("Invalid ticket UUID: {}", ticket))
425+
})
426+
})
427+
.collect::<Result<Vec<_>, _>>()?;
428+
429+
future_into_py(py, async move {
430+
let res = client
431+
.cancel_pending_orders(tickets)
432+
.await
433+
.map_err(BinaryErrorPy::from)?;
434+
let result = serde_json::to_string(&res).map_err(BinaryErrorPy::from)?;
435+
Ok(result)
436+
})
437+
}
438+
394439
pub fn closed_deals<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
395440
let client = self.client.clone();
396441
future_into_py(py, async move {

crates/binary_options_tools/examples/pending_trades_example.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ fn create_test_pending_order(req_id: Uuid) -> PendingOrder {
7777
timeframe: 60,
7878
min_payout: 85,
7979
command: 0,
80+
status: None,
8081
date_created: "2024-01-01 10:00:00".to_string(),
8182
id: 12345,
8283
}
@@ -265,6 +266,7 @@ async fn example_concurrent_pending_orders() -> PocketResult<()> {
265266
timeframe: 60,
266267
min_payout: 85,
267268
command: 0,
269+
status: None,
268270
date_created: "2024-01-01 10:00:00".to_string(),
269271
id: (1000 + i) as u64,
270272
};
@@ -304,6 +306,7 @@ async fn example_concurrent_pending_orders() -> PocketResult<()> {
304306
timeframe: 60,
305307
min_payout: 85,
306308
command: 0,
309+
status: None,
307310
date_created: "2024-01-01 10:00:00".to_string(),
308311
id: (1000 + i) as u64,
309312
};

0 commit comments

Comments
 (0)