Simple Coding Help - No Promise.

pratapvb

Well-Known Member
Add line

Filter = buy or short ;

Will show those stocks that are in buy or short mode
 

VJAY

Well-Known Member
Dear Pratap,
is it possible to make sound alert when triangle appears in chart?if possible please help :)

PlotShapes( IIf(Down AND (ValidLow OR ZeroValid),shapeSmallUpTriangle,0) ,colorBlue, 0, L,-12);
PlotShapes( IIf(Up AND (ValidHigh OR ZeroValid),shapeSmallDownTriangle,0) ,colorOrange, 0, H,-12);

complete afl here ...http://www.traderji.com/futures/91950-day-trading-futures-202.html#post951325
 

Cubt

Algo Trader
Hi guys..

Abhi has developed the below code, i would like to insert trailing stop loss with this AFL. I want to add one hour candle low/high as trailing stop loss for long and short respectively.

Please let me know if this is possible.

Code:
// Written by: Abhishek Gupta

RoundLotSize = 1;
MarginDeposit = 350;
TickSize = 0;
PointValue = 500;
SetPositionSize(4,spsShares);
SetBarsRequired(50);


_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("Trading signals");
NewDay = IIf(Day() != Ref(Day(), -1) OR BarIndex() == LastValue(BarIndex()), 1, 0);
Plot(NewDay, "", 47, 2 + 32768 + 4096, Minvalue = 0, Maxvalue = 1);
StartTime	= ParamTime("Start time", "09:25");
CloseTime	= ParamTime("Closing Time", "15:20");
Lev			= Param("Max diff O-HL", 0.01, 0, 1, 0.01);
Lev1		= Lev/100;
GapFix		= Param("Minimum stop loss", 0.05, 0, 1, 0.05);
GapFix1	= GapFix/100;

HighY	= TimeFrameGetPrice("High", inDaily, -1);
LowY	= TimeFrameGetPrice("Low", inDaily, -1);

OpenD	= TimeFrameGetPrice("Open", inDaily);
LowD	= LowestSince(NewDay, Low);
HighD	= HighestSince(NewDay, High);


Plot(HighY, "Y's H ", ParamColor("Y's High Color", colorYellow), ParamStyle("Y's High Style", styleDashed));
Plot(LowY, "Y's L ", ParamColor("Y's Low Color", colorYellow), ParamStyle("Y's Low Style", styleDashed));
Plot(OpenD, "Day's O ", ParamColor("Day's Open Color", colorRed), ParamStyle("Day's Open Style", styleDashed));
Plot(LowD, "Day's L ", ParamColor("Day's Low Color", colorGreen), ParamStyle("Day's Low Style", styleDashed));
Plot(HighD, "Day's H ", ParamColor("Day's High Color", colorGreen), ParamStyle("Day's High Style", styleDashed));
Plot((OpenD*(1-Lev1)), "Open variant", colorPink, styleDashed);
Plot((OpenD*(1+Lev1)), "Open variant", colorPink, styleDashed);

// Filter time, low and high conditions
Conds		= TimeNum()>StartTime AND TimeNum()<CloseTime AND (OpenD*(1+GapFix1))<HighY AND (OpenD*(1-GapFix1))>LowY;

Buy		= Conds AND (OpenD*(1-Lev1))<=LowD AND Cross(High, HighY);
Sell	= TimeNum()>CloseTime OR Cross(OpenD, Close);
Short	= Conds AND (OpenD*(1+Lev1))>=HighD AND Cross(LowY, Low);
Cover	= TimeNum()>CloseTime OR Cross(Close, OpenD);

Buy		= ExRem(Buy, Sell);
Sell	= ExRem(Sell, Buy);
Short	= ExRem(Short, Cover);
Cover	= ExRem(Cover, Short);

BuyPrice		= ValueWhen(Buy, HighY); 
ShortPrice		= ValueWhen(Short, LowY);
CoverPrice		= ValueWhen(Cover, IIf(Cross(High, OpenD), openD, Close));
SellPrice		= ValueWhen(Sell, IIf(Cross(OpenD, Low), OpenD, Close) );


