NF Swing Trading using Fractal AFL

oldtrader

Well-Known Member
No, THANK U, 4 all ur hard work.
You have no idea how much this code has helped me. Anyway I am working on partial exits. Percentage code u have given is working perfectly. Please help me in incorporating a SL into the AFL . I am thinking of keeping 0.73% SL . The TSL will also be there, which ever comes first will be activated. Visually I have tested it, It gives great results, have no idea whether it is possible to code it or not.

I am thinking of incorporating 8 levels of profit booking, viz., 0.93, 1.19, 1.5, 2.18,3.69, 5.33, 8.10 % and TSL. Visual checking of 1 year data has given 2599, 2710, 3261 ,3473 ,3287 ,3820, 3501 ,3502 NF points respectively, with SL at 0.65 %. With the profit code I will be checking the optimum % levels on bigger data. Once everything is checked I will post the final results.
 
Last edited:

oldtrader

Well-Known Member
You have no idea how much this code has helped me. Anyway I am working on partial exits. Percentage code u have given is working perfectly. Please help me in incorporating a SL into the AFL . I am thinking of keeping 0.73% SL . The TSL will also be there, which ever comes first will be activated. Visually I have tested it, It gives great results, have no idea whether it is possible to code it or not.

I am thinking of incorporating 8 levels of profit booking, viz., 0.93, 1.19, 1.5, 2.18,3.69, 5.33, 8.10 % and TSL. Visual checking of 1 year data has given 2599, 2710, 3261 ,3473 ,3287 ,3820, 3501 ,3502 NF points respectively, with SL at 0.65 %. With the profit code I will be checking the optimum % levels on bigger data. Once everything is checked I will post the final results.

The below pasted image will explain what I am trying to say. The red line is the SL at 0.73 %, the green lines are the profit booking levels. In the first case long is triggered at 7516.2 , Initially the SL was active , later on the TSL took over and it was triggered. The second case Short was triggered at 7489.9 and the SL got hit at 7544.58 before the TSL, thus saving us some points.

 
Hi

For TSL also you can use the standard ApplyStop code

Get into habit of reading Amibroker help, it even has sample code that can be used

ApplyStop
- apply built-in stop Trading system toolbox
(AFL 1.7)


SYNTAX ApplyStop( type, mode, amount, exitatstop, volatile = False, ReEntryDelay = 0 )
RETURNS nothing
FUNCTION controls built-in stops from the formula level (allows optimization of stops)
Parameters:

type =
0 = stopTypeLoss - maximum loss stop,
1 = stopTypeProfit - profit target stop,
2 = stopTypeTrailing - trailing stop,
3 = stopTypeNBar - N-bar stop

mode =
0 - disable stop (stopModeDisable),
1 - amount in percent (stopModePercent), or number of bars for N-bar stop (stopModeBars),
2 - amount in points (stopModePoint);
3 - amount in percent of profit (risk)
Code:
TSL = Optimize("TSL %",1.5,0,5,0.25);
ApplyStop(2, 1, TSL)
or it can be based it on retracement or
max how much profit you are ready to give back

Code:
TSL = Optimize("TSL %",50,25,75,5);
ApplyStop(2, 3, TSL)
Thanks
Happy :)
 
Last edited:
Happy sir can you rectify the AFL posted by lone wolf two days ribbon shows only last two days but when we go back it don't shows for previous days like
1),I can see only PDH/PDL and 2Days PDH/PDL from 18th May to 20th May only..

If I click on 10th May,I cant see a PDH/PDL coming form 09th May and 2 days PDL/PDL coming from 06h May.
 
Last edited:
Code:
/**************************************
******** PREV SESSION'S HLC ***********
****************************************/

_SECTION_BEGIN("Two Day Range");

// Get Previous Day's High low and Close
PrevHigh = TimeFrameGetPrice("H", inDaily, -1);
PrevLow = TimeFrameGetPrice("L", inDaily, -1);
PrevClose = TimeFrameGetPrice("C", inDaily, -1);

// Get Previous 2 Day's High low and Close
Prev2High = TimeFrameGetPrice("H", inDaily, -2);
Prev2Low = TimeFrameGetPrice("L", inDaily, -2);
Prev2Close = TimeFrameGetPrice("C", inDaily, -2);

// Check bar's time
CurrBarTime = TimeNum();
DayStartTime = 91500;
DayEndTime = 153000;

