Welcome to the official documentation for HawkAlpha, the comprehensive guide to mastering our innovative tool, TraderJS. This robust inline JavaScript editor is designed specifically for the realm of technical indicators, providing a streamlined interface for both importing essential libraries and crafting your own indicators.
In this documentation, you'll find a plethora of information covering:
To kickstart your journey, let’s delve into a practical example showcasing the power and flexibility of TraderJS. The code snippet below illustrates how you can initialize a multitude of indicators, define certain conditions based on these indicators, and control a trading bot accordingly. This example revolves around monitoring Apple Inc. stock (ticker symbol: AAPL) on a 5-minute timeframe.
// This example demonstrates how to set up a trading bot using TraderJS in HawkAlpha
// It uses various technical indicators to make trading decisions on AAPL stock with a 5-minute timeframe
// Define the ticker symbol and timeframe
BAR('tickerOnClose_aapl_5Min', {symbol: 'AAPL', timeframe: '5Min'});
// Initialize technical indicators
// Each indicator is given a unique name and configured with specific parameters
// Average True Range (ATR) - Measures market volatility
ATR('atr_sp21_stEMA_AAPL_tf5Min', {smoothingPeriod: '21', smoothingType: 'EMA', symbol: 'AAPL', timeframe: '5Min'});
// ATR Percentage (ATRP) - ATR relative to current price
ATRP('atrp_sp21_stEMA_AAPL_tf5Min', {smoothingPeriod: '21', smoothingType: 'EMA', symbol: 'AAPL', timeframe: '5Min'});
// Directional Movement Index (DMI) - Identifies the trend direction
DMI('dmi_diLength9_ema_sp7_stRMA_AAPL_tf5Min', {DILength: 9, DIMALength: 'EMA', smoothingPeriod: '7', smoothingType: 'RMA', sourceType: 'open', symbol: 'AAPL', timeframe: '5Min'});
// Exponential Moving Average (EMA) - Gives more weight to recent prices
EMA('ema_sp9_stEMA_open_AAPL_tf5Min', {smoothingPeriod: '9', smoothingType: 'EMA', sourceType: 'open', symbol: 'AAPL', timeframe: '5Min'});
// Moving Average Convergence Divergence (MACD) - Shows the relationship between two moving averages of a price
MACD('macd_fl9_sl21_signall5_oRMA_sRMA_stClose_AAPL_tf5Min', {fastLength: 9, slowLength: 21, signalLength: 5, oscillatorMAType: 'RMA', signalMAType: 'RMA', symbol: 'AAPL', timeframe: '5Min'});
// On-Balance Volume (OBV) - Measures buying and selling pressure
OBV('obv_sp7_stEMA_AAPL_tf5Min', {smoothingPeriod: '7', smoothingType: 'EMA', symbol: 'AAPL', timeframe: '5Min'});
// Running Moving Average (RMA) - A type of moving average that reduces lag
RMA('rma_sp9_stEMA_open_AAPL_tf5Min', {smoothingPeriod: '9', smoothingType: 'EMA', sourceType: 'open', symbol: 'AAPL', timeframe: '5Min'});
// Relative Strength Index (RSI) - Measures the speed and change of price movements
RSI('rsi_lgp3_lgt3_sp3_stEMA_sttOBV_AAPL_tf5Min', {loseGainPeriod: 3, loseGainType: 'EMA', smoothingPeriod: '3', smoothingType: 'EMA', sourceType: 'OBV', symbol: 'AAPL', timeframe: '5Min'});
// Simple Moving Average (SMA) - Calculates the average price over a specific period
SMA('sma_sp9_stEMA_open_AAPL_tf5Min', {smoothingPeriod: '9', smoothingType: 'EMA', sourceType: 'open', symbol: 'AAPL', timeframe: '5Min'});
// Initialize an object to store debug information
debug = {}
// The main trading logic function, called on each tick (price update)
tick = (i, bot, utils) => {
// Retrieve the current values of all indicators
debug.tickerOnClose = i.tickerOnClose_aapl_5Min();
debug.atr = i.atr_sp21_stEMA_AAPL_tf5Min();
debug.atrp = i.atrp_sp21_stEMA_AAPL_tf5Min();
debug.dmi = i.dmi_diLength9_ema_sp7_stRMA_AAPL_tf5Min();
debug.ema = i.ema_sp9_stEMA_open_AAPL_tf5Min();
debug.macd = i.macd_fl9_sl21_signall5_oRMA_sRMA_stClose_AAPL_tf5Min();
debug.obv = i.obv_sp7_stEMA_AAPL_tf5Min();
debug.rma = i.rma_sp9_stEMA_open_AAPL_tf5Min();
debug.rsi = i.rsi_lgp3_lgt3_sp3_stEMA_sttOBV_AAPL_tf5Min();
debug.sma = i.sma_sp9_stEMA_open_AAPL_tf5Min();
// Set trading conditions based on indicator values
// Each condition checks if a specific technical criterion is met
// Check if the current price is above the Simple Moving Average
debug.smaCondition = debug.tickerOnClose.gt(debug.sma);
// Check if the current price is above the Exponential Moving Average
debug.emaCondition = debug.tickerOnClose.gt(debug.ema);
// Check if the current price is above the Running Moving Average
debug.rmaCondition = debug.tickerOnClose.gt(debug.rma);
// Check if the Average True Range is greater than 10
debug.atrCondition = debug.atr.gt(10);
// Check if the ATR Percentage is greater than 1%
debug.atrpCondition = debug.atrp.gt(1);
// Check if the Relative Strength Index is greater than 55
debug.rsiCondition = debug.rsi.gt(55);
// Check if the On-Balance Volume is greater than 55
debug.obvCondition = debug.obv.gt(55);
// Check if the Average Directional Index (part of DMI) is greater than 20
debug.dmiCondition_ADX = debug.dmi.adx.gt(20);
// Check if the Positive Directional Indicator is greater than the Negative Directional Indicator
debug.dmiCondition_DMI = debug.dmi.pdi.gt(debug.dmi.mdi);
// Check if the MACD histogram is positive (MACD line above Signal line)
debug.macdCondition_hist = debug.macd.hist.gt(0);
// Configure the bot's trading parameters
// Set the price deviation for order placement, based on ATR%
// The throttle and step options help to smooth out rapid changes
bot.setPriceDeviation(debug.atrp.mul(1), { throttle: 2000, step: 1 });
// Set the maximum allowed price deviation
bot.setMaxPriceDeviation(debug.atrp.mul(4), { throttle: 2000, step: 1 });
// Set the profit-taking strategy
// This uses an ORO (One-Replaces-Other) order type
bot.setProfit({
low: {
type: 'ORO',
position: 100, // Percentage of position to close
profitLimitPriceMin: 0.2, // Minimum profit in currency units
profitLimitPrice: debug.atrp.mul(0.5), // Dynamic profit target based on ATR%
lossStopPriceMin: 0.1, // Minimum stop loss in currency units
lossStopPrice: debug.atrp.mul(0.25) // Dynamic stop loss based on ATR%
}
});
// Set the loss-cutting strategy
// This uses an ORO (One-Replaces-Other) order type
bot.setLoss({
low: {
type: 'ORO',
position: 100, // Percentage of position to close
profitLimitPrice: 100, // Fixed profit target (likely not reached in this context)
lossStopPrice: debug.atrp.mul(4) // Dynamic stop loss based on ATR%
}
});
// Determine whether to start the bot based on all conditions
if (debug.smaCondition && debug.emaCondition && debug.rmaCondition &&
debug.atrCondition && debug.atrpCondition && debug.rsiCondition &&
debug.obvCondition && debug.dmiCondition_ADX && debug.dmiCondition_DMI &&
debug.macdCondition_hist && (utils.marketIsOpen())) {
bot.start(); // Start the bot if all conditions are met and the market is open
}
// Reset the bot's state if no position is opened within 1 minute
if (bot.noPositionMinutes(1)) {
bot.resetDCA(); // Reset the Dollar Cost Averaging settings
}
// Reset the bot's state when a trade is completed (either profit or stop-loss hit)
if (bot.state.isFinished) {
bot.reset();
}
}
In this example, you’ll observe the simplicity and clarity with which TraderJS allows you to orchestrate complex trading logic. The tick function encapsulates the essence of combining indicators, creating conditions, and controlling the bot based on the evolving market conditions.
As you navigate through the rest of the documentation, you'll gain a deeper understanding of TraderJS, allowing you to unlock its full potential in designing, testing, and deploying your trading strategies. Whether you are a seasoned trader or a coding enthusiast, TraderJS empowers you to elevate your trading game to the next level.
This introductory page sets the foundation for your exploration into TraderJS. As you proceed through the subsequent sections, you’ll uncover the nuances and sophisticated features that make TraderJS a compelling tool for every trader striving for precision and efficiency.