Jump to content

Elasticity indicator


Recommended Posts

I'm working to an indicator called elasticity, which may show us if the price starts to move reported to the volumes involved. The formula should be: (%of volume change/% of price cange) > 1 to have elasticity, <1 for inelasticity. I have an attempt I made today, it's almost done, but I'm stuck. Can somebody help me, please? Any help will be great appreciated.

//+------------------------------------------------------------------+

//| E.mq4 |

//| |

//+------------------------------------------------------------------+

 

 

#property copyright ""

 

#property indicator_separate_window

#property indicator_buffers 4

#property indicator_color1 Lime

#property indicator_color2 Magenta

#property indicator_color3 Gray

#property indicator_width1 2

#property indicator_width2 2

#property indicator_width3 2

 

#property indicator_minimum 0

#property indicator_maximum 10

 

extern color LimeZone = Lime; //Elasticity conditions

extern color PinkZone = Magenta; // InelasticityConditions

extern color GrayZone = Gray; // Not defined

double Vol[];

double MA[];

 

string name;

extern string TF = "-- Time Frame To Display --";

extern int TimeFrame1 = 0;

int TimeFrame2 = 60;

int TimeFrame3 = 240;

string TimeFrames = "M1;5,15,30,60H1;240H4;1440D1;10080W1;43200MN|0-CurrentTF";

 

extern int MAPeriod=14;

extern double BarLevl = 0;

extern int range = 1;

extern int ma_shift = 0;

extern int ma_method = MODE_SMA;

extern int applied_price = PRICE_WEIGHTED;

 

extern int alertSwitch = 0; //0=OFF , 1=ON

extern int alertShift = 0; // 0=change color current Bars, 1=chamge color previous Bar, ....

//

double buffer1[], buffer2[], buffer3[], VolSMA[];

int lastABar;

 

 

 

int init()

{

SetIndexStyle(0,DRAW_ARROW, 2); SetIndexArrow(0,167); SetIndexBuffer(0,buffer1); SetIndexEmptyValue(0,EMPTY_VALUE);

SetIndexStyle(1,DRAW_ARROW, 2); SetIndexArrow(1,167); SetIndexBuffer(1,buffer2); SetIndexEmptyValue(1,EMPTY_VALUE);

SetIndexStyle(2,DRAW_ARROW, 2); SetIndexArrow(2,167); SetIndexBuffer(2,buffer3); SetIndexEmptyValue(2,EMPTY_VALUE);

SetIndexBuffer(3,VolSMA);

 

 

TimeFrame1=MathMax(TimeFrame1,Period());

TimeFrame2=MathMax(TimeFrame2,Period());

TimeFrame3=MathMax(TimeFrame3,Period());

 

 

IndicatorShortName("E_"+TimeFrame1);

 

 

return(0);

}

 

int start()

{

 

datetime TimeArray1[], TimeArray2[], TimeArray3[];

int i,shift,limit,y1=0, y2=0, y3=0, counted_bars=IndicatorCounted();

double Vol[], MA[], VolSMA[];

 

// Plot defined timeframe on to current timeframe

ArrayCopySeries(TimeArray1,MODE_TIME,Symbol(),TimeFrame1);

ArrayCopySeries(TimeArray2,MODE_TIME,Symbol(),TimeFrame2);

ArrayCopySeries(TimeArray3,MODE_TIME,Symbol(),TimeFrame3);

 

limit=Bars-counted_bars+TimeFrame1/Period();

limit=Bars-counted_bars+TimeFrame2/Period();

limit=Bars-counted_bars+TimeFrame3/Period();

for(i=0,y1=0,y2=0,y3=0;i<limit;i++)

{

if (Time<TimeArray1[y1]) y1++;

if (Time<TimeArray2[y2]) y2++;

if (Time<TimeArray3[y3]) y3++;

 

buffer1 = 0; buffer2 = 0; buffer3 = 0;

/***********************************************************

Add your main indicator loop below. You can reference an existing

indicator with its iName or iCustom.

Rule 1: Add extern inputs above for all neccesary values

Rule 2: Use 'TimeFrame' for the indicator timeframe

Rule 3: Use 'y' for the indicator's shift value

**********************************************************/

MA = iMA(NULL,TimeFrame1,MAPeriod,ma_shift,ma_method,applied_price,y1);

 

Vol = iVolume(Symbol(),TimeFrame1,y1);

VolSMA=iMAOnArray(Vol,Bars,MAPeriod,0,MODE_SMA,y1);

 

buffer1=EMPTY_VALUE; buffer2=EMPTY_VALUE; buffer3=EMPTY_VALUE;

 

if (((VolSMA[y1]-VolSMA[y1+1])/VolSMA[y1+1])/((MA[y1]-MA[y1+1])/MA[y1+1])>1) buffer1 = BarLevl;

else

if (((VolSMA[y1]-VolSMA[y1+1])/VolSMA[y1+1])/((MA[y1]-MA[y1+1])/MA[y1+1])<1) buffer2 = BarLevl;

else buffer3 = BarLevl;

}

if (alertSwitch == 1 && Bars > lastABar) {

if (buffer1[alertShift] != EMPTY_VALUE && buffer1[alertShift+1] == EMPTY_VALUE) {

Alert("Elasticity, "+Symbol()+" , M_"+Period());

lastABar = Bars;

}

if (buffer2[alertShift] != EMPTY_VALUE && buffer2[alertShift+1] == EMPTY_VALUE) {

Alert("Inelasticity, "+Symbol()+" , M_"+Period());

lastABar = Bars;

}

}

 

return(0);

}

