Simple Coding Help - No Promise.

ethan hunt

Well-Known Member
Suppose:

Buy_Valid = ExRem(Buy_Valid,Sell_Valid);
Sell_Valid = ExRem(Sell_Valid,Buy_Valid);
Buy=Buy_Valid;
Sell=Sell_Valid OR (code required: SL or TGT or TSL reached);
Short=Sell_Valid;
Cover=Buy_Valid OR (code required: SL or TGT or TSL reached);

need code for condition stated in blue above, for scan.

thanks
bumpwa ...
 

mastermind007

Well-Known Member
Dear ALL,

How to write an AFL if I want to enter the position once only each day for intraday trading. For example, enter position after 9:30 (market started at 9:15) if signal generated and closed before the close of market and 1 trade everyday only. The following AFL was used:

OpenTime = 093000;
CloseTime = 160000;
tn = TimeNum();
ExitTime = tn >= CloseTime;
EntryTime = tn >= OpenTime AND tn < CloseTime;
Buy = C > EMA(C, 60) AND EntryTime;
Short = C < EMA(C, 30) AND EntryTime;
Sell = ExitTime;
Cover = ExitTime;
Buy = ExRem (Buy, Sell);
Sell = ExRem (Sell, Buy);
Short = ExRem (Short, Cover);
Cover = ExRem (Cover, Short);
ApplyStop (stopTypeLoss, stopModePercent, 2, True, True);

Thank you!!!
 

docvijay4u

Well-Known Member
Hi, I need afl for three moving average crossover. Let us say 5, 15, 20 SMA crossover for buy and 20, 15, 5 for sell. Please Help.

Thanks.
 

Nehal_s143

Well-Known Member
Hi, I need afl for three moving average crossover. Let us say 5, 15, 20 SMA crossover for buy and 20, 15, 5 for sell. Please Help.

Thanks.
Try this

// 5, 15, 20 SMA crossover for Buy AND 20, 15, 5 for Sell.

_SECTION_BEGIN("MA Cross");

e1=MA(C,5);
e2=MA(C,15);
e3=MA(C,20);

Plot(e1,"",6,1);
Plot(e2,"",4,1);
Plot(e3,"",2,1);

b1=Cross(Cross(e1,e2),Cross(e2,e3));
s1=Cross(Cross(e3,e2),Cross(e2,e1));
Buysetup=Flip(b1,s1);
Shortsetup=Flip(s1,b1);
trend=IIf(BarsSince(Buysetup)<BarsSince(Shortsetup ),1,0);
Col=IIf(trend==1,5,4);
SetBarFillColor(Col);

Buy1= C>O AND b1;
Sell1=C<O AND Cross(e2,e1) ;
Short1= C<O AND s1;
Cover1=C>O AND Cross(e1,e2);

Buy1=ExRem(Buy1,Sell1);
Short1=ExRem(Short1,Cover1);
Sell1=ExRem(Sell1,Buy1);
Cover1=ExRem(Cover1,Short1);

PlotShapes(Buy1*shapeUpArrow,colorBlue,0,L,-45);
PlotShapes(Sell1*shapeHollowDownArrow,colorPink,0,L,-45);
PlotShapes(Short1*shapeDownArrow,colorRed,0,H,-45);
PlotShapes(Cover1*shapeHollowUpArrow,colorSkyblue,0,H,-45);

Plot(C,"Close",Col,64);


_SECTION_END();

you have not given exit rule, I have set exit rule from my side, which may be not matching your rule.
 

amitrandive

Well-Known Member
Try this

// 5, 15, 20 SMA crossover for Buy AND 20, 15, 5 for Sell.

_SECTION_BEGIN("MA Cross");

e1=MA(C,5);
e2=MA(C,15);
e3=MA(C,20);

Plot(e1,"",6,1);
Plot(e2,"",4,1);
Plot(e3,"",2,1);

b1=Cross(Cross(e1,e2),Cross(e2,e3));
s1=Cross(Cross(e3,e2),Cross(e2,e1));
Buysetup=Flip(b1,s1);
Shortsetup=Flip(s1,b1);
trend=IIf(BarsSince(Buysetup)<BarsSince(Shortsetup ),1,0);
Col=IIf(trend==1,5,4);
SetBarFillColor(Col);

Buy1= C>O AND b1;
Sell1=C<O AND Cross(e2,e1) ;
Short1= C<O AND s1;
Cover1=C>O AND Cross(e1,e2);

