Announcement

Collapse
No announcement yet.

Writing in a file and the bar-index process

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

  • Writing in a file and the bar-index process

    Hi,
    there is a problem I can't solve. I wrote a formula with many variables calculated not to display them but to write them into a file.
    I have a difficulty to place code line (writing this file) within the code of the main function.
    some explanations :
    The variables I want to calculate must be evaluated at some very precise moment (very precise bar-index) which depends on some logical conditions. And I don't know in advance the indexes for each variable. Furthermore all the variables don't have the same indexes.
    Do I have to write each variable separately or at the same time ?
    I tried at the same time and it didn't work. Precisely, I tried to write at the end of the loop (after the last bar). But how can I know I am at the end of the loop ?

    Thanks in advance for any help.

    see below summary of my code :
    PHP Code:
    function preMain(){
        
    setPriceStudy(true);
        
    setStudyTitle("writing of level");
        
    setShowTitleParameters(false);
    }
    var 
    R3_D null;
    var 
    R2_D null;
    var 
    R1_D null;
    var 
    PP_D null;
    var 
    S1_D null;
    var 
    S2_D null;
    var 
    S3_D null;

    var 
    R3_W null;
    var 
    R2_W null;
    var 
    R1_W null;
    var 
    PP_W null;
    var 
    S1_W null;
    var 
    S2_W null;
    var 
    S3_W null;

    var 
    R3_M null;
    var 
    R2_M null;
    var 
    R1_M null;
    var 
    PP_M null;
    var 
    S1_M null;
    var 
    S2_M null;
    var 
    S3_M null;

    var 
    IdxD 0;
    var 
    IdxW 0;
    var 
    IdxM 0;
    var 
    bRealTimefalse;

    var 
    Valeur null;
    var 
    precis 5;

    function 
    main(){
       var 
    nBarIndex getCurrentBarIndex();
       var 
    nBarState getBarState();

        if(
    getBarStateInterval("d") == BARSTATE_NEWBAR){
            
    IdxD nBarIndex;
            
    H_D high(-1,inv("d"));
            
    L_D low(-1,inv("d"));
            
    C_D close(-1);
        }

        if(
    getBarStateInterval("w") == BARSTATE_NEWBAR){
            
    IdxW nBarIndex;
            
    H_W high(-1,inv("w"));
            
    L_W low(-1,inv("w"));
            
    C_W close(-1);
        }

        if(
    getBarStateInterval("m") == BARSTATE_NEWBAR){
           
    IdxM nBarIndex;
            
    H_M high(-1,inv("m"));
            
    L_M low(-1,inv("m"));
            
    C_M close(-1);
        }

        if(
    C_D == null || C_W == null || C_M == null) { return; }
        if(
    nBarIndex == && nBarState == BARSTATE_CURRENTBAR){ bRealTimetrue; }
        if(
    bRealTime && nBarState == BARSTATE_NEWBAR){
            
    IdxD--;
            
    IdxW--;
            
    IdxM--;
        }

    // calculus
        // daily
        
    PP_D = (H_D L_D C_D) / 3;
        
    R1_D 2*PP_D L_D;
        
    S1_D 2*PP_D H_D;
        
    R2_D PP_D + (H_D L_D);
        
    S2_D PP_D - (H_D L_D);
        
    R3_D 2*(PP_D L_D) + H_D;
        
    S3_D 2*(PP_D H_D) + L_D;

        
    // weekly
        
    PP_W = (H_W L_W C_W) / 3;
        
    R1_W 2*PP_W L_W;
        
    S1_W 2*PP_W H_W;
        
    R2_W PP_W + (H_W L_W);
        
    S2_W PP_W - (H_W L_W);
        
    R3_W 2*(PP_W L_W) + H_W;
        
    S3_W 2*(PP_W H_W) + L_W;

        
    //Monthly
        
    PP_M = (H_M L_M C_M) / 3;
        
    R1_M 2*PP_M L_M;
        
    S1_M 2*PP_M H_M;
        
    R2_M PP_M + (H_M L_M);
        
    S2_M PP_M - (H_M L_M);
        
    R3_M 2*(PP_M L_M) + H_M;
        
    S3_M 2*(PP_M H_M) + L_M;

        
    // file open ...
            
    var Monfichier = new File("LeFichier.txt");
            
    Monfichier.open"at" );

        
    //writing of the first variable
            
    Valeur PP_M1.tofixed(precis);
            
    Monfichier.writeln("PP_M1 =" Valeur);
         
        
    //writing of the second variable
            
    Valeur R1_M1.tofixed(precis);
            
    Monfichier.writeln("R1_M1 =" Valeur);
    etc...
        
    Monfichier.close()

    Mermoz.

  • #2
    You can write the text any way you want. The way you're doing it from your example, you'll end up with a text file like..

    PP_M1 = 102.34456
    R1_M1 = 101.12335

    If this is your objective, then you seem to have accomplished your goal.

    To accomplish the "condition" of when to write the data file (text file), then you need to create some method of enclosing your FILE WRITE function into your condition... like...

    PHP Code:
    if (getBarState() == BARSTATE_NEWBAR) {
      
    //  New bar - time to write data to a file

       
    ..  Add your file write function/actions here.

    Without knowing exactly how and when you want to write the data to the file, it's difficult for me to directly assist you.

    What I can tell you is the way you have shown your code, you are writing the file every bar (in historical mode) and every TICK (in realtime mode). This is probably not what you wanted to accomplish.
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment

    Working...
    X