Pages

Subscribe:

Thursday, May 26, 2011

AMA Kaufmana

Hello, dear readers. The last message we wrote AMA Kaufmana. Because one of the readers of the magazine, under nikom Simca, in a code error indicator is found. As predlozhna of interesting scheme to optimize the code. In this edition of the response to a question from readers of the magazine will be given.
20.1 Corrections of error and optimization

The main error will consist in calculating the variable signal.
The last variable code looks like this:
signal = abs (c [change]-c [shift + n-1]);

The corrected version looks like:
signal = abs (c [change]-c [Shift + N]);

The variable signal is considered to be 1 bar less need.

Some readers say that to optimize the code so small is important, especially if the computer processor and operating some GHz memory bit sot Mb. Actually it is not so to load the MT, that would display the user an indicator of the history, it is not difficult. Especially not in highly optimized code.

And not the great moments from the past Kona will be, we read in the last indicator value directly from the file indicator. It can be avoided easily. We know that in perezapuske indicator (expert) the old values ​​of variables are stored. Because it is easier to use design of that type.

AMA (new) = AMA (old) + ...
Carry part of the letter that was written for me Dear Simca.
...
Your fragment looks like:

AMA1 = c [Nbars];
For shift = Nbars Downto 0
Begin ...
AMA = (c [change] * ssc * SSC) + GetIndexValue (Shift +1) * (1-SSC * SSC);
if shift = Nbars then AMA = (c [change] * ssc * SSC) + AMA1 * (1-SSC * SSC);
SetIndexValue (shift, AMA);
End;

I will write here also:

AMA = c [Nbars];
For shift = Nbars Downto 0
Begin ...
AMA = (c [change] * ssc * SSC) + AMA * (1-SSC * SSC);
SetIndexValue (shift, AMA);
End;


The mine, so it's easier.) There is no sense to take the previous value of AMA file, it will also be kept from the previous cycle. The initial value set by variable AMA1 and checking each iteration, it is simply a cycle of early appropriate variable but. The essence will not change, and the algorithm will be significantly simplified. Variable AMA1 in this case is not absolutely necessary.
:)...
But it has not stopped Simca Dear, I make the following quotation letter.
...
In general, my algorithm indicator looked after made changes as follows:


Inputs: range (10), FastMA (2), SlowMA (30);
Variable: CB (0) I (0), noise (0), ER (0), NRK (0), AMA (0);

SetLoopCount (0);
AMA = Close [Bars range];
for CB = bars range-1 downto 0 start
Noise = 0;
for i = CB to CB + -1 range start
Noise Noise => ABS (Close [i]-Close [i +1]);
end;
ER = abs (Close [CB]-Close [+ CB range]) / noise;
SSC = ER * (2 / (FastMA +1) -2 / (SlowMA +1)) +2 / (SlowMA +1);
AMA = Close [CB] * SSC * SSC + AMA * (1-SSC * SSC);
SetIndexValue (CB, AMA);
end;




As you can see, I do not use "amount of bars that the indicator" (Nbars) and accessible to all otobrazhaju number of bars displayed. It is certainly not the primary issue, just my computer. There is a perfect time to count all the frequencies. :)
Rather than shift I used to write CB (down from CurrentBar). :) I think the bars (bars range-1) that the internal cycle not to jump to limit the amount of bars. Thus the first value AMA has taken the price of a bar before the first count (range bars). signal I do not begin to describe the variable separately and is transmitted directly to the calculation, because the ER all the same is not used anywhere anymore. We already talked about everything else. :) It's all here so clearly and specifically to describe anything there. :)

Now we will actually bring to perfection it is. We will begin by calculating NRK. With open years is evident that there are a bunch of parameters considered does not depend on changing the cycle. Logical to calculate once a cycle and inside ready to use values. I here nothing is invented, all this was actually the clause.
2 / (SlowMA +1) - a so-called "SlowSC" (the term of the clause).
2 / (FastMA +1) is "FastSC".
As the inside of the cycle actually "FastSC" do not interest us, and the difference FastSC-SlowSC matter will only use two preliminary (pre cycle basis) calculated factor:

