Re: [amibroker] Re: Formula Help Needed [1 Attachment]

2010-08-30 Thread Edward Pottasch
Jeff,

over the last year I was able to remove a lot of this exit looping from my AFL 
codes, which makes it faster. With this specific problem I do not see how it 
can be done using array calculations only. However, when using exit loops it 
makes it very easy to add everyting you like. Added code where I add a trailing 
stop. Is very easy to implement,

rgds, 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 ] = Max(H[ j - 1 ] - sellStopLoss[ i ],stopLossArray[ j - 1 
]);
   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);

per1 = 20; 
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, per1), -1);
 
BuyXX = Cross(C, twentydayupper);
buySell_proc(BuyXX,WATR*ATRx,WATR*2,per1);
Buy = BuyAdjusted;
BuyPrice = BuyPriceAdjusted;
Sell = SellAdjusted;
SellPrice = SellPriceAdjusted;
 
// chart
GraphXSpace = 5;
SetChartOptions(0, chartShowDates);
Plot(stopLossArray,\nsellStopLoss,colorOrange,1);
Plot(stopProfitArray,\nsellStopProfit,colorGreen,1);
Plot( C, \nCandle,colorWhite, styleCandle );
Plot(twentydayupper,,colorBlue,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: Monday, August 30, 2010 2:21 AM
To: amibroker@yahoogroups.com 
Subject: [amibroker] Re: Formula Help Needed


  
Thanks for your help Ed. That works great. Any chance of changing that exit at 
stop profit(SellPriceAdjusted) to a trailing stop?

--- In amibroker@yahoogroups.com, cas soni soni...@... wrote:

 Hello Edward,wow..great .   .i was looking for something like this [ i.e- 
 your code ]Thank you
 
 --- On Sun, 29/8/10, Edward Pottasch empotta...@... wrote:
 
 From: Edward Pottasch empotta...@...
 Subject: Re: [amibroker] Re: Formula Help Needed
 To: amibroker@yahoogroups.com
 Date: Sunday, 29 August, 2010, 1:18 AM
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 Â 
 
 
 
 
 
 
 
 
 
 
 
 
 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;
 Â Â Â }
 Â Â Â

Re: [amibroker] Re: Formula Help Needed

2010-08-28 Thread Edward Pottasch
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; 
 

Re: [amibroker] Re: Formula Help Needed

2010-08-28 Thread cas soni
Hello Edward,wow..great .   .i was looking for something like this [ i.e- your 
code ]Thank you

--- On Sun, 29/8/10, Edward Pottasch empotta...@skynet.be wrote:

From: Edward Pottasch empotta...@skynet.be
Subject: Re: [amibroker] Re: Formula Help Needed
To: amibroker@yahoogroups.com
Date: Sunday, 29 August, 2010, 1:18 AM















 
 



  



  
  
  


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