Thursday, May 31, 2012

Composite Profile

Composite profiles now added. Also added a 'decay' feature so that older values in the profile slowly decay over time, leaving the profile more representative of the more recent price action.

I am liking how HVN and LVN are showing the support and resistance levels.

It is proving to be quite useful to give some structure to the chart.

Monday, May 28, 2012

Indicator update

The bugs in the Currency Strength Meter have now been fixed (hopefully). Although I still think I can optimize it.

Latest update to Market Profile indicator include the addition of the Value Area (using traditional calculation) and the Initial Balance.

Sunday, May 27, 2012

Currency Strength Meter

I found a couple of bugs in my Currency Strength Indicator. The first one was a coding error, and I was setting the "to" date to the most recent date. However, for "alive feeds" this should be set to 0.

The other issue is more serious. I had hoped that if I subscribed to a data stream which had the same start and end dates as the primary data source, then it would also have the same number of periods in and so I could essentially do source.close[period], instruments["EUR/USD"].stream.close[period], instruments["GBP/USD"].stream.close[period], etc. with the knowledge that the streams were all accessing data from the same time period. However, when debugging an error I found that I had several cases where the streams had the same start and end dates, but the number of bars/periods was actually different. I don't know actually how this can come about, but it did. Furthermore, when re-scaling the chart, the primary source can lose periods - something which does not happen with manually managed streams.

In summary, if we have several streams (primary and others) we can not guarantee that the value indexed at the "period" position refers to the same candle/bar. The only way around this is to search the streams by date. This is computationally expensive, although there are some optimizations that we can do.

Not solve it yet, but I have some ideas.

Thursday, May 24, 2012

Market Profile Update

My Market Profile indicator is going very well. I now added a composite chart to the right of the screen. This is a great tool for finding support and resistance levels. Also implemented the intra-day and weekly profile options.

Still to do:
  • Value Area
  • Initial Balance
  • Better handling of intra-day, possibly with day-time / night-time sessions.

Tuesday, May 22, 2012

Market Profile indicator

I made some very good progress porting my Market Profile indicator to MarketScope.

There already is a version of MarketProfile for MarketScope, but it still seems to be missing a few features. The one I had working on Stragey Trader was already more flexible and useful than that. So, I'm going to port it over. Actually, this should be quite an easy port. The main tricky bit is the graphics handling which are obviously quite different.

Here's the indicator in action showing TPOs (Time Price Opportunities)...

And here it is showing lines rather than the letters, also using the "rainbow effect" which colourizes the lines according to their relative size ratio...

It's basically working. I created a Lua 'class' to handle the Market Profile data structure.

Still to do:
  • Session start / end times
  • Profiles for intraday, daily and weekly
  • Only display last N profiles
  • Calculation and drawing of value area and initial balance
  • Profiles for arbitrary number of periods (e.g. last N bars)
    • Will anchor this to left or right of screen
    • Might incorporate "leaky bucket" type of idea

One interesting thing that I can do in MarketScope is to assign custom commands for the indicator. This allows me to right-click on the chart and select the command to perform some action. This could be very useful to allow me to make arbitrary splits or compounds of profiles by the click of a mouse. This could be very cool.

Friday, May 18, 2012

Currency Strength Meter

Well, good success last night! I ported my Currency Strength Indicator to MarketScope. Still needs a little refining, but essentially it is working. My old website has an article about it. It was an interesting one to code in Lua. As promised I wrote a 'class' called ManagedStream that handles the complexities of the "getHistory" and "extendHistory" calls for the 21 currency pairs used in this indicator.

I need to decide how to handle the 'reference date', currently it is hard-coded. But this is relatively minor point. The ManagedStream class seems to work ok - at least it does when all streams are the same timeframe. I suspect it will need some changes to handle different timeframes.

A nice thing about MarketScope is that the output streams from any indicator can become inputs to other indicators. For example, I can add a MACD indicator which uses the JPY currency strength as its input!

Wednesday, May 16, 2012

Loading an external Lua module

Managed to load a module ok.

Basically, put some generic Lua code in a separate file, e.g. module1.lua. Then from your indicator code, in the 'Prepare()' function write...

requires 'module1' -- NOTE: no need to write it as module1.lua

When debugging, I got an error because it could not find module1 in any of the following paths...

[string "C:\Program Files\Gehtsoft\IndicoreSDK\indic..."]:19: module 'module1' not found:
no field package.preload['module1']
no file '.\module1.lua'
no file 'C:\Program Files\Gehtsoft\IndicoreSDK\lua\module1.lua'
no file 'C:\Program Files\Gehtsoft\IndicoreSDK\lua\module1\init.lua'
no file 'C:\Program Files\Gehtsoft\IndicoreSDK\module1.lua'
no file 'C:\Program Files\Gehtsoft\IndicoreSDK\module1\init.lua'
no file 'C:\Program Files\Lua\5.1\lua\module1.luac'
no file '.\module1.dll'
no file 'C:\Program Files\Gehtsoft\IndicoreSDK\module1.dll'
no file 'C:\Program Files\Gehtsoft\IndicoreSDK\loadall.dll'

