NMA swing with variable SL & profit target for auto trading

#1
PHP:
_SECTION_BEGIN(" NMA VARIABLE SL AND PROFIT");
SetChartOptions(1,chartShowArrows|chartShowDates);
RequestTimedRefresh( 1 );
GraphXSpace=5;
HaClose = (O+H+L+C)/4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.8);
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );
xDiff = (HaHigh - Halow) * IIf(StrFind(Name(),"JPY"),100,10000);
barcolor = IIf(HaClose >= HaOpen,colorWhite,colorWhite);
if( ParamToggle("Plot Heikin-Ashi", "No,Yes", 0 ) )
   PlotOHLC( HaOpen, HaHigh, HaLow, HaClose,  Name(), colorWhite, styleCandle );
else
 Plot( C, "Regular candles " + Name(), colorWhite, styleCandle );
j=Haclose;
//P = ParamField("Price field",-1);
        //PRICE LINE
 PriceLineColor = PriceLineColor= colorCustom11;
PriceLevel = C;
Daysback    =  200;
FirstBar    = BarCount - DaysBack;
YY          = IIf(BarIndex() >= Firstbar,EndValue(PriceLevel),Null);
Plot(YY,"Current Price",PriceLineColor,ParamStyle("LineStyle",styleLine|styleDashed|styleNoTitle|styleThick,maskAll));
side = 1;
dist = 0;

//Magfied Market Price
FS=20;
GfxSelectFont("Times New Roman", FS, 700, True );
GfxSetBkMode( colorWhite ); 
GfxSetTextColor (colorBrightGreen );
Hor=1;
Ver=8;
GfxTextOut(""+C, Hor , Ver );
YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
xx=Prec((DD/YC)*100,2);
GfxSelectFont("Times New Roman", 11, 700, True );
GfxSetBkMode( colorBlack ); 
_SECTION_END();


_SECTION_BEGIN("NICK MA Swing");
EnableTextOutput(False);
SetChartOptions(0, chartShowDates);
RequestTimedRefresh(1);
Filename = StrLeft(_DEFAULT_NAME(),StrLen(_DEFAULT_NAME())-2);
_N(Title = Filename + StrFormat(" - {{DATE}} \nOpen %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " + " {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
//VarPfx = "Btn1"; // some var that reflects the trading file
SetBarsRequired(200,0);
GraphXSpace = 5;
SetChartOptions(0,chartShowArrows|chartShowDates);
k =  Optimize("K",Param("K",2.5,0.25,5,0.25),0.25,5,0.25);
Per= Optimize("atr",Param("atr",20,3,20,1),3,20,1);
 
//=======================================================================================================================
//=========================Indicator==============================================================================================
f=ATR(15);
 
rfsctor = WMA(H-L, Per);
 
revers = k * rfsctor;
 
Trend = 1; 
NW[0] = 0; 
NW[BarCount-1] = Null; 
 
 
for(i = 1; i < BarCount-1; i++)
{
 if(Trend[i-1] == 1)               
 {
  if(j[i] < NW[i-1])                
  {
   Trend[i] = -1;                  
   NW[i] = j[i] + Revers[i];       
  }
  else                             
  {
   Trend[i] = 1;
   if((j[i] - Revers[i]) > NW[i-1])
   {
    NW[i] = j[i] - Revers[i];
   }
   else
   {
    NW[i] = NW[i-1];
   }
  }
 }
 if(Trend[i-1] == -1)              
 {
  if(j[i] > NW[i-1])               
  {
   Trend[i] = 1;                   
   NW[i] = j[i] - Revers[i];       
  }
  else                             
  {
   Trend[i] = -1;
   if((j[i] + Revers[i]) < NW[i-1])
   {
    NW[i] = j[i] + Revers[i];
   }
   else
   {
    NW[i] = NW[i-1];
   }
  }
 }
}
 Plot(NW, "", IIf(Trend == 1, 27, 4), 4);


_SECTION_BEGIN("Breakout Setting");
beginTradeTime= ParamTime( "No trade B4 ", "091500" );
endTradeTime= Param("End Time",151000,151000,233000,10);

EndDay = ParamTime( "No trade after ", "15:09:00" );
EndDay2 = ParamTime( "close position ", "15:09:00" );

tn = TimeNum();
dn = DateNum();
Exit = Ref( tn < EndDay2, -1 ) AND ( tn >= EndDay2 OR dn < Ref( dn, 1 ) );

Buy = Sell = Short = Cover = 0;

Buy=NW<HACLOSE;
//Cover=NW<HACLOSE;
//Sell=NW>HACLOSE;
Short=NW>HACLOSE;


//Buy=Cross(J,NW);

//Short=Cross(NW,J);


Buysetup=Flip(J,NW);
Shortsetup=Flip(NW,J);

Sell=Shortsetup OR exit;
Cover=Buysetup OR exit;

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

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

BuyPrice1=Max(Ref(C,-1),O)+0.5;// 0.5 - for better fill
ShortPrice1=Min(Ref(C,-1),O)+0.5;

sellPrice1=Min(Ref(C,-1),O)+0.5;
CoverPrice1=Max(Ref(C,-1),O)+0.5;

BuyPrice = ValueWhen(Buy,BuyPrice1); bp=NumToStr(BuyPrice,1.2);
ShortPrice = ValueWhen(Short,shortPrice1); shp=NumToStr(ShortPrice,1.2);
SellPrice = ValueWhen(Sell,sellPrice1);
CoverPrice = ValueWhen(Cover,coverPrice1);

PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), 43, layer = 0,yposition = Low, offset = -20);
PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorRed, layer = 0, yposition = High, offset = -20); //shapeSmallCircle

