Simple Coding Help - No Promise.

amitrandive

Well-Known Member
I was also trying some tweaks and reached somewhere by using the following code :

Buy1 = Valuewhen(Buy, C, 1);
Buy2 = Valuewhen(Buy, C, 2);

Similar for Sell Values, but not able get expected results.
Found this dashboard in Amibroker library, will give cumulative P&L for last five trades.You need to put in Buy/Sell Rules from your AFL.

Code:
_SECTION_BEGIN("Last Five Trades Dashboard");

//set initial values

SetOption( "InitialEquity", 100000);
SetOption("FuturesMode" ,false);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",0);
SetOption("AccountMargin",100);
SetOption("RefreshWhenCompleted",True);
SetTradeDelays(0,0,0,0);
SetPositionSize(1,spsShares);

//set trading rules

Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );

Short = Sell;
Cover = Buy;

BuyPrice=ValueWhen(Buy,C);
SellPrice=ValueWhen(Sell,C);
ShortPrice=ValueWhen(Short,C);
CoverPrice=ValueWhen(Cover,C);

//plot buy or sell arrows

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);        
             
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);     
                
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);


// Calculate Equity Curve

eq = Equity( 1, 0 ); 

//////////////////////////////////////////////////
// Calculate the Last Five Trades Profit/Losses //
//////////////////////////////////////////////////

tradesback = 5; 
Signum = Cum( Buy ) + Cum( Short ); 
Signumstart1 = LastValue( SigNum ) - ( tradesback - 1 ); 
Signumstart2 = LastValue( SigNum ) - ( tradesback - 2 ); 
Signumstart3 = LastValue( SigNum ) - ( tradesback - 3 ); 
Signumstart4 = LastValue( SigNum ) - ( tradesback - 4 ); 
Signumstart5 = LastValue( SigNum ) - ( tradesback - 5 ); 

bi = BarIndex(); 
bistart = ValueWhen( signum == signumstart1, bi ); 
bicond = bi >= bistart AND bi <= LastValue( bi ); 


SellPL = IIf( Sell AND bicond, C-buyprice, 0 ); 
CovPL = IIf( Cover AND bicond, ShortPrice - C,0 ); 

cumPL = SellPL + CovPL;

//Plot (SellPL,"Sell",colorGreen,styleHistogram,maskhistogram);
///Plot (CovPL,"Cover", colorRed,styleHistogram,maskhistogram);

lsince = lowestSince(Sell OR Cover, cumPL, 0);	
hsince = highestSince(Sell OR cover, CumPL, 0);


vs= IIf(lsince==0,hsince,lsince);


PL1 = ValueWhen( signum == signumstart1 , vs,1 );
PL2 = ValueWhen( signum == signumstart2 , vs,1 ); 
PL3 = ValueWhen( signum == signumstart3 , vs,1 );  
PL4 = ValueWhen( signum == signumstart4 , vs,1 ); 
PL5 = ValueWhen( signum == signumstart5,  vs ,1 ); 

//////////////////////////////////////////////////
//   Plot the Last Five Trades Profit/Losses    //
//////////////////////////////////////////////////


Title = EncodeColor(colorWhite)+ "Backtest Results  from www.marketcalls.in" + "
- " +  Name() + " - " + EncodeColor(colorRed)+ Interval(2) +
EncodeColor(colorWhite) +

 "  - " + Date() +" - "+"\n" +EncodeColor(colorRed) +"Op-"+O+"  "+"Hi-"+H+" 
"+"Lo-"+L+"  "+

"Cl-"+C+"  "+ "Vol= "+ WriteVal(V)+"\n"+ EncodeColor(colorYellow)+ "\n\n\nLast 5
Trade Results\n" +
"\nTrade1= " + PL1
+"\n"+ "Trade2= " + PL2
+"\n"+ "Trade3= " + PL3
+"\n"+ "Trade4= " + PL4
+"\n"+ "Trade5= " + PL5;
 
Last edited:

msa5678

Well-Known Member
Found this dashboard in Amibroker library, will give cumulative P&L for last five trades.You need to put in Buy/Sell Rules from your AFL.

Code:
_SECTION_BEGIN("Last Five Trades Dashboard");

//set initial values

SetOption( "InitialEquity", 100000);
SetOption("FuturesMode" ,false);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",0);
SetOption("AccountMargin",100);
SetOption("RefreshWhenCompleted",True);
SetTradeDelays(0,0,0,0);
SetPositionSize(1,spsShares);

//set trading rules

Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );

Short = Sell;
Cover = Buy;

BuyPrice=ValueWhen(Buy,C);
SellPrice=ValueWhen(Sell,C);
ShortPrice=ValueWhen(Short,C);
CoverPrice=ValueWhen(Cover,C);

//plot buy or sell arrows

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);        
             
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);     
                
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);


// Calculate Equity Curve

