miércoles, 27 de enero de 2021

El sistema 3/12 SAR de Lebeau & Lucas

Hoy muestro el código del sistema de Chuck Lebeau & Lucas del libro "Computer Analysis of the Futures Markets", todo un clásico que enseña a diseñar sistemas de trading. Las reglas y la lógica del sistema están explicadas en el informe de Onda4 del 26 de enero. El sistema parece ir bien en el último año, eso sí, con unos parámetros distintos del diseño original:



CÓDIGO DEL SISTEMA:

//-------------------------------------------------------      
// SISTEMA 3/12 SAR
// CHUCK LEBEAU & LUCAS, del libro:
// "COMPUTER ANALYSIS OF THE FUTURES MARKETS"
// OSCAR G. CAGIGAS    
// 16 ENERO 2020
//-------------------------------------------------------  

//MÚLTIPLO DEL STOP LOSS Y OBJETIVO DE VOLATILIDAD
nstop = 6;     
ObjVolat = 800;
 
//--------------VARIABLES OPTIMIZABLES ---------------------  
OptimizerSetEngine("spso");
acel= Optimize("aceler",0.01,0.01,0.03,0.01);  
Msar = Optimize("Msar",0.3,0.1,0.3,0.1); //max SAR 
ma1 = Optimize("ma1",7,1,8,1);
ma2 = Optimize("ma2",18,9,20,1);

//-----------------------MONEY MANAGEMENT--------------------------------     
//DESVIACION STANDARD EXPONENCIAL      
desv_carver = sqrt( EMA( ( C-Ref(C,-1) )^2, 36 ) );   //desv de Carver
   
//CAPITAL INICIAL Y COMISIONES//  
SetOption("maxopenpositions",6);  //máximo 6 posiciones simultáneas      
SetOption("WarningLevel", 1 ); SetOption( "initialequity", 100000 );     
SetOption("CommissionMode", 2); SetOption("commissionamount",50);     //$100 por operación completa (entrada + salida)  
MarginDeposit = 1; PositionSize = Ctos = Ref( round( Nz(ObjVolat/(desv_carver*PointValue) ) ),-1);   
//-----------------------------------------------------------------------     
 
 
//+++++++++++ SISTEMA DE TRADING ++++++  
  
//PARABOLICO  
parab = SAR(acel,msar);  
   
//SISTEMA//   
Buy = Cond= MA(C,ma1) > MA(C,ma2) AND C > parab;
Sell= Cross(parab,C);   
   
Short= Cond2= MA(C,ma1) < MA(C,ma2) AND C < parab;
Cover= Cross(C,parab);   
//REMOVE EXTRA BUY and SHORT SIGNALS//    
Buy=ExRem(Buy,Sell OR Short);     
Short=ExRem(Short,Cover OR Buy);      
Sell=ExRem(Sell,Buy);      
Cover=ExRem(Cover,Short);     
 
//PANIC STOP//  
STOP = Nz(nstop * desv_carver );   
ApplyStop(stopTypeLoss,stopModePoint, STOP,1,0);  
Equity(1,0);  
 
//largo o corto//    
largo=Flip(Buy,Sell OR Short);     
corto=Flip(Short,Cover OR Buy);    
 
//SALIDA GRAFICA//     
Plot(C,"PRICE",colorLightGrey,styleBar|styleThick, Null, Null, 0, 0, 2);     
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorGreen,0,L,-15);     
PlotShapes(IIf(Buy,shapeHollowCircle,shapeNone),colorGreen,0,BuyPrice,0);     
PlotShapes(IIf(Sell AND NOT Corto,shapeDownArrow,shapeNone),colorRed,0,H,-15);     
PlotShapes(IIf(Sell AND NOT Corto,shapeHollowCircle,shapeNone),colorRed,0,SellPrice,0);     
PlotShapes(IIf(Short,shapeDownArrow,shapeNone),colorBrown,0,H,-15);     
PlotShapes(IIf(Short,shapeHollowCircle,shapeNone),colorBrown,0,ShortPrice,0);     
PlotShapes(IIf(Cover AND NOT Largo,shapeUpArrow,shapeNone),colorDarkGreen,0,L,-15);     
PlotShapes(IIf(Cover AND NOT Largo,shapeHollowCircle,shapeNone),colorDarkGreen,0,CoverPrice,0);     
   
//PARAMETRO PARA OCULTAR O DIBUJAR EL SAR Y LAS MEDIAS// 
pintarsar=ParamToggle("Pintar SAR", "No|Yes", 0); 
if (pintarsar) 

Plot(SAR(acel,msar),"SAR",colorGreen,styleLine|styleDashed|styleThick); 

pintarmedias=ParamToggle("Pintar Medias", "No|Yes", 0); 
if (pintarmedias) 

Plot(MA(C,ma1),"MA(C," + ma1 + ")",colorRed,styleLine|styleThick); 
Plot(MA(C,ma2),"MA(C," + ma2 + ")",colorblue,styleLine|styleThick); 

//PLOTTEXT//     
dist = 2*ATR(10);      
for( i = 0; i < BarCount; i++ )      
{      
if( Buy[i] ) PlotText( "Long "+NumToStr(ValueWhen(Buy[i],BuyPrice[i]),1.2), i, L[ i ]-dist[i], colorGreen );      
if( Sell[i] AND NOT Short[i]) PlotText( "Exit " + NumToStr(ValueWhen(Sell[i],SellPrice[i]),1.2), i, H[ i ]+dist[i], colorRed );      
if( Short[i] ) PlotText( "Short "+NumToStr(ValueWhen(Short[i],ShortPrice[i]),1.2), i, H[ i ]+dist[i], colorBrown );      
if( Cover[i] AND NOT Buy[i]) PlotText( "Cover " + NumToStr(ValueWhen(Cover[i],CoverPrice[i]),1.2), i, L[ i ]-dist[i], colorBlack );      
}      
 
//CHART//  
stopprice = Largo*ValueWhen(Buy,BuyPrice-stop)+Corto*ValueWhen(Short,ShortPrice+stop); 
SetChartOptions(0,chartShowDates);      
Plot(C,"PRICE",colorLavender,styleBar|styleThick,Null,Null,0,0,1);      
Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) );       

//PINTAR EL NOMBRE DEL SISTEMA SOBRE EL GRÁFICO
GfxSetOverlayMode(1); 
GfxSelectFont("Tahoma", Status("pxheight")/40 ); 
GfxSetTextAlign( 6 );// center alignment 
GfxSetTextColor( ColorRGB( 200, 200, 200 ) ); 
GfxSetBkMode(1); // transparent 
GfxTextOut( "312Sar", Status("pxwidth")/8, Status("pxheight")/1.6 ); 
 
//DESCOMENTAR ESTAS LÍNEAS PARA PINTAR EL STOP LOSS
//stopline_l = IIf(largo OR Buy OR sell, ValueWhen(Buy, Buyprice-stop), Null); //solo largo 
//stopline = IIf(corto OR short OR cover, ValueWhen(short, ShortPrice+stop), stopline_l); //los dos 
//Plot(stopline, "SL", colorRed,styleDashed); 



No hay comentarios:

Publicar un comentario

ENTRADAS POPULARES