Fasi Lunari (Moon Phases)

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #93998 quote
    Hans63
    Participant
    Average

    C’è la possibilità di avere l’indicatore delle fasi lunari?

    Ho notato che lavora molto bene su indici e materie prime

    Fasi-Lunari.png Fasi-Lunari.png
    #94010 quote
    Nicolas
    Keymaster
    Master

    hai un codice per trovarli? Vedo che hai allegato una foto da tradingview ..

    #94014 quote
    Hans63
    Participant
    Average

    ho trovato questi, ma non so dirti di più

    var Moon = {
      phase: function (year, month, day) {
        var c = e = jd = b = 0;
    
        if (month < 3) {
          year--;
          month += 12;
        }
    
        ++month;
        c = 365.25 * year;
        e = 30.6 * month;
        jd = c + e + day - 694039.09; // jd is total days elapsed
        jd /= 29.5305882; // divide by the moon cycle
        b = parseInt(jd); // int(jd) -> b, take integer part of jd
        jd -= b; // subtract integer part to leave fractional part of original jd
        b = Math.round(jd * 8); // scale fraction from 0-8 and round
    
        if (b >= 8) b = 0; // 0 and 8 are the same so turn 8 into 0
    
        switch (b) {
          case 0:
            return 'new-moon';
            break;
          case 1:
            return 'waxing-crescent-moon';
            break;
          case 2:
            return 'quarter-moon';
            break;
          case 3:
            return 'waxing-gibbous-moon';
            break;
          case 4:
            return 'full-moon';
            break;
          case 5:
            return 'waning-gibbous-moon';
            break;
          case 6:
            return 'last-quarter-moon';
            break;
          case 7:
            return 'waning-crescent-moon';
            break;
        }
      }
    };
    
    // Moon.phase('2018', '01', '19');
    I modified your script to not have a bloaty switch case, and actually return an object containing both the name and the number of the phase ;)
    
    // Original Snippet: https://gist.github.com/endel/dfe6bb2fbe679781948c
    
    var Moon = {
      phases: ['new-moon', 'waxing-crescent-moon', 'quarter-moon', 'waxing-gibbous-moon', 'full-moon', 'waning-gibbous-moon', 'last-quarter-moon', 'waning-crescent-moon'],
      phase: function (year, month, day) {
        let c = e = jd = b = 0;
    
        if (month < 3) {
          year--;
          month += 12;
        }
    
        ++month;
        c = 365.25 * year;
        e = 30.6 * month;
        jd = c + e + day - 694039.09; // jd is total days elapsed
        jd /= 29.5305882; // divide by the moon cycle
        b = parseInt(jd); // int(jd) -> b, take integer part of jd
        jd -= b; // subtract integer part to leave fractional part of original jd
        b = Math.round(jd * 8); // scale fraction from 0-8 and round
    
        if (b >= 8) b = 0; // 0 and 8 are the same so turn 8 into 0
        return {phase: b, name: Moon.phases[b]};
      }
    };
    
    // Moon.phase('2018', '01', '19');
    
    #94017 quote
    Nicolas
    Keymaster
    Master

    Grazie, è JavaScript, ma penso di poterlo tradurre in qualche modo!

    #94019 quote
    Nicolas
    Keymaster
    Master

    Penso di essergli vicino … Le nuove lune sono cerchi e la luna piena sono quadrati

    //moon phases
    
    iday=openday
    imonth=openmonth
    iyear=openyear
    
    if imonth<3 then
    iyear=iyear-1
    imonth=imonth+12
    endif
    
    imonth=imonth+1
    c = 365.25 * iyear
    e = 30.6 * imonth
    jd = c + e + iday - 694039.09 // jd is total days elapsed
    jd = jd/29.5305882 // divide by the moon cycle
    decimal=(round(jd)-jd)
    if decimal>0 then 
    decimal=1-decimal
    endif
    b = jd-abs(decimal) // take integer part of jd
    jd = jd-b // subtract integer part to leave fractional part of original jd
    b = round(jd * 8) // scale fraction from 0-8 and round
    
    decay=0
    atr=AverageTrueRange[14](close)
    if b[decay]=0 and b[decay+1]<>0 then //new moon
    drawvline(barindex) coloured(200,0,0)
    drawtext("●",barindex,min(low-atr,lowest[20](low)),dialog,bold,30)
    endif
    if b[decay]=4 and b[decay+1]<>4 then //full moon
    drawvline(barindex) coloured(0,0,200)
    drawtext("“",barindex,max(high+atr,highest[20](high)),dialog,bold,30)
    endif
    return
    bolsatrilera and swapping thanked this post
    moon-phases-prorealtime.png moon-phases-prorealtime.png
    #94049 quote
    Hans63
    Participant
    Average

    Grazie Nicolas, però ci deve essere qualcosa di sbagliato nel codice che ti ho postato

    Se confronti in grafico di TradingView e l’indicatore che hai creato (vedi esempio qui sotto) sono  completamente sfasati

    Fasi-Lunari-1.png Fasi-Lunari-1.png Fasi-Lunari-2.png Fasi-Lunari-2.png
    #94052 quote
    Nicolas
    Keymaster
    Master

    Non ho il codice di tradingview, vero? Ho tradotto il codice JavaScript che mi hai dato. Ho fatto un paragone con questo sito per la fase lunare: https://www.timeanddate.com/moon/phases/e sembra piuttosto buono.
    Assicurati di aver attivato i dati del weekend per il calcolo da calcolare correttamente.

    #94053 quote
    Nicolas
    Keymaster
    Master

    Ho invertito la luna piena e il simbolo della luna nuova su e giù, questo nuovo codice è migliore:

    //moon phases
    
    iday=openday
    imonth=openmonth
    iyear=openyear
    
    if imonth<3 then
    iyear=iyear-1
    imonth=imonth+12
    endif
    
    imonth=imonth+1
    c = 365.25 * iyear
    e = 30.6 * imonth
    jd = c + e + iday - 694039.09 // jd is total days elapsed
    jd = jd/29.5305882 // divide by the moon cycle
    decimal=(round(jd)-jd)
    if decimal>0 then
    decimal=1-decimal
    endif
    b = jd-abs(decimal) // take integer part of jd
    jd = jd-b // subtract integer part to leave fractional part of original jd
    b = round(jd * 8) // scale fraction from 0-8 and round
    
    decay=0
    atr=AverageTrueRange[14](close)
    if b[decay]=0 and b[decay+1]<>0 then //new moon
    drawvline(barindex) coloured(0,0,200)
    drawtext("",barindex,max(high+atr,highest[20](high)),dialog,bold,30)
    endif
    if b[decay]=4 and b[decay+1]<>4 then //full moon
    drawvline(barindex) coloured(200,0,0)
    drawtext("",barindex,min(low-atr,lowest[20](low)),dialog,bold,30)
    endif
    return
    Hans63 thanked this post
    moon-phases-stock-chart.png moon-phases-stock-chart.png
    #94055 quote
    Hans63
    Participant
    Average

    Bravo e grazie Nicolas!

    Sembra quasi identico a quello di tradingview

    Nel frattempo avevo trovato l’indicatore per ninja trader, ho chiesto la demo e ho installato l’indicatore

    Ti allego lo script e il grafico

    Avevo trovato anche questo sito che indica esattamente le fasi lunari

    https://www.calendar-365.com/moon/moon-phases.html

    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion
    
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
        /// <summary>
    	/// 
    	/// Print the moon phase in a daily chart
    	/// version beta 0.02, to be debugged, need more options for text offset
    	/// November 30, 2009 by sam028 (http://forum.bigmiketrading.com)
        /// </summary>
        [Description("Print the moon phase in a daily chart")]
        public class MoonPhase : Indicator
        {
            #region Variables		
    		private bool found;
    		private Font textFont = new Font("Arial", 8, FontStyle.Regular);
    		private Color textColor = Color.Blue;
    		private float offset = 0;
    		
    		private bool debug=false;
    		
    
            #endregion
    
            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {
                CalculateOnBarClose	= true;
                Overlay				= false;
                PriceTypeSupported	= false;			
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
    			int m=moon_calc(Time[0].Year,Time[0].Month, Time[0].Day);
    			
    			if (m==0){
    				if (found == false){
    					if (debug)
    						Print("New moon on "+Time[0].Month+" "+Time[0].Day);					
    			DrawText("New"+CurrentBar,true, "New moon",0, High[0]+20 * TickSize,0,textColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
    					found = true;
    		   
    				}
    			}
    			else {
    				if (m==4){
    					if (found == false){
    						if (debug)
    							Print("Full moon on "+Time[0].Month+" "+Time[0].Day);
    						DrawText("Full"+CurrentBar,true,"Full moon",0,Low[0] - 20 * TickSize,0, textColor, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0);
    						found = true;
    					}
    				}
    				else {					
    					found=false;
    				}		
    			}
            }
    		
    		protected int moon_calc(int year, int month, int day){
    			int g, e;
    			if (month == 1){
    				--day;
    			}
    			else {
    				if (month == 2) {
    					day += 30;
    				}
    				else {
    					day += 28 + (month-2)*3059/100;
    					if ((year & 3)==0) 
    						++day;
            			if ((year%100) == 0) 
    						--day;
    				}
    			}			
        		g = (year-1900)%19 + 1;
        		e = (11*g + 18) % 30;
        		if ((e == 25 && g > 11) || e == 24) 
    				e++;
        		return ((((e + day)*6+11)%177)/22 & 7);									
    		}
            #region Properties
    		[Description("Fonts.")]
            [Category("Fonts")]
    		[XmlIgnore()]
            public Font TextFont
            {
                get { return textFont; }
                set { textFont = value; }
            }
    		[Browsable(false)]
    		public string TextFontSerialize
    		{
    			get { return NinjaTrader.Gui.Design.SerializableFont.ToString(textFont); }
    			set { textFont = NinjaTrader.Gui.Design.SerializableFont.FromString(value); }
    		}
    		
    		
    		[XmlIgnore()]
    		[Description("TextColor")]
    		[Category("Colors")]	
    		public Color TextColor
    		{
    			get { return textColor; }
    			set { textColor = value; }
    		}
    		[Browsable(false)]
    		public string TextColorSerialize
    		{
    			get { return NinjaTrader.Gui.Design.SerializableColor.ToString(textColor); }
    			set { textColor = NinjaTrader.Gui.Design.SerializableColor.FromString(value); }
    		}	
    		
    		[Description("Offset for text.")]
            [Category("Display")]
            public float Offset
            {
                get { return offset; }
                set { offset = value; }
            }				
    		
            #endregion
        }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
        public partial class Indicator : IndicatorBase
        {
            private MoonPhase[] cacheMoonPhase = null;
    
            private static MoonPhase checkMoonPhase = new MoonPhase();
    
            /// <summary>
            /// Print the moon phase in a daily chart
            /// </summary>
            /// <returns></returns>
            public MoonPhase MoonPhase()
            {
                return MoonPhase(Input);
            }
    
            /// <summary>
            /// Print the moon phase in a daily chart
            /// </summary>
            /// <returns></returns>
            public MoonPhase MoonPhase(Data.IDataSeries input)
            {
                if (cacheMoonPhase != null)
                    for (int idx = 0; idx < cacheMoonPhase.Length; idx++)
                        if (cacheMoonPhase[idx].EqualsInput(input))
                            return cacheMoonPhase[idx];
    
                lock (checkMoonPhase)
                {
                    if (cacheMoonPhase != null)
                        for (int idx = 0; idx < cacheMoonPhase.Length; idx++)
                            if (cacheMoonPhase[idx].EqualsInput(input))
                                return cacheMoonPhase[idx];
    
                    MoonPhase indicator = new MoonPhase();
                    indicator.BarsRequired = BarsRequired;
                    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
                    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
                    indicator.Input = input;
                    Indicators.Add(indicator);
                    indicator.SetUp();
    
                    MoonPhase[] tmp = new MoonPhase[cacheMoonPhase == null ? 1 : cacheMoonPhase.Length + 1];
                    if (cacheMoonPhase != null)
                        cacheMoonPhase.CopyTo(tmp, 0);
                    tmp[tmp.Length - 1] = indicator;
                    cacheMoonPhase = tmp;
                    return indicator;
                }
            }
        }
    }
    
    // This namespace holds all market analyzer column definitions and is required. Do not change it.
    namespace NinjaTrader.MarketAnalyzer
    {
        public partial class Column : ColumnBase
        {
            /// <summary>
            /// Print the moon phase in a daily chart
            /// </summary>
            /// <returns></returns>
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.MoonPhase MoonPhase()
            {
                return _indicator.MoonPhase(Input);
            }
    
            /// <summary>
            /// Print the moon phase in a daily chart
            /// </summary>
            /// <returns></returns>
            public Indicator.MoonPhase MoonPhase(Data.IDataSeries input)
            {
                return _indicator.MoonPhase(input);
            }
        }
    }
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        public partial class Strategy : StrategyBase
        {
            /// <summary>
            /// Print the moon phase in a daily chart
            /// </summary>
            /// <returns></returns>
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.MoonPhase MoonPhase()
            {
                return _indicator.MoonPhase(Input);
            }
    
            /// <summary>
            /// Print the moon phase in a daily chart
            /// </summary>
            /// <returns></returns>
            public Indicator.MoonPhase MoonPhase(Data.IDataSeries input)
            {
                if (InInitialize && input == null)
                    throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
    
                return _indicator.MoonPhase(input);
            }
        }
    }
    #endregion
    
    Fasi-Lunari-Ninja-Trader.png Fasi-Lunari-Ninja-Trader.png
    #94058 quote
    Hans63
    Participant
    Average
    #150177 quote
    giadelo
    Participant
    Junior

    Ho caricato l’indicatore ma non indica la fase lunare se capita durante i giorni festivi. Che cosa devo modificare nel codice?

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.

Fasi Lunari (Moon Phases)


ProBuilder: Indicatori & Strumenti Personalizzati

New Reply
Author
author-avatar
Hans63 @hans63 Participant
Summary

This topic contains 10 replies,
has 3 voices, and was last updated by giadelo
5 years, 4 months ago.

Topic Details
Forum: ProBuilder: Indicatori & Strumenti Personalizzati
Language: Italian
Started: 03/19/2019
Status: Active
Attachments: 6 files
Logo Logo
Loading...