Use Custom Indicators
There are multiple ways to incorporate custom trading studies / indicators into your trading strategies. If you have proprietary indicators that you would like to backtest, you can import them into your account for analysis.
Option 1: Import Custom Indicators
The easiest way to use custom indicators is to import them as a CSV file into your account.
To import custom indicators into your account, please see the following tutorial on importing custom quotes / indicators. Once your indicator is imported, you can use it to make trading decisions and adjustments.
https://www.optionstack.com/kb/import-custom-quotes-indicators/
Option 2: Using Scala
If you prefer to write Scala code instead, you can also incorporate your own custom trading studies / indicators by writing your strategy in Scala using the Advanced Scala Editor.
Here are the instructions on how to incorporate your own custom studies in your strategies: (Custom Trading Indicators Example)
- Create a New Strategy using the Advanced Scala Editor. Or construct your strategy using the Visual Strategy Editor, and copy the auto-generated Scala code into a New Strategy file.
- Export your studies / indicators into a list of entry and exit dates
- Assign the list of entry / exit dates as Scala variables to be referenced in your strategy. For example, you can create an Array of Date for all the dates when your trading signals are triggered. (Note: If you are using EOD data, dates should have an EOD timestamp of “16:00 EST”)
/* Add trading signal dates using an Array of Date
* Dates should have an EOD timestamp of "16:00 EST".*/
var studies1_entryDates = Array (
new java.util.Date ("1/6/2020 16:00 EST"),
new java.util.Date ("1/13/2020 16:00 EST"),
new java.util.Date ("1/17/2020 16:00 EST")
);
var studies1_exitDates = Array (
new java.util.Date ("1/8/2020 16:00 EST"),
new java.util.Date ("1/16/2020 16:00 EST"),
new java.util.Date ("1/24/2020 16:00 EST")
);
- At every tick of data, compare the current timestamp with the custom dates specified from the step above. If the current timestamp matches any of your custom dates, execute the desired trade. You can use the this.getContext.getCurrentTimestep() to get the current timestamp inside the handleData() method.
def handleData () = {
// get the current time
var curTime= this.getContext.getCurrentTimestep();
// check if the current time matches the dates in studies1_entryDates
if (studies1_entryDates.contains(curTime) ) {
// if the current time matches entry dates, sell a strangle
var strategy1 = SPY.strategies.straddleStrangles.Strangle
strategy1.sell("sellstraddleStrangles.Strangle1", "Sell Strangle on entry signal").sizeBy.quantityFunction(() => {(1)})
}
}
- At every tick of data, compare the current timestamp with your custom exit dates. If the current timestamp matches any of your custom exit dates, close the trade.
this.selectPosition(orderID="sellstraddleStrangles.Strangle1").foreach ({ curItem =>
var curItemAsStrategy = curItem.asStrategy.strangle
// check if the current time matches the EXIT dates in studies1_exitDates
if (studies1_exitDates.contains(curTime) ) {
// if the current time matches EXIT dates, close the trade
curItemAsStrategy.close("Closing based on exit signal", fillInstantly=true);
}
}