Announcement

Collapse
No announcement yet.

Date Object Problems

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Date Object Problems

    I am having some real problems with the Date object. I am trying to print a red circle where my I have placed a trade. In this case at the price of 1290.5 at 12:36:13 on 23rd February 2006. The debug values I get are completely different to my dates and the trade plotted on the chart is completely different to my debug and input dates. Please help!!

    Here is my code.
    PHP Code:
    /*                           
        Place trades on chart    
                                */


    function preMain()
    {
        
    setPriceStudy(true);
        
    setStudyTitle("Trades");
        
    setComputeOnClose();
        
    debugClear();
    }

    function 
    main()
    {    
        
    //print trade on 23rd February 2006 at 12:36:13 at a price of 1290.5
        
    tradeBar(2006,2,23,12,36,13,1290.5);    
    }

    var 
    a=0;

    function 
    tradeBar(y,m,d,h,m,s,price)
    {
        var 
    tradeDate = new Date(y,m-1,d,h,m,s);
        
        if (
    a==0)
        {
            
    debugPrintRawtime(tradeDate,"trade");
            
    a=1;
        }
            
        var 
    tradeTime tradeDate.getTime()/1000;
        var 
    prevBarTime getValue("rawtime",-1);
        var 
    thisBarTime getValue("rawtime",0);    
     
        
        if (
    tradeTime >= prevBarTime && tradeTime thisBarTime)
        {
            var 
    prevBarDate = new Date(prevBarTime);
            var 
    thisBarDate = new Date(thisBarTime);
            
    debugPrintRawtime(prevBarDate,"prev bar");
            
    debugPrintRawtime(thisBarDate,"this bar");
            
    drawShapeRelative(-1,price,Shape.CIRCLE,null,Color.red,Shape.BOTTOM Shape.ONTOP,"trade");    
        }    
    }

    function 
    debugPrintRawtime(rttext)
    {
        
    debugPrint(rt.getFullYear() + "/" rt.getMonth() + "/" rt.getDate() + " - " +
                   
    rt.getHours() + ":" rt.getMinutes() + ":" rt.getSeconds() + " - " text +"\n");

    The output I get is this



    and the output to screen i get is this


  • #2
    Hi nitinparmar,

    When plotting trades on the chart, you have to be aware that bar-time and the date object can differ for a number of reasons. First, looking at your code, the time in miliseconds that you are using is gmt time. If you use that to create a date object, it will be off based on your offset to GMT. Further, when you start converting using dates, hours,secods, etc, it becomes even more tricky, as you have noted.

    My advice to you is to record the milisecond value of the trade, then when indexing, index against the milisecond value associated with the bar on the chart obtained using rawtime().

    This has worked well for me and there is a working example of how I indexed saved data back into a loading chart using this technique here, although it is will not be very easy to extract. The section(s) used starts at around step 457. I used the miliseconds of the saved data and associated it with a bar in the chart using the rawtime() of the charts bar.

    There is another efs that plots trades using button clicks at this link. This may also help.

    Comment


    • #3
      Hi Steve,

      Thanks for your help. Unfortunately I only have the trade time as hh:mm:ss as I get it from my broker's executed trades sheet. I am planning to write the study so it will eventually read in this sheet and shred the trade time from it. If that is impossible then I will write a Java or C++ program to generate a .efs file for each day.

      Nitin
      Last edited by nitinparmar; 02-25-2006, 02:45 AM.

      Comment


      • #4
        Hi Nitin:

        You were almost there... you just need to correctly convert between rawtime and the date object as in the following code (note the 2 new functions):

        PHP Code:
        /*                           
            Place trades on chart    
                                    */


        function preMain()
        {
            
        setPriceStudy(true);
            
        setStudyTitle("Trades");
            
        setComputeOnClose();
            
        debugClear();
        }

        function 
        main()
        {    
            
        //print trade on 23rd February 2006 at 12:36:13 at a price of 1290.5
            
        tradeBar(2006,2,23,12,36,13,1290.5);    
        }

        var 
        a=0;

        //note fix to parameters..originally you were using "m" twice
        function tradeBar(y,m,d,h,mn,s,price)
        {
            var 
        tradeDate = new Date(y,m-1,d,h,mn,s);
            
            if (
        a==0)
            {
                
        debugPrintRawtime(tradeDate,"trade");
                
        a=1;
            }
                
            var 
        tradeTime DateToRawtimetradeDate );        
                
            var 
        prevBarTime getValue("rawtime",-1);
            var 
        thisBarTime getValue("rawtime",0);    
         
            
            if (
        tradeTime >= prevBarTime && tradeTime thisBarTime)
            {
                
                var 
        prevBarDate RawtimeToDateprevBarTime );
                var 
        thisBarDate RawtimeToDatethisBarTime );
                
                
        debugPrintRawtime(prevBarDate,"prev bar");
                
        debugPrintRawtime(thisBarDate,"this bar");
                
        drawShapeRelative(-1,price,Shape.CIRCLE,null,Color.red,Shape.BOTTOM Shape.ONTOP,"trade");    
            }    
        }

        function 
        debugPrintRawtime(rttext)
        {
            
        debugPrint(rt.getFullYear() + "/" rt.getMonth() + "/" rt.getDate() + " - " +
                       
        rt.getHours() + ":" rt.getMinutes() + ":" rt.getSeconds() + " - " text +"\n");
        }

        function 
        DateToRawtime_date ) {
        var 
        aa = new Date"January 1, 1970" );
        var 
        bb _date;
        var 
        ccdd;

            if ( 
        bb=="Invalid Date" ) return( null );

            
        cc aa.getTime()/1000;
            
        dd bb.getTime()/1000;
            
            return( (
        dd-cc) );
            
        }

        function 
        RawtimeToDate_rt ) {
        var 
        aa = new Date"January 1, 1970" );
        var 
        ccdd;

            if ( 
        _rt==null ) return( null );

            
        cc aa.getTime();
            
        dd _rt*1000;
            
        aa.setTimecc+dd );
            
            return( 
        aa );

            


        Chris
        Last edited by ckryza; 02-25-2006, 05:59 AM.

        Comment


        • #5
          Chris,

          Thank you very much. That works very well. I am in the UK and my charting PC is on Central European Time. Your code fix works perfectly on all charts now.

          Nitin

          Comment

          Working...
          X