// Check range condition
IsAllDay = CurrBarTime >= DayStartTime AND CurrBarTime <= DayEndTime;
IsToday = Day() == LastValue(Day()) AND Month() == LastValue(Month());

ShowPrevHLC = ParamToggle("Show Previous day Range", "Yes|No", 1);
PrevRangeStyle = ParamToggle("Previous day range style", "Lines|Range", 0);
ShowTwoDayRange = ParamToggle("Show Two Day Range", "Yes|No", 1);
Prev2RangeStyle = ParamToggle("Previous 2day range style", "Lines|Range", 0);

ColorRangeHigh = ParamColor("Range High Color", colorBlue);
ColorRangeLow = ParamColor("Range Low Color", colorRed);


// Zone Calculation
x= Cum(1);

PDHbi = LastValue(ValueWhen(H==LastValue(PrevHigh), x));
PDH = IIf(x>PDHbi, ValueWhen(H==LastValue(PrevHigh), High), Null);
PDHO = IIf(x>PDHbi, ValueWhen(H==LastValue(PrevHigh), Open), Null);
PDHC = IIf(x>PDHbi, ValueWhen(H==LastValue(PrevHigh), Close), Null);

PDLbi = LastValue(ValueWhen(L==LastValue(prevLow), x));
PDL = IIf(x>PDLbi, ValueWhen(L==LastValue(prevLow), Low), Null);
PDLO = IIf(x>PDLbi, ValueWhen(L==LastValue(PrevLow), Open), Null);
PDLC = IIf(x>PDLbi, ValueWhen(L==LastValue(PrevLow), Close), Null);

P2DHbi = LastValue(ValueWhen(H==LastValue(Prev2High), x));
TwoDH = IIf(x>P2DHbi, ValueWhen(H==LastValue(Prev2High), High), Null);
TwoDHO = IIf(x>P2DHbi, ValueWhen(H==LastValue(Prev2High), Open), Null);
TwoDHC = IIf(x>P2DHbi, ValueWhen(H==LastValue(Prev2High), Close), Null);

P2DLbi = LastValue(ValueWhen(L==LastValue(Prev2Low), x));
TwoDL = IIf(x>P2DLbi, ValueWhen(L==LastValue(Prev2Low), Low), Null);
TwoDLO = IIf(x>P2DLbi, ValueWhen(L==LastValue(Prev2Low), Open), Null);
TwoDLC = IIf(x>P2DLbi, ValueWhen(L==LastValue(Prev2Low), Close), Null);

// Show or Hide Previous Day Range
if(ShowPrevHLC AND Interval() <= 3600)
{

// Previous Day as Lines
if(PrevRangeStyle)
{
Plot(IIf(IsToday,PrevHigh,Null),"",ColorRangeHigh,styleLine|styleThick|styleDashed|styleNoRescale|styleNoLabel);
Plot(IIf(IsToday,PrevLow,Null),"",ColorRangeLow ,styleLine|styleThick|styleDashed|styleNoRescale|styleNoLabel);
Plot(IIf(IsToday,PrevClose,Null),"",colorBlack,styleLine|styleThick|styleDashed|styleNoRescale|styleNoLabel);
}
// Prewvious Day as Range
else
{
Plot(PDH, "", ColorRangeHigh, styleLine|styleNoTitle|styleNoLabel|styleNoRescale);
Plot(PDL, "", ColorRangeLow , styleLine|styleNoTitle|styleNoLabel|styleNoRescale);
PlotOHLC(PDH,PDH,Max(PDHO, PDHC),Max(PDHO, PDHC),"", ColorBlend(ColorRangeHigh, GetChartBkColor(), 0.9),styleNoRescale|styleCloud|styleNoLabel,Null, Null, 0, -5, 5);
PlotOHLC(PDL,PDL,Min(PDLO, PDLC),Min(PDLO, PDLC),"", ColorBlend(ColorRangeLow, GetChartBkColor(), 0.8),styleNoRescale|styleCloud|styleNoLabel,Null, Null, 0, -5, 5);
Plot(IIf(IsToday, PrevClose,Null), "", colorBlack, styleDashed|styleNoRescale|styleNoTitle|styleNoLabel);
}

// Prev Day Rnage Text
PlotText("PDH - " + NumToStr(PrevHigh,1.11), BarCount+1, LastValue(PrevHigh),ColorRangeHigh );
PlotText("PDL - " + NumToStr(PrevLow,1.11), BarCount+1, LastValue(PrevLow),ColorRangeLow);
PlotText("PDC - " + NumToStr(PrevClose,1.11), BarCount+1, LastValue(PrevClose),colorBlack);

}

