Page 1 of 1

MD code not working

Posted: 05 Oct 2006
by server
Hi everyone,
I found this indicator for TS and I tried to import it, but it doesn't work. I don't know how to use PowerLanguage, so I'm asking for help. Anyone wanna take a shot at fixing it?
Thanks in advance!

code starts here
-----------------------------

inputs:
Threshold1( 150 ),
Threshold2( 300 ),
Threshold3( 600 ),
DisplayDelta( false ),
Type( 1 ), // 1 = Use Upticks/Downticks, 2 = Use Bid/Ask
BidAskArraySz( 1000 );

variables:
TickSize( MinMove / PriceScale ),
PriceAtIndex( Open ),
BaseIndex( BidAskArraySz/2 ),
Red1(RGB(255,185,185)),
Red2(RGB(255,128,128)),
Red3(RGB(255,0,0)),
Red4(RGB(128,0,0)),
Green1(RGB(210,255,210)),
Green2(RGB(125,255,125)),
Green3(RGB(0,255,0)),
Green4(RGB(0,128,0)),
i( 0 );

variables:
intrabarpersist Price( 0 ),
intrabarpersist MyVol( 0 ),
intrabarpersist VolTmp( 0 ),
intrabarpersist LastTick( 0 ),
intrabarpersist MyCurrentBar( 0 ),
intrabarpersist Index( 0 ),
intrabarpersist BidAskDelta( 0 ),
intrabarpersist xDelta( 0 ),
intrabarpersist TextString(""),
intrabarpersist MyUpTicks( 0 ),
intrabarpersist MyDownTicks( 0 );

Arrays:
intrabarpersist Bid[](0),
intrabarpersist Ask[](0);

Array_SetMaxIndex( Bid, BidAskArraySz );
Array_SetMaxIndex( Ask, BidAskArraySz );

if (Type = 1 or Type = 2) and LastBarOnChart and BarType < 2 then
begin
MyVol = Ticks;
PriceAtIndex = Open;

if CurrentBar > MyCurrentBar then
begin
VolTmp = 0;
MyCurrentBar = CurrentBar;
MyUpTicks = 0;
MyDownTicks = 0;

for i = 0 to BidAskArraySz - 1
begin
Bid = 0;
Ask = 0;
end;
end;

Index = BaseIndex + (Close - PriceAtIndex) / TickSize;

if InsideBid < InsideAsk then
begin
if Type = 1 then
begin
// Use Upticks/Downticks
if DownTicks <> MyDownTicks then
Bid[Index] = Bid[Index] + MyVol - VolTmp
else if UpTicks <> MyUpTicks then
Ask[Index] = Ask[Index] + MyVol - VolTmp
else
begin
if Close <= LastTick then
Bid[Index] = Bid[Index] + MyVol - VolTmp
else
Ask[Index] = Ask[Index] + MyVol - VolTmp;
end;
end
else
begin
// Use Bid/Ask
// Warning: TS provides snapshot of bid/ask
if Close <= InsideBid then
Bid[Index] = Bid[Index] + MyVol - VolTmp
else if Close >= InsideAsk then
Ask[Index] = Ask[Index] + MyVol - VolTmp
else
begin
// Last tick was between bid/ask
if Close <= LastTick then
Bid[Index] = Bid[Index] + MyVol - VolTmp
else
Ask[Index] = Ask[Index] + MyVol - VolTmp;
end;
end;
end;

MyUpTicks = UpTicks;
MyDownTicks = DownTicks;
VolTmp = MyVol;
LastTick = Close;

xDelta = 0;
Price = Low;

while Price <= High
begin
Index = BaseIndex + (Price - PriceAtIndex) / TickSize;
TextString = NumToStr(Bid[Index],0) + " x " + NumToStr(Ask[Index],0);
Value99 = Text_New(Date, Time, 0, " ");
Text_SetString(Value99, TextString);
Text_SetLocation(Value99, Date, Time, Price);
Text_SetStyle(Value99, 1, 1);

BidAskDelta = Ask[Index] - Bid[Index];
if BidAskDelta > Threshold3 then
Text_SetColor(Value99, Green4)
else if BidAskDelta > Threshold2 then
Text_SetColor(Value99, Green3)
else if BidAskDelta > Threshold1 then
Text_SetColor(Value99, Green2)
else if BidAskDelta >= 0 then
Text_SetColor(Value99, Green1)
else if BidAskDelta < -Threshold3 then
Text_SetColor(Value99, Red4)
else if BidAskDelta < -Threshold2 then
Text_SetColor(Value99, Red3)
else if BidAskDelta < -Threshold1 then
Text_SetColor(Value99, Red2)
else
Text_SetColor(Value99, Red1);

