Price and Volume functions

price(int offset) : var

Returns the TWAP (time-weighted average price) of the selected asset. TWAP is the mean of all price ticks of the bar or time frame with the given offset. For indicators the mean price is normally preferable to the close price, since it is less susceptible to random fluctuations and makes systems more robust and less dependent on the data feed.

priceO(int offset) : var

priceC(int offset) : var

priceH(int offset) : var

priceL(int offset) : var

Returns the open, close, maximum and minimum price of the current asset and the bar or time frame with the given offset. priceC(0) returns the current price.

priceReal() : var

Returns the current real price when OHLC prices have been artificially calculated by special bars. The TICKS flag must be set.  

marketVal(int offset) : var

marketVol(int offset) : var

Returns additional market data, such as spread, quote size, trade volume, accumulated volume, tick rate, or quote frequency - see remarks and examples below. The type of data returned depends in live trading on the Broker API, and in backtesting on the fVal and fVol fields in .t6 historical data, or on the ask-bid difference and volume field in .t1 or .t2 data. Normally marketVal returns the average historical spread of the last bar period, and marketVol returns the average historical trade volume of the last bar period. For the total historical volume of a bar, multiply marketVol() with BarPeriod divided by the historical data resolution in minutes. The market functions require Zorro S and no LEAN flag.

Parameters:

offset Bar or time frame number for which the prices are returned, relative to the current bar or time frame. 0 for the the current bar or time frame. Negative offsets return prices of future bars or time frames when the PEEK flag is set; otherwise they give an error message.

Returns:

Price or raw market data.
 

seriesO(): vars

seriesH(): vars

seriesL(): vars

seriesC(): vars

Returns pointers to a temporary open, high, low, and close price series of the current asset. Can be used as a price series parameter to any function or indicator, or for referencing a price N bars ago (example: var PreviousClose = (seriesC())[1]; - mind the parentheses). Unlike real series, they don't consume memory and can be conditionally called. Temporary series do not support TimeFrame and special bars, and keep their content only until the next indicator call.
  

priceSet (int offset, var Open, var High, var Low, var Close)

Modifies the open, high, low, close, and mean price of the current asset at the given bar, for dummy assets, artificial price curves, displaying special candles, or using a different price source. Use offset = -1 for modifying the prices of the next bar when trades should be entered at the modified prices. Does not affect intrabar prices in TICKS mode.

priceQuote (var Timestamp, var Quote) : int

Enters a new price quote of the current asset in the system; especially for HFT simulation or when prices are not available from the broker connection. Filters outliers and updates the current best ask (AskPrice) and best bid (AskPrice - Spread). Increases Bar after every BarPeriod when no run function is used. Price quotes are also printed to the log when Verbose is at 3 or above, so be careful with Verbose for preventing awfully large log files.

Parameters:

offset Optional bar number for which the prices are returned, in time frames before the current time frame. If 0, the price of the current bar is returned. Negative offsets return future prices when the PEEK flag is set; otherwise they give an error message.
Timestamp The exchange timestamp of the quote in DATE format. Quotes older than the previous quote are ignored.
Quote The price. Quote > 0 indicates an ask price, Quote < 0 a bid price.

Returns:

1 if the price quote was accepted, 0 if an Outlier was detected or the timestamp was outdated.
  

priceRecord ()

Appends the current OHLC candle and the current spread and volume of the current asset to the begin of the price history in .t1 or .t6 format; only in [Trade] mode and after the INITRUN. This allows recording live prices for re-training or other purposes.
 

Remarks:

Examples:

BarPeriod = 60; // set up 1 hour bars (60 minutes)

TimeFrame = 4;
asset("EUR/USD");
vars EUR4Hs = series(price(0));	// create a series of 4 hour EUR mean prices
...
TimeFrame = frameSync(24);
asset("SPX500");
vars SPXDays = series(priceC(0));	// create a series of daily S&P 500 close prices
TimeFrame = 1;
...
// plot a daily spread histogram
void run()
{
  BarPeriod = 10;
  PlotLabels = 4;
  plotBar("Spread",
    (60*hour(0)+minute(0))/BarPeriod,tod(0),marketVal(0),AVG,RED);
}
// print IB trade volume at any bar
// consider different live and historical volumes

var volumeSum(var* Buffer,var Volume,int NewFrame)
{
// Buffer[0] = current volume, building up;
// Buffer[1] = previous volume; 
  if(NewFrame) { 
    Buffer[1] = Buffer[0];
    Buffer[0] = 0;
  }
  return Buffer[0] += BarPeriod*Volume; // sum of 1-min volumes
}

var volumeDiff(var* Buffer,var Volume,int NewFrame)
{
// Buffer[2] = previous accumulated volume
  if(Buffer[2] == 0 || Buffer[2] > Volume)
    Buffer[2] = Volume;
  Buffer[0] = Volume-Buffer[2]; // frame volume difference
  if(NewFrame) {
    Buffer[1] = Buffer[0];
    Buffer[2] = Volume;
  }
  return Buffer[0]; 
}

void run()
{
  BarPeriod = 1;
  LookBack = 0;
  
  assetList("AssetsIB");
  brokerCommand(SET_PRICETYPE,2); // trade prices
  brokerCommand(SET_VOLTYPE,4); // trade volume
  
  vars VBuffer = series(0,-3);
  if(Live) // daily accumulated volume
    volumeDiff(VBuffer,marketVol(0),1);
  else     // average 1-min volume
    volumeSum(VBuffer,marketVol(0),1);
  var Volume = VBuffer[1];
  printf("# Vol %.0f",Volume);
}	

See also:

enterLong/Short, series, asset, Spread, PIP, Detrend, dayHigh/Low, History

► latest version online