//PlotShapes( IIf(Cover, shapeHollowUpArrow,shapeNone), colorAqua, layer = 0,yposition = Low, offset = 30);
//PlotShapes( IIf(Sell,shapeHollowDownArrow,shapeNone), colorPink, layer = 0, yposition = High, offset = -30);

StopLevel = Param( "Stop Loss (%)", 0.06, 0.01, 10, 0.01 );
TgtLevel = Param( "Profit target (%)", 0.25, 0.01, 10, 0.01 );

ApplyStop( stopTypeLoss, stopModePercent, StopLevel, True );
ApplyStop( stopTypeProfit, stopModePercent, TgtLevel, True );

Equity( 1, 0 );

sellcond1=Sell==1; covercond1=Cover==1;
sellcond2=Sell==2; covercond2=Cover==2;
sellcond3=Sell==3; covercond3=Cover==3;


//PlotShapes(IIf(sellcond1,shapeSmallDownTriangle,sh apeNone),colorOrange,0,H,-25);
PlotShapes(IIf(sellcond3,shapeHollowDownArrow,shapeNone),colorOrange,0,H,-15);
PlotShapes(IIf(sellcond2,shapeStar,shapeNone),colorOrange,0,H,-15);

//PlotShapes(IIf(covercond1,shapeSmallUpTriangle,sha peNone),colorLime,0,L,-25);
PlotShapes(IIf(covercond3,shapeHollowUpArrow,shapeNone),colorAqua,0,L,-25);
PlotShapes(IIf(covercond2,shapeStar,shapeNone),colorAqua,0,L,-25);

WriteIf( Sell == 1, "Regular exit",WriteIf( Sell == 3, "Profit Tgt",WriteIf( Sell == 2, "Max stop", "" ) ));
WriteIf( Cover == 1, "Regular exit",WriteIf( Cover == 3, "Profit Tgt",WriteIf( Cover == 2, "Max stop", "" ) ));

GraphXSpace=10;

_SECTION_END();


_SECTION_BEGIN("MASSAGE BOARD"); 
 