xDelta = xDelta + BidAskDelta;

Price = Price + TickSize;
end;

if DisplayDelta = true then
begin
Value99 = Text_New(Date, Time, 0, " ");
Text_SetString(Value99, NumToStr(xDelta, 0 ));
Text_SetLocation(Value99, Date, Time, Low - TickSize);
Text_SetStyle(Value99, 1, 1);

if xDelta >= 0 then
Text_SetColor(Value99, Green)
else
Text_SetColor(Value99, Red);
end;
end;

-----------------------
code ends here

Sorry, correcting

Posted: 05 Oct 2006
by Alex Kramer
We fixed the code, now it complies without errors:

inputs:
Threshold1( 150 ),
Threshold2( 300 ),
Threshold3( 600 ),
DisplayDelta( false ),
Type( 1 ), // 1 = Use Upticks/Downticks, 2 = Use Bid/Ask
BidAskArraySz( 1000 );

variables:
TickSize( MinMove / PriceScale ),
PriceAtIndex( Open ),
BaseIndex( BidAskArraySz/2 ),
Red1(RGB(255,185,185)),
Red2(RGB(255,128,128)),
Red3(RGB(255,0,0)),
Red4(RGB(128,0,0)),
Green1(RGB(210,255,210)),
Green2(RGB(125,255,125)),
Green3(RGB(0,255,0)),
Green4(RGB(0,128,0)),
ii( 0 );

variables:
intrabarpersist Price( 0 ),
intrabarpersist MyVol( 0 ),
intrabarpersist VolTmp( 0 ),
intrabarpersist LastTick( 0 ),
intrabarpersist MyCurrentBar( 0 ),
intrabarpersist Index( 0 ),
intrabarpersist BidAskDelta( 0 ),
intrabarpersist xDelta( 0 ),
intrabarpersist TextString(""),
intrabarpersist MyUpTicks( 0 ),
intrabarpersist MyDownTicks( 0 );

Arrays:
intrabarpersist Bid[](0),
intrabarpersist Ask[](0);

Array_SetMaxIndex( Bid, BidAskArraySz );
Array_SetMaxIndex( Ask, BidAskArraySz );

if (Type = 1 or Type = 2) and LastBarOnChart and BarType < 2 then
begin
MyVol = Ticks;
PriceAtIndex = Open;

if CurrentBar > MyCurrentBar then
begin
VolTmp = 0;
MyCurrentBar = CurrentBar;
MyUpTicks = 0;
MyDownTicks = 0;

for ii = 0 to BidAskArraySz - 1
begin
Bid[ii] = 0;
Ask[ii] = 0;
end;
end;

Index = BaseIndex + (Close - PriceAtIndex) / TickSize;

if InsideBid < InsideAsk then
begin
if Type = 1 then
begin
// Use Upticks/Downticks
if DownTicks <> MyDownTicks then
Bid[Index] = Bid[Index] + MyVol - VolTmp
else if UpTicks <> MyUpTicks then
Ask[Index] = Ask[Index] + MyVol - VolTmp
else
begin
if Close <= LastTick then
Bid[Index] = Bid[Index] + MyVol - VolTmp
else
Ask[Index] = Ask[Index] + MyVol - VolTmp;
end;
end
else
begin
// Use Bid/Ask
// Warning: TS provides snapshot of bid/ask
if Close <= InsideBid then
Bid[Index] = Bid[Index] + MyVol - VolTmp
else if Close >= InsideAsk then
Ask[Index] = Ask[Index] + MyVol - VolTmp
else
begin
// Last tick was between bid/ask
if Close <= LastTick then
Bid[Index] = Bid[Index] + MyVol - VolTmp
else
Ask[Index] = Ask[Index] + MyVol - VolTmp;
end;
end;
end;

MyUpTicks = UpTicks;
MyDownTicks = DownTicks;
VolTmp = MyVol;
LastTick = Close;

xDelta = 0;
Price = Low;

while Price <= High
begin
Index = BaseIndex + (Price - PriceAtIndex) / TickSize;
TextString = NumToStr(Bid[Index],0) + " x " + NumToStr(Ask[Index],0);
Value99 = Text_New(Date, Time, 0, " ");
Text_SetString(Value99, TextString);
Text_SetLocation(Value99, Date, Time, Price);
Text_SetStyle(Value99, 1, 1);