K1 = 2 / (SlowMA +1);
K2 = 2 / (FastMA 1) C1-;

After she gets the SSC calculation considerably shorter (and the main work faster:) sort of):

SSC = ER * K2 + K1;

Furthermore. In the calculation but in the classical form of multiplication operations too (unjustified luxury) are used. :) So that we will transform the formula to more comprehensible form (to be considered faster). Kind of transformation is implemented, but if it is not obvious, I'll paint him more in detail:

Price - Price Close [CB.]
AMA1 - AMA previous value (but we remember) that it would be possible to use simple BUT the last iteration of the cycle).

AMA = Price * SSC ^ 2 + AMA1 * (1-SSC ^ 2);

will open brackets
AMA = Price * SSC ^ 2 + AMA1-AMA1 * SSC ^ 2;

we will rearrange the elements of convenience
AMA = AMA1 + Price * SSC ^ 2-AMA1 * SSC ^ 2;

well, you will also be placed outside the brackets SSC ^ 2
AMA = AMA1 + SSC ^ 2 * (Price-AMA1)

Now AMA calculation looks like:
AMA = AMA + SSC * * SSC (Close [CB]-AMA);

Here's this other question:) only two multiplication and additions is increased. :)
This type of formula by the way was also annexed.

So here how it would be possible soptimizirovali. At least so it seemed.

Total received indicator in the following types:



Inputs: range (10), FastMA (2), SlowMA (30);
Variable: CB (0) I (0), K1 (0), K2 (0) Noise (0), ER (0), NRK (0), AMA (0);

SetLoopCount (0);
K1 = 2 / (SlowMA +1);
K2 = 2 / (FastMA 1) C1-;
AMA = Close [Bars range];
for CB = bars range-1 downto 0 start
Noise = 0;
for i = CB to CB + -1 range start
Noise Noise => ABS (Close [i]-Close [i +1]);
end;
ER = abs (Close [CB]-Close [+ CB range]) / noise;
SSC = ER * K2 + K1;
AMA = AMA + SSC * * SSC (Close [CB]-AMA);
SetIndexValue (CB, AMA);
end;


Finally make sure that all of us have done correctly (in fact we only optimized algorithm does not change its essence) launched on schedule for both indicators simultaneously. We see the full absolute matching curves. Optimization means is accurate. :) But the second version will run faster ...
Accordingly imitations!

I want to repeat one of the highlights of optimization. AMA (new) = AMA (old) +. This allows them not to read the final value of the indicator from a file. By using the last value in the formula of no longer sufficient indicators, it would be helpful for everyone to take this weapon, simple and comfortable, reception.
20.2 Answer the question

Dear Jury asks.

... How to make the counselor closed position (if those imeetsja) of a given instrument at the time of closing the bar and entered the market at the time of formation of these new bar ...

MT "understands" that only the bar will be closed. But MT knows the correct time. Since this will also continue.

Here's some code that closes the position before closing the bar I1. By analogy to open position, I think, you can do.

if min = 59, then
{
If TotalTrades> 0, then
{
for CNT = 1 TotalTrades
{
If OrderValue (CNT, VAL_TYPE) <= OP_SELL and
OrderValue (CNT, VAL_SYMBOL) = Symbol then
{
If OrderValue (CNT, VAL_TYPE) = OP_BUY then
{
CloseOrder (OrderValue (CNT, VAL_TICKET),
OrderValue (CNT, VAL_LOTS), bid, 3, Violet);
exit;
};
If OrderValue (CNT, VAL_TYPE) = OP_SELL then
{
CloseOrder (OrderValue (CNT, VAL_TICKET),
OrderValue (CNT, VAL_LOTS), to ask, 3, Violet);
Exit;
};
};
};
};
};


Here the initial value for expert D1: if Hour = 23 then {
20.3 Conclusion

Once again I want to express my deep gratitude, the reader under nikom Simca. He is convinced that it would be interesting for everyone to look at other things to not cook in their own juice. If any of you wish to share ideas, must appear write.

0 comments:

Post a Comment