[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [amibroker] further off: speaker buzz (was Really Off-Topic: CD Drive Motor



PureBytes Links

Trading Reference Links

Howard,
Is there any reason to estimate, when we know the exact tomorrow´s 
Close for an MA, EMA or DEMA cross ?
See in  http://groups.yahoo.com/group/amibroker/files/
the Cross Predictions.txt for the respective formulas.
Dimitris Tsokakis
--- In amibroker@xxxxxxxxxxxxxxx, "howard bandy" <howardbandy@xxxx> 
wrote:
> Greetings all – particularly Chuck –
>  
> Here is a version that handles Exponential Moving Averages.
>  
> If any of you find good uses for this technique, please feel free to
> let us all know about them.  
>  
> If there are other functions that would be valuable to compute
> crossover or prediction points for, let me know.
>  
> Thanks,
> Howard
>  
> //================================================
> //
> //          Solve EMA Cross Point
> //
> //          Howard Bandy
> //          January 29, 2004
> //
> //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
> //          This code is a variation of "Solve MA Cross Point".
> //          In that version, the moving averages were Simple.
> //          In this version, the moving averages are Exponential.
> //
> //          Some background for those who have forgotten:
> //          EMA needs only the previous value of the EMA,
> //          the "alpha" value, and the next data point.
> //          EMA(i) = alpha * Close(i) + (1 - alpha) * EMA(i-1)
> //
> //          alpha = 2 / (N + 1), where N is the number of days
> //          in the average.
> //
> //          In our case, we want EMA(i+1), which is
> //          alpha * Close(i+1) + (1-alpha) * EMA(i),
> //          where the Close(i+1) is varied until we find
> //          values that converge.
> //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
> //
> //
> //          Load this routine into Indicator Builder and
> //          apply it to any bar chart.
> //
> //          This routine uses a binary search to compute
> //          the value of the close 
> //          of the next bar such that two simple moving 
> //          averages will cross.
> //
> //          It plots the price necessary and the change 
> //          from the most recent close.
> //
> //          If MA1Length is shorter than MA2Length,
> //          then MA1 is the faster moving average.
> //          MA1 is plotted as a Blue line.
> //          MA2 is plotted as a Green line.
> //
> //          The computed price is plotted as a Red line.
> //          The change from today's close to the
> //          computed price is plotted as a Yellow line.
> //
> //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
> //          In order to use this technique, you must be
> //          able to compute the indicator you want to evaluate
> //          for a variety of trial values for the next bar.
> //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
> //
> //          This example makes use of the fact that the
> //          new simple moving average is computed by
> //          dropping off the oldest value and picking up the 
> //          newest value.  
> //
>  
> MA1Length = 3;
> MA2Length = 10;
> Longest = Max(MA1Length,MA2Length);
>  
> Alpha1 = 2/(MA1Length+1);
> Alpha2 = 2/(MA2Length+1);
>  
> MA1 = EMA(C,MA1Length);
> MA2 = EMA(C,MA2Length);
>  
> //          Establish initial values for the search.
> //          Since the movign average will cross over at 
> //          only one point, we only need to be sure that
> //          one startinging value is too high (above the 
> //          crossover point) and the other starting value 
> //          is too low (below the crossover point).
>  
> MinC = 0.1 * C;
> MaxC = 10.0 * C;
>  
> //          Note on computational implications --
> //          Assume the stock being processed has a Close 
> //          of $50.00.  MaxC starts at $500.00, MinC at $5.00.
> //          The range being searched is $495.00.  The search stops
> //          HighC and LowC are within $0.005.  The ratio of $495.00
> //          to $0.005 is 100000 to 1.  Each search step cuts the 
range
> //          in half.  So it will take about 17 steps to converge -- 
that
> //          is 17 passes through the While loop for each bar in the
> //          range being processed.
> //          If MinC starts at 0.5 * C and MaxC starts at 2.0 * C, 
> //          the search is cut to 14 passes through the While loop.
> //          If MinC and MaxC are 0.9 * C and 1.1 * C, 11 passes.
> //          Increasing the stopping citerion from 0.005 to 0.02 
> //          cuts 2 passes off for any MinC and MaxC.
> //          
> //          All that is necessary is that MinC and MaxC bracket the 
> //          solution.  In some cases, it might be advantageous to 
use
> //          a short routine that does not go through the
> //          entire While loop can be used to pick MinC and MaxC 
close
> //          together, but with different signs for the "Difference".
> //
>  
> //          Declare the variables and initialize them
> MA1atCP1 = C; //          MA1 at Ref(MA1,+1) using trial value of 
Close.
> MA2atCP1 = C; //          MA2 at Ref(MA2,+1) using trial value of 
Close.
> MidC = C;                     //          Next trial value of Close.
> ChgReq = 0;      //          Change in Close required for Crossover.
>  
> //          Of course, if we could use Ref(MA1,1), there would be no
> need
> //          for all this.  The difficulty is that we want to 
repeatedly
> //          change the value of the final element until we determine
> //          the solution to the crossover. 
>  
> //          Loop through all the bars
> for (i=Longest; i<BarCount; i++)
> {
>             //          Initialize the Low test value and compute 
the
> indicators
>             LowC[i] = MinC[i];
>             MA1atCP1[i] = (Alpha1*LowC[i]) + (1-Alpha1) * MA1[i-1];
>             MA2atCP1[i] = (Alpha2*LowC[i]) + (1-Alpha2) * MA2[i-1];
>             DiffLowC[i] = MA1atCP1[i] - MA2atCP1[i];
>  
>             //          Initialize the High test value and compute 
the
> indicators
>             HighC[i] = MaxC[i];
>             MA1atCP1[i] = (Alpha1*HighC[i]) + (1-Alpha1) * MA1[i-1];
>             MA2atCP1[i] = (Alpha2*HighC[i]) + (1-Alpha2) * MA2[i-1];
>             DiffHighC[i] = MA1atCP1[i] - MA2atCP1[i];
>  
>             //          Test to see if the two indicators have
> converged.
>             //          They probably have not yet, but we need a 
value
>             //          at the first test of the loop.
>             Difference[i] = abs(DiffLowC[i] - DiffHighC[i]);
>  
>             //          Stay in the search loop until the upper 
limit
> price
>             //          and lower limit price are within 0.005 (one 
half
> cent in US)
>             while (Difference[i] > 0.005)
>             {
>                         //          Pick the midpoint of LowC and 
HighC
> to test next.
>                         MidC[i] = (LowC[i] + HighC[i]) / 2;
>                         //          And compute the indicators
>                         MA1atCP1[i] = (Alpha1*MidC[i]) + (1-Alpha1) 
*
> MA1[i-1];
>                         MA2atCP1[i] = (Alpha2*MidC[i]) + (1-Alpha2) 
*
> MA2[i-1];
>                         DiffMidC[i] = MA1atCP1[i] - MA2atCP1[i];
>  
>                         //          Using the signs of the 
differences,
>                         //          decide whether to replace the 
high
> test point
>                         //          or the low test point by the new
> Middle test point 
>                         SignMid[i] = DiffMidC[i] > 0.0;
>                         SignLow[i] = DiffLowC[i] > 0.0;
>             
>                         if (SignMid[i] == SignLow[i])
>                         {
>                                     //          The sign of the
> difference at the LowC point and at the
>                                     //          MidC point are the 
same.
> That means LowC and MidC are on
>                                     //          the same side of the
> crossover point we are looking for,
>                                     //          but MidC is closer.
>                                     //          So, replace Low with
> Middle
>                                     LowC[i] = MidC[i];
>                                     MA1atCP1[i] = (Alpha1*LowC[i]) +
> (1-Alpha1) * MA1[i-1];
>                                     MA2atCP1[i] = (Alpha2*LowC[i]) +
> (1-Alpha2) * MA2[i-1];
>                                     DiffLowC[i] = MA1atCP1[i] -
> MA2atCP1[i];
>                         }
>                         else
>                         {
>                                     //          Replace High with 
Middle
>                                     HighC[i] = MidC[i];
>                                     MA1atCP1[i] = (Alpha1*HighC[i]) 
+
> (1-Alpha1) * MA1[i-1];
>                                     MA2atCP1[i] = (Alpha2*HighC[i]) 
+
> (1-Alpha2) * MA2[i-1];
>                                     DiffHighC[i] = MA1atCP1[i] -
> MA2atCP1[i];
>  
>                         }           //          if-else
>             
>                         //          One of DiffLowC or DiffHighC has
> changed as the 
>                         //          search range has tightened, the
> other
>                         //          remains as it was on the last 
pass
> through the loop.
>                         //          Compute the new difference 
between
> the two.
>                         Difference[i] = abs(DiffLowC[i] - DiffHighC
[i]);
>  
>             }           //          while
>  
>             
>             //          After the loop, the value of MidC is the 
value
>             //          at which the curves will cross
>             MidC[i] = (LowC[i] + HighC[i])/2;
>  
>             //          Limit MidC so that it plots reasonably
>             MidC[i] = IIf(MidC[i]>1.25*C[i],1.25*C[i], MidC[i]);
>             MidC[i] = IIf(MidC[i]<0.80*C[i],0.80*C[i], MidC[i]);
>  
>             //          Compute the change required from the most 
recent
>             //          Close to the next Close if crossover is to
> happen.
>             ChgReq[i] = MidC[i] - C[i];
>  
> }           //          for
>  
> //          Plot Close, MA1, MA2, and Computed Cross Point
> Plot(C,"C",colorBlack,styleCandle);
> Plot(EMA(C,MA1Length),"EMA1",colorBlue,styleLine);
> Plot(EMA(C,MA2Length),"EMA2",colorGreen,styleLine);
> Plot(MidC,"Computed Cross",colorRed,styleLine);
>  
> //          Plot Change Required to Cross
> Plot(ChgReq,"Change 
Required",colorYellow,styleDots|styleLeftAxisScale);
> Plot(0.0,"",colorYellow,styleLine|styleLeftAxisScale);
> 
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.574 / Virus Database: 364 - Release Date: 1/29/2004


Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx 
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html 

------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for your HP, Epson, Canon or Lexmark
Printer at MyInks.com. Free s/h on orders $50 or more to the US & Canada.
http://www.c1tracking.com/l.asp?cid=5511
http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/GHeqlB/TM
---------------------------------------------------------------------~->

Yahoo! Groups Links

To visit your group on the web, go to:
 http://groups.yahoo.com/group/amibroker/

To unsubscribe from this group, send an email to:
 amibroker-unsubscribe@xxxxxxxxxxxxxxx

Your use of Yahoo! Groups is subject to:
 http://docs.yahoo.com/info/terms/