Announcement

Collapse
No announcement yet.

Random Entry Strategy

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

  • Random Entry Strategy

    Hi,

    I am totally new to EFS or any sort of Programming languages. I am trying to make a strategy for backtesting.
    The idea is this:
    Generate a random number 1 for Long and 0 for short
    Draw text above Bar, "1" for long, "0" for short
    Color Bar Green for Long and red for Short

    I came up with the Script below but I do not know how to make it work. Anybody please help. Thanks. TCW

    function preMain() {
    setPriceStudy(true);
    setStudyTitle("Random");
    setCursorLabelName("Random");
    setColorPriceBars(true);
    setDefaultPriceBarColor(Color.black);
    }

    // Global Variables
    var x = null;
    var bInit = false;

    function main() {

    if(getCurrentBarIndex() == 0) return;
    if (bInit == false) {
    x = Math.floor(Math.random() * 2);
    }
    }

    var rx = x.getValue(0);

    if (rx = 0) {
    drawTextRelative(0, high(0), rx, Color.red, null, Text.BOLD|Text.BOTTOM|Text.FRAME, null, 12);
    }
    if (rx = 1) {
    drawTextRelative(0, high(0), rx, Color.lime, null, Text.BOLD|Text.BOTTOM|Text.FRAME, null, 12);

    }

  • #2
    You may find information at this, and this site useful.

    Here is what you asked for as I understand it.

    PHP Code:
    function preMain() {
     
    setPriceStudy(true);
     
    setStudyTitle("Random");
     
    setCursorLabelName("Random");
     
    setColorPriceBars(true);
     
    setDefaultPriceBarColor(Color.black);
     }

     
    // Global Variables
    //var bInit = false;//not used

     
    function main() { 

     if(
    getCurrentBarIndex() == 0) return;
        var 
    null
        
    //if (bInit == false) {//not used
        //    bInit=true;//this line is needed to stop execution after it runs once
       // }
        
    Math.floor(Math.random() * 2);//needs to execute on each tick, no need for it to be global since 
                                          //it is only used within main() scope and only the current value is needed
                                          //every iteration

        
    if (== 0) {
            
    setPriceBarColor(Color.red,Color.black,true,false);
            
    drawTextRelative(0high(0), xColor.rednullText.BOLD|Text.BOTTOM|Text.FRAMEnull12);
        }  else  
        if (
    == 1) {
            
    setPriceBarColor(Color.green,Color.black,true,false);
           
    drawTextRelative(0high(0), xColor.limenullText.BOLD|Text.BOTTOM|Text.FRAMEnull12);
        }

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	45.4 KB
ID:	243039

    Wayne
    Last edited by waynecd; 09-07-2016, 01:39 PM.

    Comment

    Working...
    X