How to code? - page 296

 

Is it possible to add color, style and weight to indicator levels lines?

Hi,

I am wondering if someone could tell me if it is possible to add color, style and weight to indicator levels lines?

As a starter I would like to specify both style and color to the RSI levels and are wondering if one can do this by adding more code to for example the line: #property indicator_level1 70 ?

If possible I would appreciate it a lot if someone could show me.

Thanks,

Laurus

 

...

Yes, you can

Use the following properties :

#property indicator_levelcolor

#property indicator_levelstyle

#property indicator_levelwidth

[/PHP]

If you use indicator_levelwidth greater than 1, only indicator_levelstyle that metatrader will allow is STYLE_SOLID (solid line). Also, you can specify only one color, style and width for all levels (if you need more than one color, style or width for levels, you either have to use buffers or objects - horizontal line, for example)

You can use the SetLevelStyle() function too for that purpose,with same limitations as described above

[PHP]

void SetLevelStyle( int draw_style, int line_width, color clr=CLR_NONE)

Laurus12:
Hi,

I am wondering if someone could tell me if it is possible to add color, style and weight to indicator levels lines?

As a starter I would like to specify both style and color to the RSI levels and are wondering if one can do this by adding more code to for example the line: #property indicator_level1 70 ?

If possible I would appreciate it a lot if someone could show me.

Thanks,

Laurus
 

Thank you for your reply Mladen, your help is always very much appreciated.

What you wrote is exactly what I was afraid of. When it comes to functions part it is still over my head.

If you take a look in the picture you see how I would like it to be. As a compromise I have used regular horizontal lines, but since I draw trend lines with the indicator itself it is not a good solution.

Thanks,

Laurus

Files:
 

...

Looking at your picture, it seems that it is the only solution you can apply (using objects). Using drawing buffers would simply drain your drawing buffers and you could not draw all the values you are using on that indicator. Sometimes we are still "paying" for metatraders 4 decision to have just 8 drawing buffers

Laurus12:
Thank you for your reply Mladen, your help is always very much appreciated.

What you wrote is exactly what I was afraid of. When it comes to functions part it is still over my head.

If you take a look in the picture you see how I would like it to be. As a compromise I have used regular horizontal lines, but since I draw trend lines with the indicator itself it is not a good solution.

Thanks,

Laurus
 

External DLL returns strange results

Hi!

Below is a very simple example of a call to an external DLL. The journal log should show increasing numbers starting from 0 when used with the strategy tester.

However, the result is pretty strange. The first row in the journal log displays a large number (ie. 18472) and then increases properly for a while until it starts jumping about 10, sometimes over 100, steps at once.

Could someone explain to me the reason for this and how to correct it?

Thanks!

// MyExpert.mql //

#import "MyDLL.dll"

int Test();

#import

void start(){

Print(Test());

}[/CODE]

// MyDLL.def //

LIBRARY MyDLL

EXPORTS

Test

[CODE]

// MyDLL.dll //

int i= 0;

int __stdcall Test() {

i++;

return i;

}
 

interesting code problem for indicator

Hi Guys,

I'm trying to learn MT4 programming, and this very moment I'm developing an PinBar detection indicator, whcih tells me to buy or well. I developed this indicator especially for the 4 hour chart, because of the special rules for buy or sell. The rule for detections is as follows:

Body of candle <= 35% of candlelength.

At least 1 wick of candle >= 50 % of candlelength.

So far I have no problems detecting those bars with the indicator.

However, the next rule tells me to buy or sell.

I zoom in to the 1hour chart and look at the 4 bars that created the pinbar on the 4 hour chart.

I used the iClose function to pickup the value of the close of the first and the last bar of that formation on the 1 hour bar.

However..as programmed now, I always look at the first and last candle on the 1hour chart itself. And that is not ok. It has to be the first and last bar of the formation and not of the chart.

Here is the code what I now have with the wrong iClose functions in it.

if ( (((100.0/CandleLength)*BodyLength)=50.0) || (((100.0/CandleLength)*LowerWick)>=50.0) ) && iClose(Symbol(),60,1) > iClose(Symbol(),60,4))

{

Buy = Close;

SetLevel(true,i+1,Close);

}

I haven't been able to pin down the right 1 hour candles in the background on that particular 4hour candle.

Is someone outthere that can give me the answer to my problem?

fixed it:

datetime H4BarTime;

int H1BarNumber;

H4BarTime = Time;

H1BarNumber = iBarShift(NULL, PERIOD_H1, H4BarTime);

tx very much!

Jacob

 
mladen:
Looking at your picture, it seems that it is the only solution you can apply (using objects). Using drawing buffers would simply drain your drawing buffers and you could not draw all the values you are using on that indicator. Sometimes we are still "paying" for metatraders 4 decision to have just 8 drawing buffers

Sorry for my late reply Mladen and thank you for clarifying the matter. Regarding what you wrote, at least now I know for sure.

Thank you,

Laurus

 

Help with Entry Criteria

Hi guys,

I am trying to do up a entry criteria using MA. My entry criteria is simple, I want to enter a long trade when Fast MA crosses Slow MA upwards on the candle close instead of the current candle and vice verse.

My current situation is that my EA will trigger a long trade when the Fast MA crosses Slow MA upwards and vice verse, however that candle has not yet close, and at times when the candle closes, the final MA might not crosses upwards thus, I should not have triggered that particular trade, and yet the system triggered it because it has crossed upwards once and back. This is the same for short trades

