Announcement

Collapse
No announcement yet.

Moving Average of Custom Study of Higher Time Frame displayed on lower time frame

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

  • Moving Average of Custom Study of Higher Time Frame displayed on lower time frame

    I am trying to display and utilize for a strategy a moving average of a custom study using a higher time interval. Specifically, I am trying to display a 30min custom stochastic on a 15 min chart. I want this 30min indicator to match the value on the 30min chart.

    here is the code i have so far:
    PHP Code:
    var xStochD null;
    var 
    xSlowStochD null;
    var 
    bInit false;

    var 
    KLength 14;
    var 
    KSmoothing 1;
    var 
    DSmoothing 3;

    function 
    main(SymbolInterval) {

        
    Interval 30;

        if(
    bInit == false){
            if(
    Symbol == nullSymbol getSymbol();
            if(
    Interval == nullInterval getInterval();
            
    xStochD efsExternal("customStochD.efs"KLength,KSmoothing,DSmoothing,Symbol,Interval);
            
    bInit true;
        }
        
        var 
    smoothingFactor DSmoothing 1;
        if (
    xSlowStochD == nullxSlowStochD ema(smoothingFactor,xStochD);
        
        return 
    xSlowStochD.getValue(0);

    When this runs on the 15 min chart, the ema is updated every 15 min rather than every 30 min which I believe causes the discrepancy. I tried to use the sym(xStochD +","+Interval), but that doesnt seem to work.

  • #2
    twannster
    In order for the external efs to run in the context of the interval or symbol that is being passed to it you must use either the inv() or sym() function and this needs to be the last parameter of the efsExternal() call (see the respective links for the syntax of these functions)
    In the case of your script you would need to modify it as follows
    PHP Code:
    if(bInit == false){
            if(
    Symbol == nullSymbol getSymbol();
            if(
    Interval == nullInterval getInterval();
            
    xStochD efsExternal("customStochD.efs"KLengthKSmoothingDSmoothingsym(Symbol+","+Interval));        
            
    bInit true;

    The next thing you may want to do is also initialize Wilder's smoothing average inside the bInit statement as follows
    PHP Code:
    if(bInit == false){
            if(
    Symbol == nullSymbol getSymbol();
            if(
    Interval == nullInterval getInterval();
            
    xStochD efsExternal("customStochD.efs"KLengthKSmoothingDSmoothingsym(Symbol+","+Interval));        
            
    xSlowStochD ema((DSmoothing 1), xStochD);
            
    bInit true;

    Lastly if you want the plot to be synchronized when running the formula in real time on an external interval (or symbol) you may want to replace
    return xSlowStochD.getValue(0);
    with
    return getSeries(xSlowStochD);
    Enclosed is a revision of your script that implements these suggestions and a screenshot showing the efs running on a 5 minute chart but computing the study on the 30 minute data.
    Alex

    PHP Code:
    var xStochD null;
    var 
    xSlowStochD null;
    var 
    bInit false;

    var 
    KLength 14;
    var 
    KSmoothing 1;
    var 
    DSmoothing 3;

    function 
    main(SymbolInterval) {
        
        if(
    bInit == false){
            if(
    Symbol == nullSymbol getSymbol();
            if(
    Interval == nullInterval getInterval();
            
    xStochD efsExternal("customStochD.efs"KLengthKSmoothingDSmoothingsym(Symbol+","+Interval));        
            
    xSlowStochD ema((DSmoothing 1), xStochD);
            
    bInit true;
        }
         
        return 
    getSeries(xSlowStochD);

    Comment


    • #3
      Thank you very much.

      Comment


      • #4
        I'm not familiar with the EfsExternal capability but interested in doing something similiar to what the last post addressed.

        I received and error that customStochD.efs was not found when I tried to run this script.

        Does this script have to be created first for the script that Alexis provided to run?

        thanks
        Glen Demarco
        [email protected]

        Comment


        • #5
          twannster
          You are most welcome
          Alex

          Comment


          • #6
            demarcog
            The efsExternal() function (see the link for a brief description and syntax) allows you to call an external efs and retrieve the value (or values) returned by that efs. In the process it creates a series of that value (or values) which can be used either on its own just like a built-in study or as a source for other studies or as a study to which you pass another series as the source.
            For some detailed examples on how efsExternal() can be used see this and this thread.
            The reason you are receiving an error when running the script posted in the prior message is because you do not have an efs called customStochD in the same folder in which that script has been saved. To see how that efs works save the script enclosed to this message in the same folder and call it customStochD.efs. Note that this is just an example and is not necessarily representative of the formula that is actually called in twannster's case.
            Alex

            PHP Code:
            var Study null;
             
            function 
            main(klength,ksmooth,dlength,symbol) {
             
                if(
            klength==nullklength 14;
                if(
            ksmooth==nullksmooth 1;
                if(
            dlength==nulldlength 3;
                
                if(
            Study==nullStudy stochD(klength,ksmooth,dlength);
             
                return 
            Study.getValue(0)

            Comment

            Working...
            X