dist	= 1.5*ATR(10);
for (i=0; i<BarCount; i++) {
	if (Cover[i]) {
		PlotText( "\nCover short: " + CoverPrice[i], i+1.5, L[ i ]-dist[i]-3, colorLime);
		PlotText( "\n\nProfit: " + (ShortPrice[i]-CoverPrice[i]), i+1.5, L[ i ]-dist[i]-3, colorLime);
	} else if (Sell[i]) {
		PlotText( "\nSell bought: " + SellPrice[i], i+1.5, H[ i ]+dist[i]+5, colorOrange);
		PlotText( "\n\nProfit: " + (SellPrice[i]-BuyPrice[i]), i+1.5, H[ i ]+dist[i]+5, colorOrange);
	}
	if(Buy[i]) {
		PlotText( "Buy: " + BuyPrice[i], i+1.5, L[ i ]-dist[i]-3, colorLime);
	} else if( Short[i]) {
		PlotText( "Short: " + ShortPrice[i], i+1.5, H[ i ]+dist[i]+5, colorOrange);
 	}
}

PlotShapes(Buy*shapeUpArrow, colorGreen, 0, Low, -28);
PlotShapes(Short*shapeDownArrow, colorRed, 0, High, -28);
PlotShapes(Cover*shapeHollowUpArrow, colorGreen, 0, Low, -45);
PlotShapes(Sell*shapeHollowDownArrow, colorRed, 0, High, -45);

printf("\nSignal came " + IIf(BarsSince(Short)>BarsSince(Buy), BarsSince(Buy), BarsSince(Short)) + " bars ago");
WriteIf(BarsSince(Short)>BarsSince(Buy), "\nBuy@ " + BuyPrice, "\nShort@ " + ShortPrice);

printf("\nPossiblities ");
printf("\nMax Profit: " + IIf(BarsSince(Short)>BarsSince(Buy), (HighD-BuyPrice), (ShortPrice-LowD)));
printf("\nMin Profit: " + IIf(BarsSince(Short)>BarsSince(Buy), (OpenD-BuyPrice), (ShortPrice-OpenD)));


// Write Messages
printf("\n\nLet the profit run.");
printf("\nClose a call only when trailing SL hits");
_SECTION_END();
 
Code:
// Written by: Abhishek Gupta

RoundLotSize = 1;
MarginDeposit = 350;
TickSize = 0;
PointValue = 500;
SetPositionSize(4,spsShares);
SetBarsRequired(50);


_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

_SECTION_BEGIN("Trading signals");
NewDay = IIf(Day() != Ref(Day(), -1) OR BarIndex() == LastValue(BarIndex()), 1, 0);
Plot(NewDay, "", 47, 2 + 32768 + 4096, Minvalue = 0, Maxvalue = 1);
StartTime	= ParamTime("Start time", "09:25");
CloseTime	= ParamTime("Closing Time", "15:20");
Lev			= Param("Max diff O-HL", 0.01, 0, 1, 0.01);
Lev1		= Lev/100;
GapFix		= Param("Minimum stop loss", 0.05, 0, 1, 0.05);
GapFix1	= GapFix/100;

HighY	= TimeFrameGetPrice("High", inDaily, -1);
LowY	= TimeFrameGetPrice("Low", inDaily, -1);

OpenD	= TimeFrameGetPrice("Open", inDaily);
LowD	= LowestSince(NewDay, Low);
HighD	= HighestSince(NewDay, High);