I have been going about this for a few days already without any progress. Can someone shed some light on how do I go about doing this? Thanks.

//--- input parameters

extern double TakeProfit=2700.0;

extern double Lots=0.1;

extern double StopLoss=2500.0;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

int Crossed (double line1 , double line2)

{

static int last_direction = 0;

static int current_dirction = 0;

if(line1>line2)current_dirction = 1; //up

if(line1<line2)current_dirction = 2; //down

if(current_dirction != last_direction) //changed

{

last_direction = current_dirction;

return (last_direction);

}

else

{

return (0);

}

}

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

//| expert start function |

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

int start()

{

//----

int cnt, ticket, total;

double shortEma, longEma;

if(Bars<100)

{

Print("bars less than 100");

return(0);

}

if(TakeProfit<10)

{

Print("TakeProfit less than 10");

return(0); // check TakeProfit

}

shortEma = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,0);

longEma = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,0);

int isCrossed = Crossed (shortEma,longEma);

total = OrdersTotal();

if(total < 1)

{

if(isCrossed == 1)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,

"My EA",12345,0,Green);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

Print("BUY order opened : ",OrderOpenPrice());

}

else Print("Error opening BUY order : ",GetLastError());

return(0);

}

if(isCrossed == 2)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,

Bid-TakeProfit*Point,"My EA",12345,0,Red);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

Print("SELL order opened : ",OrderOpenPrice());

}

else Print("Error opening SELL order : ",GetLastError());

return(0);

}

return(0);

}

return(0);

}

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

Thanks and regards

Terrance

 

...

Terrance

Try changing these lines :

shortEma = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,0);

longEma = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,0);

int isCrossed = Crossed (shortEma,longEma);[/PHP]

to something like this :

[PHP] int isCrossed = 0;

double shortEma1 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,1);

double longEma1 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,1);

double shortEma2 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,2);

double longEma2 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,2);

double diff1 = shortEma1-longEma1;

double diff2 = shortEma2-longEma2;

if ((diff1*diff2)<0)

{

if (shortEma1>longEma1)

isCrossed = 1;

else isCrossed = 2;

}

That way you even do not need the crossed function and it will check for crosses on a closed bar.

tkuan77:
Hi guys,

I am trying to do up a entry criteria using MA. My entry criteria is simple, I want to enter a long trade when Fast MA crosses Slow MA upwards on the candle close instead of the current candle and vice verse.

My current situation is that my EA will trigger a long trade when the Fast MA crosses Slow MA upwards and vice verse, however that candle has not yet close, and at times when the candle closes, the final MA might not crosses upwards thus, I should not have triggered that particular trade, and yet the system triggered it because it has crossed upwards once and back. This is the same for short trades

I have been going about this for a few days already without any progress. Can someone shed some light on how do I go about doing this? Thanks.

//--- input parameters

extern double TakeProfit=2700.0;

extern double Lots=0.1;

extern double StopLoss=2500.0;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

int Crossed (double line1 , double line2)

{

static int last_direction = 0;

static int current_dirction = 0;

if(line1>line2)current_dirction = 1; //up

if(line1<line2)current_dirction = 2; //down

if(current_dirction != last_direction) //changed

{

last_direction = current_dirction;

return (last_direction);

}

else

{

return (0);

}

}

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

//| expert start function |

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

int start()

{

//----

int cnt, ticket, total;

double shortEma, longEma;

if(Bars<100)

{

Print("bars less than 100");

return(0);

}

if(TakeProfit<10)

{

Print("TakeProfit less than 10");

return(0); // check TakeProfit

}

shortEma = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,0);

longEma = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,0);

int isCrossed = Crossed (shortEma,longEma);

total = OrdersTotal();

if(total < 1)

{

if(isCrossed == 1)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,

"My EA",12345,0,Green);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

Print("BUY order opened : ",OrderOpenPrice());

}

else Print("Error opening BUY order : ",GetLastError());

return(0);

}

if(isCrossed == 2)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,

Bid-TakeProfit*Point,"My EA",12345,0,Red);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))

Print("SELL order opened : ",OrderOpenPrice());

}

else Print("Error opening SELL order : ",GetLastError());

return(0);

}

return(0);

}

return(0);

}

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

Thanks and regards

Terrance
 
mladen:
Terrance

Try changing these lines :

shortEma = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,0);

longEma = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,0);

int isCrossed = Crossed (shortEma,longEma);[/PHP]

to something like this :

[PHP] int isCrossed = 0;

double shortEma1 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,1);

double longEma1 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,1);

double shortEma2 = iMA(NULL,0,10,0,MODE_LWMA,PRICE_CLOSE,2);

double longEma2 = iMA(NULL,0,20,0,MODE_LWMA,PRICE_CLOSE,2);

double diff1 = shortEma1-longEma1;

double diff2 = shortEma2-longEma2;

if ((diff1*diff2)<0)

{

if (shortEma1>longEma1)

isCrossed = 1;

else isCrossed = 2;

}

That way you even do not need the crossed function and it will check for crosses on a closed bar.

Hi Mladen, i tried out what you told me and it work wonders. However, I don't quite understand the logic behind the codes. why do you set the shift of iMA to 1 and 2 and why do you code this: (diff1*diff2)<0) as well? Sorry but I am currently still on the learning phrase.

Thanks and regards

Terrance