Formula Turtle Strategy

Da traderpedia.
Jump to navigation Jump to search

La definizione delle tartarughe implica nel trading system anche il money managemant (quanto rischiare in ogni operazione), quindi ho la necessita di conoscere quanto capitale ho disponibile e quante posizioni contemporanee posso aprire (piramidare in gergo).

Siccome da Easy Language non ho variabili di sistema per leggere i valori impostati nella sezione "Costi" e nella sezione "Proprietà" del pannello di formattazione, sono stato costretto a prevedere gli stessi dati come Input della strategy.

Descrizione variabili di Input:

  • DistH
    numero min di barre per rilevare un high break default (4)
  • DistL
    numero min di barre per rilevare un low break default (4)
  • Account
    capitale iniziale utilizzabile default (10000)
  • MaxPercRisk
    definisce in percentuale il rischio relativo alla volatilità "N in gergo tartarughe" lo stop-loss è 2 volte questo parametro, di default (1)
  • MaxOpenEntry
    numero massimo di posizioni aperte consecutive default (4)


Per capire meglio come inserire le variabili di Input guardare l'esempio nel workspace allegato.

  • Associazione colori break out:

• la linea blu sottile indica break high 20 barre.
• la linea blu spessa indica break high 55 barre.
• la linea ciano spessa indica break high entrate in piramide.
• la linea rossa sottile indica break low 20 barre.
• la linea rossa spessa indica break low 55 barre.
• la linea magenta spessa indica break low entrate in piramide.

  • Update Ver 0.1.0

La versione aggiornata gestisce i nuovi ticks MTS in vigore da Feb. 2005 ed è stata divisa in due sotto release. La #1 tiene un ordine attivo solo il giorno successivo il BreakOut, mentre la #2 tiene L'ordine attivo per DistH -1 per i Buy Orders e DistL -1 per i Sell Orders.

Mediamente va meglio la #2, ma su alcuni titoli è meglio la #1.

PositionMoneyMng

PositionMoneyMng(Account,PercRisk,Price,Loss) Ritorna il numero share per la posione da prendere a Price con massima perdita Loss garantendo una massima perdita PercRisk sul Account.

INIZIO FORMULA
Inputs:Account(NumericSimple),PercRisk(NumericSimple),Price(NumericSimple),Loss(NumericSimple);

PositionMoneyMng = Floor(Minlist(Account / Price,((Account * PercRisk) / 100) / Loss));

FINE FORMULA

TicksMTS

Provided By: Carlo Bastelli nov. 2004

INIZIO FORMULA

Inputs: Price(NumericSimple);

if Price <= 0.2500 then TicksMTS = 0.0001 else if Price <= 1.0 then {0.2501 to 1.0 } TicksMTS = 0.0005 else if Price <= 2.0 then {1.0001 to 2.0 } TicksMTS = 0.0010 else if Price <= 5.0 then {2.0001 to 5.0 } TicksMTS = 0.0025 else if Price <= 10.0 then {5.0001 to 10 } TicksMTS = 0.0050 else TicksMTS = 0.0100;

FINE FORMULA

TicksRound

Provided By: Carlo Bastelli feb. 2005

INIZIO FORMULA Inputs: Price(NumericSimple); Vars:Tick(0),Remaind(0);

Tick=TicksMTS(Price); Remaind=Mod(Price,Tick); if (Remaind=0) then TicksRound=Price else if (Remaind>=Tick/2) then TicksRound=Price-Remaind+Tick else TicksRound=Price-Remaind; FINE FORMULA

Tourtle Soup #1

Provided By: Carlo Bastelli 2005 carlo_bastelli@yahoo.it

INIZIO FORMULA

