EA using indicator

[Deleted]  

I want to use indicator in my EA, timeframe m30.

For instance Moving average indicator.

EA contains:

double ma = iCustom(NULL, PERIOD_M30, "Moving Average",13,0,0,0,1);

Print("MA = " + ma);


How to make EA if I want to print variable "ma" only once per bar (once every 30min)?

Now it prints many times even my graph has period m30.

onyx
35
onyx  
markog:

I want to use indicator in my EA, timeframe m30.

For instance Moving average indicator.

EA contains:

double ma = iCustom(NULL, PERIOD_M30, "Moving Average",13,0,0,0,1);

Print("MA = " + ma);


How to make EA if I want to print variable "ma" only once per bar (once every 30min)?

Now it prints many times even my graph has period m30.



Your EA and indicator function will most likely be working per tick, not per bar. The EA does this, as it might need to do several operations between each bar. A tick can be a small but variable amount of time. So if you have a print command inside the main start function of the EA, your result will be printed often.


There are several ways around this, one is to keep a tickcount you use and reset it every so often, another is to count time, using the collective results of the different TimeCurrent functions, another would be to wait till the result of the built in variable Bars, the total amount of bars on the chart, increased by 1.


NB iCustom is only for custom indicators you make or edit yourself, is that what your seeking? iMA is the standard moving average function.
[Deleted]  
Onyx:


Your EA and indicator function will most likely be working per tick, not per bar. The EA does this, as it might need to do several operations between each bar. A tick can be a small but variable amount of time. So if you have a print command inside the main start function of the EA, your result will be printed often.


There are several ways around this, one is to keep a tickcount you use and reset it every so often, another is to count time, using the collective results of the different TimeCurrent functions, another would be to wait till the result of the built in variable Bars, the total amount of bars on the chart, increased by 1.


NB iCustom is only for custom indicators you make or edit yourself, is that what your seeking? iMA is the standard moving average function.



Thank you for your reply.

Yes, I have custom indicator. I compare 2 values (different shift in icustom) and then I decide whato to do (sell, buy or close + print). As you said it works on every tick. I will try with Bars...

kctrader
8
kctrader  

Do this:

int LastClose; // set this at top of script in global vars, right after extern vars

if (LastClose!=Close[1]) {

LastClose=Close[1];

// put your code here

}

Anything inside the IF will only execure once per bar

onyx
35
onyx  
kctrader:

Do this:

int LastClose; // set this at top of script in global vars, right after extern vars

if (LastClose!=Close[1]) {

LastClose=Close[1];

// put your code here

}

Anything inside the IF will only execure once per bar


Its a good idea, except in the rare circumstance that the Close value was the same value on both bars. I would still go for time myself and + 30 minutes before each command was carried out but if you really wanted a bar option:

//--- Declarations

int CountBars = 0;

//--- Program



if (Bars < CountBars) // if its not equal or greater, we must reset its value.

{

CountBars = 0;

}


if (Bars > CountBars)

{

// Your Code inside here.

CountBars = Bars;

}



NB the other reason i'd use time instead of bars, is because in heavy lag I surmise you might miss a bar out on the smaller charts.

[Deleted]  
Onyx:


Its a good idea, except in the rare circumstance that the Close value was the same value on both bars. I would still go for time myself and + 30 minutes before each command was carried out but if you really wanted a bar option:

//--- Declarations

int CountBars = 0;

//--- Program



if (Bars < CountBars) // if its not equal or greater, we must reset its value.

{

CountBars = 0;

}


if (Bars > CountBars)

{

// Your Code inside here.

CountBars = Bars;

}



NB the other reason i'd use time instead of bars, is because in heavy lag I surmise you might miss a bar out on the smaller charts.

This works well.

Thank you.

Another question: values of the indicator on the chart are not the same as values, which I get with iCustom and same indicator.

Is it possible, that indicator's tick is different as iCustom's tick in the same bar?