How do you write your own indicators?

46 views
Skip to first unread message

Riccardo Alberti

unread,
Sep 23, 2014, 5:55:59 AM9/23/14
to tbg-quant...@googlegroups.com
Hello all,

i've just downloaded and installed the framework (1.5 beta)... I've tested it and it works.

I was playing around with strategies and i saw that there is only one indicator available the SMA.
I also see that you're importing the TA-LIB library but i saw that you're not using the built-in ta-lib SMA indicator. So my question is:

How do you write custom indicators? and where do you use the ta-lib functions?

Thank you very much
Riccardo

Sfl

unread,
Sep 23, 2014, 9:20:03 AM9/23/14
to tbg-quant...@googlegroups.com
Hi Riccardo,

SMA is the only indicator available so far but you can create your owns pretty easily by implementing IndicatorBase:

public class ExampleIndicator implements IndicatorBase{

@Override
public void add(String symbol, double value) {
// TODO Auto-generated method stub
}

@Override
public double getValue(String symbol) {
// TODO Auto-generated method stub
return 0;
}
}

I am attaching here the full SMA sourcecode:

/**
 * SMA Simple Moving Average <br>
 * <br>
 * <b>History:</b><br>
 *  - [18/dic/2012] Created. (sfl)<br>
 *
 *  @author sfl
 */
public class SMA implements IndicatorBase{

private int periods = 0;
@SuppressWarnings("rawtypes")
private HashMap<String,RollingWindow> SMA = new HashMap<String, RollingWindow>();

/**
* Constructor
* @param periods
*/
public SMA(int periods){
this.periods = periods;
}
@SuppressWarnings("unchecked")
@Override
public double getValue(String symbol) {
RollingWindow<Double> val = new RollingWindow<Double>(periods);
if (SMA.get(symbol)!=null)
val = SMA.get(symbol);
else
return 0;
double sum = 0;
int size = val.size();
for(int i=0;i<size;i++){
sum += val.get(i);
}
return (sum/size);
}

@SuppressWarnings("unchecked")
@Override
public void add(String symbol,double value) {
if ((value>0)&&(symbol!=null)){
RollingWindow<Double> val = new RollingWindow<Double>(periods);
if (SMA.get(symbol)!=null)
val = SMA.get(symbol);
val.add(value);
SMA.put(symbol, val);
}
}


// test 
public static void main(String[] args) {
SMA sma = new SMA(20);
String s = "GS";
for(int i=0;i<1000;i++){
sma.add(s, i);
if (i==100)
System.out.println("Value at 100: "+sma.getValue(s));
if (i==200)
System.out.println("Value at 200: "+sma.getValue(s));
if (i==400)
System.out.println("Value at 400: "+sma.getValue(s));
if (i==650)
System.out.println("Value at 650: "+sma.getValue(s));
if (i==999)
System.out.println("Value at 999: "+sma.getValue(s));
}
    }
}



If you are going to implement others indicators please consider to submit them to us. We can include them in an external jar for istance: tbg-quant-indicators.jar or you can publish your work and we can link it.

About using TA-LIB well, ta indicators works with array[] so you need to do some work to get them work. You can write a wrap class around each indicator implementing IndicatorBase like above or you can just use them directly in a strategy. I personally prefer the firsty approach.

Thank you
Alberto

Riccardo Alberti

unread,
Sep 24, 2014, 10:39:36 PM9/24/14
to tbg-quant...@googlegroups.com
Hi Alberto,

i see... I implemented almost all indicators in ta-lib as extension of an abstract class that in turn implements the IndicatorBase class. Basically my abstract class contains all the mappings between IndicatorBase and ta-lib... 

I will test all of them during this week and i-ll let you know how it goes.

Thank you
Riccardo

Alberto Sfolcini

unread,
Sep 25, 2014, 3:05:27 AM9/25/14
to tbg-Quant-community
That's great!!
Keep me up to date :-)
bye

--
www.thebonnotgang.com
---
You received this message because you are subscribed to the Google Groups "tbg-Quant-community" group.
To post to this group, send email to tbg-quant...@googlegroups.com.



--
Alberto Sfolcini

Riccardo Alberti

unread,
Sep 28, 2014, 9:38:29 AM9/28/14
to tbg-quant...@googlegroups.com
I've tested some indicators and they seem to provide correct results :-)
I was working on three things now... i thought it might be worth to share in case someone was working on the same things and if someone has advices or tips to give it would be great!.

