Código:
[IntrabarOrderGeneration = true]
inputs:
Price( Close ),Slow( 18 ),Fast(8),Target(25),StopL(30),Sdate(1160801),Edate(1161201),
Stime(0000),ETime(2359),offset(8),Entry_2(True),distancia(44),tune(2);
variables: var0( 0 ),var1( 0 ),f1(false),f2(false),IntraBarPersist f3(false),IntraBarPersist f4(false),touch(0),touch1(0),
Ent(0),SL(0.0035),TP(0.0025),f5(false),f6(false),IntraBarPersist f7(false),IntraBarPersist f8(false),
dist(0),off(0),Lote(10000),off1(2),ent1(0),ent2(0),sal1(0),sal2(0),dh(false),Scale(10000);
var0 = XAverage( Price, Fast ) ;
var1 = XAverage( Price, Slow ) ;
if currentbar=1 then begin
if PriceScale =1000 then Scale=PriceScale/10; // ajusta size del contract en pares con Yen
tp= Target/Scale;
sl= StopL/Scale;
off=offset/Scale; //distacia del bolsillo
dist=distancia/Scale; //recorrido desde ultimo toque de EMA8 default 44 puntos
off1=tune/Scale; //offset para entry order
end;
//condiciones para ir largo
If var0<var1 then begin f1=false; f2=false; f3=false;touch=0; end; //inicializar flags
if barstatus(1)=2 and f3=true and Marketposition=0 and Low<=var0 then begin
f3=false; //rutina que permite re-iniciar conteo cuando el low es
touch=var0; //Matematicamente <= EMA8 pero el precio nunca lo toco.
end;
Condition1 = var0>var1 and Low<=var0 and Close>var1; //1er cierre arriba de 8EMA
Condition2 = barstatus(1)=2 and f1 and (High-touch)>=dist; // validar distancia despues de toque 8EMA ( closing tick of a bar)
Condition3 = barstatus(1)=0 and f1 and f2 and f3=false and Low[1]>var0[1]+off and var0>var1;//validar bolsillo (first tick bar)
//Condiones para cortos
If var0>var1 then begin f5=false; f6=false; f7=false;touch1=0; end; // Inicializar flags
if barstatus(1)=2 and f7=true and Marketposition=0 and high>=var0 then begin
f7=false; //Rutina para reiniciar si High>=var0 cuando no se presenta la entrada
touch1=var0; //con todoas loas condicines listas
end;
condition4 = var0<var1 and High>=var0 and Close<var1; // Primer cierre abajo 8EMA
condition5 = barstatus(1)=2 and f5 and (touch1-low)>=dist; //validar Distancia
Condition6 = barstatus(1)=0 and f5 and f6 and f7=false and High[1] <var0[1]-off and var0<var1; //Bolsillo
/// Start criteria date/time
dh=Date >Sdate and time >Stime and date < EDate and time < ETime;
//ENTRADAS LARGAS //
If var0>var1 then begin
//Fases para largos
if f1=true and f2 =false and f3=false and condition1 then f1=false; //re-iniciar flags nuevo toque
if f1=true and f2 =true and f3=false and condition1 then begin f1=false; f2=false; end; //re-iniciar flags nuevo toque
if Condition1 and f1=false and f2=false and f3=false then begin
arw_delete(value1);
Value1=arw_New(Date,Time,low,False); //indicacion visual fase1 (f1) completada
Arw_SetStyle(Value1,4);
Arw_setcolor(Value1,cyan);
Arw_SetSize(Value1, 1);
f1=true;
touch=var0; // Definir valor ultimo toque EMA8
end;
If Condition2 and f2=false then begin
f2=true; // Fase 2 (f2) completada
f4=false;
Value2=Text_New(Date,Time,High+0.0010,"*"); //indicacion visual distancia minima cumplida
end;
if Condition3 then begin
f3=true; // Fase3 (f3) completada. Bolsillo validado
f1=false;
f2=false;
Value2=Text_New(Date,Time,High+0.0015,"ok");
end;
if f3=true and dh=true and Marketposition=0 then begin
buy("L1") Lote Contract next bar (var0+off1) limit ; // Colocacin primera Orden
end;
ent1=PosTradeEntryPrice(0,0); // Precio entrada 1
ent2=PosTradeEntryPrice(0,1); // Precio entrada 2
if marketposition=1 and dh=true and Entry_2=true and low[1]>var1 and close<=var1 and f4=false then begin
buy("L2") lote Contract next bar var1 limit ; //Colocacin 2da Orden
end;
if marketposition=1 and PosTradeEntryPrice(0,1)>0 then f4=true; // Fase4 (f4) completada. Orden ejecutada
if marketposition=1 and Entryname="L2" then f4=true; // evita una 3era orden en caso de que orden 1 toco stop
end;
//****************//
//ENTRADAS CORTAS
If var0<var1 then begin
//fases para cortos
if f5=true and f6 =false and f7=false and condition4 then f5=false;
if f5=true and f6 =true and f7=false and condition4 then begin f5=false; f6=false; end;
if Condition4 and f5=false and f6=false and f7=false then begin
arw_delete(value1);
Value1=arw_New(Date,Time,high,true);
Arw_SetStyle(Value1,4);
Arw_setcolor(Value1,darkred);
Arw_SetSize(Value1, 1);
f5=true;
touch1=var0;
end;
If Condition5 and f6=false then begin
f6=true;
f8=false;
Value2=Text_New(Date,Time,low+0.0010,"**");
end;
if Condition6 then begin
f7=true;
f5=false;
f6=false;
Value2=Text_New(Date,Time,low - 0.0015,"ok");
end;
if f7=true and Marketposition=0 and dh=true then begin
Sellshort("S1") Lote Contract next bar at (var0-off1) limit;
end;
ent1=PosTradeEntryPrice(0,0);
ent2=PosTradeEntryPrice(0,1);
if marketposition=-1 and dh=true and Entry_2=true and high[1]<var1 and close>=var1 and f8=false then begin
sellshort("S2") Lote Contract next bar at (var1) limit ;
end;
if marketposition=-1 and PosTradeEntryPrice(0,1)>0 then f8=true;
if marketposition=-1 and Entryname="S2" then f8=true;
end;
//Salidas (Take Profit / Stop loss //
if Marketposition=1 then begin
f3=false;
sell Entry("L1") next bar at (ent1+tp) limit; // Limit Order1
sell Entry("L1") next bar at (ent1-sl) stop; // Stop Order1
if entryname="L2" then sell next bar at (entryprice+tp) limit; // Limit Order2 con Order1 close.
if entryname="L2" then sell next bar at (entryprice-sl) stop; // Stop Order2 con Order1 Close.
if PosTradeEntryPrice(0,1)>0 then begin
sell Entry("L2") next bar at (ent2+tp) limit; // Limit Order1
sell Entry("L2") next bar at (ent2-sl) stop; // Stop Order 2
end;
end;
if Marketposition=-1 then begin
f7=false;
buytocover entry("S1") next bar at (ent1-tp) limit;
buytocover entry("S1") next bar at (ent1+sl) stop;
if entryname="S2" then buytocover next bar at (entryprice-tp) limit;
if entryname="S2" then buytocover next bar at (entryprice+sl) stop;
if PosTradeEntryPrice(0,1)>0 then begin
buytocover Entry("S2") next bar at (ent2-tp) limit;
buytocover Entry("S2") next bar at (ent2+sl) stop;
end;
end;
Saludos