Announcement

Collapse
No announcement yet.

Realtime re-calculation of indicator

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

  • Realtime re-calculation of indicator

    Hello,

    I have an indicator that I have written, it works very well for historic data, but does not recalculate correctly when trading realtime. how do I have it recalculate everytime a new tick comes into the current bar? The code is attached.

    Thanks for your help.
    Attached Files

  • #2
    Re: Realtime re-calculation of indicator

    Greg
    The reason for the behavior you are seeing is essentially because you are not accounting for the fact that your conditions may evaluate to true at some point intrabar but then may no longer be true during the same bar or at its completion [which is why it works fine on historical bars where you are only evaluating completed bars]
    The simplest solution would be to set your formula to compute only on completed bars by adding a setComputeOnClose() statement in preMain. The disadvantage of this solution is that you do not have any values on the most current bar.
    The alternative is to revise the logic to account for when neither of your two conditions is true eg
    PHP Code:
    //your code
    if(myFirstCondition){ //if my first condition is true
        
    myVar1 a+b//calculate current value of myVar1
        
    myVar2 x-z//calculate current value of myVar2
    }
    else if (
    mySecondConition){ //else if my second condition is true
        
    myVar1 a-b//calculate current value of myVar1
        
    myVar2 x+z//calculate current value of myVar2
    }
    else { 
    //else [ie if neither is true]
        
    myVar1 myPrevVar1//assign to myVar1 its previous value
        
    myVar2 myPrevVar2//assign to myVar2 its previous value
    }
    //rest of your code 
    In order to do this however some things need to happen in a specific order. First all the variables [ie myVar1, myPrevVar1, etc] need to be global so that they maintain their values between iterations of the efs and secondly you need to transfer the values between these variables once only at the beginning of each bar and prior to calculating the new values for the current bar.
    Note that in the case of your script you will also need to do this for the color of the plots [since you are setting them as defaults] else they will be set intrabar while the condition is true but will not be reset if the condition is no longer true
    So the above example would now be as follows
    PHP Code:
    var myVar1 0//global variable
    var myPrevVar1 0//global variable
    var myColor //your choice of color
    var myPrevColor //your choice of color
    //other global variables as necessary

    function main(){

        
    //initialize your variables myVar1, myPrevVar1, etc

        
    if(getBarState()==BARSTATE_NEWBAR){ //on the first tick of each new bar
            
    myPrevVar1 myVar1//assign to myPrevVar1 the last calculated value of myVar1
            
    myPrevColor myColor//as above
            //other assignements as necessary
        
    }
        
    //notice that we are performing the above assignements prior to calculating the current values of those variables
        
        
    if(myFirstCondition){ //if my first condition is true
            //same as example above
            
    myColor color.lime;
            
    setDefaultBarFgColor(myColor);
        }
        else if (
    mySecondCondition){
            
    //same as example above
            
    myColor color.red;
            
    setDefaultBarFgColor(myColor);
        }
        else{
            
    //same as example above
            
    myColor myPrevColor//assign the previous bar's color to myColor
            
    setDefaultBarFgColor(myColor); 
        }
        
    //rest of your code 
    In addition to the above you will also need to correct a logic error in your condition ie
    PHP Code:
    if(close(0)>=(RenkoTop+BoxSize)){ 
    which needs to be
    PHP Code:
    if(close(0)>=(Previous_RenkoTop+BoxSize)){ 
    else you would be evaluating the condition on the values calculated on the current bar. You will need to adjust also the second condition in a similar way
    Once you modify your script along the lines I suggested it should work correctly and you should see the plots adjust dynamically in real time as the conditions flip between true and false on the same bar.
    Alex


    Originally posted by Gkrabbe
    Hello,

    I have an indicator that I have written, it works very well for historic data, but does not recalculate correctly when trading realtime. how do I have it recalculate everytime a new tick comes into the current bar? The code is attached.

    Thanks for your help.

    Comment

    Working...
    X