Plot(HighY, "Y's H ", ParamColor("Y's High Color", colorYellow), ParamStyle("Y's High Style", styleDashed));
Plot(LowY, "Y's L ", ParamColor("Y's Low Color", colorYellow), ParamStyle("Y's Low Style", styleDashed));
Plot(OpenD, "Day's O ", ParamColor("Day's Open Color", colorRed), ParamStyle("Day's Open Style", styleDashed));
Plot(LowD, "Day's L ", ParamColor("Day's Low Color", colorGreen), ParamStyle("Day's Low Style", styleDashed));
Plot(HighD, "Day's H ", ParamColor("Day's High Color", colorGreen), ParamStyle("Day's High Style", styleDashed));
Plot((OpenD*(1-Lev1)), "Open variant", colorPink, styleDashed);
Plot((OpenD*(1+Lev1)), "Open variant", colorPink, styleDashed);


//Insert
HighH	= TimeFrameGetPrice("High", inHourly, -1);
LowH	= TimeFrameGetPrice("Low", inHourly, -1);
Plot(LowH,  "HOUR L ", ParamColor("HOURLY Low Color", colorGreen), ParamStyle("HOURLY Low Style", styleDashed));
Plot(HighH, "HOUR H ", ParamColor("HOURLY High Color", colorGreen), ParamStyle("HOURLY High Style", styleDashed));

// Filter time, low and high conditions
Conds		= TimeNum()>StartTime AND TimeNum()<CloseTime AND (OpenD*(1+GapFix1))<HighY AND (OpenD*(1-GapFix1))>LowY;

Buy		= Conds AND (OpenD*(1-Lev1))<=LowD AND Cross(High, HighY);
//Sell	= TimeNum()>CloseTime OR Cross(OpenD, Close);
Short	= Conds AND (OpenD*(1+Lev1))>=HighD AND Cross(LowY, Low);
//Cover	= TimeNum()>CloseTime OR Cross(Close, OpenD);
//Replace
Sell	= TimeNum()>CloseTime OR Cross(OpenD, Close) OR Cross(LowH, Close);
Cover	= TimeNum()>CloseTime OR Cross(Close, OpenD) OR Cross(Close, HighH);

Buy		= ExRem(Buy, Sell);
Sell	= ExRem(Sell, Buy);
Short	= ExRem(Short, Cover);
Cover	= ExRem(Cover, Short);

BuyPrice		= ValueWhen(Buy, HighY); 
ShortPrice		= ValueWhen(Short, LowY);
CoverPrice		= ValueWhen(Cover, IIf(Cross(High, OpenD), openD, Close));
SellPrice		= ValueWhen(Sell, IIf(Cross(OpenD, Low), OpenD, Close) );


dist	= 1.5*ATR(10);
for (i=0; i<BarCount; i++) {
	if (Cover[i]) {
		PlotText( "\nCover short: " + CoverPrice[i], i+1.5, L[ i ]-dist[i]-3, colorLime);
		PlotText( "\n\nProfit: " + (ShortPrice[i]-CoverPrice[i]), i+1.5, L[ i ]-dist[i]-3, colorLime);
	} else if (Sell[i]) {
		PlotText( "\nSell bought: " + SellPrice[i], i+1.5, H[ i ]+dist[i]+5, colorOrange);
		PlotText( "\n\nProfit: " + (SellPrice[i]-BuyPrice[i]), i+1.5, H[ i ]+dist[i]+5, colorOrange);
	}
	if(Buy[i]) {
		PlotText( "Buy: " + BuyPrice[i], i+1.5, L[ i ]-dist[i]-3, colorLime);
	} else if( Short[i]) {
		PlotText( "Short: " + ShortPrice[i], i+1.5, H[ i ]+dist[i]+5, colorOrange);
 	}
}

PlotShapes(Buy*shapeUpArrow, colorGreen, 0, Low, -28);
PlotShapes(Short*shapeDownArrow, colorRed, 0, High, -28);
PlotShapes(Cover*shapeHollowUpArrow, colorGreen, 0, Low, -45);
PlotShapes(Sell*shapeHollowDownArrow, colorRed, 0, High, -45);

printf("\nSignal came " + IIf(BarsSince(Short)>BarsSince(Buy), BarsSince(Buy), BarsSince(Short)) + " bars ago");
WriteIf(BarsSince(Short)>BarsSince(Buy), "\nBuy@ " + BuyPrice, "\nShort@ " + ShortPrice);