1) an eclipse plugin that creates the project scaffolding for strategies implementation;
2) a graphic (flow chart) designer to implement strategies graphically;
3) an "indicators mixer" that automatically generates groups of indicators and tests them against historical data (it's gonna be like an automated strategy generator)

I'll keep you updated.

Sfl

unread,
Oct 3, 2014, 3:51:24 PM10/3/14
to tbg-quant...@googlegroups.com
Hi Riccardo,

This is sounds awesome! 
Do you mind to share your TALib wrapper with some indicators examples ??

Do you have a github repo?

thank you
bye

Riccardo Alberti

unread,
Oct 5, 2014, 4:02:03 PM10/5/14
to tbg-quant...@googlegroups.com
Hi,

yes sure i will... I just want to finish an example of simulator so that i will upload everything together.

I've got a question on market data feed. Is it possible to get CandleEvent and TickEvent at the same time? it will be useful to mix the two in the simulator i'm developing.

Alberto Sfolcini

unread,
Oct 7, 2014, 8:32:28 AM10/7/14
to tbg-Quant-community
Hi Riccardo,

YES! Please check out this source: 

The base point is to get the TickEvent. 
Than you can use the utility TickToCandleEventService in order to get candle events at the timeframe you prefer.


Check the sourcecode above, it's very simple and you can get all the timeframes you need, ticks, candles 1,5 10 minutes etc....
This is usefull when you have to look at different timeframe in order to get a trade decision.

Let me know if you need futher details.
thank you
Alberto

Riccardo Alberti

unread,
Oct 9, 2014, 4:43:49 AM10/9/14
to tbg-quant...@googlegroups.com
Hi Alberto,

i managed to use the TickToCandleEventService... But now i have another question.

I've tried to change the MarketDataFeed from Bogus to Yahoo or Google because in the simulator it would be best to define a time window in which to execute the strategy. But if i do that then i have problems with using the TickToCandle. Apparently the moment i define a time window i only receive candle events from the market... and that will create a classcastexception in the onTick when i cast the event.

What you think?

I might try to use the csv file based marketdatafeed for backtesting... that way the time window is implicit in the length of the csv file.

P.S. i'm also having issues with the GoogleMarketDataFeed. It hangs after activating the MarketDataFeed... it dowsn't seem to process the data.

P.P.S i'm almost done with the simulator. i've finished the elaboration part... So if you provide the system with a string defining a combination of indicators and conditions it will play the strategy correctly. Now i need to complete the StrategyStringsGenerator that randomly creates the strategies. And the evaluator... the component that checks the performance of the strategy. By the end of this week i'll upload the indicators and the simulator on my gitHub... 

Alberto Sfolcini

unread,
Oct 9, 2014, 5:17:01 AM10/9/14
to tbg-Quant-community
Hi Riccardo,

GoogleMarketDataFeed is not complete, do not use it.

BogusMarketDataFeed provides both candle and tick events.

YahooMarketDataFeed supports only Candles object because fetches CSV in candles format, thus you cannot get TickEvents.
Yahoo does not provide ticks. From what I can remember I wrote a converter from candle to tick, but be carefull because the ticks are generated in a random way "within the candle", this means that if you take trade decision on ticks it may be wrong.

If you decide to work with ticks then you might need a tick data provider or a CSV with tick events and a CustomMarketDataFeed to read it and push into the strategy.  
Cant you use candles instead of ticks?

Riccardo Alberti

unread,
Oct 9, 2014, 5:21:41 AM10/9/14
to tbg-quant...@googlegroups.com
Ok thanks.

i have implemented both scenarios... a strategy that uses candle related indicators and tick related indicators. I tried to keep the strategy as general as possible so that the mixing would have been greater.

I was thinking of using a csv market data provider so that i can manage the time window as i want... I'll let you know about that.

Riccardo Alberti

unread,
Oct 16, 2014, 2:44:53 PM10/16/14
to tbg-quant...@googlegroups.com
Hi Alberto,

i've uploaded the indicators in my github https://github.com/skene2321/tbg-quant-extensions...

I'm working on some issues now.

First i'm implmenting a marketdatafeed for FXconnect. I'm able to publish tick event on the bus but i don't understand why the trading class is not subscribing to the events published.

After that i'll finish the simulator.

I'll keep you updated!
Regards
Reply all
Reply to author
Forward
0 new messages