Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from ccxt.async_support.base.exchange import Exchange
from .polymarket_abstract import ImplicitAPI
import asyncio
import hashlib
import math
import json
Expand Down Expand Up @@ -548,16 +549,31 @@ async def fetch_markets(self, params={}) -> List[Market]:
active = self.safe_bool(options, 'active', True)
if self.safe_value(params, 'closed') is None:
request['closed'] = not active
offset = self.safe_integer(request, 'offset', 0)
markets: List[Any] = []
while(True):
pageRequest = self.extend(request, {'offset': offset})
response = await self.gamma_public_get_markets(pageRequest)
page = self.safe_list(response, 'data', response) or []
markets = self.array_concat(markets, page)
if len(page) < limit:
break
offset += limit
# Fetch first page to seed the results
firstResponse = await self.gamma_public_get_markets(self.extend({}, request, {'offset': 0}))
firstPage = self.safe_list(firstResponse, 'data', firstResponse)
markets: List[Any] = firstPage
if len(firstPage) >= limit:
# API returns a plain list with no total count — fetch remaining pages in parallel batches.
# BATCH_SIZE=10 pages per round, MAX_ROUNDS=100 covers up to 500,000 markets.
BATCH_SIZE = 10
MAX_ROUNDS = 100
currentOffset = limit
done = False
for round in range(0, MAX_ROUNDS):
batchPromises = []
for i in range(0, BATCH_SIZE):
batchPromises.append(self.gamma_public_get_markets(self.extend({}, request, {'offset': currentOffset + i * limit})))
batchResponses = await asyncio.gather(*batchPromises)
for i in range(0, len(batchResponses)):
page = self.safe_list(batchResponses[i], 'data', batchResponses[i])
markets = self.array_concat(markets, page)
if len(page) < limit:
done = True
break
currentOffset += BATCH_SIZE * limit
if done:
break
filtered = []
for i in range(0, len(markets)):
market = markets[i]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,16 +548,31 @@ def fetch_markets(self, params={}) -> List[Market]:
active = self.safe_bool(options, 'active', True)
if self.safe_value(params, 'closed') is None:
request['closed'] = not active
offset = self.safe_integer(request, 'offset', 0)
markets: List[Any] = []
while(True):
pageRequest = self.extend(request, {'offset': offset})
response = self.gamma_public_get_markets(pageRequest)
page = self.safe_list(response, 'data', response) or []
markets = self.array_concat(markets, page)
if len(page) < limit:
break
offset += limit
# Fetch first page to seed the results
firstResponse = self.gamma_public_get_markets(self.extend({}, request, {'offset': 0}))
firstPage = self.safe_list(firstResponse, 'data', firstResponse)
markets: List[Any] = firstPage
if len(firstPage) >= limit:
# API returns a plain list with no total count — fetch remaining pages in parallel batches.
# BATCH_SIZE=10 pages per round, MAX_ROUNDS=100 covers up to 500,000 markets.
BATCH_SIZE = 10
MAX_ROUNDS = 100
currentOffset = limit
done = False
for round in range(0, MAX_ROUNDS):
batchPromises = []
for i in range(0, BATCH_SIZE):
batchPromises.append(self.gamma_public_get_markets(self.extend({}, request, {'offset': currentOffset + i * limit})))
batchResponses = batchPromises
for i in range(0, len(batchResponses)):
page = self.safe_list(batchResponses[i], 'data', batchResponses[i])
markets = self.array_concat(markets, page)
if len(page) < limit:
done = True
break
currentOffset += BATCH_SIZE * limit
if done:
break
filtered = []
for i in range(0, len(markets)):
market = markets[i]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
import time
import typing
import octobot_commons.constants as commons_constants
import octobot_commons.enums as commons_enums
import octobot_commons.errors as commons_errors
import octobot_commons.dsl_interpreter as dsl_interpreter
Expand All @@ -23,6 +25,7 @@
import octobot_trading.enums as trading_enums

import tentacles.Meta.DSL_operators as dsl_operators
import tentacles.Meta.DSL_operators.exchange_operators.exchange_operator as exchange_operator