Link to comment
Share on other sites

soundfx,

It should signal with lime led when we have elasticity (price change is higher then volume change; indi is >1) and magenta when we have inelasticity in the market. This may be a filter for entries and exits. Elasticity it is used in micro-economy to compare the price change from demand/supply, and also I saw some ideas using it in stock market, but I didn't find any indi on the market. The formula should be OK. The MAPeriod should be tested which one fits better.

bgrtz,

It didn't return the values and I have no leds open when it is launched.

Thanks for replies. Rgds,

Link to comment
Share on other sites

The formula for the coefficient of price elasticity of demand for a good is:

 

E_d = {%change in quantity demanded}/{%change in price} = [Delta Q/Q]/[Delta P/P]

Our quantity is the volume, the price I used the the weighted price. It also may be used as a version the ATR, just beeing aware the ATR can increase without a significant price change. For this reason I choose the price.

Link to comment
Share on other sites

The percentage of price elasticity will measure how rapidly the market responds to price changes. If the price elasticity is higher than one, then this means that the pair choosen is price elastic. When the price changes, there is a high corresponding response in number of contracts traded on the market. If the price elasticity is exactly equal to one, then that means that the pair is unit elastic, but that result is relatively rare. It is more common for the price elasticity to be lower than one. This means that the price is inelastic, and price changes will not have a corresponding effect on the number of contracts sold.

 

Now, this is the theory, we need the indicator running to check it out and optimize the parameters.

Link to comment
Share on other sites

I simplify the code. Modify to suit your needs, then share your final result with others here with more comprehensive description (what this do, how to use, or, if any, the relation to other indi's)

 

#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 Blue
#property indicator_width1 3
#property indicator_color2 Red
#property indicator_width2 3
#property indicator_color3 Yellow
#property indicator_width3 3
#property indicator_minimum 0
#property indicator_maximum 2

extern int MA_Period = 14;
extern int MA_Method = 0;
extern int MA_Price = 6;
string shortName = "Elasticity";

double buffer1[];
double buffer2[];
double buffer3[];
double valMA[];
double valVol[];
double valVolMA[];


int init() {
  SetIndexBuffer(0, buffer1);
  SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 1);
  SetIndexLabel(0, "Elasticity");
  SetIndexBuffer(1, buffer2);
  SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 1);
  SetIndexLabel(1, "Inelasticity");
  SetIndexBuffer(2, buffer3);
  SetIndexStyle(2, DRAW_LINE, STYLE_SOLID, 1);
  SetIndexLabel(2, "Idle");
  SetIndexBuffer(3, valMA);
  SetIndexStyle(3, DRAW_NONE);
  SetIndexBuffer(4, valVol);
  SetIndexStyle(4, DRAW_NONE);
  SetIndexBuffer(5, valVolMA);
  SetIndexStyle(5, DRAW_NONE);
  
  IndicatorShortName(shortName);
  
  return(0);
}


