Announcement

Collapse
No announcement yet.

Adding to a position

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

  • Adding to a position

    I am using a simple moving average crossover system below. I am trying to write code to add to winners (I have only attempted to do so for Long positions at this time). I will put my questions before the code I have questions with.

    Here is my code:

    var study9 = new MAStudy(9, 0, "Close", MAStudy.SIMPLE);
    var study16 = new MAStudy(16, 0, "Close", MAStudy.SIMPLE);

    function preMain() {

    setPriceStudy(true);

    setColorPriceBars(true);

    setDefaultPriceBarColor(Color.black);

    }

    function main() {


    var v9 = study9.getValue(MAStudy.MA);
    var v16 = study16.getValue(MAStudy.MA);
    var nOriginalEntry;

    /* Once trade moves in favor this much, add to position*/

    var nIncrementAmount;

    /* Once trade is entered,this variable is set to one and is incremented each time I add to my position*/

    var nCounter;

    nIncrementAmout = .10;

    if(v9 == null)
    return;

    if(v16 == null)

    return;

    /*Here I am trying to add to a long position if I am already long and the high of the bar has increased by .10 of my original entry point (for the first time executed. Is there a better way to do this?*/

    if(Strategy.isLong && high > (nOriginalEntry + (nCounter * nIncrementAmount)))

    /*Here I am trying to add to a position. Is this how I would do it?*/

    Strategy.doLong("Crossing Up",nOriginalEntry + (nCounter * nIncrementAmount))

    if(v9 > v16 && !Strategy.isLong())

    Strategy.doLong("Crossing Up", Strategy.MARKET, Strategy.THISBAR);

    /* I am trying to assign my original entry price to this variable. Is this correct?*/

    nOriginalEntry = Strategy.MARKET;
    nCounter = 0;
    nCounter = nCounter + 1;

    if(v9 < v16 && !Strategy.isShort())

    nCounter = 0;
    Strategy.doSell("Sell",Strategy.MARKET, Strategy.THISBAR,Strategy.ALL);
    Strategy.doShort("Crossing Down", Strategy.MARKET, Strategy.THISBAR);

    if(Strategy.isLong())

    setPriceBarColor(Color.lime);

    else if(Strategy.isShort())

    setPriceBarColor(Color.red);

    return v9;

    }

    Thanks,
    fan27
    Last edited by fan27; 04-22-2004, 08:04 AM.

  • #2
    Here is how I would do it..

    PHP Code:
    /* Once trade moves in favor this much, add to position*/
    var nIncrementAmount 0.10;
    /* Once trade is entered,this variable is set to one and is incremented each time I add to my position*/
    var nCounter;
    var 
    nOriginalEntry;
    var 
    AdditionalTradeAdded false;

    var 
    study9 = new MAStudy(90"Close"MAStudy.SIMPLE);
    var 
    study16 = new MAStudy(160"Close"MAStudy.SIMPLE);

    function 
    preMain() {
     
    setPriceStudy(true);
     
    setColorPriceBars(true);
     
    setDefaultPriceBarColor(Color.black);
     
    setComputeOnClose(true);
    }

    function 
    main() {

    var 
    v9 study9.getValue(MAStudy.MA,-1);
    var 
    v16 study16.getValue(MAStudy.MA,-1);


    if(
    v9 == null)  return;
    if(
    v16 == null)  return;

    //-----------------------------------------------------------------
    //  Long Additional Position.
    //-----------------------------------------------------------------
    if ( (Strategy.isLong()) && (high() > (nOriginalEntry + (nCounter nIncrementAmount))) && (!AdditionalTradeAdded) ) {
    /*Here I am trying to add to a position. Is this how I would do it?*/
    Strategy.doLong("Add Long",Strategy.LIMITStrategy.THISBARStrategy.DEFAULT,(nOriginalEntry + (nCounter nIncrementAmount))) ;
    AdditionalTradeAdded true;
    }

    //-----------------------------------------------------------------
    //  Short Additional Position.
    //-----------------------------------------------------------------
    if ( (Strategy.isShort()) && (low() < (nOriginalEntry - (nCounter nIncrementAmount))) && (!AdditionalTradeAdded) ) {
    /*Here I am trying to add to a position. Is this how I would do it?*/
    Strategy.doShort("Add Short",Strategy.LIMITStrategy.THISBARStrategy.DEFAULT,(nOriginalEntry + (nCounter nIncrementAmount))) ;
    AdditionalTradeAdded true;
    }


    //-----------------------------------------------------------------
    // new long entry code
    //-----------------------------------------------------------------
    if((v9 v16) && (!Strategy.isLong())) {
     
    Strategy.doLong("Crossing Up"Strategy.MARKETStrategy.THISBARStrategy.DEFAULT);
    /* I am trying to assign my original entry price to this variable. Is this correct?*/
     
    nOriginalEntry open();
     
    nCounter 0;
     
    nCounter nCounter 1;
     
    AdditionalTradeAdded false;
    }

    //-----------------------------------------------------------------
    // new short entry code
    //-----------------------------------------------------------------
    if((v9 v16) && (!Strategy.isShort()))  {
     
    Strategy.doShort("Crossing Down"Strategy.MARKETStrategy.THISBARStrategy.DEFAULT);
     
    nOriginalEntry open();
     
    nCounter 0;
     
    nCounter nCounter 1;
     
    AdditionalTradeAdded false;
    }


    if(
    Strategy.isLong())
     
    setPriceBarColor(Color.lime);
    else if(
    Strategy.isShort())
     
    setPriceBarColor(Color.red);

    return 
    v9;

    B
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      Thanks a lot Brad.

      fan27

      Comment

      Working...
      X