covering only part of a pyramid

Questions about MultiCharts and user contributed studies.
RWDickinson
Posts: 43
Joined: 01 Dec 2008
Has thanked: 2 times
Been thanked: 2 times

covering only part of a pyramid

Postby RWDickinson » 05 Feb 2009

I'm probably missing something basic here.

I am using a pyramiding scheme:
<i>
if longCondition1 then buy("L01") 1 contract next bar market;
......
if longCondition2 then buy("L02") 1 contract next bar market;
......
etc.
</i>

Each entry is a single contract. This works fine. At some point I am long, say, 3 contracts, having used 3 separate entries.

Now there is a condition that decreases my confidence in the long position, and I want to reduce to only 1 contract.

<i>
if lowerConfidence then sell("cL01") 2 contracts next bar market;
......
</i>

This does not work fine. The sell always flattens my position rather than just reducing it by the number I want (no matter what # of contracts I code in the sell statement).

I see in the help that one can code an Entry("entryLabel") clause in the sell statement, but that would be difficult for me because I don't know which entry generated the latest buy, and because the adds and covers don't match up. The help does not specify what happens if one omits the Entry("entryLabel") clause, but it looks to me as though the default is that the sell applies to each entry in the current position, trying to sell 2 contracts for each entry, rather than 2 contracts total.

I should add that an exactly congruent problem occurs if I SellShort a few times and then try to BuyToCover just two contracts.

My questions are:
1) is my analysis of the problem correct?
2) is there a simple way to make the sell cover some number of contracts total rather than per entry?
3) what should I be doing differently?

User avatar
Marina Pashkova
Posts: 2758
Joined: 27 Jul 2007

Postby Marina Pashkova » 11 Feb 2009

Hi RWDickinson,

In your code, sell works for each individual entry. It tries to sell 2 contracts from every entry. Thus, all contracts will be sold. To sell the total of two contracts you need to use the following command:

Code: Select all

if lowerConfidence then sell("cL01") 2 contracts total next bar market;


The above command will sell the total of two contracts from the first two entries which were for 1 contract each.

If you have any further questions or comments please let me know.

fibonaccci
Posts: 44
Joined: 26 Dec 2009
Has thanked: 19 times
Been thanked: 1 time

Re: covering only part of a pyramid

Postby fibonaccci » 29 Feb 2012

Hello,
can anyone help please with a simple idea for scaling in and out.
After an opening trade condition occured, I just want to add up positions each time the price moves by a certain percentage (x%) and sell each single position when profit target (y%) is hit or sell each single position when stoploss level is hit.
Below is my code which opens positions the right way but sell all positions at once or separately but at the same price level.
Thank you for any help.

Code: Select all

{[IntrabarOrderGeneration = true]}
var: ce(0),twoctr(0),threectr(0),startcounttwo(false),startcountthree(False);

inputs:
    smalength ( 200 ),
    emalength ( 100 ),
    hmalength ( 34 ),
    target1 ( 12 ),
    target2 ( 12 ),
    target3 ( 20 ),
    stopsize ( 12 ),
    BE2 ( 0 ), // 0=false, 1=true
    BE3 ( 0 ); // 0=false, 1=true
   
vars:
    TickSize    ( MinMove*10 / PriceScale ), { *10 }
    smav        ( 0 ),
    emav        ( 0 ),
    hmav        ( 0 ),
    t1        ( Target1 * TickSize ),
    t2        ( (Target1 + Target2) * TickSize ),
    t3        ( (Target1 + Target2 + Target3) * TickSize ),
    st1        ( 0 ),
    st2        ( 0 ),
    st3        ( 0 );
   





inputs: 
     Price( Close ) , 
     Length( 9 ) ,
     IncrementPct( 0.05 ) ;
 
variables:
     AddPrice1( 0 ) ,
     AddPrice2( 0 ) ,
     AddPrice3( 0 ) ,
     AddPrice4( 0 ) ,
     AddPrice5( 0 ) , 
     MP( 0 ) ,
     PosHigh ( 0 ) ;
 

if MarketPosition = 0 and Price Crosses over AverageFC( Price, Length ) then 
     Buy ( "one" ) 100000 contracts next bar at market ;
 
MP = MarketPosition ;
 
PosHigh = MaxList( PosHigh , High ) ;
 
if MP = 1 and MP <> MP[1] then 
     begin
     PosHigh = High ;
     AddPrice1 = EntryPrice * ( 1 + IncrementPct / 100 ) ;
     AddPrice2 = EntryPrice * ( 1 + IncrementPct * 2 / 100 ) ;
     end ;

 
if MarketPosition = 1 and PosHigh < AddPrice1 then
     Buy ("Inc1")100000 contracts next bar at AddPrice1 stop ;
if MarketPosition = 1 and PosHigh < AddPrice2 then
     Buy ("Inc2")100000 contracts next bar at AddPrice2 stop ;

     
     
     
{-------------  EXITS  -----------------------------------}
     
if marketposition >= 1 then setstoploss(200);   

if marketposition = 1 then sell ("c.one")100000 contracts Total next bar At (EntryPrice *1.005) Limit;     
if marketposition = 2 then sell ("c.Inc1")100000 contracts Total next bar At (EntryPrice *1.005) Limit;
if marketposition = 3 then sell ("c.Inc2")100000 contracts Total next bar At (EntryPrice *1.005) Limit;


or

Code: Select all

{-------------  EXITS  -----------------------------------}
     
if marketposition >= 1 then setstoploss(200);   

if marketposition = 1 then Sell ("LX-1") from entry ("one") 100000 contracts total next bar at (EntryPrice *1.005) Limit;     
if marketposition = 2 then Sell ("LX-2") from entry ("Inc1") 100000 contracts total next bar at (EntryPrice *1.005) Limit;     
if marketposition = 3 then Sell ("LX-3") from entry ("Inc2") 100000 contracts total next bar at (EntryPrice *1.005) Limit; 

User avatar
JoshM
Posts: 2089
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1486 times
Contact:

Re: covering only part of a pyramid

Postby JoshM » 02 Mar 2012

fibonaccci wrote:

Code: Select all

{-------------  EXITS  -----------------------------------}
     
if marketposition >= 1 then setstoploss(200);   

if marketposition = 1 then Sell ("LX-1") from entry ("one") 100000 contracts total next bar at (EntryPrice *1.005) Limit;     
if marketposition = 2 then Sell ("LX-2") from entry ("Inc1") 100000 contracts total next bar at (EntryPrice *1.005) Limit;     
if marketposition = 3 then Sell ("LX-3") from entry ("Inc2") 100000 contracts total next bar at (EntryPrice *1.005) Limit; 

MarketPosition never returns a 2 or a 3, which might explain why your second and third exit orders might not work correctly.

Have you tried something like this?

Code: Select all

if (MarketPosition(0) = 1) then begin

   if (my exit condition ) then begin
      
      Sell ("XL #1") from Entry ("EL #1") all contracts next bar at market;
      Sell ("XL #2") from Entry ("EL #2") all contracts next bar at market;
      Sell ("XL #3") from Entry ("EL #3") all contracts next bar at market;
      Sell ("XL #4") from Entry ("EL #4") all contracts next bar at market;                  
   
   end;

end; //: long MarketPosition
These users thanked the author JoshM for the post:
fibonaccci


Return to “MultiCharts”