Jeff,

I agree with cas soni that plotting the problem makes it easier to solve.

You can use the valuewhen() function but the problem is that before a sell is 
found new buy signals emerge. That is why I would solve such a problem using an 
exit loop. See code below. If this can be solved using array calculations only 
I woud not know how,

regards, Ed


procedure buySell_proc(BuyXX,sellStopProfit,sellStopLoss,initPer) 
{
global BuyAdjusted;
global BuyPriceAdjusted;
global SellAdjusted;
global SellPriceAdjusted;
global stopProfitArray;
global stopLossArray;

BuyAdjusted = 0;
BuyPriceAdjusted = 0;
SellAdjusted = 0;
SellPriceAdjusted = 0;
stopProfitArray = Null;
stopLossArray = Null;
slip = 0;

for( i = initPer + 1; i < BarCount; i++ ) 
{
 if (BuyXX[ i ]) 
 {
  BuyAdjusted[ i ] = 1;
  BuyPriceAdjusted[ i ] = C[ i ] + slip;
  stopProfitArray[ i ] = BuyPriceAdjusted[ i ] + sellStopProfit[ i ];
  stopLossArray[ i ] = BuyPriceAdjusted[ i ] - sellStopLoss[ i ];
  
  for (j = i + 1; j < BarCount; j++) 
  { 
   stopLossArray[ j ] = stopLossArray[ i ];
   stopProfitArray[ j ] = stopProfitArray[ i ];
   // exit at stop loss
   if (C[ j ] < stopLossArray[ j ])
   {
    SellAdjusted[ j ] = 1;
    SellPriceAdjusted[ j ] = C[ j ] - slip;
    i = j;
    break;
   }  
   // exit at stop profit
   else if (C[ j ] > stopProfitArray[ j ])
   {
    SellAdjusted[ j ] = 1;
    SellPriceAdjusted[ j ] = C[ j ]- slip;
    i = j;
    break;
   }
   // to avoid problems at the end of the array
   else if (j == BarCount - 1) 
   { 
    i = BarCount;
    break;
   }
  }
 } 
} 
}

SetBarsRequired(sbrAll,sbrAll);

ATRx = Optimize("ATR Multiplier", 3, 1, 5, 1); // ATR Multiplier
WATR = ((Ref(ATR(8),-2)*8) + (ATR(1)*1.125) + (Ref(ATR(1)*1.125,-1)))/10; 
//Weighted ATR (last 20% has a 12.5% weighting factor)
twentydayupper = Ref(HHV(H, 20), -1);

BuyXX = Cross(C, twentydayupper);
buySell_proc(BuyXX,WATR*ATRx,WATR*2,8);
Buy = BuyAdjusted;
BuyPrice = BuyPriceAdjusted;
Sell = SellAdjusted;
SellPrice = SellPriceAdjusted;

// chart
GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
Plot( C, "\nCandle",colorWhite, styleCandle );
Plot(twentydayupper,"",colorBlue,1); 
Plot(stopLossArray,"\nsellStopLoss",colorRed,1);
Plot(stopProfitArray,"\nsellStopProfit",colorGreen,1);
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorGreen,0,L,-15);
PlotShapes(IIf(Buy,shapeHollowSmallCircle,shapeNone),colorWhite,0,BuyPrice,0);
PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,H,-15);
PlotShapes(IIf(Sell,shapeHollowSmallCircle,shapeNone),colorWhite,0,SellPrice,0);



From: JEFF F 
Sent: Saturday, August 28, 2010 7:14 PM
To: amibroker@yahoogroups.com 
Subject: [amibroker] Re: Formula Help Needed


  
Thanks cas soni,
I appreciate your input, and adding the plot. The problem I am having is the 
sellprice and sellstop seem to be floating numbers and recalculated every day. 
I want them to be calculated once at entry, and remain that fixed value until a 
sell is triggered. Any help is much appreciated.

