[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [amibroker] Profit stops with loop - not working as it should



PureBytes Links

Trading Reference Links

Well, it's not completely clear to me so here are some ideas:

 

SellPrice is a built-in ARRAY used to hold the ARRAY of Sell prices
which are normally set to Open or Close. It seems you are trying to
change that array to mean something else, like your target price. Plus
it IS an ARRAY so when it's set it does not carry forward to the next
bar unless you code it to do so.

 

Plus you are trying to set the SellPrice based on hitting a High, but
you have daily data. You don't know if the High came first or the Low.
Your code assumes the high always comes first.

 

It's a lot of code to go through so the above (partial) analysis may be
off base. I hope it is not and gives you a place to start. I have to do
my own trading for the evening now :-)

--

Terry

 

-----Original Message-----
From: amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx] On
Behalf Of onelkm
Sent: Monday, October 02, 2006 19:23
To: amibroker@xxxxxxxxxxxxxxx
Subject: [amibroker] Profit stops with loop - not working as it should

 

I am trying to learn loops by using the example below from the help 

files. The sell price when the profit stop is hit should be defined 

by the line:

SellPrice[ i ] = Max( Open[ i ], ( 1 + firstProfitTarget * 0.01 ) *
priceatbuy ); 

Instead of this, the close for the day is the sell price. Why? I am 

using Yahoo data with OHLC.

Thanks in advance

Larry

 

/*Example 4: partial exit (scaling out) on profit target stops

Scale In/Out - showing the number of scale-in and scale-out 

operations within given trade 

Example of code that exits 50% on first profit target, 50% on next 

profit target AND everything at trailing stop:*/

 

SetOption("NoDefaultColumns",0);       

SetOption("InitialEquity",1000);         

SetOption("AllowSameBarExit",1);             

SetOption("ActivateStopsImmediately",1);    

SetOption("AllowPositionShrinking",1);      

SetOption("FuturesMode",0);              

SetOption("InterestRate",0);              

SetOption("MaxOpenPositions",1000);       

SetOption("MinShares",.001);              

SetOption("PriceBoundChecking",1) ;        

SetOption("CommissionMode",1); //% per trade  

SetOption("CommissionAmount",0);            

SetOption("MarginRequirement",100); //No margin   

SetOption("ReverseSignalForcesExit",1);       

SetOption("UsePrevBarEquityForPosSizing",1);                

SetTradeDelays(0,0,0,0);                              

 

Buy = Cross( MA( C, 10 ), MA( C, 50 ) ); 

Sell = 0; 

 

// the system will exit 

// 50% of position if FIRST PROFIT TARGET stop is hit 

// 50% of position is SECOND PROFIT TARGET stop is hit 

// 100% of position if TRAILING STOP is hit */

 

FirstProfitTarget = 1; // profit 

SecondProfitTarget = 2;// in percent 

TrailingStop = 3;// also in percent 

 

priceatbuy=0; 

highsincebuy = 0; 

 

exit = 0; 

 

for( i = 0; i < BarCount; i++ ) 

{ 

   if( priceatbuy == 0 AND Buy[ i ] ) 

    { 

       priceatbuy = BuyPrice[ i ]; 

    } 

 

   if( priceatbuy > 0 ) 

    { 

       highsincebuy = Max( High[ i ], highsincebuy ); 

 

      if( exit == 0 AND High[ i ] >= ( 1 + FirstProfitTarget * 0.01 ) *
priceatbuy ) 

                        

       { 

         // first profit target hit - scale-out 

         exit = 1; 

              SellPrice[ i ] = Max( Open[ i ], ( 1 + firstProfitTarget *
0.01 ) * priceatbuy ); 

         Buy[ i ] = sigScaleOut; 

       } 

 

      if( exit == 1 AND High[ i ] >= ( 1 + SecondProfitTarget * 0.01 ) *
priceatbuy ) 

       { 

         // second profit target hit - exit 

         exit = 2; 

         SellPrice[ i ] = Max( Open[ i ], ( 1 + SecondProfitTarget *
0.01 ) * priceatbuy ); 

       } 

 

      if( Low[ i ] <= ( 1 - TrailingStop * 0.01 ) * highsincebuy ) 

       { 

         // trailing stop hit - exit 

         exit = 3;    

         SellPrice[ i ] = Min( Open[ i ], ( 1 - TrailingStop * 0.01 ) *
highsincebuy ); 

       } 

 

      if( exit >= 2 ) 

       { 

         Buy[ i ] = 0; 

         Sell[ i ] = exit + 1; // mark appropriate exit code 

         exit = 0; 

         priceatbuy = 0; // reset price 

         highsincebuy = 0; 

       } 

    } 

} 

 

SetPositionSize( 100, spsPercentOfEquity ); 

SetPositionSize( 50, spsPercentOfPosition * ( Buy == 

sigScaleOut ) ); // scale out 50% of position