Announcement

Collapse
No announcement yet.

Generic Broker functions

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

  • Generic Broker functions

    Good morning,
    I would like to have an help from you. I translated this code from easy language. It runs and I could test it, but since I relayed the function "strategy.dolong" with "buyatmarket", the code doesn't read the exit rules and, if the initial condition is still true, it buys again.
    What it doesn't work in this manner? How I should modify the code with the generic broker function? Could you make an example?
    I paste only the incriminated part for the long version.
    Thanks.
    Enrico

    var vSMA40 = new MAStudy(40, 0, "Close", MAStudy.SIMPLE);

    function main() {
    var Dev = StdDev(40);
    if ((Strategy.isLong() == false) &&
    close() >= vSMA40.getValue(MAStudy.MA) + 2*Dev)
    {
    // Long entry
    Strategy.doLong("Enterlong", Strategy.MARKET, Strategy.NEXTBAR, Strategy.DEFAULT, 0);
    }

    else if (Strategy.isLong() == true && close() < vSMA40.getValue(MAStudy.MA))
    {
    // Long exit
    Strategy.doSell("Exitlong", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT, 0);
    drawShapeRelative(0, high() + (high() - low())/4,
    }

  • #2
    Enrico
    The Strategy functions are intended for Back Testing only. When processing real time data and/or using Broker functions you need to replace the Strategy calls [ie Strategy.isLong() in your example] with your own global variables that you will use to keep track of the position status of the system (see example below)
    Alex

    PHP Code:
    var IsLong false;
     
    function 
    main(){
     
        if(
    IsLong == false && close(0) >= vSMA40.getValue(MAStudy.MA) + (2*Dev)){
            
    buyMarket(getSymbol(), 1"YourBrokerTrade");
            
    IsLong true;
        }
        if(
    IsLong == true && etc etc){
            
    sellMarket(getSymbol(), 1"YourBrokerTrade");
            
    IsLong false;
        } 

    Comment

    Working...
    X