{Сигнал F_optF – стратегия, передача полученного профита и оптимизация.}
Input:
Price((H+L+C)/3),
PToChange(4.), {Max correction to change trend}
InitCapital(100000), {Initial capital to trade}
TSLink(0), {0 - dont work TSLink.dll 1 - work}
MinTradeToOpt(10), {Minimum amount an trades for beginning of optimization}
Path(""), {Path to Excel file with obligatory sign \ on end}
NameFile(""); {name of Excel file}
Var:
Trend(0), LL(99999), HH(0), MP(0), Trades(0),
Str(""), Tr2(""), Tr3(""), Kap(""), PProf(0);
{Declaration TSLink.dll functions}
defineDLLFunc: "tslink.dll", int, "TS_StartExcel",LPSTR,LPSTR; {initalisation excel file 1 - Short BookName, 2 - full path bookname}
defineDLLFunc: "tslink.dll", int, "TS_SetValue",float,int,int,int,int; {value, sh num, row, column, spleep milisekonds}
defineDLLFunc: "tslink.dll", float, "TS_GetValue",int,int,int,int; {sh num, row, column, spleep milisekonds}
defineDLLFunc: "tslink.dll", int, "TS_SetString",lpstr,int,int,int,int; {text, sh num, row, column, spleep milisekonds}
defineDLLFunc: "tslink.dll", lpstr, "TS_GetString",int,int,int,int; {sh num, row, column, spleep milisekonds}
{End declaration}
MP = MarketPosition;
Trades = totaltrades;
Tr2 = numtostr(trades+2,0);
Tr3 = numtostr(trades+3,0);
Kap = numtostr(InitCapital,0);
{****************** Generate trend signal *******************}
if MP <= 0 then
begin
if Price < LL then LL = Price;
if Price cross above LL*(1 + PToChange*.01) then
begin
Trend = 1;
{растущий тренд}
HH = Price;
end;
end;
if MP >= 0 then
begin
if Price > HH then HH = Price;
if Price cross below HH*(1 - PToChange*.01) then
begin
Trend = -1;
{падающий тренд}
LL = Price;
end;
end;
{Generate entries}
if trend = 1 and trend[1] <> 1 then buy("OpenLong") at market;
if trend = -1 and trend[1] <> -1 then sell("OpelShort") at market;
{******************** End of ***********************}
{***************** Interface with Excel ***************}
if (TSLink=1) and (Path<>"") and (NameFile <> "") then
begin
{****** если первый бар, то открывается Excel файл, происходит очистка рабочей области Excel файла и запись размера InitCapital в Excel *******}
if currentbar = 1 then
begin
Value22 = TS_StartExcel(NameFile,Path+NameFile);
{Begin clear workspace}
TS_SetValue(1,1,1,8,0);
{Write InitCapital in to workspace}
TS_SetValue(InitCapital,1,2,1,0);
end;
{*************************************}
{********** Запись текущего количества трейдов, профита, формулы HPR, в зависимости от того первая или нет сделка запись формулы для TWR и Equity }
if trades <> trades[1] then
begin
{Writing an amount of trades}
TS_SetValue(trades,1,2,4,0);
{Writing an profit of trades}
TS_SetValue(round(positionProfit(1)/(EntryPrice(1)*MaxContracts(1)),3),1,trades+3,1,0);
{Write HPR}
TS_SetString("=1-C$2*A"+Tr3+"/B$2",1,trades+3,2,0);
if Trades=1 then
begin
TS_SetString("=B4",1,trades+3,3,0);
TS_SetString("=A$2*C4",1,trades+3,4,0);
end
else
begin
TS_SetString("=C"+Tr2+"*B"+Tr3,1,trades+3,3,0);
TS_SetString("=A$2*C"+Tr3,1,trades+3,4,0);
end;
{***************** }
{Begin optimization}
if trades>MinTradeToOpt then TS_SetValue(1,1,2,8,0);
end;
end;
Метод Change в файле OptF.xls, Лист1
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("H4").Value = 1 Then
Range("H4").Value = 0
Rows("5:5").Select
Selection.Delete Shift:=xlUp
End If
If Range("H1").Value = 1 Then
Range("H1").Value = 0
Range("A4:E1009").Select
Selection.ClearContents
EnableCalculation = False
' Worksheets(1).Calculate
End If
If Range("H2").Value = 1 Then
Range("H2").Value = 0
EnableCalculation = True
Dim pr
pr = "$C$" + Range("E2").Text
SOLVER.SolverReset
SOLVER.SolverAdd CellRef:="$C$2", Relation:=1, FormulaText:="1"
SOLVER.SolverAdd CellRef:="$C$2", Relation:=3, FormulaText:="0"
SOLVER.SolverOk SetCell:=pr, MaxMinVal:=1, ValueOf:="0", ByChange:="$C$2"
' "$C$" + Str(Range("D2").Value + 3)
' "$C$109"
SOLVER.SolverSolve UserFinish:=True
EnableCalculation = False
End If
If Range("H3").Value = 1 Then
Range("H3").Value = 0
Worksheets(1).Calculate
End If
End Sub