Certamente! Di seguito trovi il listato completo aggiornato, dove la versione A di BookUtils è stata sostituita con la versione B e tutte le chiamate pertinenti nel resto del codice sono state adeguate di conseguenza.
var CONFIG = {
ORDER_SIDE: 'BID',
SIZE: 1,
MAX_OPERATIONS: 8,
LEVELS: {
BID: 6,
ASK: 6
},
SECONDS: 5,
MONITORING_MODE: 'MONITORA_yourlevel' // oppure 'MONITORA_1Level'
};
var separator = "---------------------------------------------------\r\n";
// Versione B di BookUtils
var BookUtils = {
dataTypes: {
'Price': function(level) { return this.Price(level); },
'Quantity': function(level) { return this.Quantity(level); }
},
getData: function(book, side, level, type) {
var sideData = (side === 'BID') ? book.Bid : book.Ask;
var typeFunc = this.dataTypes[type];
return typeFunc.call(sideData, level);
},
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) {
if (currentOrder.Price === price) return 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',
'Price Variation': "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) {
return template.replace(/\{(\w+)\}/g, function(match, key) {
return data.hasOwnProperty(key) ? data[key] : match;
});
},
formatState: function (bookState) {
return 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;
},
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 === 'Price Variation')) {
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 bidPriceCurrent = BookUtils.getData(book, 'BID', 0, 'Price');
var bidPriceOldLevel = BookUtils.getData(book, 'BID', getTargetPriceLevel('BID'), 'Price');
var askPriceCurrent = BookUtils.getData(book, 'ASK', 0, 'Price');
var askPriceOldLevel = BookUtils.getData(book, 'ASK', getTargetPriceLevel('ASK'), 'Price');
var bidTargetPriceLevel = getTargetPriceLevel('BID');
var askTargetPriceLevel = getTargetPriceLevel('ASK');
UserContext = {
book: book,
order: { current: null },
status: { run: true, secondCount: 0, numOperazione: 1 },
prices: {
BID: {
current: bidPriceCurrent,
oldLevel: bidPriceOldLevel
},
ASK: {
current: askPriceCurrent,
oldLevel: askPriceOldLevel
}
},
targetPriceLevels: {
BID: bidTargetPriceLevel,
ASK: askTargetPriceLevel
}
};
var bookState = BookUtils.getBookState(UserContext.book);
Helper.log('Initialization Complete', {
bidPrice: bookState.bidPrice,
askPrice: bookState.askPrice
}, false);
var side = CONFIG.ORDER_SIDE;
var priceLevel = UserContext.targetPriceLevels[side];
var price = BookUtils.getData(book, side, priceLevel, 'Price');
var 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) {
var level = (CONFIG.MONITORING_MODE === 'MONITORA_1Level' ? 1 : CONFIG.LEVELS[side]);
return level - 1;
}
function priceUpdate(side, book) {
var context = UserContext;
var status = context.status;
var pricesSide = context.prices[side];
var targetPriceLevel = context.targetPriceLevels[side]; // Utilizzo del valore memorizzato
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 payload = {
lastLevel: CONFIG.LEVELS[side] + Helper.priceDirection(targetPrice, oldPrice),
priceLevel: targetPriceLevel
};
Helper.log('Price Variation', payload);
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 handleTimeEvent() {
UserContext.status.secondCount++;
manageCycle();
}
function handlePriceEvent(currentPrice) {
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;
}
}
var eventHandlers = {};
eventHandlers[TSEventType.Price] = function() {
handlePriceEvent(Val(ps, "LAST"));
};
eventHandlers[TSEventType.Time] = handleTimeEvent;
function marketEvent() {
if (!UserContext.status.run) return;
var handler = eventHandlers[SystemContext.EventType];
if (handler) handler();
}
if (SystemContext.IsFirst) {
setupTradeContext();
}
marketEvent();
Modifiche Applicate
-
Sostituzione di BookUtils Versione A con Versione B:
- La versione originale di
BookUtils che includeva getSideData, getPrice, getQuantity è stata sostituita con la versione B che utilizza getData e una mappa di dataTypes.
-
Adeguamento delle Chiamate a BookUtils.getPrice e BookUtils.getQuantity:
- Tutte le chiamate precedenti a
BookUtils.getPrice(book, side, level) sono state sostituite con BookUtils.getData(book, side, level, 'Price').
- Analogamente, se ci fossero state chiamate a
BookUtils.getQuantity(book, side, level), sarebbero state sostituite con BookUtils.getData(book, side, level, 'Quantity').
- Nel listato fornito, le chiamate a
getQuantity erano presenti solo nella versione A di BookUtils.getBookState, ma poiché ora getBookState utilizza getData, non è necessario modificare ulteriormente.
-
Funzione setupTradeContext:
- Tutte le chiamate a
BookUtils.getPrice all'interno di questa funzione sono state aggiornate per utilizzare getData con il parametro "Price".
-
Funzione priceUpdate:
- La chiamata a
BookUtils.getPrice è stata sostituita con BookUtils.getData(book, side, targetPriceLevel, 'Price').
Considerazioni Finali
Assicurati che il resto del tuo ambiente di esecuzione supporti correttamente le modifiche apportate e che le funzioni come OrderManager, UserContext, Log, Now, Val, e le costanti come TSEventType e SystemContext siano definite e funzionino come previsto. Testa accuratamente il nuovo script in un ambiente di sviluppo prima di implementarlo in produzione.