printf("\nPossiblities ");
printf("\nMax Profit: " + IIf(BarsSince(Short)>BarsSince(Buy), (HighD-BuyPrice), (ShortPrice-LowD)));
printf("\nMin Profit: " + IIf(BarsSince(Short)>BarsSince(Buy), (OpenD-BuyPrice), (ShortPrice-OpenD)));


// Write Messages
printf("\n\nLet the profit run.");
printf("\nClose a call only when trailing SL hits");
_SECTION_END();

Happy :)
 

amitrandive

Well-Known Member
How to Identify Range bound Markets ?



Got this indicator from the net.Can any expert convert it to AFL?

Code:
//+------------------------------------------------------------------+
//|                                                      MAAngle.mq4 |
//|                                               original    jpkfox |
//|                                               edited by dariuske |
//| You can use this indicator to measure when the MA angle is       |
//| "near zero". AngleTreshold determines when the angle for the     |
//| EMA is "about zero": This is when the value is between           |
//| [-AngleTreshold, AngleTreshold] (or when the histogram is red).  |
//|   MAMode : 0 = SMA, 1 = EMA, 2 = Smoothed, 3 = Weighted          |
//|   MAPeriod: MA period                                            |
//|   AngleTreshold: The angle value is "about zero" when it is      |
//|     between the values [-AngleTreshold, AngleTreshold].          |      
//|   StartMAShift: The starting point to calculate the              |   
//|     angle. This is a shift value to the left from the            |
//|     observation point. Should be StartEMAShift > EndEMAShift.    | 
//|   StartMAShift: The ending point to calculate the                |
//|     angle. This is a shift value to the left from the            | 
//|     observation point. Should be StartEMAShift > EndEMAShift.    |
//|                                                                  |
//|   Modified by MrPip                                              |
//|       Red for down                                               |
//|       Yellow for near zero                                       |
//|       Green for up                                               |
//|  10/15/05  MrPip                                                 |
//|            Corrected problem with USDJPY and optimized code      |
//|  10/23/05  Added other JPY crosses                               |
//|                                                                  |
//|                                                                  |  
//|                                                                  |  
//|  12/01/07  Dariuske: Changed code for SMA50 (PhilNelSystem)      |
//|  18/01/07  Dariuske: Changed code for multiple mode MA's         |
//+------------------------------------------------------------------+

#property  copyright "jpkfox"
#property  link      "http://www.strategybuilderfx.com/forums/showthread.php?t=15274&page=1&pp=8"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 3
#property  indicator_color1  LimeGreen
#property  indicator_color2  FireBrick 
#property  indicator_color3 Yellow
//---- indicator parameters
extern int MAMode = 0;
extern int MAPeriod=50;
extern int Price=4;
extern double AngleTreshold=0.25;
extern int StartMAShift=2;
extern int EndMAShift=0;


//---- indicator buffers
double UpBuffer[];
double DownBuffer[];
double ZeroBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(3);
//---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,4);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,4);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,4);

   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);

//---- 3 indicator buffers mapping
   if(!SetIndexBuffer(0,UpBuffer) &&
      !SetIndexBuffer(1,DownBuffer) &&
      !SetIndexBuffer(2,ZeroBuffer))
      Print("cannot set indicator buffers!");
//---- name for DataWindow and indicator subwindow label
   //IndicatorShortName("MAAngle("+MAPeriod+","+AngleTreshold+","+StartMAShift+","+EndMAShift+")");
   IndicatorShortName("MAAngle");