Buy1=ExRem(Buy1,Sell1);
Short1=ExRem(Short1,Cover1);
Sell1=ExRem(Sell1,Buy1);
Cover1=ExRem(Cover1,Short1);

PlotShapes(Buy1*shapeUpArrow,colorBlue,0,L,-45);
PlotShapes(Sell1*shapeHollowDownArrow,colorPink,0,L,-45);
PlotShapes(Short1*shapeDownArrow,colorRed,0,H,-45);
PlotShapes(Cover1*shapeHollowUpArrow,colorSkyblue,0,H,-45);

Plot(C,"Close",Col,64);


_SECTION_END();

you have not given exit rule, I have set exit rule from my side, which may be not matching your rule.
Nehal_s143

Thanks for the code .
Can you please make this code adjustable for any period MA and any type of MA like MA,EMA,etc?
 

Nehal_s143

Well-Known Member
hi

I want to use MA with RWI, I am getting error which says, missing arguments
if I dont use MA, afl is working properly

please help

_SECTION_BEGIN("RWI");

//RW=RWI(9,40);
RW=MA(RWI(9,40));

Plot(RW," rwi",colorBlue,styleThick);

Plot(1,"1",colorYellow,styleDashed);

_SECTION_END();
 

amitrandive

Well-Known Member
hi

I want to use MA with RWI, I am getting error which says, missing arguments
if I dont use MA, afl is working properly

please help

_SECTION_BEGIN("RWI");

//RW=RWI(9,40);
RW=MA(RWI(9,40));

Plot(RW," rwi",colorBlue,styleThick);

Plot(1,"1",colorYellow,styleDashed);

_SECTION_END();
Nehal_s143

Attached code working properly

Code:
_SECTION_BEGIN("RWI");

//RW=RWI(9,40);
RW=MA(RWI(9,40),5);

Plot(RW," rwi",colorBlue,styleThick);

Plot(1,"1",colorYellow,styleDashed);

_SECTION_END();
Also see the link for further advanced RWI code

http://www.traderji.com/amibroker/88630-rwi-system.html

:thumb:
 

sanjn84

Active Member
i want to export my ieod data for a specific date range , i have tried by below afl , which is posted by anant sir , but it is not giving data , it is only showing starting date of databse , plz , can any one correct this afl or can post a afl to export ieod data ...........to csv or txt
for a specific date range .......

// Set the Starting and End Dates
// Make sure these dates are not holidays.

StartDate = ParamDate("Starting Date", "3-6-2013");
EndDate = ParamDate("Start Date", "31-12-2013");

//Find the corresponding Bar Numbers

StartBar = Max(0, LastValue(ValueWhen(DateNum() == StartDate, BarIndex(),1)));
EndBar = Min(BarCount - 1, LastValue(ValueWhen(DateNum() == EndDate, BarIndex(),1)));

Filter = 1; // This allows all required data to be included

// Before running the AFL Make sure that C:\SaveData directory exists

fh = fopen( "c:\\SaveData\\"+Name()+".csv", "w");
if( fh )
{
fputs( "Ticker,Date,time,Open,High,Low,Close,Volume \n", fh );
y = Year();
m = Month();
d = Day();
r = Hour();
e = Minute();
n = Second();

for( i = StartBar; i <= EndBar; i++ )
{
fputs( Name() + "," , fh );
ds = StrFormat("%02.0f-%02.0f-%02.0f,",
y[ i ], m[ i ], d[ i ] );
fputs( ds, fh );

ts = StrFormat("%02.0f:%02.0f:%02.0f,",
r[ i ],e[ i ],n[ i ] );
fputs( ts, fh );

qs = StrFormat("%.4f,%.4f,%.4f,%.4f,%.0f\n",
O[ i ],H[ i ],L[ i ],C[ i ],V[ i ] );
fputs( qs, fh );
}

fclose( fh );
}

// The following lines are redundant but are required for the Exploration to work.
// These lines will just output the data being written to the file.

SetOption("NoDefaultColumns", True);
AddTextColumn(Name(), "Symbol", 77);
AddColumn(DateTime(), "Date", formatDateTime);
AddColumn(O, "Open", 6.2);
AddColumn(H, "High", 6.2);
AddColumn(L, "Low", 6.2);
AddColumn(C, "Colse", 6.2);
AddColumn(V, "Volume", 10.0);
 

Similar threads