Announcement

Collapse
No announcement yet.

Problems with code?

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

  • Problems with code?

    Hey everyone,

    I am attempting to code a backtest of a strategy I have been developing, but after I hit the "Test" button, the dialog box that pops shows that no trades have been recorded. I am wondering where the problem might lie in my code. Here it is:

    PHP Code:
    var study null;

    function 
    preMain() {
        
    setPriceStudy(true);

        
    setColorPriceBars(true);

        
    setDefaultPriceBarColor(Color.black);

        }

    function 
    main() {

        if(
    study == nullstudy atr(3);
        var 
    ATR study.getValue(0);
        if(
    ATR == null)
        return;
        var 
    ATR .01;
        var 
    dailyHigh high(0inv("D"));
        var 
    dailyLow low(0inv("D"));
        var 
    dailyOpen open(0inv("D"));
        var 
    longPrice dailyHigh .02;
        
        if(
    high(0) < dailyHigh && high(-1) < dailyHigh && high(-2) < dailyHigh && high(-3) < dailyHigh){
            
    Strategy.doLong("Buying new high"Strategy.STOP400longPrice);
            
        if(
    Strategy.getPositionSize() == 400)
            
    Strategy.setStop(longPrice N); 

        if(
    Strategy.isLong() == true && .05 && Strategy.getPositionSize() == 400
            
    Strategy.doSell("Selling 1/4 position for 2R"Strategy.LIMIT100longPrice .10); 
        
        if(
    Strategy.getPositionSize() == 300) {
            
    Strategy.clearStop();
            
    Strategy.setStop(longPrice N); }
            
        if(
    Strategy.isLong() == true && .05 && Strategy.getPositionSize() == 300
            
    Strategy.doSell("Selling 1/4 position for 3R"Strategy.LIMIT100longPrice .10 + (2*N)); 
        
        if(
    Strategy.getPositionSize() == 200) {
            
    Strategy.clearStop();
            
    Strategy.setStop(longPrice); }
            
        if(
    Strategy.isLong() == true && .05 && Strategy.getPositionSize() == 200
            
    Strategy.doSell("Selling remaining position for 4R"Strategy.LIMIT200longPrice .10 + (4*N));  
        
        if(
    Strategy.isLong() == true && >= .05 && Strategy.getPositionSize() == 400)
            
    Strategy.doSell("Selling 1/4 position for 2R"Strategy.LIMIT100longPrice + (2*N));
        
        if(
    Strategy.getPositionSize() == 300
            
    Strategy.clearStop();
            
    Strategy.setStop(longPrice N);
            
        if(
    Strategy.isLong() == true && >= .05 && Strategy.getPositionSize() == 300
            
    Strategy.doSell("Selling 1/4 position for 3R"Strategy.LIMIT100longPrice + (3*N));
            
        if(
    Strategy.getPositionSize() == 200) {
            
    Strategy.clearStop();
            
    Strategy.setStop(longPrice); }
            
        if(
    Strategy.isLong() == true && >= .05 && Strategy.getPositionSize() == 200
            
    Strategy.doSell("Selling remaining position for 4R"Strategy.LIMIT200longPrice + (4*N));
        
        if(
    Strategy.getPositionSize() == 0
            
    Strategy.clearStop();

        return 
    ATR;

    Thanks so much for your help!

    Best,
    Jake

  • #2
    Strategy description

    I neglected to describe the strategy, which would probably help! I buy/short new daily highs/lows and use a stop loss that is based on very recent volatility, which is measured by the ATR indicator. When I am right in a position, I liquidate 1/4 of my position at 2R (2 x initial risk); 1/4 of my position at 3R, moving my stop to breakeven; and the remainder at 4R. I've only programmed the long side so far as a test.

    Thanks!

    Comment

    Working...
    X