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

[amibroker] Moon Phase Detector



PureBytes Links

Trading Reference Links

Hi,
Has anyone a code to plot moon phases on the chart?
Or can anybody translate wealth-lab code below into amibroker one?
Thanks!

//Moon Phase Detector III
//Corrects an Int problem with Ver II

//Julian to Calendar Date
function JulianToCal( J, F : float ) : float;
begin
  var A, A1, B, C, D, D1, E, H, H1, M, M9 : float;
  var Y : integer;

  F := F + 0.5;
  if ( F >= 1 ) then
  begin
    F := F - 1.0;
    J := J + 1.0;
  end;
  A1 := Int( ( J / 36524.25 ) - 51.12264 );
  A := J + 1 + A1 - Int( A1 / 4 );
  B := A + 1524;
  C := Int( ( B / 365.25 ) - 0.3343 );
  D := Int( 365.25 * C );
  E := Int( ( B - D ) / 30.61 );
  D := B - D - Int( 30.61 * E ) + F;
  M := E - 1;
  Y := Int( C - 4716 );
  if ( E > 13.5 ) then M := M - 12;
  if ( M < 2.5 ) then Inc( Y );
  D1 := Int( D );

  //Hours and Minutes - not needed
  //H := 24 * ( D - D1 );
  //H1 := Int( H );
  //M9 := Int( 60 * ( H - H1 ) );

  D := ( Y * 10000 ) + ( M * 100 ) + D1;  
  Result := D;
end;

function GetMoonPhase( Date : integer ) : string;
//Date Input Format = YYYYMMDD
//Return 'FULL', 'NEW' or 'NA'.

begin
  const R1 = 3.14159265 / 180;
  var U : boolean;
  var Y : integer;
  var S : string;
  var K0, X, T, T2, T3, J0, F0, J, F, M0, M1, B1, K9, K, M5, M6, 
B6 : float;

  Result := 'NA';
  Y := Trunc( Date Div 10000 );
  U := false;
  K0 := Int( ( Y - 1900 ) * 12.3685 );
  T := ( Y - 1899.5 ) / 100;
  T2 := T * T;
  T3 := T * T * T;
  J0 := 2415020 + ( 29 * K0 );
  F0 := ( 0.0001178 * T2 ) - ( 0.000000155 * T3 );
  F0 := F0 + ( 0.75933 + ( 0.53058868 * K0 ) );
  F0 := F0 - ( 0.000837 * T + ( 0.000335 * T2 ) );
  M0 := K0 * 0.08084821133;
  M0 := 360 * ( M0 - Int( M0 ) ) + 359.2242;
  M0 := M0 - ( 0.0000333 * T2 );
  M0 := M0 - ( 0.00000347 * T3 );
  M1 := K0 * 0.07171366128;
  M1 := ( 360 * ( M1 - Int( M1 ) ) ) + 306.0253;
  M1 := M1 + ( 0.0107306 * T2 );
  M1 := M1 + ( 0.00001236 * T3 );
  B1 := K0 * 0.08519585128;
  B1 := 360 * ( ( B1 - Int( B1 ) ) ) + 21.2964;
  B1 := B1 - ( 0.0016528 * T2 );
  B1 := B1 - ( 0.00000239 * T3 );
  for K9 := 0 to 28 do
  begin
    J := J0 + 14 * K9;
    F := F0 + 0.765294 * K9;
    K := K9 / 2;
    M5 := ( M0 + ( K * 29.10535608 )  ) * R1;
    M6 := ( M1 + ( K * 385.81691806 ) ) * R1;
    B6 := ( B1 + ( K * 390.67050646 ) ) * R1;
    F := F - ( 0.4068 * Sin( M6 ) );
    F := F + ((  0.1734 - ( 0.000393 * T ) ) * Sin( M5 ) );
    F := F + ( 0.0161 * Sin( 2 * M6 ) );
    F := F + ( 0.0104 * Sin( 2 * B6 ) );
    F := F - ( 0.0074 * Sin( M5 - M6 ) );
    F := F - ( 0.0051 * Sin( M5 + M6 ) );
    F := F + ( 0.0021 * Sin( 2 * M5 ) );
    F := F + ( 0.0010 * Sin( 2 * B6 - M6 ) );
    F := F + ( 0.5 / 1440 );
    J := J + Int( F );
    F := F - Int( F );
    if ( not U ) then S := 'NEW' else S := 'FULL';
    X := JulianToCal( J, F );
    if ( X = Date ) then
    begin
      Result := S;
      Exit;
    end;
    U := ( not U );
  end;
end;

//Mockup Sample Trading System
var Bar, X, E, Y, M, D, Z : integer;
var S : string;

EnableNotes( false );
EnableTradeNotes( false, false, true );

procedure IncDay( );
begin
  E := 31;
  Case M of
    4,6,9,11 : E := 30;
    2 : E := 28;
  end;
  Inc ( D );
  if ( D > E ) then
  begin
    Inc ( M );
    D := 1;
    if ( M > 12 ) then
    begin
      Inc( Y );
      M := 1;
      D := 1;
    end;
  end;
  Z := ( Y * 10000 ) + ( M * 100 ) + D;
  Bar := DateToBar( Z );
end;

Y := GetYear( 0 );
M := GetMonth( 0 );
D := GetDay( 0 );
Z := ( Y * 10000 ) + ( M * 100 ) + D;

while  ( Z < GetDate( BarCount - 1 ) ) do
begin

  IncDay;
  S := GetMoonPhase( Z );

  //Skip Any Non Trading Dates
  while ( Bar <=0 ) do
  begin
    IncDay;
    if ( S = 'NA' ) then S := GetMoonPhase( Z );
  end;
  if ( Bar < BarCount ) then
  begin
    SetBackgroundColor( Bar, #BlueBkg );

    //Buy
    if ( MarketPosition = 0 )
    and ( S = 'FULL' ) then
    begin
      BuyAtClose( Bar, S+' Moon Buy' );
      SetBackgroundColor( Bar, #GreenBkg );
      AnnotateBar( 'FULL', Bar, true, #Black, 7 )
    end;
 

    //Sell
    if ( MarketPosition = 1 )
    and ( S = 'NEW' ) then
    begin
      SellAtClose( Bar, LastPosition, S+' Moon Sell' );
      SetBackgroundColor( Bar, #RedBkg );
      AnnotateBar( 'NEW', Bar, true, #Black, 7 )
    end;

  end;
end;