for(i=BarCount-1;i>1;i--)
{
if(Buy[i]  == 1)
{
entry = C[i];
sig = "BUY";
sl = Ref(NW,-1);
tar1 = entry + (entry * .00354);
tar2 = entry + (entry * .0092);
tar3 = entry + (entry * .0179);
  
bars = i;
i = 0;
}
if(Short[i] == 1)
{
sig = "SELL";
entry = C[i] ;
sl = Ref(NW,-1);
tar1 = entry - (entry * .00354);
tar2 = entry - (entry * .0092);
tar3 = entry - (entry * .0179);
  
  
bars = i;
i = 0;
}
}
Offset = 20;
Clr = IIf(sig == "BUY", colorLime, colorRed);
ssl = IIf(bars == BarCount-1, NW[BarCount-1], Ref(NW, -1));
sl = ssl[BarCount-1];
 
Plot(LineArray(bars-Offset, tar1, BarCount, tar1,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, tar2, BarCount, tar2,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
Plot(LineArray(bars-Offset, tar3, BarCount, tar3,1), "", Clr, styleLine|styleDots, Null, Null, Offset);
 
 
Plot(LineArray(bars-Offset, sl, BarCount, sl,1), "", colorDarkRed, styleLine|styleLine, Null, Null, Offset);

Plot(LineArray(bars-Offset, entry, BarCount, entry,1), "", colorYellow, styleLine|styleLine, Null, Null, Offset);


messageboard = ParamToggle("Message Board","Show|Hide",1);
if (messageboard == 1 )
{
GfxSelectFont( "Tahoma", 13, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );
  
if ( sig =="BUY")
{
GfxSelectSolidBrush( colorDarkGreen ); // this is the box background color
}
else
{
GfxSelectSolidBrush( colorRed ); // this is the box background color
}
pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth");
Left = 1100;
width = 310;
x = 5;
x2 = 290;
  
y = pxHeight;
  
GfxSelectPen( colorGreen, 1); // broader color
GfxRoundRect( x, y - 98, x2, y , 7, 7 ) ;
GfxTextOut( ( "NMA Trading System"),13,y-100);
GfxTextOut( (" "),27,y-100);
GfxTextOut( ("Last " + sig + " Signal came " + (BarCount-bars-1) * Interval()/60 + " mins ago"), 13, y-80) ; // The text format location
GfxTextOut( ("" + WriteIf(sig =="BUY",sig + " @ ",sig + " @") + " : " + entry), 13, y-60);
GfxTextOut( ("Trailing SL : " + sl + " (" + WriteVal(IIf(sig == "SELL",entry-sl,sl-entry), 2.2) + ")"), 13, y-40);
/*GfxTextOut( ("TGT:1 : " + tar1), 13, y -80);
GfxTextOut( ("TGT:2 : " + tar2), 13,y-60);
GfxTextOut( ("TGT:3 : " + tar3), 13,y-40);*/
GfxTextOut( ("Current P/L : " + WriteVal(IIf(sig == "BUY",(C-entry),(entry-C)),2.2)), 13, y-22);
 
}
 
_SECTION_END();
 
#4
Can you tell me something about how to use the afl?
This AFL is normal NMA swing with buy sell signal, beside this it has additional variable target based buy sell hollow arrows which is set to 0.25% for nifty future which is 15 points approx. You can change it from settings as per your targets and there is variable stop loss also, showing * signal which is set to 0.06%. For auto trading just drag drop or paste auto trading plugin after this afl and auto trade
 
#5
hi , hrishi9000

the afl is superb and works efficiently if u can use it effectively.
thanks a lot dear for posting it.:rofl:


i have a small question.
i am trading on signal bar and i want to apply stop=nbarstop.
it means stop is executed after completion of nbar but i am trading on signal bar so how can i do that(stop execution automatically after competion of nthbar)

i got evalution signal (arrow) on afl from amibroker when nth bar finish but exit is not done by the auto execution software.

for exam
/* N-bar stop */
ApplyStop( stopTypeNBar, stopModeBars, 5 );

now exit at stop=1 and ActivateStopsImmediately turned Off

it it ok?

pl guide .................................
 

Similar threads