Announcement

Collapse
No announcement yet.

Trading Multiple Futures contracts with pofit targets

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

  • Trading Multiple Futures contracts with pofit targets

    Can anyone help me with multiple profit targets or part lot sizes? I am trying to backtest a strategy I use for FTSE Futures where I enter the trade with three contracts and then close each contract as the various profit targets get hit. The difficulty I am having is that I can not find a way to close part of a position. I am certain that it is possible but I have trawled the board and can not find any code to do this. Here is the code I am using at present. Currently when the first target is hit it closes out the whole position, so the second profit target vanishes from the cursor window. If the market moves up strongly this does not happen but if the bars retrace after hitting the first target all the positions are closed.

    if (Strategy.isInTrade() == true && (Strategy.isLong() == true)) {
    // Check if the long 'Narrow' profit target has been reached/breached
    if (high() >= (nTradeEntryPrice + ProfitTarget1)) {
    // Profit Target Breached, Execute Cover order.
    if(getBarState()==BARSTATE_NEWBAR){
    Alert.playSound("choir1.wav");
    Strategy.doSell("Long PT-A Exit", Strategy.STOP, Strategy.THISBAR,
    Strategy.getDefaultLotSize(), (nTradeEntryPrice + ProfitTarget1));
    ProfitTarget1 = ProfitTarget2;
    }
    }
    }

    if (Strategy.isInTrade() == true && (Strategy.isLong() == true)) {
    // Check if the long 'Wide' profit target has been reached/breached
    if (high() >= (nTradeEntryPrice + ProfitTarget2)) {
    // Profit Target Breached, Execute Cover order.
    if(getBarState()==BARSTATE_NEWBAR){
    Alert.playSound("choir1.wav");
    Strategy.doSell("Long PT-B Exit", Strategy.STOP, Strategy.THISBAR,
    Strategy.getDefaultLotSize(), (nTradeEntryPrice + ProfitTarget2));
    }
    }
    }
    Attached Files

  • #2
    Hello fairly,

    In your profit target .doSell() calls replace Strategy.getDefaultLotSize() with 1.

    PHP Code:
    Strategy.doSell("Long PT-A Exit"Strategy.STOPStrategy.THISBAR
    1, (nTradeEntryPrice ProfitTarget1)); 
    Then for the stop loss .doSell() calls find out what your current position size is with Strategy.getPositionSize(). You can call that directly in the lot size parameter or assign it to a variable and use the variable for the lot size parameter.

    PHP Code:
    Strategy.doSell("Stop Exit"Strategy.STOPStrategy.THISBARStrategy.getPositionSize(), (nTradeEntryPrice nStopAmount)); 
    or

    PHP Code:
    var nPosition Strategy.getPositionSize();
    Strategy.doSell("Stop Exit"Strategy.STOPStrategy.THISBARnPosition, (nTradeEntryPrice nStopAmount)); 
    As an alternative, if you don't specify a lot size and enter null for that parameter, the Strategy Analyzer will close the remaining contracts for you.

    PHP Code:
    Strategy.doSell("Stop Exit"Strategy.STOPStrategy.THISBARnull, (nTradeEntryPrice nStopAmount)); 
    Strategy Object
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      Thank you, thank you, thank you!

      Works perfectly.

      Comment


      • #4
        Multi Contract problems with 7.9 Backtester

        I am now able to back test multiple contract strategies after help on this thread. However my multicontract efs strategies which worked on v7.7 does not work on v7.9. Either the tester crashes or produce spurious results. I am unable to find an option to select £ for currency either. The detailed equity curve also does not function. My single contract efs works, but profit targets are not exiting on v7.9 when I use more than one contract. The code JasonK helpfully suggested worked before and I really do not want to uninstal this version but can't see why it no longer works. Here is the old code which used to work. Can anyone tell me what is going wrong?

        if (Strategy.isInTrade() == true && (Strategy.isLong() == true) && Strategy.getPositionSize()>10) {//only executes if position size if greater than 10
        // Check if the long 'Narrow' profit target has been reached/breached
        if (high() >= (nTradeEntryPrice + ProfitTarget1)) {
        // Profit Target Breached, Execute Cover order.
        if(getBarState()==BARSTATE_NEWBAR){
        Alert.playSound("choir1.wav");
        Strategy.doSell("Long PT-A Exit", Strategy.STOP, Strategy.THISBAR,
        10, (nTradeEntryPrice + ProfitTarget1)); //value 10 is position size to sell part lot of 20?
        ProfitTarget1 = ProfitTarget2;

        }
        }
        }

        if (Strategy.isInTrade() == true && (Strategy.isLong() == true)) {
        // Check if the long 'Wide' profit target has been reached/breached
        if (high() >= (nTradeEntryPrice + ProfitTarget2)) {
        // Profit Target Breached, Execute Cover order.
        if(getBarState()==BARSTATE_NEWBAR){
        Alert.playSound("Warning.wav");
        Strategy.doSell("Long PT-B Exit", Strategy.STOP, Strategy.THISBAR,
        null, (nTradeEntryPrice + ProfitTarget2));//null is set to clear remaining positions?
        }
        }
        }

        Comment


        • #5
          Hello fairly,

          There have been a few errors found in the new version of the strategy analyzer in version 7.9.1. These have been corrected and will be available in the next version (7.9.2), which is due out shortly. This is most likely the source of the problem and not your formula code. If you post your formula here, I'd be happy to test it with the latest version. The detailed equity graph was one of the problems that has been fixed.
          Jason K.
          Project Manager
          eSignal - an Interactive Data company

          EFS KnowledgeBase
          JavaScript for EFS Video Series
          EFS Beginner Tutorial Series
          EFS Glossary
          Custom EFS Development Policy

          New User Orientation

          Comment


          • #6
            Much appreciated. Let us know when the update is ready. I will post a cut down version in a day or two.

            Comment


            • #7
              Hello fairly,

              Please download the updated installer from this post.
              Jason K.
              Project Manager
              eSignal - an Interactive Data company

              EFS KnowledgeBase
              JavaScript for EFS Video Series
              EFS Beginner Tutorial Series
              EFS Glossary
              Custom EFS Development Policy

              New User Orientation

              Comment

              Working...
              X