eq = Equity( 1, 0 ); 

//////////////////////////////////////////////////
// Calculate the Last Five Trades Profit/Losses //
//////////////////////////////////////////////////

tradesback = 5; 
Signum = Cum( Buy ) + Cum( Short ); 
Signumstart1 = LastValue( SigNum ) - ( tradesback - 1 ); 
Signumstart2 = LastValue( SigNum ) - ( tradesback - 2 ); 
Signumstart3 = LastValue( SigNum ) - ( tradesback - 3 ); 
Signumstart4 = LastValue( SigNum ) - ( tradesback - 4 ); 
Signumstart5 = LastValue( SigNum ) - ( tradesback - 5 ); 

bi = BarIndex(); 
bistart = ValueWhen( signum == signumstart1, bi ); 
bicond = bi >= bistart AND bi <= LastValue( bi ); 


SellPL = IIf( Sell AND bicond, C-buyprice, 0 ); 
CovPL = IIf( Cover AND bicond, ShortPrice - C,0 ); 

cumPL = SellPL + CovPL;

//Plot (SellPL,"Sell",colorGreen,styleHistogram,maskhistogram);
///Plot (CovPL,"Cover", colorRed,styleHistogram,maskhistogram);

lsince = lowestSince(Sell OR Cover, cumPL, 0);	
hsince = highestSince(Sell OR cover, CumPL, 0);


vs= IIf(lsince==0,hsince,lsince);


PL1 = ValueWhen( signum == signumstart1 , vs,1 );
PL2 = ValueWhen( signum == signumstart2 , vs,1 ); 
PL3 = ValueWhen( signum == signumstart3 , vs,1 );  
PL4 = ValueWhen( signum == signumstart4 , vs,1 ); 
PL5 = ValueWhen( signum == signumstart5,  vs ,1 ); 

//////////////////////////////////////////////////
//   Plot the Last Five Trades Profit/Losses    //
//////////////////////////////////////////////////


Title = EncodeColor(colorWhite)+ "Backtest Results  from www.marketcalls.in" + "
- " +  Name() + " - " + EncodeColor(colorRed)+ Interval(2) +
EncodeColor(colorWhite) +

 "  - " + Date() +" - "+"\n" +EncodeColor(colorRed) +"Op-"+O+"  "+"Hi-"+H+" 
"+"Lo-"+L+"  "+

"Cl-"+C+"  "+ "Vol= "+ WriteVal(V)+"\n"+ EncodeColor(colorYellow)+ "\n\n\nLast 5
Trade Results\n" +
"\nTrade1= " + PL1
+"\n"+ "Trade2= " + PL2
+"\n"+ "Trade3= " + PL3
+"\n"+ "Trade4= " + PL4
+"\n"+ "Trade5= " + PL5;

Thanks a lot Amit. Was racking my brains since morning. I think finally I got it.
 
Dear Amit,

Please provide me an afl considering the following condition. It will prepare a list of stocks using scan in amibroker

for Buy condition
Price> Rs.30
Volume>100000
Price increased more than 3 % in last 5 session

for Sell condition
Price> Rs.30
Volume>100000
Price decreased more than 3 % in last 5 session

The List should be in the following format

(1) Symbol(2)Trade(Buy/Sell)(3) Closing Price on last day of trading (4) Volume on last day of trading (5) Average volume of last 5 trading sessions(6) Percentage of Price increased or decreased on last day of trading (7)Percentage of Price increased or decreased in last 5 trading sessions

Thanks in advance
 
Last edited:

rvlv

Active Member
hi
can you help me get this afl code for doji with conditions please
--------------------------------------------------------------------------------
Software code for the TradeStation Easy Language platform

This code is for the Doji pattern:

Vars: oFastK(0), oSlowK(0), oFastD(0), oSlowD(0);
Value1 = Stochastic(High, Low, Close, 12, 3, 3, 1, oFastK, oSlowK,
oFastD, oSlowD);