int start() {
  int limit = 1500;
  
  for (int i=0; i<=limit; i++) {
     valMA[i] = iMA(Symbol(), 0, MA_Period, 0, MA_Method, MA_Price, i);
     valVol[i] = iVolume(Symbol(), 0, i);
     valVolMA[i] = iMAOnArray(valVol, 0, MA_Period, 0, MODE_SMA, i);
     
     if (((valVolMA[i] - valVolMA[i+1]) / valVolMA[i+1]) / ((valMA[i] - valMA[i+1]) / valMA[i+1]) > 1) {
        buffer1[i] = 1;
        buffer2[i] = EMPTY_VALUE;
        buffer3[i] = EMPTY_VALUE;
        
     } else if (((valVolMA[i] - valVolMA[i+1]) / valVolMA[i+1]) / ((valMA[i] - valMA[i+1]) / valMA[i+1]) < 1) {
        buffer1[i] = EMPTY_VALUE;
        buffer2[i] = 1;
        buffer3[i] = EMPTY_VALUE;
        
     } else {
        buffer1[i] = EMPTY_VALUE;
        buffer2[i] = EMPTY_VALUE;
        buffer3[i] = 1;
     }
  }
  return(0);
}

 

http://img857.imageshack.us/img857/7340/elasticity.png

Blue line is "elasticity", red is "inelasticity"

 

Regards

Link to comment
Share on other sites

Thank you a lot, bgrtz! You are a pro! I will up date you with the results from my side.

The code seems to have a small problem, if I press compile it's working as you showed me, if I try installing the indicator from MT4 I have just a red line and I should press compile each time I change something. Can you, please, help me out fixing it?

Link to comment
Share on other sites

sorry matrixfx, 4xmeter, my bad...

 

  for (i=0; i<=limit; i++) {
     valMA[i] = iMA(Symbol(), 0, MA_Period, 0, MA_Method, MA_Price, i);
     valVol[i] = iVolume(Symbol(), 0, i);
  }
  
  for (i=0; i<=limit; i++) {
     valVolMA[i] = iMAOnArray(valVol, 0, MA_Period, 0, MODE_SMA, i);
     
     if (((valVolMA[i] - valVolMA[i+1]) / valVolMA[i+1]) / ((valMA[i] - valMA[i+1]) / valMA[i+1]) > 1) {
        buffer1[i] = 1;
        buffer2[i] = EMPTY_VALUE;
        buffer3[i] = EMPTY_VALUE;
        
     } else if (((valVolMA[i] - valVolMA[i+1]) / valVolMA[i+1]) / ((valMA[i] - valMA[i+1]) / valMA[i+1]) < 1) {
        buffer1[i] = EMPTY_VALUE;
        buffer2[i] = 1;
        buffer3[i] = EMPTY_VALUE;
        
     } else {
        buffer1[i] = EMPTY_VALUE;
        buffer2[i] = EMPTY_VALUE;
        buffer3[i] = 1;
     }
  }

Edited by bgrtz
Link to comment
Share on other sites

sorry matrixfx, 4xmeter, my bad...

 

  for (i=0; i<=limit; i++) {
     valMA[i] = iMA(Symbol(), 0, MA_Period, 0, MA_Method, MA_Price, i);
     valVol[i] = iVolume(Symbol(), 0, i);
  }
  
  for (i=0; i<=limit; i++) {
     valVolMA[i] = iMAOnArray(valVol, 0, MA_Period, 0, MODE_SMA, i);
     
     if (((valVolMA[i] - valVolMA[i+1]) / valVolMA[i+1]) / ((valMA[i] - valMA[i+1]) / valMA[i+1]) > 1) {
        buffer1[i] = 1;
        buffer2[i] = EMPTY_VALUE;
        buffer3[i] = EMPTY_VALUE;
        
     } else if (((valVolMA[i] - valVolMA[i+1]) / valVolMA[i+1]) / ((valMA[i] - valMA[i+1]) / valMA[i+1]) < 1) {
        buffer1[i] = EMPTY_VALUE;
        buffer2[i] = 1;
        buffer3[i] = EMPTY_VALUE;
        
     } else {
        buffer1[i] = EMPTY_VALUE;
        buffer2[i] = EMPTY_VALUE;
        buffer3[i] = 1;
     }
  }

 

could you post in full mq4 files in code? part of code is hard to read and do not know where to put it.

 

thanks in advice

Link to comment
Share on other sites

Hi bgrtz,

 

Thank you for your time and interest. Now the indicator it's working. I realized I did a mistake beacause the price and volume change values should be absolute value or modulus (http://en.wikipedia.org/wiki/Elasticity_%28economics%29), otherwise comparing with "1" doesn't make sense. The elasticity ideea I have it from the time I worked for my PhD from Harry Markovitz (who received the Nobel Price in 1990 I think).

 

