Australian (ASX) Stock Market Forum

CTA

Joined
8 June 2015
Posts
2
Reactions
0
Hi,

Does anyone have code for pyramiding / scaling into positions based on ATR? I would like to e.g., add to an existing position if the price moves up 2ATRs from the initial entry price using the ATR at the time of trade entry.

I have check the SigScaleIn function from the Amibroker help website, but can't seem to be able to adapt this to the above.

Many thanks for your help!
 
Hi,

Does anyone have code for pyramiding / scaling into positions based on ATR? I would like to e.g., add to an existing position if the price moves up 2ATRs from the initial entry price using the ATR at the time of trade entry.

I used this example code to do something similar:
http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/

You'll need to store the ATR change since trade entry array as a static variable to access the data in the backtester.
 
Hi,

Does anyone have code for pyramiding / scaling into positions based on ATR? I would like to e.g., add to an existing position if the price moves up 2ATRs from the initial entry price using the ATR at the time of trade entry.

I have check the SigScaleIn function from the Amibroker help website, but can't seem to be able to adapt this to the above.

Many thanks for your help!


Were you able to do this implementation? I need it.

Thanks.
 
I used this example code to do something similar:
http://www.amibroker.com/kb/2006/03/06/re-balancing-open-positions/

You'll need to store the ATR change since trade entry array as a static variable to access the data in the backtester.

I started to implement could you give me some hint? to complete?

for( pos = bo.GetFirstOpenPos(); pos; pos = bo.GetNextOpenPos() )
{

price = pos.GetPrice( bar, "C" );
symbolATR = StaticVarGet( sig.Symbol + "ATR" );
price1 = pos.EntryPrice;

if( price1 > (price + 0.5*symbolATR));
{
bo.ScaleTrade( bar, pos.Symbol, diff < 0, price, 10000, MarginDeposit);
}
}
 
Here is a part of the code ..


for(bar=0; bar < BarCount; bar++)
{
bo.ProcessTradeSignals( bar );

//CurEquity = bo.Equity;

for( pos = bo.GetFirstOpenPos(); pos; pos = bo.GetNextOpenPos() )
{

price = pos.GetPrice( bar, "C" );
symbolATR = StaticVarGet( pos.Symbol + "ATR" );
price1 = pos.EntryPrice;
investi = -10;

if( price > (price1 + 0.5*symbolATR[0]));
{
bo.ScaleTrade( bar, pos.Symbol, investi < 0, price, investi, MarginDeposit);
}
}
}
 
I am trying to set a pyramiding in based on liquidity. This is an extract of the code

Code:
Liq = EMA( C * V, 10 ); 
CV = 500000; // Volume 
Liqfactor = Liq > CV; // Liquidity filter   
 
LowLiq =     Liq <=     1000000; 
MedLiq =     Liq >     1000000 AND Liq <= 5000000; 
NormalLiq = Liq >     5000000;
 
Buysig = cond1  AND cond2;  
  
buysigLow = buysig AND LowLiq;  
buysigMed = buysig AND MedLiq;  
BuysigNormal = buysig AND NormalLiq;  
  
sellsig1 = C<eMA(C,15);  
  
x = SumSince(sellsig1,buysigLow OR buysigMed OR buysigNormal) ;  

xsigsLow= x<=3;   
xsigsMed = x<=2;  
xsigsNorm= x <=1;   
 
   
BuyLow = buysigLow AND xsigsLow;  
BuyMed = buysigMed AND xsigsMed;  
BuyNorm = buysigNormal AND xsigsNorm;  
  
Buy = IIf(BuyLow OR BuyMed,sigScaleIn,IIf(BuyNorm,1,0));  
Sell = Sellsig1;  
  
//Buy = ExRem( Buy, Sell ); // Removes additional buy signals  
Sell = ExRem( Sell, Buy ); // Removes additional sell signals  
 
PositionSize = Ref( PositionSize, -1 );  

Filter = Buy OR Sell; // Buy & Sell Filters  

BuyOffered = Close * 1.03; // +3% Buy premium over the last closing price  
BuyOffer = ceil( BuyOffered * 100 ) / 100; // The amount is rounded up no matter the price (ceil function used)  
  
SellOffered = Close * 0.97; // -3% Sell premium below the last closing price  
SellOffer = floor( SellOffered * 100 ) / 100; // The amount is rounded down no matter the price (floor function used)  

ToBuyPosSize = floor( TradingFunds / BuyOffer ); // Trading Funds divided by buy offer of (+3%) buy premium over the last closing price  
ToBuyPosCost = BuyOffer * ToBuyPosSize; // The cost of buying the amount of share  
  
 SetPositionSize(IIf(sigScaleIn, IIf(BuyLow, 3000,IIf(BuyMed,6000,10000)),ToBuyPosSize),spsValue);

I am struggling with this code. In the case where a buysig has occurred and a BuyMed condition is in play, the first buy is correct at $6000, but the second buy is generated at $10,000, notwithstanding the BuyMed condition is still true.

Can someone identify what is wrong in the setpositionsize code?

TIA
 


Write your reply...
Top