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 prices = context.prices;
var targetPriceLevel = getTargetPriceLevel(side);
var targetPrice = BookUtils.getPrice(book, side, targetPriceLevel);
var oldPrice = prices[side].oldLevel;
code
if (targetPrice !== oldPrice) {
var newOrder = OrderUtils.processOrModOrder(side, stock, targetPrice);
if (newOrder) {
prices[side].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) {
priceUpdate(CONFIG.ORDER_SIDE, UserContext.book);
}
}
function marketEvent() {
if (!UserContext.status.run) return;
var eventType = SystemContext.EventType;
code
if (!UserContext.book) {
UserContext.book = Book(ps);
}
var book = UserContext.book;
switch(eventType) {
case TSEventType.Price:
var currentPrice = Val(ps, "LAST");
if (UserContext.lastPrice !== currentPrice) {
var currentTime = Now().ToString("HH:mm:ss.ff");
Helper.log('PriceChange', {
currentPrice: currentPrice,
previousPrice: UserContext.lastPrice,
currentTime: currentTime
});
UserContext.lastPrice = currentPrice;
}
break;
case TSEventType.Time:
UserContext.status.secondCount++;
manageCycle();
break;
}
}
if (SystemContext.IsFirst) {
setupTradeContext();
}
marketEvent(); la sequenza di operazioni e log e' la seguente: 1 Initialization Complete: 2 Formatted State 3 Order Action. Io vorrei che 2 e 3 fossero invertite, cioe' che la nuova priorita' diventi la seguente : Sequence of Operations and Logs:
Initialization Complete:
Inizializzazione completata:
Bid Price 0: [bidPrice]
Ask Price 0: [askPrice]
Order Action:
Ordine Created: [OrderCode]
Formatted State:
6. ---------------------------------------------------
7. Bid Price Bid Quant. Ask Price Ask Quant.
8. ---------------------------------------------------
9. [bidPrice] [bidQuantity] [askPrice] [askQuantity]
10. ---------------------------------------------------
11. Numero Operazione: [numOperazione]
Puoi riscrivere il codice intero invertendo l'ordine di formatted state e order creation ?
Certamente! Di seguito trovi il codice completo riscritto in modo che l'ordine dei log sia invertito come richiesto. Ora, dopo l'inizializzazione, verrà prima registrata l'azione dell'ordine e successivamente lo stato formattato.
Modifiche Principali:
Separazione dei Log: Abbiamo separato il log dello stato formattato (Formatted State) dal log di Initialization Complete.
Aggiornamento della Funzione Helper.log: La funzione Helper.log ora gestisce separatamente i messaggi senza concatenare automaticamente lo stato formattato con Initialization Complete.
Chiamate di Log Appropriate: Dopo la creazione o la modifica di un ordine, viene registrato l'azione dell'ordine e poi lo stato formattato.
Ecco il codice completo aggiornato:
javascript
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";
var BookUtils = {
getSideData: function (book, side) {
return (side === 'BID') ? book.Bid : book.Ask;
},
getPrice: function (book, side, level) {
var sideData = this.getSideData(book, side);
return sideData.Price(level);
},
getQuantity: function (book, side, level) {
var sideData = this.getSideData(book, side);
return sideData.Quantity(level);
},
getBookState: function (book) {
return {
bidPrice: book.Bid.Price(0),
bidQuantity: book.Bid.Quantity(0),
askPrice: book.Ask.Price(0),
askQuantity: book.Ask.Quantity(0)
};
}
};
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;
} else {
var modifiedOrder = OrderManager.Modify(currentOrder.OrderCode, price);
Helper.log('Order Action', { action: 'Modified', orderCode: modifiedOrder.OrderCode });
Helper.log('Formatted State');
return this.updateOrderContext(modifiedOrder, side, price);
}
}
var newOrder = this.execOrder(side, stock, price, CONFIG.SIZE);
Helper.log('Order Action', { action: 'Created', orderCode: newOrder.OrderCode });
Helper.log('Formatted State');
return this.updateOrderContext(newOrder, side, price);
},
deleteOrder: function (orderCode) {
var deletedOrder = OrderManager.Delete(orderCode);
Helper.log('Order Deleted', { orderCode: deletedOrder.OrderCode });
Helper.log('Formatted State');
return deletedOrder;
}
};
var Helper = {
placeholderRegex: /\{(\w+)\}/g,
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,
'Formatted State': "---------------------------------------------------\r\n" +
"Bid Price\tBid Quant.\tAsk Price\tAsk Quant.\r\n" +
"---------------------------------------------------\r\n" +
"{bidPrice}\t\t{bidQuantity}\t\t{askPrice}\t\t{askQuantity}\r\n" +
"---------------------------------------------------\r\n" +
"Numero Operazione: {numOperazione}\r\n" +
"---------------------------------------------------"
},
formatMessage: function(template, data) {
return template.replace(this.placeholderRegex, function(match, key) {
return typeof data[key] !== 'undefined' ? data[key] : match;
});
},
formatState: function () {
var bookState = BookUtils.getBookState(UserContext.book);
return this.formatMessage(this.messages['Formatted State'], {
bidPrice: bookState.bidPrice,
bidQuantity: bookState.bidQuantity,
askPrice: bookState.askPrice,
askQuantity: bookState.askQuantity,
numOperazione: UserContext.status.numOperazione
});
},
log: function (type, data) {
var message = '';
var template = this.messages[type];
if (template) {
if (type === 'Formatted State') {
message = this.formatState();
} else {
message = this.formatMessage(template, data || {});
if (type === 'Initialization Complete') {
// Solo il messaggio di inizializzazione senza lo stato formattato
}
}
Log(separator + message);
}
},
priceDirection: function (targetPrice, oldPrice) {
return (targetPrice > oldPrice) ? 1 : ((targetPrice < oldPrice) ? -1 : 0);
}
};
function setupTradeContext() {
var book = Book(ps);
UserContext = {
book: book,
order: { current: null },
status: { run: true, secondCount: 0, numOperazione: 1 },
prices: {
BID: {
current: BookUtils.getPrice(book, 'BID', 0),
oldLevel: BookUtils.getPrice(book, 'BID', getTargetPriceLevel('BID'))
},
ASK: {
current: BookUtils.getPrice(book, 'ASK', 0),
oldLevel: BookUtils.getPrice(book, 'ASK', getTargetPriceLevel('ASK'))
}
}
};
Helper.log('Initialization Complete', {
bidPrice: UserContext.prices.BID.current,
askPrice: UserContext.prices.ASK.current
});
Helper.log('Formatted State'); // Log separato dello stato formattato
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 prices = context.prices;
var targetPriceLevel = getTargetPriceLevel(side);
var targetPrice = BookUtils.getPrice(book, side, targetPriceLevel);
var oldPrice = prices[side].oldLevel;
if (targetPrice !== oldPrice) {
var newOrder = OrderUtils.processOrModOrder(side, stock, targetPrice);
if (newOrder) {
prices[side].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);
status.run = false;
return true;
}
}
return false;
}
function manageCycle() {
var status = UserContext.status;
if (checkOps()) return;
if (status.secondCount >= CONFIG.SECONDS) {
priceUpdate(CONFIG.ORDER_SIDE, UserContext.book);
}
}
function marketEvent() {
if (!UserContext.status.run) return;
var eventType = SystemContext.EventType;
if (!UserContext.book) {
UserContext.book = Book(ps);
}
var book = UserContext.book;
switch(eventType) {
case TSEventType.Price:
var currentPrice = Val(ps, "LAST");
if (UserContext.lastPrice !== currentPrice) {
var currentTime = Now().ToString("HH:mm:ss.ff");
Helper.log('PriceChange', {
currentPrice: currentPrice,
previousPrice: UserContext.lastPrice,
currentTime: currentTime
});
UserContext.lastPrice = currentPrice;
}
break;
case TSEventType.Time:
UserContext.status.secondCount++;
manageCycle();
break;
}
}
if (SystemContext.IsFirst) {
setupTradeContext();
}
marketEvent();
Spiegazione delle Modifiche:
Helper.messages:
Aggiunta una nuova voce 'Formatted State' che contiene il formato desiderato per lo stato formattato.
Helper.formatState:
Nuova funzione che formatta lo stato corrente del libro utilizzando il template 'Formatted State'.
Helper.log:
Modificata per gestire separatamente il tipo 'Formatted State' senza concatenarlo automaticamente ad altri messaggi.
OrderUtils.processOrModOrder e OrderUtils.deleteOrder:
Dopo la creazione, modifica o cancellazione di un ordine, viene chiamato Helper.log('Order Action', ...) seguito da Helper.log('Formatted State') per garantire che lo stato formattato venga registrato dopo l'azione dell'ordine.
setupTradeContext:
Dopo aver loggato 'Initialization Complete', viene chiamato Helper.log('Formatted State') per registrare lo stato iniziale separatamente.
Risultato del Log:
Con queste modifiche, i log seguiranno la sequenza desiderata: