Announcement

Collapse
No announcement yet.

Variable lot size?

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

  • Variable lot size?

    Instead of specifying a constant lot size for each new position in a backtest, is it possible to do the backtest with a fixed dollar amount for the position, i.e. have the backtest calculate the number of shares that could be traded at the current price?

  • #2
    ryowell
    Assuming I understood correctly what you are asking you would need to add the required logic in the efs.
    Alex

    Comment


    • #3
      Alex:

      Wondering if you could elaborate more on your reply to ryowel. I have been struggling with establishing a lot size that is based on %account size / ATR. It is a basic volatility position sizing algorithm. I am using the following parameters:

      if(high() > LongSig1 && Strategy.isLong() == false && Strategy.isShort() == false && Strategy.isInTrade() == false) {
      Unit = Math.floor(.01 * 50000 / ATR34);
      NRiskL1 = (ATR34 * 2);
      EntryLimitL1 = (LongSig1 + .05);
      onAction1() }

      (I realize the entry doesn't consider gaps)

      function onAction1() {
      Strategy.doLong("Long", Strategy.LIMIT, Strategy.THISBAR, Strategy.DEFAULT, EntryLimitL1);
      Strategy.setStop(EntryLimitL1 - NRiskL1);
      drawShapeRelative(0, high()+.07, Shape.DIAMOND, "", Color.green, Shape.TOP); }

      The error is that it cannot covert "Unit" to an integer. Not sure what I need to do. Please help! Thanks.

      Comment


      • #4
        dschram
        Math.floor() should return the integer. However from the snippet of code you posted it is not possible to determine if ATR34 is a valid number. Please post a complete working example of the code you are using
        Alex

        Comment


        • #5
          Alex:

          It is a ttached. I also tried setting up Unit as var Unit, but couldn't get that to work either. I'm trying

          If you notice any other problems feel free to point them out. I do find that the short side of trades does poorly on many markets I have tested. But I couldn't find any error in the logic and I think the poor performance is due to the bullish nature of the markets I have tested it in.

          Thank you for looking at it.

          David
          Attached Files

          Comment


          • #6
            dschram
            The reason you are getting the error message is because Unit is returning Infinity. This is due to the fact that ATR34 returns null while the ATR study is priming. You need to add a null check for that variable and also a check for 0 so as to avoid any "divide by 0" errors when calculating Unit.
            Try adding if(ATR34==null || ATR34==0) return; after you have assigned a value to that variable and the script should work.
            Alex

            Comment


            • #7
              Thank you so much Alex . It works.

              Comment


              • #8
                dschram
                You are most welcome
                Alex

                Comment


                • #9
                  Hi Alex. I really appreciate your help. I am continuing to have a problem with my code with respect to consistency of plotting on the price chart a diamond signal of the entries and exits AND the trade actually taking place at that point as shown in the backtest.

                  I find that the entry matches with the data in the BT (at least as far as I can tell), but I get double exits plotted, typically spaced 1 to 3 days apart with the actual exit in the BT taking place on the later day. The BT ought to reflect the first signaled exit.

                  I am working on adding a second breakout system to the code, which I have removed until I can get the bugs out of the first system. I do notice that the same thing happens on both systems independently of the other.

                  Thanks.

                  David
                  Attached Files

                  Comment


                  • #10
                    David
                    In order to better understand what a strategy is doing I would suggest using the following process:
                    - Limit (at least initially) the script to the entries and exits for either the long or short side only and add the next set of logic only when you are completely satisfied that the current one is behaving as expected. This will make it easier to identify possible inconsistencies in the current logic.
                    - Plot the stops, target and profit levels that are being used in the conditions. This way there is a visual confirmation of how and why the strategy is behaving as it is.
                    Enclosed is a revision of your script modified as per the suggestions given above.
                    In the script I declared the variables ExitLimit and NRiskL1 globally as they are being used also in the onAction1 and onAction5 functions
                    Once you load the script on the chart you should see the reason for some of the discrepancies between the shapes drawn on the chart and the trades reported in the Strategy Analyzer. In some cases in fact the trades get stopped out by the stop you set with Strategy.setStop(EntryLimitL1 - NRiskL1) however you do not have any logic that will draw a shape when those events occurr.
                    Hope this helps
                    Alex

                    PHP Code:
                    var vDonchian20 = new DonchianStudy(200);
                    var 
                    vDonchian10 = new DonchianStudy(100);
                    var 
                    vATR34 = new ATRStudy(34);
                    var 
                    Unit null;
                    var 
                    Sys1L null;
                    var 
                    Sys2L null;
                    var 
                    EntryLimitL1 null;
                    var 
                    EntryLimitL2 null;
                    var 
                    ExitLimit null;//added by ACM - needs to be declared globally
                    var NRiskL1 null;//added by ACM - needs to be declared globally

                    function preMain() {

                        
                    setPriceStudy(true)
                        
                    setStudyTitle("DBO")
                        
                    setDefaultBarFgColor(Color.blue,0);//added by ACM for ExitLongSig1
                        
                    setDefaultBarFgColor(Color.red,1);//added by ACM for EntryLimitL1
                        
                    setDefaultBarFgColor(Color.green,2);//added by ACM for EntryLimitL1-NRiskL1
                    }

                    function 
                    main() {

                        var 
                    ATR34 vATR34.getValue(ATRStudy.ATR);
                        var 
                    LongSig1 vDonchian20.getValue(DonchianStudy.UPPER, -1);
                        var 
                    ExitLongSig1 vDonchian10.getValue(DonchianStudy.LOWER, -1);
                       
                        if(
                    ATR34==null || ATR34==0) return;

                        if(
                    low() < ExitLongSig1 && ExitLongSig1 EntryLimitL1 && Strategy.isLong() == true){
                            
                    ExitLimit = (ExitLongSig1 .05);
                            
                    Strategy.clearStop();
                            
                    onAction5()
                        }

                        if(
                    high() > LongSig1 && Strategy.isInTrade() == false) {
                            
                    Unit Math.floor(.01 50000 ATR34);
                            
                    NRiskL1 = (ATR34 2);
                            
                    EntryLimitL1 = (LongSig1 .05);
                            
                    onAction1() 
                        }
                        
                        if(
                    NRiskL1==null) return;//ACM added null check

                        
                    return new Array (ExitLongSig1,EntryLimitL1EntryLimitL1-NRiskL1)
                    }

                    function 
                    onAction1() {
                        
                    Strategy.doLong("Long"Strategy.LIMITStrategy.THISBARUnitEntryLimitL1);
                        
                    Strategy.setStop(EntryLimitL1 NRiskL1);
                        
                    drawShapeRelative(0high()+high()*.01Shape.DIAMOND""Color.greenShape.TOP)
                    }

                    function 
                    onAction5() {
                        
                    Strategy.doSell("Sell"Strategy.STOPStrategy.THISBARStrategy.ALLExitLimit);
                        
                    drawShapeRelative(0low()-low()*.01Shape.DIAMOND""Color.yellowShape.BOTTOM)

                    Comment


                    • #11
                      Thank you very very much Alex for taking the time to explain this. I will reconfigure the original code step by step to make sure it works. There are some aspects of the code that I have trouble with like the "return new Array" that you added--never would have occurred to me. I knew that the stops would not show up-I have to work on adding it.

                      Thanks!

                      Comment

                      Working...
                      X