{ Turtle mode #1 Ver.0.1.0 03.02.2005 Ordine Buy/Sell solo la barra dopo break out }
{ Turtle mode #1 Ver.0.1.1 12.10.2005 Aggiunto Expert Commentary }

Input:DistH(4),DistL(4),Account(10000),MaxPercRisk(2),MaxOpenEntry(4);
Var:TradeAccount(-1),PosSize(0),TrailBars(10),Eq(0),T1(0);
Var:cCurrentEntries(0),cAvgEntryPrice(0),cEntryPrice(0),cCurrentContracts(0);
Var:BuyStop(-1),BuyLimit(-1),LExitStop(-1),SellStop(-1),SellLimit(-1),SExitStop(-1);
Var:NextHigh(-1),NextLow(-1);
Var:BreakType(0),BreakStop(-1),BreakBars(0),BreakIsLoss(false),Break20Found(false),Break20SkipCnt(0);

if Barnumber=1 then begin
VALUE29=-1;
VALUE38=-1;
end;

cCurrentEntries=CurrentEntries;
cAvgEntryPrice=AvgEntryPrice;
cCurrentContracts=CurrentContracts;
if (cCurrentEntries>cCurrentEntries[1]) then begin
cEntryPrice=(CurrentContracts*AvgEntryPrice-cCurrentContracts[1]*cAvgEntryPrice[1])/
(CurrentContracts-cCurrentContracts[1]);
if MarketPosition=1 then begin
LExitStop=TicksRound(cEntryPrice-2*XAverage(TrueRange,20));
NextHigh=cEntryPrice+0.5*XAverage(TrueRange,20);
NextHigh=TicksRound(NextHigh+TicksMTS(NextHigh));
VALUE29=-1;
end
else begin
SExitStop=TicksRound(cEntryPrice+2*XAverage(TrueRange,20));
NextLow=cEntryPrice-0.5*XAverage(TrueRange,20);
NextLow=TicksRound(NextLow-TicksMTS(NextLow));
VALUE38=-1;
end;
{print(cEntryPrice," ",cCurrentEntries," ",CurrentContracts-cCurrentContracts[1]," ",date);}
value99=date; { Date & Time per linea entry multiple Buy/Sell }
value98=time;
end;
if (cCurrentEntries=0 and cCurrentEntries[1]<>0) then begin
NextHigh=-1;
NextLow=-1;
LExitStop=-1;
SExitStop=-1;
if VALUE29>=0 then
TL_Delete(VALUE29);
VALUE29=-1;
if VALUE38>=0 then
TL_Delete(VALUE38);
VALUE38=-1;
end;

{ Gestione Exit Stop & Trailing }
if MarketPosition=1 then begin
ExitLong ("LES") next bar at LExitStop stop;
if BarsSinceEntry>TrailBars then
ExitLong ("LET") next bar at TicksRound(Lowest(Low,TrailBars)) stop;
end
else if MarketPosition=-1 then begin
ExitShort ("SES") next bar at SExitStop stop;
if BarsSinceEntry>TrailBars then
ExitShort ("SET") next bar at TicksRound(Highest(High,TrailBars)) stop;
end;

Break20Found=false;
{ Genera segnali Buy Break High 20 bars }
if H>=Highest(H,20)[1] then begin
value1=H;
value2=BarNumber;
value3=value2[0]-value2[1];

if value3>DistH and value1[0]>=value1[1] and value1[1]<>0 and NextHigh=-1 then begin
{BuyStop=value1[1]-TicksMTS(value1[1])-XAverage(TrueRange,20);
BuyLimit=value1[1]+XAverage(TrueRange,20);}
if BreakIsLoss or Break20SkipCnt>0 then begin
BuyStop=TicksRound(value1[1]+TicksMTS(value1[1]));
BuyLimit=TicksRound(value1[0]-XAverage(TrueRange,20));
Break20SkipCnt=0;
Break20Found=true;
end
else
Break20SkipCnt=Break20SkipCnt+1;
VALUE9=TL_New(date[value3+1],time[value3+1],high[value3],date,time,high[value3]);
TL_SetSize(VALUE9, 1);
TL_SetColor(VALUE9, Blue);
if BreakType=0 then begin
BreakBars=0;
BreakType=1;
{BreakIsLoss=false;}
end;
end;
end;
{ Genera segnali Buy Break High 55 bars }
if H>=Highest(H,55)[1] then begin
value4=H;
value5=BarNumber;
value6=value5[0]-value5[1];

if value6>DistH and value4[0]>=value4[1] and value4[1]<>0 and Break20Found<>true and NextHigh=-1 then begin
BuyStop=TicksRound(value4[1]+TicksMTS(value4[1]));
BuyLimit=TicksRound(value4[0]-XAverage(TrueRange,20));
Break20SkipCnt=0;
VALUE9=TL_New(date[value6+1],time[value6+1],high[value6],date,time,high[value6]);
TL_SetSize(VALUE9, 3);
TL_SetColor(VALUE9, blue);
if BreakType=0 then begin
BreakBars=0;
BreakType=1;
{BreakIsLoss=false;}
end;
end;
end;

Break20Found=false;
{ Genera segnali Sell Break Low 20 bars }
if L<=Lowest(L,20)[1] then begin
value10=L;
value11=BarNumber;
value12=value11[0]-value11[1];

if value12>DistL and value10[0]<=value10[1] and value10[1]<>0 and NextLow=-1 then begin
if BreakIsLoss or Break20SkipCnt>0 then begin
SellStop=TicksRound(value10[1]-TicksMTS(value10[1]));
SellLimit=TicksRound(value10[0]+XAverage(TrueRange,20));
Break20SkipCnt=0;
Break20Found=true;
end
else
Break20SkipCnt=Break20SkipCnt+1;
VALUE19=TL_New(date[value12+1],time[value12+1],Low[value12],date,time,Low[value12]);
TL_SetSize(VALUE19, 1);
TL_SetColor(VALUE19, Red);
if BreakType=0 then begin
BreakBars=0;
BreakType=-1;
{BreakIsLoss=false;}
end;
end;

end;
{ Genera segnali Sell Break Low 55 bars }
if L<=Lowest(L,55)[1] then begin
value13=L;
value14=BarNumber;
value15=value14[0]-value14[1];

if value15>DistL and value13[0]<=value13[1] and value13[1]<>0 and Break20Found<>true and NextLow=-1 then begin
SellStop=TicksRound(value13[1]-TicksMTS(value13[1]));
SellLimit=TicksRound(value13[0]+XAverage(TrueRange,20));
Break20SkipCnt=0;
VALUE19=TL_New(date[value15+1],time[value15+1],Low[value15],date,time,Low[value15]);
TL_SetSize(VALUE19, 3);
TL_SetColor(VALUE19, Red);
if BreakType=0 then begin
BreakBars=0;
BreakType=-1;
{BreakIsLoss=false;}
end;
end;
end;

{ Gestione entry multiple high }
if NextHigh<>-1 and cCurrentEntries<MaxOpenEntry and TradeAccount>0 then begin
if H>=Nexthigh then begin
{print("BuyStop :",BuyStop," ",NextHigh," ",date);}
BuyStop=NextHigh;
BuyLimit=NextHigh;
end;
if VALUE29>=0 then
TL_Delete(VALUE29);
VALUE29=TL_New(value99,value98,NextHigh,date+1,time,NextHigh);
TL_SetSize(VALUE29, 3);
TL_SetColor(VALUE29, Cyan);
end;

{ Gestione entry multiple low }
if NextLow<>-1 and cCurrentEntries<MaxOpenEntry and TradeAccount>0 then begin
if L<=NextLow then begin
SellStop=NextLow;
SellLimit=NextLow;
end;
if VALUE38>=0 then
TL_Delete(VALUE38);
value38=TL_New(value99,value98,NextLow,date+1,time,NextLow);
TL_SetSize(value38, 3);
TL_SetColor(value38, Magenta);
end;

{ Verifica ultimo break in loss }
if BreakType<>0 then begin
if BreakBars<=11 then begin
if BreakType=1 then
BreakIsLoss=BreakIsLoss or (BreakStop<=Lowest(Low,BreakBars-1))
else
BreakIsLoss=BreakIsLoss or (BreakStop>=Highest(High,BreakBars-1));
BreakBars=BreakBars+1;
end
else begin
BreakType=0;
BreakIsLoss=false;
end;
end;

if MarketPosition<>-1 and BuyStop<>-1 then begin { Genera ordini Buy }
TradeAccount=Account-(CurrentContracts*AvgEntryPrice)+NetProfit;
{print("BuyStop :",BuyStop," ",TradeAccount," ",date);}
if TradeAccount>2*maxlist(BuyStop,BuyLimit) then begin
PosSize=PositionMoneyMng(TradeAccount,MaxPercRisk,BuyLimit,2*XAverage(TrueRange,20));
Buy ("TLE") PosSize shares next bar at BuyStop stop;
end
else
TradeAccount=-1;
BuyStop=-1;
BuyLimit=-1;
end
else if MarketPosition<>1 and SellStop<>-1 then begin { Genera ordini Sell }
TradeAccount=Account-(CurrentContracts*AvgEntryPrice)+NetProfit;
if TradeAccount>2*minlist(SellStop,SellLimit)then begin
PosSize=PositionMoneyMng(TradeAccount,MaxPercRisk,SellLimit,2*XAverage(TrueRange,20));
Sell ("TSE") PosSize shares next bar at SellStop stop;
end
else
TradeAccount=-1;
SellStop=-1;
SellLimit=-1;
end;

{ Expert Commentary Report }
If AtCommentaryBar then begin
Eq=Account-(CurrentContracts*AvgEntryPrice)+NetProfit;
Vars:Str("");
Str=Str+"*************** System inputs **************************"+NewLine;
Str=Str+"Starting Capital : "+NumToStr(Account,2)+NewLine;
Str=Str+"Percentage of Equity to risk : "+NumToStr(MaxPercRisk,2)+" % "+NewLine;
Str=Str+"*************** Current statistics ************************"+NewLine;
Str=Str+"Market position : "+NumToStr(MarketPosition,0)+NewLine;
Str=Str+"Current Shares held : "+NumToStr(cCurrentContracts,0)+NewLine;
Str=Str+"Current Equity : "+NumToStr(Eq,2)+NewLine;
if BuyStop<>-1 or SellStop<>-1 then
Str=Str+"Percentage of current Equity to risk : "+NumToStr(MaxPercRisk,2)+" % equals "+NumToStr(-Eq*MaxPercRisk/100,2)+" dollars "+NewLine;
If BuyStop<>-1 then begin
Str=Str+"Entry Long : "+NumToStr(BuyStop,4)+NewLine;
Str=Str+"Exit Long : " +NumToStr(TicksRound(BuyStop-2*XAverage(TrueRange,20)),4)+NewLine;
Str=Str+"Difference between Entry Long and Exit Long in Dollars : "+NumToStr(TicksRound(2*XAverage(TrueRange,20)),4)+NewLine;
Str=Str+"Number of shares to buy to risk "+NumToStr(MaxPercRisk,2)+" % of capital : "+NumToStr(PosSize,0)+NewLine;
end
else If MarketPosition=1 then begin
Eq=Account+NetProfit;
T1=-cCurrentContracts*(cAvgEntryPrice-LExitStop);
Str=Str+"Percentage of current Equity to risk : "+NumToStr(AbsValue(100*T1/Eq),2)+" % equals "+NumToStr(T1,2)+" dollars "+NewLine;
Str=Str+"Entry Long : "+NumToStr(cEntryPrice,4)+NewLine;
Str=Str+"Exit Long : " +NumToStr(LExitStop,4)+NewLine;
Str=Str+"Difference between Entry Long and Exit Long in Dollars : "+NumToStr(cEntryPrice-LExitStop,4)+NewLine;
Str=Str+"Open Position Profit in Dollars : "+NumToStr(OpenPositionProfit,2)+NewLine;
end;
If SellStop<>-1 then begin
Str=Str+"Entry Short : "+NumToStr(SellStop,4)+NewLine;
Str=Str+"Exit Short : " +NumToStr(TicksRound(SellStop+2*XAverage(TrueRange,20)),4)+NewLine;
Str=Str+"Difference between Entry Short and Exit Short in Dollars : "+NumToStr(TicksRound(2*XAverage(TrueRange,20)),4)+NewLine;
Str=Str+"Number of shares to Sell to risk "+NumToStr(MaxPercRisk,2)+" % of capital : "+NumToStr(PosSize,0)+NewLine;
end
else If MarketPosition=-1 then begin
Eq=Account+NetProfit;
T1=-cCurrentContracts*(SExitStop-cAvgEntryPrice);
Str=Str+"Percentage of current Equity to risk : "+NumToStr(AbsValue(100*T1/Eq),2)+" % equals "+NumToStr(T1,2)+" dollars "+NewLine;
Str=Str+"Entry Short : "+NumToStr(cEntryPrice,4)+NewLine;
Str=Str+"Exit Short : " +NumToStr(SExitStop,4)+NewLine;
Str=Str+"Difference between Entry Short and Exit Short in Dollars : "+NumToStr(SExitStop-cEntryPrice,4)+NewLine;
Str=Str+"Open Position Profit in Dollars : "+NumToStr(OpenPositionProfit,2)+NewLine;
end;
Commentary(Str);
end;

FINE FORMULA

Tourtle Soup #2

Provided By: Carlo Bastelli 2005 carlo_bastelli@yahoo.it
Turtle mode #2 Ver.0.1.0 03.02.2005 Ordini ripetuti piu' barre }
{ Turtle mode #2 Ver.0.1.1 12.10.2005 Aggiunto Expert Commentary

INIZIO FORMULA

Input:DistH(4),DistL(4),Account(10000),MaxPercRisk(2),MaxOpenEntry(4);
Var:TradeAccount(-1),PosSize(0),TrailBars(10),Eq(0),T1(0);
Var:cCurrentEntries(0),cAvgEntryPrice(0),cEntryPrice(0),cCurrentContracts(0);
Var:BuyStop(-1),BuyLimit(-1),LExitStop(-1),SellStop(-1),SellLimit(-1),SExitStop(-1);
Var:NextHigh(-1),NextLow(-1),BarUntil(-1);
Var:BreakType(0),BreakStop(-1),BreakBars(0),BreakIsLoss(false),Break20Found(false),Break20SkipCnt(0);

if Barnumber=1 then begin
VALUE29=-1;
VALUE38=-1;
end;

cCurrentEntries=CurrentEntries;
cAvgEntryPrice=AvgEntryPrice;
cCurrentContracts=CurrentContracts;
if (cCurrentEntries>cCurrentEntries[1]) then begin
cEntryPrice=(CurrentContracts*AvgEntryPrice-cCurrentContracts[1]*cAvgEntryPrice[1])/
(CurrentContracts-cCurrentContracts[1]);
if MarketPosition=1 then begin
LExitStop=TicksRound(cEntryPrice-2*XAverage(TrueRange,20));
NextHigh=cEntryPrice+0.5*XAverage(TrueRange,20);
NextHigh=TicksRound(NextHigh+TicksMTS(NextHigh));
BuyStop=-1;
BuyLimit=-1;
BarUntil=-1;
VALUE29=-1;
end
else begin
SExitStop=TicksRound(cEntryPrice+2*XAverage(TrueRange,20));
NextLow=cEntryPrice-0.5*XAverage(TrueRange,20);
NextLow=TicksRound(NextLow-TicksMTS(NextLow));
SellStop=-1;
SellLimit=-1;
BarUntil=-1;
VALUE38=-1;
end;
value99=date; { Date & Time per linea entry multiple Buy/Sell }
value98=time;
end;
if (cCurrentEntries=0 and cCurrentEntries[1]<>0) then begin
NextHigh=-1;
NextLow=-1;
LExitStop=-1;
SExitStop=-1;
BuyStop=-1;
BuyLimit=-1;
SellStop=-1;
SellLimit=-1;
BarUntil=-1;
if VALUE29>=0 then
TL_Delete(VALUE29);
VALUE29=-1;
if VALUE38>=0 then
TL_Delete(VALUE38);
VALUE38=-1;
end;

{ Gestione Exit Stop & Trailing }
if MarketPosition=1 then begin
ExitLong ("LES") next bar at LExitStop stop;
if BarsSinceEntry>TrailBars then
ExitLong ("LET") next bar at TicksRound(Lowest(Low,TrailBars)) stop;
end
else if MarketPosition=-1 then begin
ExitShort ("SES") next bar at SExitStop stop;
if BarsSinceEntry>TrailBars then
ExitShort ("SET") next bar at TicksRound(Highest(High,TrailBars)) stop;
end;

Break20Found=false;
{ Genera segnali Buy Break High 20 bars }
if H>=Highest(H,20)[1] then begin
value1=H;
value2=BarNumber;
value3=value2[0]-value2[1];

if value3>DistH and value1[0]>=value1[1] and value1[1]<>0 and NextHigh=-1 then begin
{BuyStop=value1[1]-TicksMTS(value1[1])-XAverage(TrueRange,20);
BuyLimit=value1[1]+XAverage(TrueRange,20);}
if BreakIsLoss or Break20SkipCnt>0 then begin
BuyStop=TicksRound(value1[1]+TicksMTS(value1[1]));
BuyLimit=TicksRound(value1[0]-XAverage(TrueRange,20));
Break20SkipCnt=0;
Break20Found=true;
end
else
Break20SkipCnt=Break20SkipCnt+1;
VALUE9=TL_New(date[value3+1],time[value3+1],high[value3],date,time,high[value3]);
TL_SetSize(VALUE9, 1);
TL_SetColor(VALUE9, Blue);
if BreakType=0 then begin
BreakBars=0;
BreakType=1;
{BreakIsLoss=false;}
end;
end;
end;
{ Genera segnali Buy Break High 55 bars }
if H>=Highest(H,55)[1] then begin
value4=H;
value5=BarNumber;
value6=value5[0]-value5[1];

if value6>DistH and value4[0]>=value4[1] and value4[1]<>0 and Break20Found<>true and NextHigh=-1 then begin
BuyStop=TicksRound(value4[1]+TicksMTS(value4[1]));
BuyLimit=TicksRound(value4[0]-XAverage(TrueRange,20));
Break20SkipCnt=0;
VALUE9=TL_New(date[value6+1],time[value6+1],high[value6],date,time,high[value6]);
TL_SetSize(VALUE9, 3);
TL_SetColor(VALUE9, blue);
if BreakType=0 then begin
BreakBars=0;
BreakType=1;
{BreakIsLoss=false;}
end;
end;
end;

Break20Found=false;
{ Genera segnali Sell Break Low 20 bars }
if L<=Lowest(L,20)[1] then begin
value10=L;
value11=BarNumber;
value12=value11[0]-value11[1];

if value12>DistL and value10[0]<=value10[1] and value10[1]<>0 and NextLow=-1 then begin
if BreakIsLoss or Break20SkipCnt>0 then begin
SellStop=TicksRound(value10[1]-TicksMTS(value10[1]));
SellLimit=TicksRound(value10[0]+XAverage(TrueRange,20));
Break20SkipCnt=0;
Break20Found=true;
end
else
Break20SkipCnt=Break20SkipCnt+1;
VALUE19=TL_New(date[value12+1],time[value12+1],Low[value12],date,time,Low[value12]);
TL_SetSize(VALUE19, 1);
TL_SetColor(VALUE19, Red);
if BreakType=0 then begin
BreakBars=0;
BreakType=-1;
{BreakIsLoss=false;}
end;
end;

end;
{ Genera segnali Sell Break Low 55 bars }
if L<=Lowest(L,55)[1] then begin
value13=L;
value14=BarNumber;
value15=value14[0]-value14[1];

if value15>DistL and value13[0]<=value13[1] and value13[1]<>0 and Break20Found<>true and NextLow=-1 then begin
SellStop=TicksRound(value13[1]-TicksMTS(value13[1]));
SellLimit=TicksRound(value13[0]+XAverage(TrueRange,20));
Break20SkipCnt=0;
VALUE19=TL_New(date[value15+1],time[value15+1],Low[value15],date,time,Low[value15]);
TL_SetSize(VALUE19, 3);
TL_SetColor(VALUE19, Red);
if BreakType=0 then begin
BreakBars=0;
BreakType=-1;
{BreakIsLoss=false;}
end;
end;
end;

{ Gestione entry multiple high }
if NextHigh<>-1 and cCurrentEntries<MaxOpenEntry and TradeAccount>0 then begin
if H>=Nexthigh then begin
BuyStop=NextHigh;
BuyLimit=NextHigh;
end;
if VALUE29>=0 then
TL_Delete(VALUE29);
VALUE29=TL_New(value99,value98,NextHigh,date+1,time,NextHigh);
TL_SetSize(VALUE29, 3);
TL_SetColor(VALUE29, Cyan);
end;

{ Gestione entry multiple low }
if NextLow<>-1 and cCurrentEntries<MaxOpenEntry and TradeAccount>0 then begin
if L<=NextLow then begin
SellStop=NextLow;
SellLimit=NextLow;
end;
if VALUE38>=0 then
TL_Delete(VALUE38);
value38=TL_New(value99,value98,NextLow,date+1,time,NextLow);
TL_SetSize(value38, 3);
TL_SetColor(value38, Magenta);
end;

{ Verifica ultimo break in loss }
if BreakType<>0 then begin
if BreakBars<=11 then begin
if BreakType=1 then
BreakIsLoss=BreakIsLoss or (BreakStop<=Lowest(Low,BreakBars-1))
else
BreakIsLoss=BreakIsLoss or (BreakStop>=Highest(High,BreakBars-1));
BreakBars=BreakBars+1;
end
else begin
BreakType=0;
BreakIsLoss=false;
end;
end;

if MarketPosition<>-1 and BuyStop<>-1 then begin { Genera ordini Buy }
TradeAccount=Account-(CurrentContracts*AvgEntryPrice)+NetProfit;
if TradeAccount>2*maxlist(BuyStop,BuyLimit) then begin
if BarUntil=-1 then begin
PosSize=PositionMoneyMng(TradeAccount,MaxPercRisk,BuyLimit,2*XAverage(TrueRange,20));
BarUntil=BarNumber+DistH;
end;
end
else begin
BuyStop=-1;
BuyLimit=-1;
TradeAccount=-1;
end;
if BarNumber<BarUntil then
Buy ("TLE") PosSize shares next bar at BuyStop stop
else begin
BuyStop=-1;
BuyLimit=-1;
BarUntil=-1;
end;
end
else if MarketPosition<>1 and SellStop<>-1 then begin { Genera ordini Sell }
TradeAccount=Account-(CurrentContracts*AvgEntryPrice)+NetProfit;
if TradeAccount>2*minlist(SellStop,SellLimit)then begin
if BarUntil=-1 then begin
PosSize=PositionMoneyMng(TradeAccount,MaxPercRisk,SellLimit,2*XAverage(TrueRange,20));
BarUntil=BarNumber+DistL;
end;
end
else begin
SellStop=-1;
SellLimit=-1;
TradeAccount=-1;
end;
if BarNumber<BarUntil then
Sell ("TSE") PosSize shares next bar at SellStop stop
else begin
SellStop=-1;
SellLimit=-1;
BarUntil=-1;
end;
end;

{ Expert Commentary Report }
If AtCommentaryBar then begin
Eq=Account-(CurrentContracts*AvgEntryPrice)+NetProfit;
Vars:Str("");
Str=Str+"*************** System inputs **************************"+NewLine;
Str=Str+"Starting Capital : "+NumToStr(Account,2)+NewLine;
Str=Str+"Percentage of Equity to risk : "+NumToStr(MaxPercRisk,2)+" % "+NewLine;
Str=Str+"*************** Current statistics ************************"+NewLine;
Str=Str+"Market position : "+NumToStr(MarketPosition,0)+NewLine;
Str=Str+"Current Shares held : "+NumToStr(cCurrentContracts,0)+NewLine;
Str=Str+"Current Equity : "+NumToStr(Eq,2)+NewLine;
if BuyStop<>-1 or SellStop<>-1 then
Str=Str+"Percentage of current Equity to risk : "+NumToStr(MaxPercRisk,2)+" % equals "+NumToStr(-Eq*MaxPercRisk/100,2)+" dollars "+NewLine;
If BuyStop<>-1 then begin
Str=Str+"Entry Long : "+NumToStr(BuyStop,4)+NewLine;
Str=Str+"Exit Long : " +NumToStr(TicksRound(BuyStop-2*XAverage(TrueRange,20)),4)+NewLine;
Str=Str+"Difference between Entry Long and Exit Long in Dollars : "+NumToStr(TicksRound(2*XAverage(TrueRange,20)),4)+NewLine;
Str=Str+"Number of shares to buy to risk "+NumToStr(MaxPercRisk,2)+" % of capital : "+NumToStr(PosSize,0)+NewLine;
end
else If MarketPosition=1 then begin
Eq=Account+NetProfit;
T1=-cCurrentContracts*(cAvgEntryPrice-LExitStop);
Str=Str+"Percentage of current Equity to risk : "+NumToStr(AbsValue(100*T1/Eq),2)+" % equals "+NumToStr(T1,2)+" dollars "+NewLine;
Str=Str+"Entry Long : "+NumToStr(cEntryPrice,4)+NewLine;
Str=Str+"Exit Long : " +NumToStr(LExitStop,4)+NewLine;
Str=Str+"Difference between Entry Long and Exit Long in Dollars : "+NumToStr(cEntryPrice-LExitStop,4)+NewLine;
Str=Str+"Open Position Profit in Dollars : "+NumToStr(OpenPositionProfit,2)+NewLine;
end;
If SellStop<>-1 then begin
Str=Str+"Entry Short : "+NumToStr(SellStop,4)+NewLine;
Str=Str+"Exit Short : " +NumToStr(TicksRound(SellStop+2*XAverage(TrueRange,20)),4)+NewLine;
Str=Str+"Difference between Entry Short and Exit Short in Dollars : "+NumToStr(TicksRound(2*XAverage(TrueRange,20)),4)+NewLine;
Str=Str+"Number of shares to Sell to risk "+NumToStr(MaxPercRisk,2)+" % of capital : "+NumToStr(PosSize,0)+NewLine;
end
else If MarketPosition=-1 then begin
Eq=Account+NetProfit;
T1=-cCurrentContracts*(SExitStop-cAvgEntryPrice);
Str=Str+"Percentage of current Equity to risk : "+NumToStr(AbsValue(100*T1/Eq),2)+" % equals "+NumToStr(T1,2)+" dollars "+NewLine;
Str=Str+"Entry Short : "+NumToStr(cEntryPrice,4)+NewLine;
Str=Str+"Exit Short : " +NumToStr(SExitStop,4)+NewLine;
Str=Str+"Difference between Entry Short and Exit Short in Dollars : "+NumToStr(SExitStop-cEntryPrice,4)+NewLine;
Str=Str+"Open Position Profit in Dollars : "+NumToStr(OpenPositionProfit,2)+NewLine;
end;
Commentary(Str);
end;
FINE FORMULA

Vedi anche: