for (var i = 0; i < placeholders.length; i++) {
var placeholder = placeholders[i];
var key = placeholder.slice(1, -1); // Rimuove le parentesi graffe
if (data.hasOwnProperty(key)) {
result = result.replace(placeholder, data[key]);
}
}
return result;
},
function priceUpdate(side, book) {
var context = UserContext;
var status = context.status;
var pricesSide = context.prices[side];
var targetPriceLevel = context.targetPriceLevels[side];
var targetPrice = BookUtils.getData(book, side, targetPriceLevel, 'Price');
var oldPrice = pricesSide.oldLevel;
code
if (targetPrice !== oldPrice) {
var newOrder = OrderUtils.processOrModOrder(side, ps, targetPrice);
if (newOrder) {
pricesSide.current = targetPrice;
var direction = Helper.priceDirection(targetPrice, oldPrice);
var levelDiff = CONFIG.LEVELS[side] + direction;
Helper.log('Level Variation', { lastLevel: levelDiff, priceLevel: targetPriceLevel });
status.secondCount = 0;
status.numOperazione++;
}
}
}
function checkOps() {
var status = UserContext.status;
var currentOrder = UserContext.order.current;
if (status.numOperazione >= CONFIG.MAX_OPERATIONS) {
if (currentOrder) {
var deletedOrder = OrderUtils.deleteOrder(currentOrder.OrderCode);
Helper.log('Order Deleted', { orderCode: deletedOrder.OrderCode });
status.run = false;
return true;
}
}
return false;
}
function manageCycle() {
var status = UserContext.status;
if (checkOps()) return;
if (status.secondCount >= CONFIG.SECONDS) {
var side = CONFIG.ORDER_SIDE;
var book = UserContext.book;
priceUpdate(side, book);
status.secondCount = 0;
}
}
function marketEvent() {
if (!UserContext.status.run) return;
code
var eventType = SystemContext.EventType;
if (eventType === TSEventType.Price) {
var currentPrice = Val(ps, "LAST");
var context = UserContext;
if (context.lastPrice !== currentPrice) {
var currentTime = Now().ToString("HH:mm:ss.ff");
Helper.log('PriceChange', {
currentPrice: currentPrice,
previousPrice: context.lastPrice,
currentTime: currentTime
});
context.lastPrice = currentPrice;
}
} else if (eventType === TSEventType.Time) {
UserContext.status.secondCount++;
manageCycle();
}
}
if (SystemContext.IsFirst) {
setupTradeContext();
}
marketEvent(); mi puoi sostituire la riga 'Level Variation': "Livello da {lastLevel} all'attuale livello {priceLevel}\n", con la riga 'LevelChange': "Livello da {lastLevel} all'attuale livello {priceLevel}\n", ?E poi modificare anche tutte le chiamate e riscrivere il codice intero corretto
Certo! Di seguito trovi il codice completo aggiornato con la sostituzione della chiave 'Level Variation' con 'LevelChange' e tutte le relative chiamate modificate di conseguenza.
javascript
var CONFIG = {
ORDER_SIDE: 'BID',
SIZE: 1,
MAX_OPERATIONS: 8,
LEVELS: { BID: 6, ASK: 6 },
SECONDS: 5,
MONITORING_MODE: 'MONITORA_yourlevel'
};
var separator = "---------------------------------------------------\r\n";
var BookUtils = {
getData: function(book, side, level, type) {
var sideData = (side === 'BID') ? book.Bid : book.Ask;
if (type === 'Price') {
return sideData.Price(level);
} else if (type === 'Quantity') {
return sideData.Quantity(level);
}
return null;
},
getBookState: function(book) {
return {
bidPrice: this.getData(book, 'BID', 0, 'Price'),
bidQuantity: this.getData(book, 'BID', 0, 'Quantity'),
askPrice: this.getData(book, 'ASK', 0, 'Price'),
askQuantity: this.getData(book, 'ASK', 0, 'Quantity')
};
}
};
var OrderUtils = {
execOrder: function(side, stock, price, size) {
return (side === 'BID') ? OrderManager.Buy(stock, price, size) : OrderManager.Sell(stock, price, size);
},
updateOrderContext: function(order, side, price) {
UserContext.order.current = order;
UserContext.prices[side].oldLevel = price;
return order;
},
processOrModOrder: function(side, stock, price) {
var currentOrder = UserContext.order.current;
if (currentOrder && currentOrder.Price === price) {
return currentOrder;
}
if (currentOrder) {
return this.updateOrderContext(OrderManager.Modify(currentOrder.OrderCode, price), side, price);
}
return this.updateOrderContext(this.execOrder(side, stock, price, CONFIG.SIZE), side, price);
},
deleteOrder: function(orderCode) {
return OrderManager.Delete(orderCode);
}
};
var Helper = {
messages: {
'Initialization Complete': "Inizializzazione completata:\nBid Price 0: {bidPrice}\nAsk Price 0: {askPrice}\n",
'Order Action': 'Ordine {action}: {orderCode}\n',
'LevelChange': "Livello da {lastLevel} all'attuale livello {priceLevel}\n",
'Order Deleted': 'Ordine cancellato: {orderCode}\n',
'PriceChange': "ULTIMO PREZZO: {currentPrice}, Precedente: {previousPrice}, Ora: {currentTime}\r\n" + separator,
'message': "{message}\r\n" + separator
},
formatMessage: function(template, data) {
var placeholders = template.match(/\{(\w+)\}/g) || [];
var result = template;
for (var i = 0; i < placeholders.length; i++) {
var placeholder = placeholders[i];
var key = placeholder.slice(1, -1); // Rimuove le parentesi graffe
if (data.hasOwnProperty(key)) {
result = result.replace(placeholder, data[key]);
}
}
return result;
},
formatState: function(bookState) {
var stateLines = [
separator,
"Bid Price \tBid Quant.\tAsk Price \tAsk Quant.\r\n",
separator,
bookState.bidPrice + "\t\t" + bookState.bidQuantity + "\t\t" + bookState.askPrice + "\t\t" + bookState.askQuantity + "\r\n",
separator,
"Numero Operazione: " + UserContext.status.numOperazione + "\r\n",
separator
];
return stateLines.join('');
},
log: function(type, data, includeFormattedState) {
includeFormattedState = (typeof includeFormattedState !== 'undefined') ? includeFormattedState : true;
var template = this.messages[type];
if (template) {
var message = this.formatMessage(template, data || {});
if (includeFormattedState && (type === 'Initialization Complete' || type === 'LevelChange')) {
message += this.formatState(BookUtils.getBookState(UserContext.book));
}
Log(separator + message);
}
},
logFormattedState: function() {
var bookState = BookUtils.getBookState(UserContext.book);
Log(this.formatState(bookState));
},
priceDirection: function(targetPrice, oldPrice) {
return (targetPrice > oldPrice) ? 1 : ((targetPrice < oldPrice) ? -1 : 0);
}
};
function setupTradeContext() {
var book = Book(ps);
var bidPrice = BookUtils.getData(book, 'BID', 0, 'Price'),
bidOldLevel = BookUtils.getData(book, 'BID', getTargetPriceLevel('BID'), 'Price'),
askPrice = BookUtils.getData(book, 'ASK', 0, 'Price'),
askOldLevel = BookUtils.getData(book, 'ASK', getTargetPriceLevel('ASK'), 'Price'),
targetBidLevel = getTargetPriceLevel('BID'),
targetAskLevel = getTargetPriceLevel('ASK');
UserContext = {
book: book,
order: { current: null },
status: { run: true, secondCount: 0, numOperazione: 0 },
prices: {
BID: { current: bidPrice, oldLevel: bidOldLevel },
ASK: { current: askPrice, oldLevel: askOldLevel }
},
targetPriceLevels: {
BID: targetBidLevel,
ASK: targetAskLevel
}
};
var bookState = BookUtils.getBookState(book);
Helper.log('Initialization Complete', {
bidPrice: bookState.bidPrice,
askPrice: bookState.askPrice
}, false);
var side = CONFIG.ORDER_SIDE,
price = BookUtils.getData(book, side, UserContext.targetPriceLevels[side], 'Price'),
size = CONFIG.SIZE;
UserContext.prices[side].current = price;
var initialOrder = OrderUtils.execOrder(side, stock, price, size);
OrderUtils.updateOrderContext(initialOrder, side, price);
Helper.log('Order Action', { action: 'inserito', orderCode: initialOrder.OrderCode }, false);
Helper.logFormattedState();
manageCycle();
}
function getTargetPriceLevel(side) {
return (CONFIG.MONITORING_MODE === 'MONITORA_1Level' ? 0 : CONFIG.LEVELS[side] - 1);
}
function priceUpdate(side, book) {
var context = UserContext;
var status = context.status;
var pricesSide = context.prices[side];
var targetPriceLevel = context.targetPriceLevels[side];
var targetPrice = BookUtils.getData(book, side, targetPriceLevel, 'Price');
var oldPrice = pricesSide.oldLevel;
if (targetPrice !== oldPrice) {
var newOrder = OrderUtils.processOrModOrder(side, ps, targetPrice);
if (newOrder) {
pricesSide.current = targetPrice;
var direction = Helper.priceDirection(targetPrice, oldPrice);
var levelDiff = CONFIG.LEVELS[side] + direction;
Helper.log('LevelChange', { lastLevel: levelDiff, priceLevel: targetPriceLevel });
status.secondCount = 0;
status.numOperazione++;
}
}
}
function checkOps() {
var status = UserContext.status;
var currentOrder = UserContext.order.current;
if (status.numOperazione >= CONFIG.MAX_OPERATIONS) {
if (currentOrder) {
var deletedOrder = OrderUtils.deleteOrder(currentOrder.OrderCode);
Helper.log('Order Deleted', { orderCode: deletedOrder.OrderCode });
status.run = false;
return true;
}
}
return false;
}
function manageCycle() {
var status = UserContext.status;
if (checkOps()) return;
if (status.secondCount >= CONFIG.SECONDS) {
var side = CONFIG.ORDER_SIDE;
var book = UserContext.book;
priceUpdate(side, book);
status.secondCount = 0;
}
}
function marketEvent() {
if (!UserContext.status.run) return;
var eventType = SystemContext.EventType;
if (eventType === TSEventType.Price) {
var currentPrice = Val(ps, "LAST");
var context = UserContext;
if (context.lastPrice !== currentPrice) {
var currentTime = Now().ToString("HH:mm:ss.ff");
Helper.log('PriceChange', {
currentPrice: currentPrice,
previousPrice: context.lastPrice,
currentTime: currentTime
});
context.lastPrice = currentPrice;
}
} else if (eventType === TSEventType.Time) {
UserContext.status.secondCount++;
manageCycle();
}
}
if (SystemContext.IsFirst) {
setupTradeContext();
}
marketEvent();
Modifiche Effettuate:
Sostituzione nella Definizione dei Messaggi:
Prima:
javascript
'Level Variation': "Livello da {lastLevel} all'attuale livello {priceLevel}\n",
Dopo:
javascript
'LevelChange': "Livello da {lastLevel} all'attuale livello {priceLevel}\n",
Aggiornamento della Condizione nel Metodo log di Helper:
Prima:
javascript
if (includeFormattedState && (type === 'Initialization Complete' || type === 'Level Variation')) {
Dopo:
javascript
if (includeFormattedState && (type === 'Initialization Complete' || type === 'LevelChange')) {
Queste modifiche assicurano che tutte le occorrenze di 'Level Variation' siano sostituite correttamente con 'LevelChange' nel codice, mantenendo la coerenza e prevenendo potenziali errori durante l'esecuzione.
Se hai bisogno di ulteriori modifiche o chiarimenti, non esitare a chiedere!