كود:
var
Bar
: integer;
// Put Trading System Vars Here ---
var
iRSISeries, iStochDSeries, iRSIStochSeries, iPriceOscSeries,
iMA19, iMA39, iMA30, iCountSeries,
iCount
: integer;
// End of Trading System Vars ---
// Integers required by ST Loop ---
var
StartScan, StopScan,
P1, P2, P3, P4, P5, P6, P7, P1Max,
Start,
SmoothedEquity
: integer;
// Floats required by ST Loop ---
var
EquityFinal, EquityMax,
tempf
: float;
// Booleans required by ST Loop ---
var
FinalRun
: boolean;
// Limits Of Our Self Tuning Loop ---
StartScan := 3;
StopScan := 13;
// Reset our Vars ---
EquityMax := 0.0 ;
P1Max := 0 ;
FinalRun := False ;
// Start of our self tuning loop ---
for P1 := StartScan to StopScan do
begin
//If we are at the end of the run, set up for the best Equity Results ---
if ( P1 = StopScan )
then
begin
FinalRun := True ;
P1 := P1Max ;
end;
// Calculate our current tuning Paramaters ---
P2 := trunc( P1 * 14/5 ) ;
P3 := trunc( P1 * 21/5 ) ;
P4 := trunc( P1 * 13/5 ) ;
P5 := trunc( P1 * 19/5 ) ;
P6 := trunc( P1 * 30/5 ) ;
P7 := trunc( P1 * 39/5 ) ;
// starting bar to fill longest period indicator ---
Start := P7 + 1 ;
// Uncomment For Debug ---
//Print('P1:' + IntToStr(P1) + ' P2:' + IntToStr(P2) + ' P3:' + IntToStr(P3) + ' Start:' + IntToStr(Start) );
//-------------------------------------------
//Build our Working Indicator Series Here ---
//-------------------------------------------
iCountSeries := CreateSeries();
iRSISeries := RSISeries(#Close, P2 );
iStochDSeries := StochDSeries( P3 , P4 );
iMA19 := SMASeries( #Close, P5 );
iMA30 := SMASeries( #Close, P6 );
iMA39 := SMASeries( #Close, P7 );
iRSIStochSeries := DivideSeriesValue(AddSeries(iRSISeries, iStochDSeries),2);
iPriceOscSeries := SubtractSeries(iMA19, iMA39);
for Bar := Start to BarCount -1 do
begin
iCount := 0;
if @iRSIStochSeries[Bar] >= 50 then
Inc(iCount);
if @iRSIStochSeries[Bar] >= 65 then
Inc(iCount);
if @iPriceOscSeries[Bar] > 0 then
Inc(iCount);
if (@iPriceOscSeries[Bar] - @iPriceOscSeries[Bar-6]) > 0 then
Inc(iCount);
if (@iRSIStochSeries[Bar] - @iRSIStochSeries[Bar-6]) > 0 then
Inc(iCount);
if PriceClose(Bar) > @iMA30[Bar] then
Inc(iCount);
@iCountSeries[Bar] := iCount;
if iCount <= 0.5 then
SetSeriesBarColor(Bar, iCountSeries, #red );
if (iCount > 0.5) and (iCount < 2) then
SetSeriesBarColor(Bar, iCountSeries, #black );
if (iCount >= 2) and (iCount < 3) then
SetSeriesBarColor(Bar, iCountSeries, #Fuchsia );
if (iCount >= 3) and (iCount < 4.5) then
SetSeriesBarColor(Bar, iCountSeries, #blue );
if (iCount >= 4.5) and (iCount <= 5) then
SetSeriesBarColor(Bar, iCountSeries, #lime );
if iCount > 5 then
SetSeriesBarColor(Bar, iCountSeries, #Green );
end;
//-------------------------------------------
//Build our Trading Loop Here ---
//-------------------------------------------
for Bar := Start to BarCount - 1 do
begin
if not LastPositionActive then
{ Entry Rules }
begin
if @iCountSeries[Bar] >= 5 then
BuyAtMarket(Bar+1, 'Turtle Buy');
end
else
{ Exit Rules }
begin
if @iRSIStochSeries[Bar] < 65 then
if @iCountSeries[Bar] <= 3 then
SellAtMarket(Bar+1, LastPosition, 'Turtle Sell');
end;
end; //Trading Loop ---
// smooth the equity curve for the last 30 samples ---
SmoothedEquity := CreateSeries;
tempf := 0.0 ;
//Sum up the Equity ---
For Bar := Start to BarCount -1 do
tempf := tempf + Equity(Bar);
// ave equity of the last 30 bars ---
EquityFinal := tempf / 30 ;
// Uncomment For Debug ---
//ShowMessage('Test for P1 '+IntToStr(P1)+' Equity = '+ FormatFloat( '#,##0.00', EquityFinal ) );
// See if we have a new max equity ---
if ( EquityFinal > EquityMax )
then
begin
EquityMax := EquityFinal ;
P1Max := P1 ;
end;
// If this is the final run, break the loop so our Indicators can print ---
if (FinalRun )then break ;
// Reset the Position and try again ---
ClearPositions ;
end; //P1 Loop ---
// Uncomment For Debug ---
Print('P1Max:' + IntToStr(P1Max) + ' EquityMax:' + FloatToStr(EquityMax));
// Display All Indicators Here ---
hidevolume;
var RSIPane: integer;
RSIPane := CreatePane(50, true, true );
PlotSeries( iRSISeries, RSIPane, 900, #Thick );
DrawLabel( 'RSI(Close,'+IntToStr(P2)+')', RSIPane );
PlotSeries( iStochDSeries, RSIPane, 009, #Thick );
DrawLabel( 'StochD('+IntToStr(P3)+','+IntToStr(P4)+')', RSIPane );
var RSIStochPane: integer;
RSIStochPane := CreatePane(50, true, true );
PlotSeries( iRSIStochSeries, RSIStochPane, 900, #Thick );
DrawLabel( 'RSI/Stoc', RSIStochPane );
PlotSeries( iMA19, 0, 050, #Thin );
DrawLabel( 'SMA(Close,'+IntToStr(P5)+')', 0 );
PlotSeries( iMA39, 0, 009, #Thin );
DrawLabel( 'SMA(Close,'+IntToStr(P7)+')', 0 );
PlotSeries( iMA30, 0, 900, #thick );
DrawLabel( 'SMA(Close,'+IntToStr(P6)+')', 0 );
var CountPane: integer;
CountPane := CreatePane(100, false, true );
PlotSeries( iCountSeries, CountPane, 900, #ThickHist );
DrawLabel( 'Turtle Count Green-Buy / Blue-Hold / Red-Sell', CountPane );
SetPaneMinMax(CountPane, 0, 7);
DrawLabel( 'Base Tuning Variable P1 = '+IntToStr(P1), CountPane );
DrawLabel( ' ' , CountPane);
// warn that tune chose the start value
if ( P1 = StartScan ) then
begin
DrawLabel( 'WARNING P1 = the starting scan value - Not a Good Choice! ' , CountPane );
end;
if ( P1 = StopScan ) then
begin
DrawLabel( 'WARNING P1 = the stop scan value ' , CountPane );
end;