Announcement

Collapse
No announcement yet.

Averaging System with multiple entry's and exit's

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

  • #16
    Here is the 2nd half of the code from last post. On your end just paste it below the above code:
    PHP Code:
        //Exit Signals  
        
    if (Strategy.isInTrade() == true)  
        {  
          
            if (
    Strategy.isShort() == true)  
            {  
                  
                if (
    bShort1 == true && bShort2 == false && bShort3 == false && bShort4 == false)  
                {  
                      
                    if (
    low(0)< nPrice && low(-1)>= nPrice)  
                    {  
                        
    nExitPrice nPrice;  
                        
    Strategy.doCover("Cover Short1 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                        
    drawShape(Shape.DIAMONDBelowBar2Color.green,"DoCover1"+gID());//added tagID 
                    
    }  
                } 
                if (
    bShort1 == true && bShort2 == true && bShort3 == false && bShort4 == false)  
                {  
                      
                    if (
    low(0)< nPrice+Res1 && low(-1)>= nPrice+Res1)  
                    {  
                        
    nExitPrice nPrice+Res1;  
                        
    Strategy.doCover("Cover Short2 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                        
    drawShape(Shape.DIAMONDBelowBar2Color.green,"DoCover2"+gID());//added tagID  
                    
    }  
                } 
                if (
    bShort1 == true && bShort2 == true && bShort3 == true && bShort4 == false)  
                {  
                      
                    if (
    low(0)< nPrice+Res2 && low(-1)>= nPrice+Res2)  
                    {  
                        
    nExitPrice nPrice+Res2;  
                        
    Strategy.doCover("Cover Short3 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                        
    drawShape(Shape.DIAMONDBelowBar2Color.green,"DoCover3"+gID());//added tagID  
                    
    }  
                } 
                if (
    bShort1 == true && bShort2 == true && bShort3 == true && bShort4 == true)  
                {  
                      
                    if (
    low(0)< nPrice+Res3 && low(-1)>= nPrice+Res3)  
                    {  
                        
    nExitPrice nPrice+Res3;  
                        
    Strategy.doCover("Cover Short4 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                        
    drawShape(Shape.DIAMONDBelowBar2Color.green,"DoCover4"+gID());//added tagID  
                    
    }  
                }  
            }  else if (
    Strategy.isLong() == true)  
            {  
                  
                if (
    bLong1 == true && bLong2 == false && bLong3 == false && bLong4 == false)  
                {  
                      
                    if (
    high(0)> nPrice && high(-1)<= nPrice)  
                    {  
                        
    nExitPrice nPrice;  
                        
    Strategy.doSell("Sell Long1 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                        
    drawShape(Shape.DIAMONDAboveBar2Color.green,"DoSell1"+gID());//added tagID  
                    
    }  
                } 
                if (
    bLong1 == true && bLong2 == true && bLong3 == false && bLong4 == false)  
                {  
                      
                    if (
    high(0)> nPrice-Sup1 && high(-1)<= nPrice-Sup1)  
                    {  
                        
    nExitPrice nPrice-Sup1;  
                        
    Strategy.doSell("Sell Long2 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                        
    drawShape(Shape.DIAMONDAboveBar2Color.green,"DoSell2"+gID());//added tagID 
                    
    }  
                } 
                if (
    bLong1 == true && bLong2 == true && bLong3 == true && bLong4 == false)  
                {  
                      
                    if (
    high(0)> nPrice-Sup2 && high(-1)<= nPrice-Sup2)  
                    {  
                        
    nExitPrice nPrice-Sup2;  
                        
    Strategy.doSell("Sell Long3 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                        
    drawShape(Shape.DIAMONDAboveBar2Color.green,"DoSell3"+gID());//added tagID  
                    
    }  
                } 
                if (
    bLong1 == true && bLong2 == true && bLong3 == true && bLong4 == true)  
                {  
                      
                    if (
    high(0)> nPrice-Sup3 && high(-1)<= nPrice-Sup3)  
                    {  
                        
    nExitPrice nPrice-Sup3;  
                        
    Strategy.doSell("Sell Long4 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                        
    drawShape(Shape.DIAMONDAboveBar2Color.green,"DoSell4"+gID());//added tagID  
                    
    }  
                }  
            }  
        } 

        if(
    plotPrice.indexOf("Open")!=-1drawTextRelative(PriceOffsetnPricenPriceColor.bluenullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP");  
        if(
    plotPrice.indexOf("Res1")!=-1drawTextRelative(PriceOffsetnPrice+Res1nPrice+Res1Color.yellownullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_H");  
        if(
    plotPrice.indexOf("Res2")!=-1drawTextRelative(PriceOffsetnPrice+Res2nPrice+Res2Color.greennullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_K");  
        if(
    plotPrice.indexOf("Res3")!=-1drawTextRelative(PriceOffsetnPrice+Res3nPrice+Res3Color.rednullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_L");  
        if(
    plotPrice.indexOf("Res4")!=-1drawTextRelative(PriceOffsetnPrice+Res4nPrice+Res4Color.rednullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_M");  
        if(
    plotPrice.indexOf("Sup1")!=-1drawTextRelative(PriceOffsetnPrice-Sup1nPrice-Sup1Color.yellownullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_N");  
        if(
    plotPrice.indexOf("Sup2")!=-1drawTextRelative(PriceOffsetnPrice-Sup2nPrice-Sup2Color.greennullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_O");  
        if(
    plotPrice.indexOf("Sup3")!=-1drawTextRelative(PriceOffsetnPrice-Sup3nPrice-Sup3Color.rednullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_P");  
        if(
    plotPrice.indexOf("Sup4")!=-1drawTextRelative(PriceOffsetnPrice-Sup4nPrice-Sup4Color.rednullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_Q");  

        return [
    Strategy.isInTrade(),Strategy.isLong(),Strategy.isShort(),doTrade];  //added to show "in Trade" conditions in Data Window
    }  
    var 
    grID 0;  
    function 
    gID()  
    {  
        
    grID ++;  
        return( 
    grID );  

    NOTE: This is the second half of the post. The first half is in the previous post.

    Wayne
    Last edited by waynecd; 08-13-2013, 01:23 AM.

    Comment


    • #17
      Hi Wayne,
      been away for a couple of days hence delayed reply. Thanks for the above. I think I have it working now, just some logic to adapt for overnight positions.
      I'll be back !
      Thanks again for all your help, very much appreciated.
      Regards
      Daniel

      Comment


      • #18
        Yw

        Wayne

        Comment


        • #19
          Hi Wayne,

          Ive written a small adaptation of the strategy in which the idea is for it to Buy (or Sell) the first two levels with a Stop Order 30 ticks below (or above) the second level. It also includes your Close all trades function, but I have used my own code for the Non trading time (I'm interested to hear your opinion of it - it is the " if((hour(0)*100)+minute(0)< StartTime || (hour(0)*100)+minute(0)> EndTime) return; " line.)

          The one issue Im having with this code is that the order it is in at the moment means that if End Time (time to stop trading) is at 20.00 hrs it will not take any of the exit signals after this. I thought this should be a simple fix to move the Exit signals (or even just the stop loss signals) above the " if((hour(0)*100)+minute(0)< StartTime || (hour(0)*100)+minute(0)> EndTime) return; " line of code. However, when I did this I got an error message referring to this line saying that "return is not in the function." Although I'm sure this should be a simple fix it is somewhat eluding me so far...

          function preMain()
          {
          var aFPArray = new Array();
          setPriceStudy(true);
          setCursorLabelName("TodayOpen");

          var x=0;
          aFPArray[x] = new FunctionParameter("UserTime", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("Start Time For Open (2300)");
          setLowerLimit(0);
          setUpperLimit(2400);
          setDefault(100);
          }
          aFPArray[x] = new FunctionParameter("StartTime", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("Start Time For Trading");
          setLowerLimit(0);
          setUpperLimit(2400);
          setDefault(700);
          }
          aFPArray[x] = new FunctionParameter("EndTime", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("End Time For Trading");
          setLowerLimit(0);
          setUpperLimit(2400);
          setDefault(2100);
          }
          aFPArray[x] = new FunctionParameter("LineLen", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("Line Length");
          setLowerLimit(1);
          setDefault(200);
          }
          aFPArray[x] = new FunctionParameter("Stop", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("Stop Loss");
          setLowerLimit(0);
          setDefault(.3);
          }
          aFPArray[x] = new FunctionParameter("Res1", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("Res1");
          setLowerLimit(0);
          setDefault(.5);
          }
          aFPArray[x] = new FunctionParameter("Res2", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("Res2");
          setLowerLimit(0);
          setDefault(.75);
          }
          aFPArray[x] = new FunctionParameter("Res3", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("Res3");
          setLowerLimit(0);
          setDefault(1);
          }
          aFPArray[x] = new FunctionParameter("Res4", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("Res4");
          setLowerLimit(0);
          setDefault(1.25);
          }
          aFPArray[x] = new FunctionParameter("Sup1", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("Sup1");
          setLowerLimit(0);
          setDefault(.5);
          }
          aFPArray[x] = new FunctionParameter("Sup2", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("Sup2");
          setLowerLimit(0);
          setDefault(.75);
          }
          aFPArray[x] = new FunctionParameter("Sup3", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("Sup3");
          setLowerLimit(0);
          setDefault(1);
          }
          aFPArray[x] = new FunctionParameter("Sup4", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("Sup4");
          setLowerLimit(0);
          setDefault(1.5);
          }
          aFPArray[x] = new FunctionParameter("TrdSz1", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("TrdSz1");
          setLowerLimit(0);
          setDefault(2);
          }
          aFPArray[x] = new FunctionParameter("TrdSz2", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("TrdSz2");
          setLowerLimit(0);
          setDefault(1);
          }
          aFPArray[x] = new FunctionParameter("CloseAllTime", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("Close All Open Trades");
          setLowerLimit(0);
          setUpperLimit(2400);
          setDefault(2030);
          }
          aFPArray[x] = new FunctionParameter("TodayOnly", FunctionParameter.BOOLEAN);
          with(aFPArray[x++])
          {
          setDefault(true);
          }
          aFPArray[x] = new FunctionParameter("PriceOffset", FunctionParameter.NUMBER);
          with(aFPArray[x++])
          {
          setName("Price Offset");
          setLowerLimit(0);
          setDefault(3);
          }
          aFPArray[x] = new FunctionParameter("plotPrice", FunctionParameter.STRING);
          with(aFPArray[x++])
          {
          setName("Plot Price At");
          addOption("Open Only");
          addOption("Open & Res1");
          addOption("Res1 & Res2 & Res3");
          addOption("Res1 & Res2 & Res3 & Res4")
          setDefault("Open & Res1 & Res2 & Res3 & Res4 & Sup1 & Sup2 & Sup3 & Sup4");
          }
          }


          var bLong1 = null;
          var bLong2 = null;

          var bShort1 = null;
          var bShort2 = null;

          var nStop = null;
          var bExitFlag = null;

          var bInit = false;
          var nPrice = 0;


          function main(UserTime,StartTime,EndTime,CloseAllTime,LineL en,Stop,Res1,Res2,Res3,Res4,Sup1,Sup2,Sup3,Sup4,Tr dSz1,TrdSz2,TodayOnly,plotPrice,PriceOffset)
          {
          var xOpen = LineTag = null;
          if(TodayOnly) LineTag = 105;
          else LineTag = gID();
          if(isMonthly() || isWeekly() || isDaily()) return;

          if (day(0)!= day(-1))
          {
          bLong1 = false;
          bLong2 = false;

          bShort1 = false;
          bShort2 = false;

          bExitFlag = false;

          }
          if((hour(0)*100)+minute(0)==UserTime)
          {
          nPrice = open(0);
          drawLineRelative( 0, open(0)+Res1, LineLen, open(0)+Res1, PS_DOT, 1, Color.yellow, "Up"+LineTag );
          drawLineRelative( 0, open(0)+Res2, LineLen, open(0)+Res2, PS_DOT, 1, Color.green, "Up1"+LineTag );
          drawLineRelative( 0, open(0)+Res3, LineLen, open(0)+Res3, PS_DOT, 1, Color.blue, "Up2"+LineTag );
          drawLineRelative( 0, open(0)-Sup1, LineLen, open(0)-Sup1, PS_DOT, 1, Color.yellow, "Dwn1"+LineTag );
          drawLineRelative( 0, open(0)-Sup2, LineLen, open(0)-Sup2, PS_DOT, 1, Color.green, "Dwn2"+LineTag );
          drawLineRelative( 0, open(0)-Sup3, LineLen, open(0)-Sup3, PS_DOT, 1, Color.blue, "Dwn3"+LineTag );
          drawLineRelative( 0, open(0), LineLen, open(0), PS_SOLID, 1, Color.blue, "OpenMid"+LineTag );

          }

          if(plotPrice.indexOf("Open")!=-1) drawTextRelative(PriceOffset, nPrice, nPrice, Color.blue, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP");
          if(plotPrice.indexOf("Res1")!=-1) drawTextRelative(PriceOffset, nPrice+Res1, nPrice+Res1, Color.yellow, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_H");
          if(plotPrice.indexOf("Res2")!=-1) drawTextRelative(PriceOffset, nPrice+Res2, nPrice+Res2, Color.green, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_K");
          if(plotPrice.indexOf("Res3")!=-1) drawTextRelative(PriceOffset, nPrice+Res3, nPrice+Res3, Color.red, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_L");
          if(plotPrice.indexOf("Res4")!=-1) drawTextRelative(PriceOffset, nPrice+Res4, nPrice+Res4, Color.red, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_M");
          if(plotPrice.indexOf("Sup1")!=-1) drawTextRelative(PriceOffset, nPrice-Sup1, nPrice-Sup1, Color.yellow, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_N");
          if(plotPrice.indexOf("Sup2")!=-1) drawTextRelative(PriceOffset, nPrice-Sup2, nPrice-Sup2, Color.green, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_O");
          if(plotPrice.indexOf("Sup3")!=-1) drawTextRelative(PriceOffset, nPrice-Sup3, nPrice-Sup3, Color.red, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_P");
          if(plotPrice.indexOf("Sup4")!=-1) drawTextRelative(PriceOffset, nPrice-Sup4, nPrice-Sup4, Color.red, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_Q");


          //Start of Strategy Object

          // The only problem with this is if the price goes above Sell 3 for example before the "Start Trading" time, then the strategy will not Short at Sell 2,3 or 4 even after the "Start Trading" time because the bShort1 variable will still be set to false.


          if(Strategy.isInTrade() == true){
          if((hour(0)*100)+minute(0)>=CloseAllTime){
          if(Strategy.isLong() == true)
          {
          Strategy.doSell("Sell EOD Signal", Strategy.MARKET, Strategy.THISBAR, Strategy.ALL);
          drawShape(Shape.DIAMOND, AboveBar2, Color.blue,"DoSell_EOD"+gID());
          }
          if(Strategy.isShort() == true)
          {
          Strategy.doCover("Cover EOD Signal", Strategy.MARKET, Strategy.THISBAR, Strategy.ALL);
          drawShape(Shape.DIAMOND, BelowBar2, Color.blue,"DoCover_EOD"+gID());
          }
          }
          }



          if((hour(0)*100)+minute(0)< StartTime || (hour(0)*100)+minute(0)> EndTime) return;

          if (getCurrentBarIndex() == 0) return;



          // Entry Short Signals

          if (bShort1 == false && bShort2 == false && bLong1 == false && bLong2 == false && bExitFlag == false)
          {
          if (high(0)> nPrice+Res1 && high(-1)<= nPrice+Res1)
          {
          nEntryPrice = nPrice+Res1;
          Strategy.doShort("Entry Short1 Signal", Strategy.LIMIT, Strategy.THISBAR, TrdSz1, nEntryPrice);
          drawShape(Shape.DOWNARROW, AboveBar2, Color.red);
          bShort1 = true;
          drawLineRelative( 0, nPrice+Res2+Stop, LineLen, nPrice+Res2+Stop, PS_DOT, 1, Color.red, "Stop"+LineTag );
          }
          }
          if (bShort1 == true && bShort2 == false && bLong1 == false && bLong2 == false && bExitFlag == false)
          {
          if (high(0)> nPrice+Res2 && high(-1)<= nPrice+Res2)
          {
          nEntryPrice = nPrice+Res2;
          Strategy.doShort("Entry Short2 Signal", Strategy.LIMIT, Strategy.THISBAR, TrdSz2, nEntryPrice);
          drawShape(Shape.DOWNARROW, AboveBar2, Color.red);
          bShort2 = true;
          nStop = nEntryPrice + Stop
          }
          }

          //Entry Long Signals

          if (bShort1 == false && bShort2 == false && bLong1 == false && bLong2 == false && bExitFlag == false)
          {
          if (low(0)< nPrice-Sup1 && low(-1)>= nPrice-Sup1)
          {
          nEntryPrice = nPrice-Sup1;
          Strategy.doLong("Entry Long1 Signal", Strategy.LIMIT, Strategy.THISBAR, TrdSz1, nEntryPrice);
          drawShape(Shape.UPARROW, BelowBar2, Color.blue);
          bLong1 = true;
          drawLineRelative( 0, nPrice-Sup2-Stop, LineLen, nPrice-Sup2-Stop, PS_DOT, 1, Color.red, "Stop"+LineTag );
          }
          }
          if (bShort1 == false && bShort2 == false && bLong1 == true && bLong2 == false && bExitFlag == false)
          {
          if (low(0)< nPrice-Sup2 && low(-1)>= nPrice-Sup2)
          {
          nEntryPrice = nPrice-Sup2;
          Strategy.doLong("Entry Long2 Signal", Strategy.LIMIT, Strategy.THISBAR, TrdSz2, nEntryPrice);
          drawShape(Shape.UPARROW, BelowBar2, Color.blue);
          bLong2 = true;
          nStop = nEntryPrice - Stop
          }
          }


          //Exit Signals

          if (Strategy.isInTrade() == true)
          {

          if (Strategy.isShort() == true)
          {

          if (bShort1 == true && bShort2 == false)
          {

          if (low(0)< nPrice && low(-1)>= nPrice)
          {
          nExitPrice = nPrice;
          Strategy.doCover("Cover Short1 Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nExitPrice);
          drawShape(Shape.DIAMOND, BelowBar2, Color.green);
          bShort1 = false;
          }
          }
          if (bShort1 == true && bShort2 == true)
          {

          if (low(0)< nPrice+Res1 && low(-1)>= nPrice+Res1)
          {
          nExitPrice = nPrice+Res1;
          Strategy.doCover("Cover Short2 Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nExitPrice);
          drawShape(Shape.DIAMOND, BelowBar2, Color.green);
          bShort2 = false;
          }
          if (high(0) > nStop && high(-1) <= nStop) {
          nExitPrice = nStop + 0.05;
          Strategy.doCover("Short Stop Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nExitPrice);
          drawShape(Shape.SQUARE, AboveBar2, Color.red);
          bShort2 = false;
          bExitFlag = true;
          }

          }

          } else if (Strategy.isLong() == true)
          {

          if (bLong1 == true && bLong2 == false)
          {

          if (high(0)> nPrice && high(-1)<= nPrice)
          {
          nExitPrice = nPrice;
          Strategy.doSell("Sell Long1 Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nExitPrice);
          drawShape(Shape.DIAMOND, AboveBar2, Color.green);
          bLong1 = false;
          }
          }
          if (bLong1 == true && bLong2 == true)
          {

          if (high(0)> nPrice-Sup1 && high(-1)<= nPrice-Sup1)
          {
          nExitPrice = nPrice-Sup1;
          Strategy.doSell("Sell Long2 Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nExitPrice);
          drawShape(Shape.DIAMOND, AboveBar2, Color.green);
          bLong2 = false;
          }
          if (low(0) < nStop && low(-1) >= nStop) {
          nExitPrice = nStop - 0.05;
          Strategy.doSell("Long Stop Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nExitPrice);
          drawShape(Shape.SQUARE, BelowBar2, Color.red);
          bLong2 = false;
          bExitFlag = true;
          }
          }

          }
          }

          }
          var grID = 0;
          function gID()
          {
          grID ++;
          return( grID );
          }

          Comment


          • #20
            Right, I think Ive got my head around it finally - just a matter of getting the order and the wiggly brackets in the correct places:

            function main(UserTime,StartTime,EndTime,CloseAllTime,LineL en,Stop,Res1,Res2,Res3,Res4,Sup1,Sup2,Sup3,Sup4,Tr dSz1,TrdSz2,TodayOnly,plotPrice,PriceOffset)
            {
            var xOpen = LineTag = null;
            if(TodayOnly) LineTag = 105;
            else LineTag = gID();
            if(isMonthly() || isWeekly() || isDaily()) return;

            if (day(0)!= day(-1))
            {
            bLong1 = false;
            bLong2 = false;

            bShort1 = false;
            bShort2 = false;

            bExitFlag = false;

            }
            if((hour(0)*100)+minute(0)==UserTime)
            {
            nPrice = open(0);
            drawLineRelative( 0, open(0)+Res1, LineLen, open(0)+Res1, PS_DOT, 1, Color.yellow, "Up"+LineTag );
            drawLineRelative( 0, open(0)+Res2, LineLen, open(0)+Res2, PS_DOT, 1, Color.green, "Up1"+LineTag );
            drawLineRelative( 0, open(0)+Res3, LineLen, open(0)+Res3, PS_DOT, 1, Color.blue, "Up2"+LineTag );
            drawLineRelative( 0, open(0)-Sup1, LineLen, open(0)-Sup1, PS_DOT, 1, Color.yellow, "Dwn1"+LineTag );
            drawLineRelative( 0, open(0)-Sup2, LineLen, open(0)-Sup2, PS_DOT, 1, Color.green, "Dwn2"+LineTag );
            drawLineRelative( 0, open(0)-Sup3, LineLen, open(0)-Sup3, PS_DOT, 1, Color.blue, "Dwn3"+LineTag );
            drawLineRelative( 0, open(0), LineLen, open(0), PS_SOLID, 1, Color.blue, "OpenMid"+LineTag );

            }

            if(plotPrice.indexOf("Open")!=-1) drawTextRelative(PriceOffset, nPrice, nPrice, Color.blue, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP");
            if(plotPrice.indexOf("Res1")!=-1) drawTextRelative(PriceOffset, nPrice+Res1, nPrice+Res1, Color.yellow, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_H");
            if(plotPrice.indexOf("Res2")!=-1) drawTextRelative(PriceOffset, nPrice+Res2, nPrice+Res2, Color.green, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_K");
            if(plotPrice.indexOf("Res3")!=-1) drawTextRelative(PriceOffset, nPrice+Res3, nPrice+Res3, Color.red, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_L");
            if(plotPrice.indexOf("Res4")!=-1) drawTextRelative(PriceOffset, nPrice+Res4, nPrice+Res4, Color.red, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_M");
            if(plotPrice.indexOf("Sup1")!=-1) drawTextRelative(PriceOffset, nPrice-Sup1, nPrice-Sup1, Color.yellow, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_N");
            if(plotPrice.indexOf("Sup2")!=-1) drawTextRelative(PriceOffset, nPrice-Sup2, nPrice-Sup2, Color.green, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_O");
            if(plotPrice.indexOf("Sup3")!=-1) drawTextRelative(PriceOffset, nPrice-Sup3, nPrice-Sup3, Color.red, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_P");
            if(plotPrice.indexOf("Sup4")!=-1) drawTextRelative(PriceOffset, nPrice-Sup4, nPrice-Sup4, Color.red, null, Text.BOLD | Text.LEFT | Text.VCENTER, null, 10, "TimeP_Q");


            //Start of Strategy Object

            // The only problem with this is if the price goes above Sell 3 for example before the "Start Trading" time, then the strategy will not Short at Sell 2,3 or 4 even after the "Start Trading" time because the bShort1 variable will still be set to false.

            if (getCurrentBarIndex() == 0) return;

            if(Strategy.isInTrade() == true){
            if((hour(0)*100)+minute(0)>=CloseAllTime){
            if(Strategy.isLong() == true)
            {
            Strategy.doSell("Sell EOD Signal", Strategy.MARKET, Strategy.THISBAR, Strategy.ALL);
            drawShape(Shape.DIAMOND, AboveBar2, Color.blue,"DoSell_EOD"+gID());
            }
            if(Strategy.isShort() == true)
            {
            Strategy.doCover("Cover EOD Signal", Strategy.MARKET, Strategy.THISBAR, Strategy.ALL);
            drawShape(Shape.DIAMOND, BelowBar2, Color.blue,"DoCover_EOD"+gID());
            }
            }
            }


            //Exit Signals

            if (Strategy.isInTrade() == true)
            {

            if (Strategy.isShort() == true)
            {

            if (bShort1 == true && bShort2 == false)
            {

            if (low(0)< nPrice && low(-1)>= nPrice)
            {
            nExitPrice = nPrice;
            Strategy.doCover("Cover Short1 Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nExitPrice);
            drawShape(Shape.DIAMOND, BelowBar2, Color.green);
            bShort1 = false;
            }
            }

            if (bShort1 == true && bShort2 == true)
            {

            if (low(0)< nPrice+Res1 && low(-1)>= nPrice+Res1)
            {
            nExitPrice = nPrice+Res1;
            Strategy.doCover("Cover Short2 Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nExitPrice);
            drawShape(Shape.DIAMOND, BelowBar2, Color.green);
            bShort2 = false;
            }
            if (high(0) > nStop && high(-1) <= nStop) {
            nExitPrice = nStop + 0.05;
            Strategy.doCover("Short Stop Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nExitPrice);
            drawShape(Shape.SQUARE, AboveBar2, Color.red);
            bShort2 = false;
            bExitFlag = true;
            }

            }

            } else if (Strategy.isLong() == true)
            {

            if (bLong1 == true && bLong2 == false)
            {

            if (high(0)> nPrice && high(-1)<= nPrice)
            {
            nExitPrice = nPrice;
            Strategy.doSell("Sell Long1 Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nExitPrice);
            drawShape(Shape.DIAMOND, AboveBar2, Color.green);
            bLong1 = false;
            }
            }
            if (bLong1 == true && bLong2 == true)
            {

            if (high(0)> nPrice-Sup1 && high(-1)<= nPrice-Sup1)
            {
            nExitPrice = nPrice-Sup1;
            Strategy.doSell("Sell Long2 Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nExitPrice);
            drawShape(Shape.DIAMOND, AboveBar2, Color.green);
            bLong2 = false;
            }
            if (low(0) < nStop && low(-1) >= nStop) {
            nExitPrice = nStop - 0.05;
            Strategy.doSell("Long Stop Signal", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nExitPrice);
            drawShape(Shape.SQUARE, BelowBar2, Color.red);
            bLong2 = false;
            bExitFlag = true;
            }
            }

            }

            } if ((Strategy.isInTrade() == true) || (Strategy.isInTrade() == false)) {

            if((hour(0)*100)+minute(0)< StartTime || (hour(0)*100)+minute(0)> EndTime) return;


            // Entry Short Signals

            if (bShort1 == false && bShort2 == false && bLong1 == false && bLong2 == false && bExitFlag == false)
            {
            if (high(0)> nPrice+Res1 && high(-1)<= nPrice+Res1)
            {
            nEntryPrice = nPrice+Res1;
            Strategy.doShort("Entry Short1 Signal", Strategy.LIMIT, Strategy.THISBAR, TrdSz1, nEntryPrice);
            drawShape(Shape.DOWNARROW, AboveBar2, Color.red);
            bShort1 = true;
            drawLineRelative( 0, nPrice+Res2+Stop, LineLen, nPrice+Res2+Stop, PS_DOT, 1, Color.red, "Stop"+LineTag );
            }
            }
            if (bShort1 == true && bShort2 == false && bLong1 == false && bLong2 == false && bExitFlag == false)
            {
            if (high(0)> nPrice+Res2 && high(-1)<= nPrice+Res2)
            {
            nEntryPrice = nPrice+Res2;
            Strategy.doShort("Entry Short2 Signal", Strategy.LIMIT, Strategy.THISBAR, TrdSz2, nEntryPrice);
            drawShape(Shape.DOWNARROW, AboveBar2, Color.red);
            bShort2 = true;
            nStop = nEntryPrice + Stop
            }
            }

            //Entry Long Signals

            if (bShort1 == false && bShort2 == false && bLong1 == false && bLong2 == false && bExitFlag == false)
            {
            if (low(0)< nPrice-Sup1 && low(-1)>= nPrice-Sup1)
            {
            nEntryPrice = nPrice-Sup1;
            Strategy.doLong("Entry Long1 Signal", Strategy.LIMIT, Strategy.THISBAR, TrdSz1, nEntryPrice);
            drawShape(Shape.UPARROW, BelowBar2, Color.blue);
            bLong1 = true;
            drawLineRelative( 0, nPrice-Sup2-Stop, LineLen, nPrice-Sup2-Stop, PS_DOT, 1, Color.red, "Stop"+LineTag );
            }
            }
            if (bShort1 == false && bShort2 == false && bLong1 == true && bLong2 == false && bExitFlag == false)
            {
            if (low(0)< nPrice-Sup2 && low(-1)>= nPrice-Sup2)
            {
            nEntryPrice = nPrice-Sup2;
            Strategy.doLong("Entry Long2 Signal", Strategy.LIMIT, Strategy.THISBAR, TrdSz2, nEntryPrice);
            drawShape(Shape.UPARROW, BelowBar2, Color.blue);
            bLong2 = true;
            nStop = nEntryPrice - Stop
            }
            }
            }
            }

            var grID = 0;
            function gID()
            {
            grID ++;
            return( grID );
            }

            Comment


            • #21
              When the strat enters a trade, it draws a red line where the stop price is. This line is the standard line length though. Is it possible to set this red stop line to start as the strategy enters the trade and end when the exit occurs?? Thanks again in advance for any help.

              Daniel

              Comment


              • #22
                Originally posted by ferret View Post
                When the strat enters a trade, it draws a red line where the stop price is. This line is the standard line length though. Is it possible to set this red stop line to start as the strategy enters the trade and end when the exit occurs?? Thanks again in advance for any help.

                Daniel
                See "//xx" for my comments.

                PHP Code:
                function preMain() {
                    var 
                aFPArray = new Array();
                    
                setPriceStudy(true);

                    var 
                0;
                    
                aFPArray[x] = new FunctionParameter("UserTime"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("Start Time For Open (2300)");
                        
                setLowerLimit(0);
                        
                setUpperLimit(2400);
                        
                setDefault(100);
                    }
                    
                aFPArray[x] = new FunctionParameter("StartTime"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("Start Time For Trading");
                        
                setLowerLimit(0);
                        
                setUpperLimit(2400);
                        
                setDefault(700);
                    }
                    
                aFPArray[x] = new FunctionParameter("EndTime"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("End Time For Trading");
                        
                setLowerLimit(0);
                        
                setUpperLimit(2400);
                        
                setDefault(2100);
                    }
                    
                aFPArray[x] = new FunctionParameter("LineLen"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("Line Length");
                        
                setLowerLimit(1);
                        
                setDefault(200);
                    }
                    
                aFPArray[x] = new FunctionParameter("Stop"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("Stop Loss");
                        
                setLowerLimit(0);
                        
                setDefault(.3);
                    }
                    
                aFPArray[x] = new FunctionParameter("Res1"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("Res1");
                        
                setLowerLimit(0);
                        
                setDefault(.5);
                    }
                    
                aFPArray[x] = new FunctionParameter("Res2"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("Res2");
                        
                setLowerLimit(0);
                        
                setDefault(.75);
                    }
                    
                aFPArray[x] = new FunctionParameter("Res3"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("Res3");
                        
                setLowerLimit(0);
                        
                setDefault(1);
                    }
                    
                aFPArray[x] = new FunctionParameter("Res4"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("Res4");
                        
                setLowerLimit(0);
                        
                setDefault(1.25);
                    }
                    
                aFPArray[x] = new FunctionParameter("Sup1"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("Sup1");
                        
                setLowerLimit(0);
                        
                setDefault(.5);
                    }
                    
                aFPArray[x] = new FunctionParameter("Sup2"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("Sup2");
                        
                setLowerLimit(0);
                        
                setDefault(.75);
                    }
                    
                aFPArray[x] = new FunctionParameter("Sup3"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("Sup3");
                        
                setLowerLimit(0);
                        
                setDefault(1);
                    }
                    
                aFPArray[x] = new FunctionParameter("Sup4"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("Sup4");
                        
                setLowerLimit(0);
                        
                setDefault(1.5);
                    }
                    
                aFPArray[x] = new FunctionParameter("TrdSz1"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("TrdSz1");
                        
                setLowerLimit(0);
                        
                setDefault(2);
                    }
                    
                aFPArray[x] = new FunctionParameter("TrdSz2"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("TrdSz2");
                        
                setLowerLimit(0);
                        
                setDefault(1);
                    }
                    
                aFPArray[x] = new FunctionParameter("CloseAllTime"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("Close All Open Trades");
                        
                setLowerLimit(0);
                        
                setUpperLimit(2400);
                        
                setDefault(2030);
                    }
                    
                aFPArray[x] = new FunctionParameter("TodayOnly"FunctionParameter.BOOLEAN);
                    
                with(aFPArray[x++]) {
                        
                setDefault(true);
                    }
                    
                aFPArray[x] = new FunctionParameter("PriceOffset"FunctionParameter.NUMBER);
                    
                with(aFPArray[x++]) {
                        
                setName("Price Offset");
                        
                setLowerLimit(0);
                        
                setDefault(3);
                    }
                    
                aFPArray[x] = new FunctionParameter("plotPrice"FunctionParameter.STRING);
                    
                with(aFPArray[x++]) {
                        
                setName("Plot Price At");
                        
                addOption("Open Only");
                        
                addOption("Open & Res1");
                        
                addOption("Res1 & Res2 & Res3");
                        
                addOption("Res1 & Res2 & Res3 & Res4")
                        
                setDefault("Open & Res1 & Res2 & Res3 & Res4 & Sup1 & Sup2 & Sup3 & Sup4");
                    }
                }


                var 
                bLong1 null;
                var 
                bLong2 null;

                var 
                bShort1 null;
                var 
                bShort2 null;

                var 
                nStop null;
                var 
                bExitFlag null;

                var 
                bInit false;
                var 
                nPrice 0;
                var 
                nEntryPrice;
                var 
                barCntEntry 0;//xx
                var stopLinePrice 0;//xx
                function main(UserTimeStartTimeEndTimeCloseAllTimeLineLenStopRes1Res2Res3Res4Sup1Sup2Sup3Sup4TrdSz1TrdSz2TodayOnlyplotPricePriceOffset) {
                    var 
                xOpen LineTag null;
                    if (
                TodayOnlyLineTag 105;
                    else 
                LineTag gID();
                    if (
                isMonthly() || isWeekly() || isDaily()) return;

                    if (
                day(0) != day(-1)) {
                        
                bLong1 false;
                        
                bLong2 false;
                        
                bShort1 false;
                        
                bShort2 false;
                        
                bExitFlag false;
                    }
                    if ((
                hour(0) * 100) + minute(0) == UserTime) {
                        
                nPrice open(0);
                        
                drawLineRelative(0open(0) + Res1LineLenopen(0) + Res1PS_DOT1Color.yellow"Up" LineTag);
                        
                drawLineRelative(0open(0) + Res2LineLenopen(0) + Res2PS_DOT1Color.green"Up1" LineTag);
                        
                drawLineRelative(0open(0) + Res3LineLenopen(0) + Res3PS_DOT1Color.blue"Up2" LineTag);
                        
                drawLineRelative(0open(0) - Sup1LineLenopen(0) - Sup1PS_DOT1Color.yellow"Dwn1" LineTag);
                        
                drawLineRelative(0open(0) - Sup2LineLenopen(0) - Sup2PS_DOT1Color.green"Dwn2" LineTag);
                        
                drawLineRelative(0open(0) - Sup3LineLenopen(0) - Sup3PS_DOT1Color.blue"Dwn3" LineTag);
                        
                drawLineRelative(0open(0), LineLenopen(0), PS_SOLID1Color.blue"OpenMid" LineTag);

                    }

                    if (
                plotPrice.indexOf("Open") != -1drawTextRelative(PriceOffsetnPricenPriceColor.bluenullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP");
                    if (
                plotPrice.indexOf("Res1") != -1drawTextRelative(PriceOffsetnPrice Res1nPrice Res1Color.yellownullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_H");
                    if (
                plotPrice.indexOf("Res2") != -1drawTextRelative(PriceOffsetnPrice Res2nPrice Res2Color.greennullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_K");
                    if (
                plotPrice.indexOf("Res3") != -1drawTextRelative(PriceOffsetnPrice Res3nPrice Res3Color.rednullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_L");
                    if (
                plotPrice.indexOf("Res4") != -1drawTextRelative(PriceOffsetnPrice Res4nPrice Res4Color.rednullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_M");
                    if (
                plotPrice.indexOf("Sup1") != -1drawTextRelative(PriceOffsetnPrice Sup1nPrice Sup1Color.yellownullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_N");
                    if (
                plotPrice.indexOf("Sup2") != -1drawTextRelative(PriceOffsetnPrice Sup2nPrice Sup2Color.greennullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_O");
                    if (
                plotPrice.indexOf("Sup3") != -1drawTextRelative(PriceOffsetnPrice Sup3nPrice Sup3Color.rednullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_P");
                    if (
                plotPrice.indexOf("Sup4") != -1drawTextRelative(PriceOffsetnPrice Sup4nPrice Sup4Color.rednullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_Q");


                    
                //Start of Strategy Object 

                    // The only problem with this is if the price goes above Sell 3 for example before the "Start Trading" time, then the strategy will not Short at Sell 2,3 or 4 even after the "Start Trading" time because the bShort1 variable will still be set to false. 

                    
                if (getCurrentBarIndex() == 0) return;

                    if (
                Strategy.isInTrade() == true) {
                        
                drawLineRelative(barCntEntry getCurrentBarCount(), stopLinePricestopLinePricePS_DOT1Color.red"Stop" LineTag);//xx
                        
                if ((hour(0) * 100) + minute(0) >= CloseAllTime) {
                            if (
                Strategy.isLong() == true) {
                                
                Strategy.doSell("Sell EOD Signal"Strategy.MARKETStrategy.THISBARStrategy.ALL);
                                
                drawShape(Shape.DIAMONDAboveBar2Color.blue"DoSell_EOD" gID());
                            }
                            if (
                Strategy.isShort() == true) {
                                
                Strategy.doCover("Cover EOD Signal"Strategy.MARKETStrategy.THISBARStrategy.ALL);
                                
                drawShape(Shape.DIAMONDBelowBar2Color.blue"DoCover_EOD" gID());
                            }
                        }
                    }


                    
                //Exit Signals 

                    
                if (Strategy.isInTrade() == true) {

                        if (
                Strategy.isShort() == true) {

                            if (
                bShort1 == true && bShort2 == false) {

                                if (
                low(0) < nPrice && low(-1) >= nPrice) {
                                    
                nExitPrice nPrice;
                                    
                Strategy.doCover("Cover Short1 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);
                                    
                drawShape(Shape.DIAMONDBelowBar2Color.green);
                                    
                bShort1 false;
                                }
                            }
                            if (
                bShort1 == true && bShort2 == true) {
                                if (
                low(0) < nPrice Res1 && low(-1) >= nPrice Res1) {
                                    
                nExitPrice nPrice Res1;
                                    
                Strategy.doCover("Cover Short2 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);
                                    
                drawShape(Shape.DIAMONDBelowBar2Color.green);
                                    
                bShort2 false;
                                }
                                if (
                high(0) > nStop && high(-1) <= nStop) {
                                    
                nExitPrice nStop 0.05;
                                    
                Strategy.doCover("Short Stop Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);
                                    
                drawShape(Shape.SQUAREAboveBar2Color.red);
                                    
                bShort2 false;
                                    
                bExitFlag true;
                                }

                            }

                        } else if (
                Strategy.isLong() == true) {

                            if (
                bLong1 == true && bLong2 == false) {

                                if (
                high(0) > nPrice && high(-1) <= nPrice) {
                                    
                nExitPrice nPrice;
                                    
                Strategy.doSell("Sell Long1 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);
                                    
                drawShape(Shape.DIAMONDAboveBar2Color.green);
                                    
                bLong1 false;
                                }
                            }
                            if (
                bLong1 == true && bLong2 == true) {

                                if (
                high(0) > nPrice Sup1 && high(-1) <= nPrice Sup1) {
                                    
                nExitPrice nPrice Sup1;
                                    
                Strategy.doSell("Sell Long2 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);
                                    
                drawShape(Shape.DIAMONDAboveBar2Color.green);
                                    
                bLong2 false;
                                }
                                if (
                low(0) < nStop && low(-1) >= nStop) {
                                    
                nExitPrice nStop 0.05;
                                    
                Strategy.doSell("Long Stop Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);
                                    
                drawShape(Shape.SQUAREBelowBar2Color.red);
                                    
                bLong2 false;
                                    
                bExitFlag true;
                                }
                            }

                        }

                    }

                    if ((
                Strategy.isInTrade() == true) || (Strategy.isInTrade() == false)) {//xx won't this always evaluate to true?

                        
                if ((hour(0) * 100) + minute(0) < StartTime || (hour(0) * 100) + minute(0) > EndTime) return;//xx your revision works and is efficient

                        // Entry Short Signals 

                        
                if (bShort1 == false && bShort2 == false && bLong1 == false && bLong2 == false && bExitFlag == false) {
                            if (
                high(0) > nPrice Res1 && high(-1) <= nPrice Res1) {
                                
                nEntryPrice nPrice Res1;
                                
                Strategy.doShort("Entry Short1 Signal"Strategy.LIMITStrategy.THISBARTrdSz1nEntryPrice);
                                
                drawShape(Shape.DOWNARROWAboveBar2Color.red);
                                
                bShort1 true;
                                
                barCntEntry getCurrentBarCount();//xx
                                //drawLineRelative(0, nPrice + Res2 + Stop, LineLen, nPrice + Res2 + Stop, PS_DOT, 1, Color.red, "Stop" + LineTag);//xx
                                 
                stopLinePrice nPrice Res2 Stop;//xx needed to "fix" stopLinePrice value in time
                            
                }
                        }
                        if (
                bShort1 == true && bShort2 == false && bLong1 == false && bLong2 == false && bExitFlag == false) {
                            if (
                high(0) > nPrice Res2 && high(-1) <= nPrice Res2) {
                                
                nEntryPrice nPrice Res2;
                                
                Strategy.doShort("Entry Short2 Signal"Strategy.LIMITStrategy.THISBARTrdSz2nEntryPrice);
                                
                drawShape(Shape.DOWNARROWAboveBar2Color.red);
                                
                bShort2 true;
                                
                nStop nEntryPrice Stop;
                            }
                        }

                        
                //Entry Long Signals 

                        
                if (bShort1 == false && bShort2 == false && bLong1 == false && bLong2 == false && bExitFlag == false) {
                            if (
                low(0) < nPrice Sup1 && low(-1) >= nPrice Sup1) {
                                
                nEntryPrice nPrice Sup1;
                                
                Strategy.doLong("Entry Long1 Signal"Strategy.LIMITStrategy.THISBARTrdSz1nEntryPrice);
                                
                drawShape(Shape.UPARROWBelowBar2Color.blue);
                                
                bLong1 true;
                                
                barCntEntry getCurrentBarCount();//xx
                                //drawLineRelative(0, nPrice - Sup2 - Stop, LineLen, nPrice - Sup2 - Stop, PS_DOT, 1, Color.red, "Stop" + LineTag);//xx
                                
                stopLinePrice nPrice Sup2 Stop;//xx needed to "fix" stopLinePrice value in time
                            
                }
                        }
                        if (
                bShort1 == false && bShort2 == false && bLong1 == true && bLong2 == false && bExitFlag == false) {
                            if (
                low(0) < nPrice Sup2 && low(-1) >= nPrice Sup2) {
                                
                nEntryPrice nPrice Sup2;
                                
                Strategy.doLong("Entry Long2 Signal"Strategy.LIMITStrategy.THISBARTrdSz2nEntryPrice);
                                
                drawShape(Shape.UPARROWBelowBar2Color.blue);
                                
                bLong2 true;
                                
                nStop nEntryPrice Stop;
                            }
                        }
                    }
                }

                var 
                grID 0;

                function 
                gID() {
                    
                grID++;
                    return (
                grID);

                Wayne
                Last edited by waynecd; 08-17-2013, 12:22 AM.

                Comment


                • #23
                  In case it is useful, here is a synopsis of drawing a line with length limited by a condition. Using the code in my previous post as an example:

                  To draw a line from a particular bar where an entry condition is true until an exit condition is true:

                  - for a straight line:
                  At the time the entry condition is true:
                  * Set a global variable to the price the line will be draw on (stopLinePrice)
                  * Set a global variable to the current barcount (barCntEntry)
                  * Set a global variable to a unique tag ID for each instance the line will be drawn (LineTag). One method is using function gID(). This value must not change while the line is to be drawn.

                  Draw line while a 3rd condition is true "if(Strategy.isInTrade())" using:
                  PHP Code:
                  drawLineRelative(x1y1x2y2StyleThicknessColorTagName
                  Where:
                  * x1 is (barCntEntry - getCurrentBarCount())
                  * y1 is stopLinePrice
                  * x2 is 0
                  * y2 is stopLinePrice
                  * TagName is "Stop" + LineTag

                  PHP Code:
                  drawLineRelative(barCntEntry getCurrentBarCount(), stopLinePricestopLinePricePS_DOT1Color.red"Stop" LineTag); 
                  Wayne
                  Last edited by waynecd; 08-17-2013, 12:46 AM.

                  Comment


                  • #24
                    Hello

                    I'm getting very close to achieving what I want to with the code, thanks so much for your help so far!

                    However, I'm struggling with a slight issue regarding overnight positions, and was wondering if you could shed some light on the issue?

                    I have tried to add some different exit signals for overnight positions. I am trying to make the strategy use the previous day's exit signals on the new day for the trades that are carried overnight. The logic we have used, uses Boolean variables (bLong1, bLong2, bShort1, bShort2 etc) to record which trade the system is in, in order to determine which exit signal to look for. In order for the system to continue trading on a new day, these variables are reset if its a new day at the start of the strategy object. However, in order for the system to remember what trade it was in the previous day, I have assigned a new variable (bYLong1, bYLong2 etc) to the bLong1, bLong2 variables before resetting them. The system can then look for exit signals for the over night positions based upon the if (bYLong1 == true.... etc) logic at prices based upon the previous day's open [ open(-22,inv"60") ] (its a 22 hour trading session).

                    The problem is, this doesn't seem to be recognising the overnight exit signals at all. I have tried forcing the bYLong1 etc variables accordingly to true or false to force an exit but to no avail. I have also used the drawlinerelative function to draw a line of yesterdays opening price using the 'open(-22,inv"60")' code and that all seems to work fine, so Im pretty stuck as to the root of the problem.

                    As always any help would be greatly appreciated!

                    Comment


                    • #25
                      PHP Code:

                      //http://forum.esignal.com/showthread.php?38494-Bands-based-on-Open-Price  
                      //http://forum.esignal.com/showthread.php?38847-Averaging-System-with-multiple-entry-s-and-exit-s 
                      function preMain()  
                      {  
                          var 
                      aFPArray = new Array();  
                          
                      setPriceStudy(true);  
                          
                      setCursorLabelName("TodayOpen");  

                          var 
                      x=0;  
                          
                      aFPArray[x] = new FunctionParameter("UserTime"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("Start Time For Open (2300)");  
                              
                      setLowerLimit(0);          
                              
                      setUpperLimit(2400);          
                              
                      setDefault(100);  
                          }  
                          
                      aFPArray[x] = new FunctionParameter("StartTime"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("Start Time For Trading");  
                              
                      setLowerLimit(0);          
                              
                      setUpperLimit(2400);          
                              
                      setDefault(700);  
                          }
                          
                      aFPArray[x] = new FunctionParameter("EndTime"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("End Time For Trading");  
                              
                      setLowerLimit(0);          
                              
                      setUpperLimit(2400);          
                              
                      setDefault(2100);  
                          }
                          
                      aFPArray[x] = new FunctionParameter("LineLen"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("Line Length");  
                              
                      setLowerLimit(1);          
                              
                      setDefault(200);  
                          }  
                          
                      aFPArray[x] = new FunctionParameter("Res1"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("Res1");  
                              
                      setLowerLimit(0);          
                              
                      setDefault(.6);  
                          }  
                          
                      aFPArray[x] = new FunctionParameter("Res2"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("Res2");  
                              
                      setLowerLimit(0);          
                              
                      setDefault(.9);  
                          }  
                          
                      aFPArray[x] = new FunctionParameter("Res3"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("Res3");  
                              
                      setLowerLimit(0);          
                              
                      setDefault(1.5);  
                          }  
                          
                      aFPArray[x] = new FunctionParameter("Res4"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("Res4");  
                              
                      setLowerLimit(0);          
                              
                      setDefault(3.25);  
                          }  
                        
                      aFPArray[x] = new FunctionParameter("Sup1"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("Sup1");  
                              
                      setLowerLimit(0);          
                              
                      setDefault(.6);  
                          }  
                          
                      aFPArray[x] = new FunctionParameter("Sup2"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("Sup2");  
                              
                      setLowerLimit(0);          
                              
                      setDefault(.9);  
                          }  
                          
                      aFPArray[x] = new FunctionParameter("Sup3"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("Sup3");  
                              
                      setLowerLimit(0);          
                              
                      setDefault(1.5);  
                          }  
                          
                      aFPArray[x] = new FunctionParameter("Sup4"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("Sup4");  
                              
                      setLowerLimit(0);          
                              
                      setDefault(3.25);  
                          }
                          
                      aFPArray[x] = new FunctionParameter("TodayOnly"FunctionParameter.BOOLEAN);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setDefault(true);  
                          }  
                          
                      aFPArray[x] = new FunctionParameter("PriceOffset"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("Price Offset");  
                              
                      setLowerLimit(0);          
                              
                      setDefault(3);  
                          }  
                          
                      aFPArray[x] = new FunctionParameter("TrdSz1"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("TrdSz1");  
                              
                      setLowerLimit(0);          
                              
                      setDefault(2);  
                          } 
                      aFPArray[x] = new FunctionParameter("TrdSz2"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("TrdSz2");  
                              
                      setLowerLimit(0);          
                              
                      setDefault(1);  
                          } 
                      aFPArray[x] = new FunctionParameter("TrdSz3"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("TrdSz3");  
                              
                      setLowerLimit(0);          
                              
                      setDefault(1);  
                          } 
                      aFPArray[x] = new FunctionParameter("TrdSz4"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("TrdSz4");  
                              
                      setLowerLimit(0);          
                              
                      setDefault(2);  
                          } 
                      aFPArray[x] = new FunctionParameter("MaxClip"FunctionParameter.NUMBER);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("MaxClip");  
                              
                      setLowerLimit(0);          
                              
                      setDefault(10);  
                          }
                          
                      aFPArray[x] = new FunctionParameter("plotPrice"FunctionParameter.STRING);  
                          
                      with(aFPArray[x++]) 
                          {  
                              
                      setName("Plot Price At");  
                              
                      addOption("Open Only");  
                              
                      addOption("Open & Res1");  
                              
                      addOption("Res1 & Res2 & Res3");  
                              
                      addOption("Res1 & Res2 & Res3 & Res4")  
                              
                      setDefault("Open & Res1 & Res2 & Res3 & Res4 & Sup1 & Sup2 & Sup3 & Sup4");  
                          }  
                      }  

                          var 
                      bInit false;

                          var 
                      bLong1 null;  
                          var 
                      bLong2 null;  
                          var 
                      bLong3 null;  
                          var 
                      bLong4 null;  
                          var 
                      bShort1 null;  
                          var 
                      bShort2 null;  
                          var 
                      bShort3 null;  
                          var 
                      bShort4 null;  
                          var 
                      nPrice 0;  

                          
                      //var bYLong1 = null;  
                          //var bYLong2 = null;  
                          //var bYLong3 = null;  
                          //var bYLong4 = null;  
                          //var bYShort1 = null; 
                          //var bYShort2 = null; 
                          //var bYShort3 = null;  
                          //var bYShort4 = null;
                          
                      var nYPrice 0;
                          
                          var 
                      bYLong1 false;  
                          var 
                      bYLong2 false;  
                          var 
                      bYLong3 false;  
                          var 
                      bYLong4 false;  
                          var 
                      bYShort1 false;  
                          var 
                      bYShort2 false;  
                          var 
                      bYShort3 false;  
                          var 
                      bYShort4 false;
                          
                          var 
                      nPosSize null;


                      function 
                      main(UserTime,StartTime,EndTime,LineLen,Res1,Res2,Res3,Res4,Sup1,Sup2,Sup3,Sup4,TodayOnly,plotPrice,PriceOffset,TrdSz1,TrdSz2,TrdSz3,TrdSz4,MaxClip)  
                      {  
                          var 
                      xOpen  LineTag null;  
                          if(
                      TodayOnlyLineTag 105;  
                          else 
                      LineTag gID();  
                          if(
                      isMonthly() || isWeekly() || isDaily()) return;
                          
                            if((
                      hour(0)*100)+minute(0)==UserTime
                          {  
                              
                      nPrice open(0);  
                              
                      drawLineRelative0open(0)+Res1LineLenopen(0)+Res1PS_DOT1Color.maroon"Up"+LineTag );  
                              
                      drawLineRelative0open(0)+Res2LineLenopen(0)+Res2PS_DOT1Color.green"Up1"+LineTag );  
                              
                      drawLineRelative0open(0)+Res3LineLenopen(0)+Res3PS_DOT1Color.red"Up2"+LineTag );  
                              
                      drawLineRelative0open(0)+Res4LineLenopen(0)+Res4PS_DOT2Color.red"Up3"+LineTag );  
                              
                      drawLineRelative0open(0)-Sup1LineLenopen(0)-Sup1PS_DOT1Color.maroon"Dwn1"+LineTag );  
                              
                      drawLineRelative0open(0)-Sup2LineLenopen(0)-Sup2PS_DOT1Color.green"Dwn2"+LineTag );  
                              
                      drawLineRelative0open(0)-Sup3LineLenopen(0)-Sup3PS_DOT1Color.red"Dwn3"+LineTag );  
                              
                      drawLineRelative0open(0)-Sup4LineLenopen(0)-Sup4PS_DOT2Color.red"Dwn4"+LineTag );  
                              
                      drawLineRelative0open(0), LineLenopen(0), PS_SOLID1Color.blue"OpenMid"+LineTag );   
                                
                          }

                          var 
                      nPosSize Strategy.getPositionSize();


                      // Daily Variable Reset and follow on if overnight
                       
                          
                      if (day(0)!= day(-1)) {
                              
                              if (
                      Strategy.isInTrade()) {
                              
                                  var 
                      bYLong1 bLong1;  
                                  var 
                      bYLong2 bLong2;  
                                  var 
                      bYLong3 bLong3;  
                                  var 
                      bYLong4 bLong4;  
                                  var 
                      bYShort1 bShort1;  
                                  var 
                      bYShort2 bShort2;  
                                  var 
                      bYShort3 bShort3;  
                                  var 
                      bYShort4 bShort4;
                                  var 
                      nYPrice open(-22,inv("60"));
                              }
                          
                              
                      bLong1 false;  
                              
                      bLong2 false;  
                              
                      bLong3 false;  
                              
                      bLong4 false;  
                              
                      bShort1 false;  
                              
                      bShort2 false;  
                              
                      bShort3 false;  
                              
                      bShort4 false;  
                               
                          }

                          if((
                      hour(0)*100)+minute(0)< StartTime || (hour(0)*100)+minute(0)> EndTime) return;
                          
                          

                      //Overnight exit logic
                          
                              
                      if (Strategy.isShort() == true)  
                                  {  
                                    
                                      if (
                      bYShort1 == true && bYShort2 == false && bYShort3 == false && bYShort4 == false)  
                                      {  
                                        
                                          if (
                      low(0)< nYPrice
                                          {  
                                              
                      nOExitPrice nYPrice;  
                                              
                      Strategy.doCover("Overnight Exit S1 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnOExitPrice);  
                                              
                      drawShape(Shape.DIAMONDBelowBar2Color.green);
                                              
                      bYShort1 false;  bYShort2 falsebYShort3 falsebYShort4 false
                                          }  
                                      } 
                                      if (
                      bYShort1 == true && bYShort2 == true && bYShort3 == false && bYShort4 == false)  
                                      {  
                                                
                                          if (
                      low(0)< nYPrice+Res1)  
                                          {  
                                              
                      nOExitPrice nYPrice+Res1;  
                                              
                      Strategy.doCover("Overnight Exit S2 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnOExitPrice);  
                                              
                      drawShape(Shape.DIAMONDBelowBar2Color.green); 
                                              
                      bYShort1 false;  bYShort2 falsebYShort3 falsebYShort4 false
                                          }  
                                      } 
                                      if (
                      bYShort1 == true && bYShort2 == true && bYShort3 == true && bYShort4 == false)  
                                      {  
                                              
                                          if (
                      low(0)< nYPrice+Res2)  
                                          {  
                                              
                      nOExitPrice nYPrice+Res2;  
                                              
                      Strategy.doCover("Overnight Exit S3 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnOExitPrice);  
                                              
                      drawShape(Shape.DIAMONDBelowBar2Color.green); 
                                              
                      bYShort1 false;  bYShort2 falsebYShort3 falsebYShort4 false;  
                                          }  
                                      } 
                                      if (
                      bYShort1 == true && bYShort2 == true && bYShort3 == true && bYShort4 == true)  
                                      {  
                                              
                                          if (
                      low(0)< nYPrice+Res3)  
                                          {  
                                              
                      nOExitPrice nYPrice+Res3;  
                                              
                      Strategy.doCover("Overnight Exit S4 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnOExitPrice);  
                                              
                      drawShape(Shape.DIAMONDBelowBar2Color.green);  
                                              
                      bYShort1 false;  bYShort2 falsebYShort3 falsebYShort4 false;  
                                          }  
                                      }  
                                  }  else if (
                      Strategy.isLong() == true)  
                                  {  
                                          
                                      if (
                      bYLong1 == true && bYLong2 == false && bYLong3 == false && bYLong4 == false)  
                                      {  
                                                
                                          if (
                      high(0)> nYPrice)  
                                          {  
                                              
                      nOExitPrice nYPrice;  
                                              
                      Strategy.doSell("Overnight Exit L1 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnOExitPrice);  
                                              
                      drawShape(Shape.DIAMONDAboveBar2Color.green); 
                                              
                      bYLong1 falsebYLong2 falsebYLong3 falsebYLong4 false
                                          }  
                                      } 
                                      if (
                      bYLong1 == true && bYLong2 == true && bYLong3 == false && bYLong4 == false)  
                                      {  
                                              
                                          if (
                      high(0)> nYPrice-Sup1)  
                                          {  
                                              
                      nOExitPrice nYPrice-Sup1;  
                                              
                      Strategy.doSell("Overnight Exit L2 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnOExitPrice);  
                                              
                      drawShape(Shape.DIAMONDAboveBar2Color.green); 
                                              
                      bYLong1 falsebYLong2 falsebYLong3 falsebYLong4 false;  
                                          }  
                                      } 
                                      if (
                      bYLong1 == true && bYLong2 == true && bYLong3 == true && bYLong4 == false)  
                                      {  
                                              
                                          if (
                      high(0)> nYPrice-Sup2)  
                                          {  
                                              
                      nOExitPrice nYPrice-Sup2;  
                                              
                      Strategy.doSell("Overnight Exit L3 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnOExitPrice);  
                                              
                      drawShape(Shape.DIAMONDAboveBar2Color.green); 
                                              
                      bYLong1 falsebYLong2 falsebYLong3 falsebYLong4 false;
                                          }  
                                      } 
                                      if (
                      bYLong1 == true && bYLong2 == true && bYLong3 == true && bYLong4 == true)  
                                      {  
                                              
                                          if (
                      high(0)> nYPrice-Sup3)  
                                          {  
                                              
                      nOExitPrice nYPrice-Sup3;  
                                              
                      Strategy.doSell("Overnight Exit L4 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnOExitPrice);  
                                              
                      drawShape(Shape.DIAMONDAboveBar2Color.green); 
                                              
                      bYLong1 falsebYLong2 falsebYLong3 falsebYLong4 false
                                          }  
                                     }  
                                  
                              } 
                      Last edited by ferret; 10-07-2013, 06:23 AM.

                      Comment


                      • #26
                        //Normal Daily Strategy

                        PHP Code:
                            
                            
                            
                        if (getCurrentBarIndex() == 0) return;  
                            
                         
                        // Entry Short Signals  

                            
                        if (nPosSize > -MaxClip && bShort1 == false && bShort2 == false && bShort3 == false && bShort4 == false && bLong1 == false && bLong2 == false && bLong3 == false && bLong4 == false)  
                            {  
                                if (
                        high(0)> nPrice+Res1)  
                                {  
                                    
                        nEntryPrice nPrice+Res1;  
                                    
                        Strategy.doShort("Entry Short1 Signal"Strategy.LIMITStrategy.THISBARTrdSz1nEntryPrice);  
                                    
                        drawShape(Shape.DOWNARROWAboveBar2Color.red);  
                                    
                        bShort1 true;
                                    
                        bYShort1 false;  bYShort2 falsebYShort3 falsebYShort4 false;
                                    
                        bYLong1 falsebYLong2 falsebYLong3 falsebYLong4 false;   
                                } 
                            } 
                            if (
                        nPosSize > -MaxClip && bShort1 == true && bShort2 == false && bShort3 == false && bShort4 == false && bLong1 == false && bLong2 == false && bLong3 == false && bLong4 == false)  
                            {  
                                if (
                        high(0)> nPrice+Res2)  
                                {  
                                    
                        nEntryPrice nPrice+Res2;  
                                    
                        Strategy.doShort("Entry Short2 Signal"Strategy.LIMITStrategy.THISBARTrdSz2nEntryPrice);  
                                    
                        drawShape(Shape.DOWNARROWAboveBar2Color.red);  
                                    
                        bShort2 true;  
                                }  
                            } 
                            if (
                        nPosSize > -MaxClip && bShort1 == true && bShort2 == true && bShort3 == false && bShort4 == false && bLong1 == false && bLong2 == false && bLong3 == false && bLong4 == false)  
                            {  
                                if (
                        high(0)> nPrice+Res3)  
                                {  
                                    
                        nEntryPrice nPrice+Res3;  
                                    
                        Strategy.doShort("Entry Short3 Signal"Strategy.LIMITStrategy.THISBARTrdSz3nEntryPrice);  
                                    
                        drawShape(Shape.DOWNARROWAboveBar2Color.red);  
                                    
                        bShort3 true;  
                                }  
                            } 
                            if (
                        nPosSize > -MaxClip && bShort1 == true && bShort2 == true && bShort3 == true && bShort4 == false && bLong1 == false && bLong2 == false && bLong3 == false && bLong4 == false)  
                            {  
                                if (
                        high(0)> nPrice+Res4)  
                                {  
                                    
                        nEntryPrice nPrice+Res4;  
                                    
                        Strategy.doShort("Entry Short4 Signal"Strategy.LIMITStrategy.THISBARTrdSz4nEntryPrice);  
                                    
                        drawShape(Shape.DOWNARROWAboveBar2Color.red);  
                                    
                        bShort4 true;     
                                }  
                            } 
                            
                        //Entry Long Signals  
                             
                            
                        if (nPosSize MaxClip && bShort1 == false && bShort2 == false && bShort3 == false && bShort4 == false && bLong1 == false && bLong2 == false && bLong3 == false && bLong4 == false)  
                            {  
                                if (
                        low(0)< nPrice-Sup1)  
                                {  
                                    
                        nEntryPrice nPrice-Sup1;  
                                    
                        Strategy.doLong("Entry Long1 Signal"Strategy.LIMITStrategy.THISBARTrdSz1nEntryPrice);  
                                    
                        drawShape(Shape.UPARROWBelowBar2Color.blue);  
                                    
                        bLong1 true;
                                    
                        bYShort1 false;  bYShort2 falsebYShort3 falsebYShort4 false;
                                    
                        bYLong1 falsebYLong2 falsebYLong3 falsebYLong4 false;   
                                }  
                            } 
                            if (
                        nPosSize MaxClip && bShort1 == false && bShort2 == false && bShort3 == false && bShort4 == false && bLong1 == true && bLong2 == false && bLong3 == false && bLong4 == false)  
                            {  
                                if (
                        low(0)< nPrice-Sup2)  
                                {  
                                    
                        nEntryPrice nPrice-Sup2;  
                                    
                        Strategy.doLong("Entry Long2 Signal"Strategy.LIMITStrategy.THISBARTrdSz2nEntryPrice);  
                                    
                        drawShape(Shape.UPARROWBelowBar2Color.blue);  
                                    
                        bLong2 true;  
                                }  
                            } 
                            if (
                        nPosSize MaxClip && bShort1 == false && bShort2 == false && bShort3 == false && bShort4 == false && bLong1 == true && bLong2 == true && bLong3 == false && bLong4 == false)  
                            {  
                                if (
                        low(0)< nPrice-Sup3)  
                                {  
                                    
                        nEntryPrice nPrice-Sup3;  
                                    
                        Strategy.doLong("Entry Long3 Signal"Strategy.LIMITStrategy.THISBARTrdSz3nEntryPrice);  
                                    
                        drawShape(Shape.UPARROWBelowBar2Color.blue);  
                                    
                        bLong3 true;  
                                }  
                            } 
                            if (
                        nPosSize MaxClip && bShort1 == false && bShort2 == false && bShort3 == false && bShort4 == false && bLong1 == true && bLong2 == true && bLong3 == true && bLong4 == false)  
                            {  
                                if (
                        low(0)< nPrice-Sup4)  
                                {  
                                    
                        nEntryPrice nPrice-Sup4;  
                                    
                        Strategy.doLong("Entry Long4 Signal"Strategy.LIMITStrategy.THISBARTrdSz4nEntryPrice);  
                                    
                        drawShape(Shape.UPARROWBelowBar2Color.blue);  
                                    
                        bLong4 true;  
                                }  
                            } 
                              
                            
                        //Exit Signals  
                            
                        if (Strategy.isInTrade() == true)  
                            {  
                              
                                if (
                        Strategy.isShort() == true)  
                                {  
                                      
                                    if (
                        bShort1 == true && bShort2 == false && bShort3 == false && bShort4 == false)  
                                    {  
                                          
                                        if (
                        low(0)< nPrice)  
                                        {  
                                            
                        nExitPrice nPrice;  
                                            
                        Strategy.doCover("Cover Short1 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                                            
                        drawShape(Shape.DIAMONDBelowBar2Color.green);
                                            
                        bShort1 false
                                            
                        bYShort1 false;  bYShort2 falsebYShort3 falsebYShort4 false;
                                            
                        bYLong1 falsebYLong2 falsebYLong3 falsebYLong4 false
                                        }  
                                    } 
                                    if (
                        bShort1 == true && bShort2 == true && bShort3 == false && bShort4 == false)  
                                    {  
                                          
                                        if (
                        low(0)< nPrice+Res1)  
                                        {  
                                            
                        nExitPrice nPrice+Res1;  
                                            
                        Strategy.doCover("Cover Short2 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                                            
                        drawShape(Shape.DIAMONDBelowBar2Color.green); 
                                            
                        bShort2 false
                                            
                        bYShort1 false;  bYShort2 falsebYShort3 falsebYShort4 false;
                                            
                        bYLong1 falsebYLong2 falsebYLong3 falsebYLong4 false;  
                                        }  
                                    } 
                                    if (
                        bShort1 == true && bShort2 == true && bShort3 == true && bShort4 == false)  
                                    {  
                                          
                                        if (
                        low(0)< nPrice+Res2)  
                                        {  
                                            
                        nExitPrice nPrice+Res2;  
                                            
                        Strategy.doCover("Cover Short3 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                                            
                        drawShape(Shape.DIAMONDBelowBar2Color.green); 
                                            
                        bShort3 false
                                            
                        bYShort1 false;  bYShort2 falsebYShort3 falsebYShort4 false;
                                            
                        bYLong1 falsebYLong2 falsebYLong3 falsebYLong4 false;  
                                        }  
                                    } 
                                    if (
                        bShort1 == true && bShort2 == true && bShort3 == true && bShort4 == true)  
                                    {  
                                          
                                        if (
                        low(0)< nPrice+Res3)  
                                        {  
                                            
                        nExitPrice nPrice+Res3;  
                                            
                        Strategy.doCover("Cover Short4 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                                            
                        drawShape(Shape.DIAMONDBelowBar2Color.green);  
                                            
                        bShort4 false
                                            
                        bYShort1 false;  bYShort2 falsebYShort3 falsebYShort4 false;
                                            
                        bYLong1 falsebYLong2 falsebYLong3 falsebYLong4 false
                                        }  
                                    }  
                                }  else if (
                        Strategy.isLong() == true)  
                                {  
                                      
                                    if (
                        bLong1 == true && bLong2 == false && bLong3 == false && bLong4 == false)  
                                    {  
                                          
                                        if (
                        high(0)> nPrice)  
                                        {  
                                            
                        nExitPrice nPrice;  
                                            
                        Strategy.doSell("Sell Long1 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                                            
                        drawShape(Shape.DIAMONDAboveBar2Color.green); 
                                            
                        bLong1 false;  
                                            
                        bYShort1 false;  bYShort2 falsebYShort3 falsebYShort4 false;
                                            
                        bYLong1 falsebYLong2 falsebYLong3 falsebYLong4 false
                                        }  
                                    } 
                                    if (
                        bLong1 == true && bLong2 == true && bLong3 == false && bLong4 == false)  
                                    {  
                                          
                                        if (
                        high(0)> nPrice-Sup1)  
                                        {  
                                            
                        nExitPrice nPrice-Sup1;  
                                            
                        Strategy.doSell("Sell Long2 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                                            
                        drawShape(Shape.DIAMONDAboveBar2Color.green); 
                                            
                        bLong2 false
                                            
                        bYShort1 false;  bYShort2 falsebYShort3 falsebYShort4 false;
                                            
                        bYLong1 falsebYLong2 falsebYLong3 falsebYLong4 false
                                        }  
                                    } 
                                    if (
                        bLong1 == true && bLong2 == true && bLong3 == true && bLong4 == false)  
                                    {  
                                          
                                        if (
                        high(0)> nPrice-Sup2)  
                                        {  
                                            
                        nExitPrice nPrice-Sup2;  
                                            
                        Strategy.doSell("Sell Long3 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                                            
                        drawShape(Shape.DIAMONDAboveBar2Color.green); 
                                            
                        bLong3 false
                                            
                        bYShort1 false;  bYShort2 falsebYShort3 falsebYShort4 false;
                                            
                        bYLong1 falsebYLong2 falsebYLong3 falsebYLong4 false
                                        }  
                                    } 
                                    if (
                        bLong1 == true && bLong2 == true && bLong3 == true && bLong4 == true)  
                                    {  
                                          
                                        if (
                        high(0)> nPrice-Sup3)  
                                        {  
                                            
                        nExitPrice nPrice-Sup3;  
                                            
                        Strategy.doSell("Sell Long4 Signal"Strategy.LIMITStrategy.THISBARStrategy.ALLnExitPrice);  
                                            
                        drawShape(Shape.DIAMONDAboveBar2Color.green); 
                                            
                        bLong4 false
                                            
                        bYShort1 false;  bYShort2 falsebYShort3 falsebYShort4 false;
                                            
                        bYLong1 falsebYLong2 falsebYLong3 falsebYLong4 false
                                        }  
                                    }  
                                }  
                            } 



                            if(
                        plotPrice.indexOf("Open")!=-1drawTextRelative(PriceOffsetnPricenPriceColor.bluenullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP");  
                            if(
                        plotPrice.indexOf("Res1")!=-1drawTextRelative(PriceOffsetnPrice+Res1nPrice+Res1Color.maroonnullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_H");  
                            if(
                        plotPrice.indexOf("Res2")!=-1drawTextRelative(PriceOffsetnPrice+Res2nPrice+Res2Color.greennullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_K");  
                            if(
                        plotPrice.indexOf("Res3")!=-1drawTextRelative(PriceOffsetnPrice+Res3nPrice+Res3Color.rednullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_L");  
                            if(
                        plotPrice.indexOf("Res4")!=-1drawTextRelative(PriceOffsetnPrice+Res4nPrice+Res4Color.rednullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_M");  
                            if(
                        plotPrice.indexOf("Sup1")!=-1drawTextRelative(PriceOffsetnPrice-Sup1nPrice-Sup1Color.maroonnullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_N");  
                            if(
                        plotPrice.indexOf("Sup2")!=-1drawTextRelative(PriceOffsetnPrice-Sup2nPrice-Sup2Color.greennullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_O");  
                            if(
                        plotPrice.indexOf("Sup3")!=-1drawTextRelative(PriceOffsetnPrice-Sup3nPrice-Sup3Color.rednullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_P");  
                            if(
                        plotPrice.indexOf("Sup4")!=-1drawTextRelative(PriceOffsetnPrice-Sup4nPrice-Sup4Color.rednullText.BOLD Text.LEFT Text.VCENTERnull10"TimeP_Q");  
                                  
                        }  
                        var 
                        grID 0;  
                        function 
                        gID()  
                        {  
                            
                        grID ++;  
                            return( 
                        grID );  

                        Last edited by ferret; 10-07-2013, 06:20 AM.

                        Comment


                        • #27
                          Hi,

                          If I understand correctly what you are trying to do there is no need to add additional flags (too many of these are hard to keep track of and unnecessarily complicate the code).

                          If you want open trades to run until the strategy triggers a doCover or doSell then remove the code that closes it at the end of the day.

                          If instead you want open trades to run until x bars (or specific time) before the start of the next day's trading RTH, change the code that closes open trades at the end of the day to close them at x bars (or specific time) before the start of the next day's trading RTH.

                          Wayne
                          Last edited by waynecd; 10-08-2013, 05:36 PM.

                          Comment


                          • #28
                            Hi Wayne,

                            Sorry I don't think I explained what Im trying to do very well.

                            Basically I want all trades to run until they reach an exit signal. However, each day, the entry levels (and exit levels) change but I want the strategy to still look for the previous days exit signals for trades that have been held overnight. (or if a buy/sell signal is generated on the new day from the new levels before the previous day's exit signals are hit then the previous day's exit signals become obsolete - basically whichever comes first!)

                            Thanks again

                            Comment


                            • #29
                              Hi,

                              I assume the two code snipets you posted above must be joined to get the full script.
                              PHP Code:
                                          var bYLong1 bLong1;  
                                          var 
                              bYLong2 bLong2;  
                                          var 
                              bYLong3 bLong3;  
                                          var 
                              bYLong4 bLong4;  
                                          var 
                              bYShort1 bShort1;  
                                          var 
                              bYShort2 bShort2;  
                                          var 
                              bYShort3 bShort3;  
                                          var 
                              bYShort4 bShort4;
                                          var 
                              nYPrice open(-22,inv("60")); 
                              These variables are declared twice, as global and again as local within main(). Within main() the local variable overrides the global variable of the same name and since they are declared on the first bar after midnight, they remain undefined thereafter as shown when you run the following test script where "bInit" has the same effect (though not daily) as the conditional in your script "if(day(0)!=day(-1)){".
                              [php]debugClear();
                              var bInit=false;
                              var x=0;
                              function main(){
                              debugPrintln("4: "+x);//output: undefined
                              if(!bInit){
                              var x=3;
                              bInit=true;
                              }
                              debugPrintln("9: "+
                              Last edited by waynecd; 10-15-2013, 07:59 PM.

                              Comment


                              • #30
                                Oh and I would also create a global variable flag to have the code inside the "day(0)!=day(-1) execute only once in real time instead of for every tick inside the first bar of the day as shown below:
                                PHP Code:
                                var dayFlag=false;
                                function 
                                main(....
                                    if (
                                day(0)!= day(-1)) {
                                        
                                        if (
                                Strategy.isInTrade() && !dayFlag) {
                                        
                                            
                                //var bYLong1 = bLong1;  
                                            //var bYLong2 = bLong2;  
                                            //var bYLong3 = bLong3;  
                                            //var bYLong4 = bLong4;  
                                            //var bYShort1 = bShort1;  
                                            //var bYShort2 = bShort2;  
                                            //var bYShort3 = bShort3;  
                                            //var bYShort4 = bShort4;
                                            //var nYPrice = open(-22,inv("60"));
                                            
                                bYLong1 bLong1;  
                                            
                                bYLong2 bLong2;  
                                            
                                bYLong3 bLong3;  
                                            
                                bYLong4 bLong4;  
                                            
                                bYShort1 bShort1;  
                                            
                                bYShort2 bShort2;  
                                            
                                bYShort3 bShort3;  
                                            
                                bYShort4 bShort4;
                                            
                                nYPrice open(-22,inv("60"));
                                            
                                dayFlag=true;
                                        }
                                    
                                        
                                bLong1 false;  
                                        
                                bLong2 false;  
                                        
                                bLong3 false;  
                                        
                                bLong4 false;  
                                        
                                bShort1 false;  
                                        
                                bShort2 false;  
                                        
                                bShort3 false;  
                                        
                                bShort4 false;  
                                         
                                    }else 
                                dayFlag=false;
                                .... 
                                Then the code:
                                PHP Code:
                                if ((hour(0) * 100) + minute(0) < StartTime || (hour(0) * 100) + minute(0) > EndTime) return; 
                                stops execution for all times not between the "StartTime" and "EndTime" so in effect, other than resetting the variables and drawing some lines (code above the "return;") nothing below the "return" executes during hours after "EndTime", overnight, or in the morning before "StartTime". This line by itself prevents all overnight trade executions. Nothing after a "return;" statement is ever executed.

                                Wayne
                                Last edited by waynecd; 10-15-2013, 09:55 PM.

                                Comment

                                Working...
                                X