This was expected. Now, I know where it was looking. So, just put the file in one of those locations, e.g. C:\Program Files\Gehtsoft\IndicoreSDK\module1.lua, and run the Indicator in the debugger. Magically, I can access the functions in the module code.

There's actually several ways to define the module, and here's a detailed discussion on the topic.

Ok, that wasn't so hard. Now I know I can develop common code, utilities and classes in a separate module, and use it (via 'requires' function) wherever I need it.

My short term goals for Lua and MarketScope

There is some important things that I could do in Strategy Trader, and I want to be able to do in MarketScope:
1) encapsulate data/methods in object orientated style
2) multi-timeframe or multi-instrument charts
3) draw lines, text, etc. to the chart
4) put common code in a separate file for re-use

Lua doesn't have a built-in notion of classes, but  they can be implemented fairly easily.

Vector = {}                   -- Create a table to hold the class methods
function Vector:new(x, y, z)  -- The constructor
  local object = { x = x, y = y, z = z }
  setmetatable(object, { __index = Vector })  -- Inheritance
  return object
function Vector:magnitude()   -- Another member function
  -- Reference the implicit object using self
  return math.sqrt(self.x^2 + self.y^2 + self.z^2)

vec = Vector:new(0, 1, 0)     -- Create a vector
print(vec:magnitude())        -- Call a member function using ":" (output: 1)

Basically, in Lua functions are first class objects, just like variables. It means you can assign them, copy them, etc. So you can assign functions (methods) to an object's table. The next thing is the meta-table and meta-methods. If we assign the meta-method __index with a value of another table, then and failed lookup will invoke the __index method which now points to another table, and so the lookup will be directed there. More or less. Anyway, basically it means that an object can inherit the methods of the class.

In the examples of Lua indicators I didn't see any of this going on. But I think it will be incredibly useful. One common pattern that I see, is when we use host:execute("getHistory", ...) and host:execute("extendtHistory", ...) methods. It's all done by hand each time. It would be much better to make a class called ManagedDataStream or something that encapsulates all of this, to avoid the need to copy/paste the same boiler plate code each time.

Bottom line: We can do classes. Perhaps not as elegantly as C# or C++, but enough to enable us to encapsulate data and methods.

There are examples of both multi-timeframe and multi-chart. One difficulty is managing these explicit data streams, as the data needs to be loaded and extended by hand. But by making a ManagedDataStream class a lot of the leg work can be removed. The other thing is to synchronize the streams and calculations, i.e. we need some way to ensure that we only start 'doing something' when the all streams are on the same bar.

This is still a work in progress, but at least there are examples of this, so I believe it can be done.

Text can be drawn in a few ways. One way is to create an output text stream using instance:createTextOutput. This stream is synchronised to the source stream like a normal output stream, and allows text labels to be printed each bar for example.

In addition it is possible to output arbitrary text labels using host:execute("drawLabel", ...) or host:execute("drawLabel1", ...). The latter also allowing positioning of the text which is anchored to the chart window, e.g. top/left, bottom/right, centre, etc. This means that something like my Dashboard class can be implemented.

Lines are also be drawn using host:execute("drawLine", ...). If the 'from' field is specified as -1 the lines are anchored to the left of the chart, and if it is -2 the lines are anchored to the right of the chart; and in both cases the 'to' field now means the number of points from these anchors. This is perfect for drawing things like volume profiles, support & resistance lines, pivots, etc.

Both text and lines are given unique 'ids' which can be used to delete them. It's also possible to delete all text and lines. So, it looks like we can use graphics in similar way to Strategy Trader. I didn't see a way to 'extend' lines

You can put code into a separate module in Lua, and so I guess so in MarketScope too. But I'm not sure where the module file goes, and whether you need to 'Import' it when you load the indicator into MarketScope. But I believe it is possible.

Summary: It looks like I can do many of the things that I am familiar with. The biggest difference is how to manage multiple instruments, etc. it seems a lot more manual than Strategy Trader. But hopefully, I can write some helper classes so I only have to do the hard work once.

Monday, May 14, 2012

Intrigued by Lua

I am very intrigued by the Lua language. Requires a bit of a mindset change, but I think it will do the job, and it could be useful for other 'pet projects' that I have in mind for the future.

Anyway, here are some more links: (this is an online Lua interpreter / demo, useful for quickly trying code snippets)

Friday, May 11, 2012

Changing platform

For some time now I have been developing computer programs and algorithms to analyse financial instruments (generally forex, stock indices and commodities) for the purpose of trading. Check out my own webpage on the topic.

This work has primarily been using an application called Strategy Trader, from FXCM. The application uses C# programming language, and despite the severe lack of good documentation or examples, it was a very good platform. Unfortunately, support for this platform has been withdrawn, and therefore my development using it is now at an end.

So, where next? The very popular MetaTrader platform perhaps (MT4 or MT5)? Or perhaps NinjaTrader? Or even something else?

Well, for now I decided to stay with FXCM, and change to the MarketScope platform.

I'm blogging my experiences here for posterity... and maybe it will be useful.