BidAskDelta = Ask[Index] - Bid[Index];
if BidAskDelta > Threshold3 then
Text_SetColor(Value99, Green4)
else if BidAskDelta > Threshold2 then
Text_SetColor(Value99, Green3)
else if BidAskDelta > Threshold1 then
Text_SetColor(Value99, Green2)
else if BidAskDelta >= 0 then
Text_SetColor(Value99, Green1)
else if BidAskDelta < -Threshold3 then
Text_SetColor(Value99, Red4)
else if BidAskDelta < -Threshold2 then
Text_SetColor(Value99, Red3)
else if BidAskDelta < -Threshold1 then
Text_SetColor(Value99, Red2)
else
Text_SetColor(Value99, Red1);

xDelta = xDelta + BidAskDelta;

Price = Price + TickSize;
end;

if DisplayDelta = true then
begin
Value99 = Text_New(Date, Time, 0, " ");
Text_SetString(Value99, NumToStr(xDelta, 0 ));
Text_SetLocation(Value99, Date, Time, Low - TickSize);
Text_SetStyle(Value99, 1, 1);

if xDelta >= 0 then
Text_SetColor(Value99, Green)
else
Text_SetColor(Value99, Red);
end;
end;

Posted: 05 Oct 2006
by damageboy
Doesn't work with latest beta...
I can compile the code but I get an "out of bounds" error on inde -7000... :(

Posted: 06 Oct 2006
by server
works fine on my ver 1.90.468.865
thanks!

Posted: 06 Oct 2006
by Alex Kramer
The problem here was with using "i" as a variable name - this is a reserved word, an abbreviation of "open interest". Replacing it with a neutral expression like "iii" makes the code complie without errors.

Posted: 06 Oct 2006
by Chris
Doesn't work with latest beta...
I can compile the code but I get an "out of bounds" error on inde -7000... :(
Increasing the Input for BidAskArraySz, thus resulting in an larger array, should help you.

Or remove the ArraySetMaxIndex and set the array size in the code manually.

Posted: 06 Oct 2006
by guest2
I have ver 1.90.468.865 too but for me the code doesnt work. It shows lots of 0x0 lines one below the other without relation to price scale / mintick. Most lines are empty (just 0x0). (Please see attached file- hope it is attached)

I'm using MC with data from IB. Any input would be welcome. [/img]

Posted: 09 Oct 2006
by Nick
Hi Server,

Might I ask where you got the code...it goes a bit further than mine - colouring based on a threshold. I was interested if there was a discussion that went along with it?

I am not sure why the bid ask is held in array - dosent seem neccassary as things are. I guess that at some stage it was going to save the array so disc so when you load TS/MC you can read the last 1000 bid ask values. I am not sure.

It would be great if MC had an option of 'playing' every tickof every data series into a worksheet so indicators like this would load on startup. Something like a competitors TickPrecise technology. I guess that should go in the suggestions thread.

Cheers,
Nick.

Posted: 09 Oct 2006
by Alex Kramer
Nick -
Could you please elaborate on this, not sure we understand you right.

Posted: 09 Oct 2006
by Guest
Hi Nick,

the threshold code is from obx

https://www.TS.com/Discussio ... c_ID=55757

Posted: 10 Oct 2006
by Nick
Hi Alex,

Its kind of hard to explain. The code only displays the delta study from when multicharts starts, even if you have full bid ask last history in the database. To get it to calculate correctly for historical data you would need to 'send each tick into the chart' when the chart loads.

Let me try from a differetn angle. What the code does is take the volume of each tick and calculate if its on the bid or ask. It then either adds or subtracts it from the overall delta. You might see how this wont work currently for historical data?

Cheers,
Nick.

Posted: 10 Oct 2006
by Alex Kramer
I have a question, Nick - what is the principal difference between this and our Volume Profile?

Posted: 10 Oct 2006
by damageboy
Hi, I'm using the new latest beta of MC and now, after increasing the array size it does indeed "plot" on the screen.
However , I'm having the same problem Guest2 reported.
Lots of "0x0" plot one on top of the other...
I've tried the same code on TS and is works fine...

Posted: 10 Oct 2006
by SP
@damageboy

Did you make sure that you have the same settings for Price Scale and MinMovement in the Quotemanager as on TS?

Posted: 10 Oct 2006
by damageboy
It's the price scale alright, I just solved the problem myself when I noticed "SP"'s post :)

Posted: 10 Oct 2006
by damageboy
Quick question regarding price scale....
If I set it to 1/4 in the QM window, I get the price displayed with only one decimal point for the ES and rounded up: 1360.75 -> 1360.8 can someone explain to me how should the QM/Window for ES be configured to both have the correct Minimal Movement / Price Scale and Display Settings?

Posted: 11 Oct 2006
by Alex Kramer
Please refrain from using fractions in the Price Scale; this is a feature meant specifically for the few instruments traded in fractional, not decimal prices, and this has not yet been implemented completely.

So applying 1/4, 1/32 or other fractional scale to a usual ES symbol is likely to give you all kinds of worries and no reliable pricing.

Posted: 11 Oct 2006
by guest2
Please can somebody post the correct inputs for Min.Movement / Price Scale for ER2 / ES/ FDAX to use the MD Code? THANKs.

Posted: 11 Oct 2006
by SP
..............Pricescale.........MinM.......BPV
FDAX.......1/10................5...........25
FGBL........1/100..............1...........1000
ER2..........1/10...............1...........100
ES............1/100..............25..........50

Posted: 11 Oct 2006
by guest2
SP...many thanks for your superfast response...will see if it works.

Posted: 11 Oct 2006
by Alex Kramer
Dear SP - thank you very much for the input, you posted complete information before anyone blinked an eye!

Posted: 11 Oct 2006
by guest2
Well...the scaling works fine but the code doesnt work. If a new bar starts it counts correct until the price within that bar changes or the trade at the ask is followed from a "bid-trade". Then every number for this bar goes to 0. I assume it has to do with the unability to get the last size of the trade right: I tried this simple code that should provide the size/volume of the last trade but it doesnt:
*********
variables: intrabarpersist prev_volume(0);

value1=ticks;

plot1(prev_volume-value1, "TradeSize", blue);

prev_volume=value1;
*************
And I guess if this doesnt work the MD-Code cannot work correct.

As already said I am using the stable version of MC together with IB. Has anybody the same problem or is somebody any idea why the MD code does not work?

Posted: 11 Oct 2006
by guest2
To be more specific: If price changes the line at the previous price turns to 0x0 and the line with the last price is showing the complete volume for that bar, color coded and displayed on the bid or ask-side depending on wich side that last trade was.

So if the bar is finished I have some 0x0 lines, 1 line with the complete volume for that bar (i.e. 115x0) and a delta value showing the volume (i.e. 115).

Posted: 11 Oct 2006
by SP
@guest2

This is what i get for the FDAX with IB-Data.

I only get 0x0 lines in fast markets when IB sends only snapshot data, not the true sales.

Do you use 1 or 2 for the input "Type"?

Posted: 11 Oct 2006
by blauboot
changed from guest2 to my login-name so I hope I can attach files...

okay, so if it works for you it is not an issue with IB. But as described I get just 0x0 lines with sometimes 1 line showing the complete volume for that bar. I assume you are using the latest beta...could this be the difference?

SP, thanks again for your time and fast responses.

Posted: 11 Oct 2006
by blauboot
@SP: Overlooked your question: I tried both, 1 and 2 and there is no difference.

Posted: 11 Oct 2006
by Sigma Trader
That MD code is great for TS. It seems to do the trick. I do like the totals of net delta at the bottom of the price bars. Note the clear divergence at the lows in the accompanying chart. Sorry about posting this using TS, but haven't downloaded the recent beta's. Should be similar i would guess.

Posted: 12 Oct 2006
by SP
@blauboot

Yes, i use the latest beta.

I added a line TestVolume = Ask[Index] + Bid[Index]; to plot the total volume of all ...x...

I found out that with IB data the code will miss some ticks each bars.

Posted: 13 Oct 2006
by Nick
I have a question, Nick - what is the principal difference between this and our Volume Profile?
Volume Profile plots volume @ price as a histogram. Delta displays volume @ bid and volume @ ask at each price level. It can be displayed in several ways. It has cuased quite a buzz because it is a good method to guage order flow. Several charting packages have implemented it natively Ensign Software being the most recent. They have done a pretty good job it would seem.

If you plot any of these delta studies in MC they only start ploting from when you apply them. They do not plot historical data. Try one of the studies and you will see. Here is a screenshot of delta plotted over time in a couple of different ways. You will see it dosen't start plotting untill 7.38 when I opened MC.

Cheers,
Nick.

Posted: 16 Oct 2006
by blauboot
@SP: Installed new beta and finally it's working...so thanks again for your time and help, blauboot.

Posted: 19 Oct 2006
by fundjunkie
It's the price scale alright, I just solved the problem myself when I noticed "SP"'s post :)
I decided to take a look at this indicator but am getting this problem also. Is it caused by the Exchange settings or by the chart window itself?

