Announcement

Collapse
No announcement yet.

Simultaneously create an Array while reading another?

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

  • Simultaneously create an Array while reading another?

    Maybe someone can help with this? My question is within the scripting. Thank you beforehand.

    PHP Code:
    debugClear();

        var 
    BarCntr 0;
        var 
    TradeCntr 0;
        
        var 
    nLong 0;
        var 
    nShort 0;
        
        var 
    SsT 0;
        
    function 
    preMain() {

        
    setPriceStudy(false);

        
    setDefaultBarStyle(PS_SOLID0);
        
    setDefaultBarStyle(PS_SOLID1);
        
    setDefaultBarStyle(PS_SOLID2);
        
    setDefaultBarStyle(PS_SOLID3);
        
    setDefaultBarStyle(PS_SOLID4);
        
    setDefaultBarStyle(PS_SOLID5);

        
    setDefaultBarFgColor(Color.green0);
        
    setDefaultBarFgColor(Color.navy1);
        
    setDefaultBarFgColor(Color.maroon2);
        
    setDefaultBarFgColor(Color.lime3);
        
    setDefaultBarFgColor(Color.blue4);
        
    setDefaultBarFgColor(Color.red5);

        
    setDefaultBarThickness(10);
        
    setDefaultBarThickness(11);
        
    setDefaultBarThickness(12);
        
    setDefaultBarThickness(13);
        
    setDefaultBarThickness(14);
        
    setDefaultBarThickness(15);

        
    setPlotType(PLOTTYPE_LINE0);
        
    setPlotType(PLOTTYPE_LINE1);
        
    setPlotType(PLOTTYPE_LINE2);
        
    setPlotType(PLOTTYPE_LINE3);
        
    setPlotType(PLOTTYPE_LINE4);
        
    setPlotType(PLOTTYPE_LINE5);
        }

    function 
    main(){

    debugClear();

        var 
    i;
        var 
    j;
        var 
    = new Array();
        var 
    = new Array();
        var 
    = new Array();
        var 
    = new Array();
        
    ////////////This section creates 3 pairs of indexed averages////////
    ////////////at three different lengths/////////////

    for( 033<= 13i++, ){ 

        
    C[i] = sma(j,close(),0);
        
    O[i] = sma(j,open(),0);
        
        }    
        
    ////////////////////////////////////////////////////////////////////

    /////////This section recalls the pairs of elements,
    /////////does the calculation if the condition is met,
    /////////and simultaneously should create a new X[k] Array?//////


        
    var k;
        
        for( 
    0k++ ){
        
        if( 
    C[k] > O[k] ){
        
    SsT += close(0) - open(0);
               }
        
    X[k] = SsT;
        }
        
    ////////////////But when I recall the individual elements of the new Array, the values are identical?///

    debugPrintln(  X[2], "   "X[1], "   "X[0], "   "SsT );

        
    C.concat(O);
        
    return 
    A;



    ////////////////////////////////////////////////////////////////////////// 
    Here is the same script, but broken down to try to find the problem.

    PHP Code:
    /////////////////////////////////////////////////////////////////////////

    debugClear();

        var 
    BarCntr 0;
        var 
    TradeCntr 0;
        
        var 
    nLong 0;
        var 
    nShort 0;
        
        var 
    SsT 0;
        
    function 
    preMain() {

        
    setPriceStudy(false);

        
    setDefaultBarStyle(PS_SOLID0);
        
    setDefaultBarStyle(PS_SOLID1);
        
    setDefaultBarStyle(PS_SOLID2);
        
    setDefaultBarStyle(PS_SOLID3);
        
    setDefaultBarStyle(PS_SOLID4);
        
    setDefaultBarStyle(PS_SOLID5);

        
    setDefaultBarFgColor(Color.green0);
        
    setDefaultBarFgColor(Color.navy1);
        
    setDefaultBarFgColor(Color.maroon2);
        
    setDefaultBarFgColor(Color.lime3);
        
    setDefaultBarFgColor(Color.blue4);
        
    setDefaultBarFgColor(Color.red5);

        
    setDefaultBarThickness(10);
        
    setDefaultBarThickness(11);
        
    setDefaultBarThickness(12);
        
    setDefaultBarThickness(13);
        
    setDefaultBarThickness(14);
        
    setDefaultBarThickness(15);

        
    setPlotType(PLOTTYPE_LINE0);
        
    setPlotType(PLOTTYPE_LINE1);
        
    setPlotType(PLOTTYPE_LINE2);
        
    setPlotType(PLOTTYPE_LINE3);
        
    setPlotType(PLOTTYPE_LINE4);
        
    setPlotType(PLOTTYPE_LINE5);
        }

    function 
    main(){

    debugClear();

        var 
    i;
        var 
    j;
        var 
    = new Array();
        var 
    = new Array();
        var 
    = new Array();
        var 
    = new Array();

    ///////////////This is the Same./////////////////////////////
     
    for( 033<= 13i++, ){ 

        
    C[i] = sma(j,close(),0);
        
    O[i] = sma(j,open(),0);
        
        }    
        
    ////////////////////////////////////////////////////////////////////

    /////////If I do the same thing, but break down the k-loop to find the problem
    /////////and manualy enter values for k, I do get the different values I am looking for  to put into a X[k] Array. 

    ///Here I can substitute the other element indexes 0 and 1 for k, and get the different values I would like to Array.///
        
    var 2

        if( 
    C[k] > O[k] ){
        
    SsT += close(0) - open(0);
               }
        
    X[k] = SsT;
        
    debugPrintln(   X[k], "   "SsT );

        
    C.concat(O);
        
    return 
    A;


    Why won't the first script array these values?
    I have tried for a day now, if any one could help, it is appreciated.

    Thank you again,
    Robert
    Last edited by robertkovari; 05-12-2006, 06:59 PM.

  • #2
    you are not using the array functions...

    you are not using the array functions to populate the arrays. You are just assigning a null array a value.

    Here are a list of the array values..

    Array Methods:

    concat(array2) Concatenates array2 into the array object.

    Array a = new Array(1,2,3);

    Array b = new Array(4,5,6);

    a.concat(b);

    Contents of a is now: { 1, 2, 3, 4, 5, 6 }

    join(sSeparator) Returns a string consisting of all the elements concatenated together.

    Array a = new Array(1,2,3);

    var s = a.join(",");

    /* s = "1,2,3" */

    length Returns the number of elements in the array.

    Array a = new Array(1,2,3,9,2);

    var len = a.length;

    /* length == 5 */

    pop() Removes the last element from the array and returns that element. This method changes the length of the array.

    Array a = new Array(1,2,3);

    var s = a.pop();

    /* s = "3" and Array a now contains (1,2)*/

    push() Adds one or more element to the end of an array and returns the new length of the array. This method changes the length of the array.

    Array a = new Array(1,2,3);

    var s = a.push(6,7);

    /* s = "7" and Array a now contains (1,2,3,6,7)*/

    shift() Removes the first element from an array and returns that element. This method changes the length of the array.

    Array a = new Array(1,2,3);

    var s = a.shift();

    /* s = "1" and Array a now contains (2,3)*/

    unshift() Adds one or more elements to the beginning of the array and returns the new length of the array.

    Array a = new Array(1,2,3);

    var s = a.unshift(0);

    /* s = "4" and Array a now contains (0,1,2,3)*/

    reverse() Reverses all the objects in the array object.

    Array a = new Array(1,2,3,4,5);

    /* Before Reverse, { 1,2,3,4,5} */

    a.reverse();

    /* After Reverse, { 5,4,3,2,1 } */

    slice(nStartIndex, [nEndIndex]) Returns a section of the array from nStartIndex to (optionally) nEndIndex.

    Array src = new Array(1,2,3,4,5,6);

    Array dest = src.slice(2,4);

    /* Contents of dest: { 3,4,5 } */

    splice(nStartIndex, Quantity, [item1, ..., itemN]) Changes the contents of an array, adding new elements while removing old elements.

    Array src = new Array(1,2,3,4,5,6);

    var s = src.splice(2, 0, 9);

    /* Contents of src: {1,2,9,3,4,5,6);

    sort(sortFunction) Sorts the array using sortFunction

    Array a = new Array(2,4,6,1,3,5);

    a.sort();

    /* Contents of a: { 1,2,3,4,5,6 } */

    If you supply a sort function, it must be defined as:

    function mySortFunction(firstarg, secondarg)

    Return a negative value if firstarg < secondarg

    Return a positive value if firstarg > secondarg.

    Otherwise return 0.
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      Hello Robert,

      Your scripts are working as far as I can see. I don't see any problems with how you are using the arrays. I think you are not seeing the results properly because of the debugClear() statement you have inside main. This is only allowing you to see the value of the X array on the last bar of the chart. If the conditional statement in the k loop does not evaluate to true on the last bar on the chart then the last value of SsT (because its global) gets stuffed into all three elements of the X array. Try the code below and view the data results for each bar in the chart. You will see that on the historical bars where your condition inside the k loop is true for more than one iteration of the loop (i.e. where k=1 and 2), the values of the X array are not all the same value.



      PHP Code:
      debugClear();

      var 
      BarCntr 0;
      var 
      TradeCntr 0;
          
      var 
      nLong 0;
      var 
      nShort 0;
          
      var 
      SsT 0;
          
      function 
      preMain() {

          
      setPriceStudy(false);

          
      setDefaultBarStyle(PS_SOLID0);
          
      setDefaultBarStyle(PS_SOLID1);
          
      setDefaultBarStyle(PS_SOLID2);
          
      setDefaultBarStyle(PS_SOLID3);
          
      setDefaultBarStyle(PS_SOLID4);
          
      setDefaultBarStyle(PS_SOLID5);

          
      setDefaultBarFgColor(Color.green0);
          
      setDefaultBarFgColor(Color.navy1);
          
      setDefaultBarFgColor(Color.maroon2);
          
      setDefaultBarFgColor(Color.lime3);
          
      setDefaultBarFgColor(Color.blue4);
          
      setDefaultBarFgColor(Color.red5);

          
      setDefaultBarThickness(10);
          
      setDefaultBarThickness(11);
          
      setDefaultBarThickness(12);
          
      setDefaultBarThickness(13);
          
      setDefaultBarThickness(14);
          
      setDefaultBarThickness(15);

          
      setPlotType(PLOTTYPE_LINE0);
          
      setPlotType(PLOTTYPE_LINE1);
          
      setPlotType(PLOTTYPE_LINE2);
          
      setPlotType(PLOTTYPE_LINE3);
          
      setPlotType(PLOTTYPE_LINE4);
          
      setPlotType(PLOTTYPE_LINE5);
      }

      function 
      main(){

          
      //debugClear();

          
      var i;
          var 
      j;
          var 
      = new Array();
          var 
      = new Array();
          var 
      = new Array();
          var 
      = new Array();
          
      ////////////This section creates 3 pairs of indexed averages////////
      ////////////at three different lengths/////////////

          
      for( 033<= 13i++, ){ 
              
      C[i] = sma(j,close(),0);
              
      O[i] = sma(j,open(),0);
          }    
          
      ////////////////////////////////////////////////////////////////////

      /////////This section recalls the pairs of elements,
      /////////does the calculation if the condition is met,
      /////////and simultaneously should create a new X[k] Array?//////


          
      var k;
          
          for( 
      0k++ ){
              if( 
      C[k] > O[k] ){
                  
      SsT += close(0) - open(0);
                  
      debugPrintln(getCurrentBarIndex() + "  k: " " close is > open.  SsT= " SsT);
              }
              
      X[k] = SsT;
          }
          
      ////////////////But when I recall the individual elements of the new Array, the values are identical?///

          
      debugPrintln(getCurrentBarIndex() + "  " +  X[2] + " , " X[1] + " , " X[0] + " , SsT: " SsT );

          
      C.concat(O);
          
          return 
      A;

      I think you are dealing with a logic problem rather than a problem with the array object.
      Jason K.
      Project Manager
      eSignal - an Interactive Data company

      EFS KnowledgeBase
      JavaScript for EFS Video Series
      EFS Beginner Tutorial Series
      EFS Glossary
      Custom EFS Development Policy

      New User Orientation

      Comment


      • #4
        reloadEFS();?

        Jason and Doji,

        Thanks for your replies.

        I haven't had time to look at your suggestions yet, but I will soon. I am looking for a more desirable method than I am using now, but I think reloadEFS(); is the only route?

        Overall, I was trying to create an optimizer. The problem I was running into is, each bar on the chart starts the function main().
        So logically, I finally realized that I could use this fact as a built in loop. So now, I have my first pair of moving averages checked for return, reach the # of bars on the chart, increment the average(s), and then reloadEFS();. I do this for each pair of combinations of averages within a range of MA periods while keeping track of the returns for each pair as a seperate array sorting these elements for the highest.

        Thank you,

        Robert

        Comment

        Working...
        X