Announcement

Collapse
No announcement yet.

Dynamic, backadjusting study with different scaling than price chart supported?

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

  • Dynamic, backadjusting study with different scaling than price chart supported?

    The current study I'm doing is only generated on the last bar. When a new bar comes in, the study redraws all indicators on previous bars. Where indicators are drawn on past bars can be changed because of the new bar.

    This all works more-or-less fine with a price study using the drawing functions.

    However, because my study is complex and has multiple stages, I need to "link" studies. Basically, the price study above determines a recent range of the chart that needs further analysis. (I can pass this info via a global variable, so that should be ok.) At this point I really need a non-price study that isn't scaled the same as the price chart.

    Problem is, I can't figure out how to back-adjust a non-price study and none of the drawing functions work either.

    What I really need is all the functionality of a price study (changing stuff on previous "bars" and support for drawing functions anywhere on the study), but in a window separate from the price window and with it's own scaling. (The non-price study seems awfully rigid.)

    Or, am I missing something? (Based on recent posts, it seems I'm not the only person doing these types of "adjust the indicator in the past based on the latest bar" studies.)
    Last edited by shortandlong; 11-04-2009, 12:16 PM.

  • #2
    At least I figured out how to back-adjust a non-price study (using data from another study):

    PHP Code:
    /*
     *  Gets 2 global arrays from another study and plots them 
     *  in this non-price study.
     *
     *  Global arrays must be set from another study, most likely 
     *  a price study.  For example:
     *
     *     var aGlobalArray0 = new Array (1, 2, 3);
     *     setGlobalValue ("aGlobalArray0", aGlobalArray0);
     *
     *     var aGlobalArray0 = new Array (7, 8, 9, 10, 11);
     *     setGlobalValue ("aGlobalArray1", aGlobalArray1);
     *
     *  Plotting of global arrays is ONLY done when a bar index 
     *  of -1 is received.  Plotting is always done on the last bar 
     *  and plot values are back-adjusted.
     */

    function preMain(){
        
    setPriceStudy (false);

        
    setPlotType (PLOTTYPE_LINE);
       
        
    setDefaultBarFgColor (Color.red0);
        
    setDefaultBarFgColor (Color.lime1);

        
    // we want complete bars only:
        
    setComputeOnClose();    // don't send main() BARSTATE_CURRENTBAR or bar index 0
    }

    function 
    main(){
        var 
    currentBarIndex getCurrentBarIndex();

        
    // Only do real processing on the last bar:
        
    if (currentBarIndex == -1) {
            
            var 
    aGlobalArray0 getGlobalValue ("aGlobalArray0");
            var 
    aGlobalArray1 getGlobalValue ("aGlobalArray1");
            if (
    aGlobalArray0 == null) {
                
    debugPrintln ("aGlobalArray0 == null");
                return;
            }
            if (
    aGlobalArray1 == null) {
                
    debugPrintln ("aGlobalArray1 == null");
                return;
            }

            
    // (Separate loops to allow for global arrays that are 
            // a different size.)
            
    for (0aGlobalArray0 .lengthi++) {
                
    setBar (Bar.Value, -i0aGlobalArray0 [i]);
            }
            
            for (
    0aGlobalArray1 .lengthi++) {
                
    setBar (Bar.Value, -i1aGlobalArray1 [i]);
            }
        }

        
    // Do a "proper return" for all bars:
        
    if (currentBarIndex != -1
            
    // setBar() won't change a bar that hasn't be set by a 
            // return from main(), so make it think it's been set before:
            
    return (new Array (nullnull))
        else
            
    // Got these values, return them since values passed 
            // to setBar() may not take effect.
            
    return (new Array (aGlobalArray0 [0], aGlobalArray1 [0]))

    Last edited by shortandlong; 11-06-2009, 01:58 PM.

    Comment


    • #3
      Even though I don't need it, turns out the return from main() is the "trick" to get everything working. With it the drawing functions work (at least line and image drawing) on a non-price study.

      So, basically if you're doing a non-price study and only want to update the entire study (all bars) only on the last bar then use setBar() and do your return from main() something like:

      PHP Code:
      .
          
      // one element for each plot, two in this example
          
      if (getCurrentBarIndex() != -1)
              return (new Array (
      nullnull));
          else
              return (new Array (
      lastPlot0ValuelastPlot1Value
      Haven't tried it, but for a single plot value, the following would probably work:

      PHP Code:
      .
          if (
      getCurrentBarIndex() != -1)
              return 
      null;
          else
              return 
      lastPlotValue
      Last edited by shortandlong; 11-06-2009, 02:36 PM.

      Comment

      Working...
      X