Unfortunately, I don't have two different charting packages to compare price scales etc. I've tried some changes to get this to work but they haven't worked. can someone let me know so i can make the update and see how this thing works?


Thx
D

Posted: 19 Oct 2006
by damageboy
@fundjunkie: You need to setup the symbol in QuoteManager AND you have to do it BEFORE opening any charts for that symbol,
if you are using any of the symbol SP gave the config for you can refer to this copy-paste of his original text....:

..............Pricescale.........MinM.......BPV
FDAX.......1/10................5...........25
FGBL........1/100..............1...........1000
ER2..........1/10...............1...........100
ES............1/100..............25..........50

Once again, do this BEFORE opening a chart for that symbol.
If you use these setting (or proper settings for whatever symbol you are using) you need not touch the code...

Alternatively, you can also change the settings, shutdown MC and reload it, that will do the same thing (in case the workspaces load the charts before you can intervene)...

Posted: 19 Oct 2006
by SP
@damageboy

I found out that the reason for the "missing ticks" is the line:

[code]

if InsideBid < InsideAsk then
begin

[/code]

If i delete this line the code seems to work good.
In the picture the upper chart is without the line and shows no missing ticks, compared to the lower.

Posted: 19 Oct 2006
by damageboy
@SP: Oh, I deleted that one too while working on my DB powered version of the indicator... Just didnt' find any good reason for it.