Anyhow, I was impressed how outstanding it is the indicator (the wrong one :)) in this form, so I decided to keep it and allocate more time to test and implement it. I just pray not to repaint (and I think there are no reasons) and to see it tomorrow performing as it did in the history.

Link to comment
Share on other sites

Here it is the indicator code up-dated to be tested.

 

//+------------------------------------------------------------------+

//| Price-Volume Changes.mq4 |

//+------------------------------------------------------------------+

#property copyright "No copyright, 2011, bgrtz and matrixfx"

#property indicator_separate_window

#property indicator_buffers 6

#property indicator_color1 Red

#property indicator_width1 3

#property indicator_color2 Blue

#property indicator_width2 3

#property indicator_color3 Gray

#property indicator_width3 3

#property indicator_minimum 0

#property indicator_maximum 2

 

extern int MA_Period = 14;

extern int MA_Method = 0;

extern int MA_Price = 6;

string shortName = "Elasticity";

 

double buffer1[];

double buffer2[];

double buffer3[];

double valMA[];

double valVol[];

double valVolMA[];

 

 

int init() {

SetIndexBuffer(0, buffer1);

SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 3);

SetIndexLabel(0, "Elasticity");

SetIndexBuffer(1, buffer2);

SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 3);

SetIndexLabel(1, "Inelasticity");

SetIndexBuffer(2, buffer3);

SetIndexStyle(2, DRAW_LINE, STYLE_SOLID, 3);

SetIndexLabel(2, "Idle");

SetIndexBuffer(3, valMA);

SetIndexStyle(3, DRAW_NONE);

SetIndexBuffer(4, valVol);

SetIndexStyle(4, DRAW_NONE);

SetIndexBuffer(5, valVolMA);

SetIndexStyle(5, DRAW_NONE);

 

IndicatorShortName("Elasticity_"+MA_Period);

 

return(0);

}

 

 

int start() {

int i, limit = 1500;

 

for (i=0; i<=limit; i++) {

valMA = iMA(Symbol(), 0, MA_Period, 0, MA_Method, MA_Price, i);

valVol = iVolume(Symbol(), 0, i);

}

 

for (i=0; i<=limit; i++) {

valVolMA = iMAOnArray(valVol, 0, MA_Period, 0, MODE_SMA, i);

 

if (((valVolMA - valVolMA[i+1]) / valVolMA[i+1]) / ((valMA - valMA[i+1]) / valMA[i+1]) > 0) {

buffer1 = 1;

buffer2 = EMPTY_VALUE;

buffer3 = EMPTY_VALUE;

 

} else if (((valVolMA - valVolMA[i+1]) / valVolMA[i+1]) / ((valMA - valMA[i+1]) / valMA[i+1]) <= 0) {

buffer1 = EMPTY_VALUE;

buffer2 = 1;

buffer3 = EMPTY_VALUE;

 

} else {

buffer1 = EMPTY_VALUE;

buffer2 = EMPTY_VALUE;

buffer3 = 1;

}

}

return(0);

}

Link to comment
Share on other sites

I used MA Periods: 5; 8; 14. The main ideea is to cummulate their signals (blue for all for a pre-buy signal, red for all for a pre-sell signal). When 5 or 8 change their color may be the entry point, re-entry or compounding, but also a sign that the trend may be finished. I also use the ATR slope as a filter in the testing; if the signal is with a negativ ATR slope I would consider a reverse chance also. I may consider using a MTF filter if it will work well.

 

I have tried to paste a print screen with my set-up but it did not take it.

bgrtz, if you have any question please contact me. Many thanks again for your help and I'll keep in touch as things progress.

Edited by matrixfx
Link to comment
Share on other sites

Thanks a lot bgrtz. You have did a step forward Version 2 looks good, just should be changed the colors (blue to red and red to blue). I start testing it. Most likely should be added a trend indicator (like Price channel v1.2) as a filter, too. Let's see how its going.

I wonder if we can increase the leds back for a visual back testing.

Thanks again.

Link to comment
Share on other sites

Sorry for the colors, there were OK or does not metter too much.

Honestly, there are some differences from what I saw yesterday, without data feeding. I should think again if we can find a use to this indicator. Anyway, I thank bgrtz for his help.

Actually, what this indicator mesure is when the the change in price is made with a opposite change in volume. For example, if the price decrease with an increase in volume or if the price increase with a decrease in volume we have the red led. If the price increase were with an increase in volume we have blue red.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...