If AbsValue(Open - Close) <= Range * 0.1 Then
If Average(Volume, 90) > 200000 Then
If Close > 25 Then
If oSlowK < 20 Then
Plot1(Low, "Doji");
---------------------------------------------------------------
buycond =doji1
buy =IIF((abs(high -low)<= range*0.10 and c>=25 and ma(v,90)>=200000 and stochk(12,3)<20),plot(1,"1",colorred,stylebar);
The first line declares the variables that will be used in the code.
The second line calculates the Stochastic values.
The third line identifies the Doji pattern.
The fourth line requires that at least 200,000 shares of stock
have been traded (on average) for the last 90 days.
The fifth line will require the price to be above 25. This is done
for the purpose of being able to trade options on the stocks
that are found.
The sixth line requires that the stock be in an oversold
condition with the Stochastic below 20.
Be sure to reference oSlowK.
The last line identifies the bar as a Doji.
Before saving the code, it’s important to tell TradeStation how
the Doji will appear on a price chart. This is done under the
‘Properties’ selection under ‘Chart Style’. Make sure the Type is
set to Point. Also, there are 7 choices for Weight, choose at least
#4 or larger.
This code is saved and verified given the name Bigalow Doji.
=======================================
doji plot
idea is locate and find such doji by exploration and see it on chart as well.


THIS CODE IS ONLY FOR DAILY CHARTS.
sorry I forgot to mention it.
 
Last edited:
Code:
function Cbl(bars)
{
cblArr = Null;
if (bars > 0)
{
for (i = 1; i < BarCount; i++)
{
steps = bars - 1; 
mostLow = Low[i]; 
for (j = i-1; j >= 0 && steps > 0; j--) 
{ 
if (Low[j] < mostLow) 
{
mostLow = Low[j]; 
steps--; 
} 
} 
cblArr[i] = mostLow; 
} 
} 
return cblArr; 
} 

function TrailingStop(data) 
{
stop[0] = data[0]; 
for (i = 1; i < BarCount; i++) 
{
if (Close[i] >= stop[i-1]) 
stop[i] = Max(data[i], stop[i-1]); 
else 
stop[i] = data[i]; 
} 
return stop; 
}

cblStop = TrailingStop (Cbl(3));
Sirs/Masters,
Found the above Code on Web which gives desired results for Trailing Stop But the Deterrent is --> IT is Painfully SLOW, takes eons to compltete the Looping Process so making it practically UnUsable for RealTime Trade.
Considering the code was created a few years back and as AMIBROKER & AFL hve Evolved far far ahead since, im Hoping, Praying dat the MASTERs might've come up wid a FASTER replacement/alternative/modified Code which wud give the SAME or almost Same Results wid greater SPEED.
Pls HELP
Thank U All

Rgrds
Shiraj
 
Last edited:

amitrandive

Well-Known Member
Code:
function Cbl(bars)
{
cblArr = Null;
if (bars > 0)
{
for (i = 1; i < BarCount; i++)
{
steps = bars - 1; 
mostLow = Low[i]; 
for (j = i-1; j >= 0 && steps > 0; j--) 
{ 
if (Low[j] < mostLow) 
{
mostLow = Low[j]; 
steps--; 
} 
} 
cblArr[i] = mostLow; 
} 
} 
return cblArr; 
} 

function TrailingStop(data) 
{
stop[0] = data[0]; 
for (i = 1; i < BarCount; i++) 
{
if (Close[i] >= stop[i-1]) 
stop[i] = Max(data[i], stop[i-1]); 
else 
stop[i] = data[i]; 
} 
return stop; 
}

cblStop = TrailingStop (Cbl(3));
Sirs/Masters,
Found the above Code on Web which gives desired results for Trailing Stop But the Deterrent is --> IT is Painfully SLOW, takes eons to compltete the Looping Process so making it practically UnUsable for RealTime Trade.
Considering the code was created a few years back and as AMIBROKER & AFL hve Evolved far far ahead since, im Hoping, Praying dat the MASTERs might've come up wid a FASTER replacement/alternative/modified Code which wud give the SAME or almost Same Results wid greater SPEED.
Pls HELP
Thank U All

Rgrds
Shiraj
Simpler version available on the net:D

Code:
_SECTION_BEGIN("trailstops");
EntrySignal = C > ( LLV( L, 20 ) + 2 * ATR( 10 ) ); 
ExitSignal = C < ( HHV( H, 20 ) - 2 * ATR( 10 ) ); 
Color = IIf( EntrySignal, colorBlue, IIf( ExitSignal, colorOrange, colorGrey50 )); 
TrailStop = HHV( C - 2 * ATR(10), 15 ); 
ProfitTaker = EMA( H, 13 ) + 2 * ATR(10); 
 
/* plot price chart and stops */
Plot( TrailStop, "Trailing stop", colorGold, styleThick | styleLine  ); 
Plot( C, "Price", color, styleBar  ); 
 
/* plot color ribbon */
Plot( 2, "", Color, styleArea | styleOwnScale | styleNoLabel, -0.1, 50 );
 

amitrandive

Well-Known Member
Dear Amit,

Please provide me an afl considering the following condition. It will prepare a list of stocks using scan in amibroker

for Buy condition
Price> Rs.30
Volume>100000
Price increased more than 3 % in last 5 session

for Sell condition
Price> Rs.30
Volume>100000
Price decreased more than 3 % in last 5 session

The List should be in the following format

(1) Symbol(2)Trade(Buy/Sell)(3) Closing Price on last day of trading (4) Volume on last day of trading (5) Average volume of last 5 trading sessions(6) Percentage of Price increased or decreased on last day of trading (7)Percentage of Price increased or decreased in last 5 trading sessions

Thanks in advance
What are your Buy/Sell conditions ?
 

Similar threads