While we're on the subject of the given code,
I'm not sure this does that "we" want it to:

Code: Select all

// Use Upticks/Downticks
if DownTicks <> MyDownTicks then
Bid[Index] = Bid[Index] + MyVol - VolTmp
else if UpTicks <> MyUpTicks then
Ask[Index] = Ask[Index] + MyVol - VolTmp
else
You'll notice, that when a DownTicks/UpTicks "delta" is detected (XXX <> MyXXX) the entire delta volume is added to that side: "... + MyVol - VolTmp", I think it should be something along the lines of (incomplete code):

Code: Select all

// Use Upticks/Downticks
if DownTicks <> MyDownTicks then
Bid[Index] = Bid[Index] + DownTicks - MyDownTicks
else if UpTicks <> MyUpTicks then
Ask[Index] = Ask[Index] + UpTicks - MyUpTicks
else
Unless there's something even more basic that I'm failing to see here...


I think this relies on TS behavior (unless it s a bug there too) that ensures that the function will run two times for each DownTick/UpTicks change, while this may be very different in MC, depending on the data provider... (perhaps with some prividers such data maybe batched up)

It may be true for MC, that both DnTicks and UpTicks may change between subsequent calls, and THEN, this code would not work properly...

I haven't got around to testing this, since I'm bogged down by making my DB version work flawlessly (lots of ADE related crap), but I think this should be tested as well...

What do you think?

Posted: 26 Oct 2006
by Nick
To be more specific: If price changes the line at the previous price turns to 0x0 and the line with the last price is showing the complete volume for that bar, color coded and displayed on the bid or ask-side depending on wich side that last trade was.

So if the bar is finished I have some 0x0 lines, 1 line with the complete volume for that bar (i.e. 115x0) and a delta value showing the volume (i.e. 115).
I believe insidebid and insideask where introduced in the release after the 'stable' one.

Cheers,
Nick.

P.S. you can code your way around that by using 3 data series one for bid one for ask and one for last and comparing last to the bid and ask.

Posted: 26 Oct 2006
by Nick
By The way I like the 'dynamic' modifications (was that SP who did those).

Have you thought of using standard deviations for the thresholds? Not sure if they would provide enough garnularity for colour changes maybe if they where done in .5 steps up to say 3 standard deviations.

Cheers.

Posted: 26 Oct 2006
by Nick
This missing ticks thing concerns me a little. For this condition the market must be crossed (or IB report its crossed). I am not seeing this happen in TWS even with tick aggregation. Perhaps a tick outside the market implies a bid/ask change and that is not implicitly sent? I wonder what is going on here? Could there be a problem with how MC is returning insidebid and insideask? For example in the senario I mentioned if a tick occurs below the previous bid that implies a new bid that is not actually sent?

As an aside there is an alternate way of deciding +ve delta or -ve delta and that is by comparing to the mid point between bid and ask.

Cheers
Nick.

Posted: 26 Oct 2006
by Nick
On a roll today...dont really feel like trading much.

It might be nice to colour the total delta for the bar based on the dynamic colour algorithm too? This would allow you to look at a chart and at a glance see extremes.

Cheers.
Nick,

Posted: 26 Oct 2006
by Alex Kramer
Dear Nick -
A good visual idea of the kind you're famous for, thank you. Just a question, why not post such in the feature request topic?