//---- initialization done
   return(0);
}
//+------------------------------------------------------------------+
//| The angle for EMA                                                |
//+------------------------------------------------------------------+
int start()
{
   double fEndMA, fStartMA;
   double fAngle, mFactor, dFactor;
   int nLimit, i;
   int nCountedBars;
   double angle;
   int ShiftDif;
   string Sym;
   
   if (MAMode >= 4) MAMode = 0;
 
   if(EndMAShift >= StartMAShift)
   {
      Print("Error: EndMAShift >= StartMAShift");
      StartMAShift = 6;
      EndMAShift = 0;      
   }  
         
   nCountedBars = IndicatorCounted();
//---- check for possible errors
   if(nCountedBars<0) 
      return(-1);
//---- last counted bar will be recounted
   if(nCountedBars>0) 
      nCountedBars--;
   nLimit = Bars-nCountedBars;
   dFactor = 2*3.14159/180.0;
   mFactor = 10000.0;
   Sym = StringSubstr(Symbol(),3,3);
   if (Sym == "JPY") mFactor = 100.0;
   ShiftDif = StartMAShift-EndMAShift;
   mFactor /= ShiftDif; 
//---- main loop
   for(i=0; i<nLimit; i++)
   {
      fEndMA=iMA(NULL,0,MAPeriod,0,MAMode,Price,i+EndMAShift);
      fStartMA=iMA(NULL,0,MAPeriod,0,MAMode,Price,i+StartMAShift);
      // 10000.0 : Multiply by 10000 so that the fAngle is not too small
      // for the indicator Window.
      fAngle = mFactor * (fEndMA - fStartMA)/2.0;
      //fAngle = MathArctan(fAngle)/dFactor;

      DownBuffer[i] = 0.0;
      UpBuffer[i] = 0.0;
      ZeroBuffer[i] = 0.0;
      
      if(fAngle > AngleTreshold)
         UpBuffer[i] = fAngle;
      else if (fAngle < -AngleTreshold)
         DownBuffer[i] = fAngle;
      else ZeroBuffer[i] = fAngle;
   }

   return(0);
  }
//+------------------------------------------------------------------+
 
Code:
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
//Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1);
factor = Param("factor",0.01,0.01,1,0.01)/1000;
e =  EMA( P, Periods );
d = ROC(e)/C;
//Plot(e, "EMA", IIf(d > factor, colorBlue, IIf(d < -factor,colorRed, colorYellow)), styleThick); 
Plot(d,"",IIf(d > factor, colorBlue, IIf(d < -factor,colorRed, colorYellow)), styleThick|styleHistogram|styleOwnScale);
_SECTION_END();
Similar concept

Happy :)
 

amitrandive

Well-Known Member
Code:
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
//Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
P = ParamField("Price field",-1);
Periods = Param("Periods", 15, 2, 300, 1);
factor = Param("factor",0.01,0.01,1,0.01)/1000;
e =  EMA( P, Periods );
d = ROC(e)/C;
//Plot(e, "EMA", IIf(d > factor, colorBlue, IIf(d < -factor,colorRed, colorYellow)), styleThick); 
Plot(d,"",IIf(d > factor, colorBlue, IIf(d < -factor,colorRed, colorYellow)), styleThick|styleHistogram|styleOwnScale);
_SECTION_END();
Similar concept

Happy :)
Happy Sir

Fantastic !!! Thanks for quick reply.:clap:
 
Respected
hmsanil

Can us share the Corrected AFL with exploration

Warm Regards
Chinmay
here it is

Code:
_SECTION_BEGIN("Price1");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

sp = Param( "RSI Period", 7, 1, 100 );
r = RSI( sp );
Buy = r > 70;
Short = r < 30;

shape = Buy * shapeUpArrow + Short * shapeDownArrow;
PlotShapes(shape, IIf( Buy, colorBrightGreen, colorRed ), 0, IIf( Buy, Low, High ) );

Sell = Ref(Buy, -1) AND r < 70 ;
Cover = Ref(Short, -1) AND r >30 ;


Sell = ExRem(Sell, Buy) ;
Cover = ExRem(Cover, Short) ;

shape1 = Cover * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes(shape, colorBlack, 0, IIf( Buy, High, Low ) );

Filter = Buy OR Short ;
thanks

Sudha
 

Similar threads