--- In amibroker@yahoogroups.com, cas soni <soni...@...> wrote:
>
> Hello Jeff ,
> First let me say i am not an afl expert , 
> so please try this .... 
> ATRx = Optimize("ATR Multiplier", 3, 1, 5, 1); // ATR Multiplier 
> WATR = (Ref(ATR(8),-2)*8 + (ATR(1)*1.125) + (Ref(ATR(1)*1.125,-1)))/10; 
> //Weighted ATR (last 20% has a 12.5% weighting factor) 
> twentydayupper = Ref(HHV(H, 20), -1); 
> 
> 
> 
> Plot(C,"",3,64); 
> Plot(twentydayupper,"",7,1); 
> 
> 
> Buy = Cross(C, twentydayupper); 
> 
> BuyPrice=ValueWhen(Buy,C,1); 
> 
> Sellstop = BuyPrice - WATR*2; 
> 
> SellPrice = BuyPrice + WATR*ATRx; 
> Sell = Cross(Sellstop, C) OR Cross(C, SellPrice); 
> Plot(SellPrice,"sellprice",4,1); 
> Plot(Sellstop,"sellstop",6,1); 
> 
> PlotShapes(Buy*shapeUpArrow,colorGreen,0,L,-5); 
> PlotShapes(Sell*shapeDownArrow,colorRed,0,H,-5); 
> 
> AddColumn(ATRx, "ATRx"); 
> AddColumn(ATR(10), "ATR"); 
> AddColumn(WATR, "Weighted ATR"); 
> AddColumn(Ref(ATR(8),-2), "ATR(8) Two Days Ago"); 
> AddColumn(ATR(1)*1.125, "Weighted ATR Today"); 
> AddColumn(Ref(ATR(1)*1.125,-1), "Weighted ATR Yesterday"); 
> AddColumn(twentydayupper, "20 Day Upper"); 
> AddColumn(Sellstop, "Sellstop"); 
> AddColumn(SellPrice, "SellPrice"); 
> AddColumn(BuyPrice, "BuyPrice"); 
> AddColumn(C, "Close"); 
> 
> Filter=1; 
> Hope this helps
> Thank you
> 
> --- On Sat, 28/8/10, JEFF F <je...@...> wrote:
> 
> 
> From: JEFF F <je...@...>
> Subject: [amibroker] Formula Help Needed
> To: amibroker@yahoogroups.com
> Date: Saturday, 28 August, 2010, 8:53 PM
> 
> 
> Â  
> 
> 
> 
> If one of the AFL experts out there could help me out, it would be greatly 
> appreciated. I do not understand why, in this formula the sell price is not 
> being executed according to my stops. Please understand this is a 
> work-in-progress and not meant to be a complete formula. I put in all the 
> columns to try and trace the calculations. I'm sure it is a simple answer, 
> but I seem to be missing it.
> 
> ATRx = Optimize("ATR Multiplier", 3, 1, 5, 1); // ATR Multiplier
> WATR = ((Ref(ATR(8),-2)*8) + (ATR(1)*1.125) + (Ref(ATR(1)*1.125,-1)))/10; 
> //Weighted ATR (last 20% has a 12.5% weighting factor)
> twentydayupper = Ref(HHV(H, 20), -1);
> 
> Sellstop = BuyPrice - WATR*2;
> SellPrice = BuyPrice + WATR*ATRx;
> 
> Buy = Cross(C, twentydayupper);
> Sell = Cross(Sellstop, C) OR Cross(C, SellPrice);
> 
> AddColumn(ATRx, "ATRx");
> AddColumn(ATR(10), "ATR");
> AddColumn(WATR, "Weighted ATR");
> AddColumn(Ref(ATR(8),-2), "ATR(8) Two Days Ago");
> AddColumn(ATR(1)*1.125, "Weighted ATR Today");
> AddColumn(Ref(ATR(1)*1.125,-1), "Weighted ATR Yesterday");
> AddColumn(twentydayupper, "20 Day Upper");
> AddColumn(Sellstop, "Sellstop");
> AddColumn(SellPrice, "SellPrice");
> AddColumn(BuyPrice, "BuyPrice");
> AddColumn(C, "Close");
> 
> Filter=1;
> 
> Any help is appreciated
>



Reply via email to