class DSLTradingModeConsumer(trading_modes.AbstractTradingModeConsumer):
Expand All @@ -37,7 +40,11 @@ async def set_final_eval(
self.logger.info(
f"Executing DSL script trigger by {matrix_id=}, {cryptocurrency=}, {symbol=}, {time_frame=}, {trigger_source=}"
)
result = await self.trading_mode.interpret_dsl_script() # type: ignore
if symbol not in self.exchange_manager.exchange_config.traded_symbol_pairs:
self.logger.info(f"Registering new trading pair: {symbol}")
await self.exchange_manager.exchange_config.add_traded_symbols([symbol], [])
self.trading_mode.triggered_symbol = symbol
result = await self.trading_mode.interpret_dsl_script()
self.logger.info(f"DSL script successfully executed. Result: {result.result}")

@classmethod
Expand All @@ -57,6 +64,7 @@ class DSLTradingMode(trading_modes.AbstractTradingMode):
def __init__(self, config, exchange_manager):
super().__init__(config, exchange_manager)
self.dsl_script: str = ""
self.triggered_symbol: str = ""
self.interpreter: dsl_interpreter.Interpreter = None # type: ignore

def init_user_inputs(self, inputs: dict) -> None:
Expand Down Expand Up @@ -116,6 +124,11 @@ def _create_interpreter(
self.exchange_manager, trading_mode=self, dependencies=dependencies
)
+ dsl_operators.create_blockchain_wallet_operators(self.exchange_manager)
+ [
_triggered_symbol_operator(self),
_market_expiry_operator(self),
_now_ms_operator(),
]
)

async def interpret_dsl_script(self) -> dsl_interpreter.DSLCallResult:
Expand Down Expand Up @@ -145,4 +158,73 @@ def get_supported_exchange_types(cls) -> list:
return [
trading_enums.ExchangeTypes.SPOT,
trading_enums.ExchangeTypes.FUTURE,
trading_enums.ExchangeTypes.OPTION,
]


def _triggered_symbol_operator(trading_mode) -> type[exchange_operator.ExchangeOperator]:
class _TriggeredSymbolOperator(exchange_operator.ExchangeOperator):
DESCRIPTION = "Returns the symbol that triggered the current DSL script execution"
EXAMPLE = "triggered_symbol()"

@staticmethod
def get_library() -> str:
return commons_constants.CONTEXTUAL_OPERATORS_LIBRARY

@classmethod
def get_parameters(cls) -> list:
return []

@staticmethod
def get_name() -> str:
return "triggered_symbol"

async def pre_compute(self) -> None:
await super().pre_compute()
self.value = trading_mode.triggered_symbol

return _TriggeredSymbolOperator


def _market_expiry_operator(trading_mode) -> type[exchange_operator.ExchangeOperator]:
class _MarketExpiryOperator(exchange_operator.ExchangeOperator):
DESCRIPTION = "Returns the expiry timestamp in milliseconds for the given symbol's market, or None"
EXAMPLE = "market_expiry(triggered_symbol())"

@classmethod
def get_parameters(cls) -> list:
return [dsl_interpreter.OperatorParameter("symbol", "The market symbol", True, str)]

@staticmethod
def get_name() -> str:
return "market_expiry"

async def pre_compute(self) -> None:
await super().pre_compute()
symbol = self.get_computed_parameters()[0]
markets = trading_mode.exchange_manager.exchange.connector.client.markets
self.value = (markets.get(symbol) or {}).get(
trading_enums.ExchangeConstantsMarketStatusColumns.EXPIRY.value
)

return _MarketExpiryOperator


def _now_ms_operator() -> type[exchange_operator.ExchangeOperator]:
class _NowMsOperator(exchange_operator.ExchangeOperator):
DESCRIPTION = "Returns the current time in milliseconds"
EXAMPLE = "now_ms()"

@classmethod
def get_parameters(cls) -> list:
return []

@staticmethod
def get_name() -> str:
return "now_ms"

async def pre_compute(self) -> None:
await super().pre_compute()
self.value = time.time() * 1000

return _NowMsOperator
45 changes: 45 additions & 0 deletions packages/tentacles/profiles/itm_predictions_market/profile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"config": {
"crypto-currencies": {},
"distribution": "default",
"exchanges": {
"polymarket": {
"enabled": true,
"exchange-type": "option"
}
},
"trader": {
"enabled": false,
"load-trade-history": false
},
"trader-simulator": {
"enabled": true,
"fees": {
"maker": 0.0,
"taker": 0.0
},
"starting-portfolio": {
"USDC": 1000
}
},
"trading": {
"reference-market": "USDC",
"risk": 0.5
}
},
"profile": {
"auto_update": false,
"avatar": "default_profile.png",
"complexity": 2,
"description": "ITM Predictions Market profile: buys in-the-money binary options on predictions markets (e.g. Polymarket) using limit orders with stop loss and take profit.",
"extra_backtesting_time_frames": [],
"id": "itm_predictions_market",
"imported": false,
"name": "ITM Predictions Market",
"origin_url": null,
"read_only": true,
"risk": 2,
"slug": "",
"type": "live"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dsl_script": "limit('buy', triggered_symbol(), available('USDC') * 0.05, price='-1%', stop_loss_price='-10%', take_profit_prices=['0.99'], allow_holdings_adaptation=True) if market_expiry(triggered_symbol()) is not None and 0 < market_expiry(triggered_symbol()) - now_ms() <= 300000 and close(triggered_symbol())[-1] >= 0.90 else None"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"tentacle_activation": {
"Trading": {
"DSLTradingMode": true
}
}
}
Loading