// Show or Hide 2 Day Range
if(ShowTwoDayRange AND Interval() <= 3600)
{

// 2 Day range as Line
if(Prev2RangeStyle)
{
Plot(IIf(IsToday,Prev2High ,Null),"",ColorRangeHigh ,styleLine|styleDashed|styleNoRescale|styleNoLabel);
Plot(IIf(IsToday,Prev2Low,Null),"",ColorRangeLow,styleLine|styleDashed|styleNoRescale|styleNoLabel);
}

// 2 Day Range as Zone
else
{
Plot(TwoDH, "", ColorRangeHigh , styleLine|styleNoTitle|styleNoLabel|styleNoRescale);
Plot(TwoDL, "", ColorRangeLow, styleLine|styleNoTitle|styleNoLabel|styleNoRescale);
PlotOHLC(TwoDH,TwoDH,Max(TwoDHO, TwoDHC),Max(TwoDHO, TwoDHC),"", ColorBlend(ColorRangeHigh , GetChartBkColor(), 0.8),styleNoRescale|styleCloud|styleNoLabel,Null, Null, 0, -5, 5);
PlotOHLC(TwoDL,TwoDL,Min(TwoDLO, TwoDLC),Min(TwoDLO, TwoDLC),"", ColorBlend(ColorRangeLow, GetChartBkColor(), 0.8),styleNoRescale|styleCloud|styleNoLabel,Null, Null, 0, -5, 5);
}

// 2 Day Range TExt
PlotText("2DH - " + NumToStr(Prev2High ,1.11), BarCount+1, LastValue(Prev2High ),ColorRangeHigh );
PlotText("2DL - " + NumToStr(Prev2Low,1.11), BarCount+1, LastValue(Prev2Low),ColorRangeLow);
 

oldtrader

Well-Known Member
I wanted to export data from Ami to .csv. I used the following code

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

StartDate = ParamDate("Starting Date", "31-12-2007");
EndDate = ParamDate("Start Date", "31-12-2008");

//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,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);
I am getting the data in the following format



I need in the following format to merge with existing data



I have exhausted all my resources for solving this, including going through the Ami help. If somebody can help in solving this issue, I will be thankful.
 

vijkris

Learner and Follower
I wanted to export data from Ami to .csv. I used the following code

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

StartDate = ParamDate("Starting Date", "31-12-2007");
EndDate = ParamDate("Start Date", "31-12-2008");

//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,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);
I am getting the data in the following format



I need in the following format to merge with existing data



I have exhausted all my resources for solving this, including going through the Ami help. If somebody can help in solving this issue, I will be thankful.
HI, I HAVE NOT TESTED your afl. I use this.
Code:
Filter=1; 
AddColumn(O,"Open"); 
AddColumn(H,"High"); 
AddColumn(L,"Low"); 
AddColumn(C,"Close"); 
AddColumn(V,"Volume",1.0); 
AddColumn(OI,"Open Interest",1.0);
just put it in analysis window and click explore.

date/time is format is dependent on your pc's date and time format I think.
my snapshot.
 

Mungus

Well-Known Member
Happy sir can you rectify the AFL posted by lone wolf two days ribbon shows only last two days but when we go back it don't shows for previous days like
1),I can see only PDH/PDL and 2Days PDH/PDL from 18th May to 20th May only..

If I click on 10th May,I cant see a PDH/PDL coming form 09th May and 2 days PDL/PDL coming from 06h May.
This was my query/request to LoneWolf.
Please Copy the Original message like this way-

1)In above image,I can see only PDH/PDL and 2Days PDH/PDL from 18th May to 20th May only..

If I click on 10th May,I cant see a PDH/PDL coming form 09th May and 2 days PDL/PDL coming from 06h May.

2)1 week level is more thn enough to take intraday trades,so pls add Weekly high and Low with lines and zones,so we could on-off it if its overlapping with other zones.
Color is Must for a Color Bind Trader like me :D

3)Bold line for Previous day close with on-off and color.
Else,people will think that we both are same with different ID's :lol:

anyway,

@Happy Sir,
Please look at the request we posted.

Thanks
 

Similar threads