Um backtesting adequado deve buscar simular situações práticas do mercado (slippage, custo de operação, etc...)
Olá Johnathas,
const //Relação risco/ganho de 3:1 cStopEmTicks = 18; cStopOffsetEmTicks = 3; cTrailingStopOffset = 3; cBreakevenEmTicks = 3; cTraillingStopTrigger = 3; cAlvoEmTicks = 25; dAccAgrssSaldo = 2; MediaEntrada = 5; MediaFiltro = 11; dAgressionVolBalance = 1; input bGestaoDeRiscoPelaEstrategia(true); bModuloAutomacao(false); iHorarioInicioAberturaPosicao(0905); iHorarioFimAberturaPosicao(1640); iHorarioEncerramentoDaytrade(1700); var bSinalCompra,bSinalVenda,bPosicionado : boolean; fPrecoStop,fPrecoAlvo,fPrecoStopOffset : float; bBreakevenAtivado : boolean; bConfigurouRiscoInicial : boolean; bTrailingStopAtivado : boolean; dSaldoAgressão,vSaldo : float; mEntrada,mFiltro,aux : Float; begin aux := IFR(9); mEntrada := MediaExp(MediaEntrada,Fechamento); mFiltro := MediaExp(MediaFiltro,Fechamento); vSaldo := Summation((AgressionVolBuy - AgressionVolSell),dAgressionVolBalance + dAccAgrssSaldo); dSaldoAgressao := AccAgressSaldo(2); bPosicionado := hasPosition; if Not bPosicionado and (Time >= iHorarioInicioAberturaPosicao) and (Time <= iHorarioFimAberturaPosicao) then begin se (Close >= Close[1]) e (Minima < mFiltro) e (fechamento > mEntrada) e (MediaEntrada > MediaFiltro) e (AgressionVolBuy > AgressionVolSell) e (Volume > Volume[1]) then BuyLimit(mEntrada); se (Close <= Close[1]) e (Maxima > mFiltro) e (fechamento < mEntrada) e (MediaEntrada < MediaFiltro) e (AgressionVolSell > AgressionVolBuy) e (Volume > Volume[1]) then SellShortLimit(mEntrada); end; if bGestaoDeRiscoPelaEstrategia then begin //Código responsável pela manutenção das ordens de stoploss e take profit if isBought then begin if Not bConfigurouRiscoInicial then begin fPrecoStop := BuyPrice - cStopEmTicks * MinPriceIncrement; fPrecoAlvo := BuyPrice + cAlvoEmTicks * MinPriceIncrement; fPrecoStopOffset := fPrecoStop - cStopOffsetEmTicks * MinPriceIncrement; bConfigurouRiscoInicial := true; end; if ((Close >= (BuyPrice + cTraillingStopTrigger * MinPriceIncrement)) or (cTraillingStopTrigger = 0)) and ( not bTrailingStopAtivado) then bTrailingStopAtivado := true; if ((Close - cTrailingStopOffset * MinPriceIncrement) >= fPrecoStop) and bTrailingStopAtivado then fPrecoStop := Close - cTrailingStopOffset * MinPriceIncrement; if (Close >= (BuyPrice + cBreakevenEmTicks * MinPriceIncrement)) and ( not bBreakevenAtivado) then begin bBreakevenAtivado := true; if fPrecoStop < BuyPrice then fPrecoStop := BuyPrice + MinPriceIncrement * 3; end; fPrecoStopOffset := fPrecoStop - cStopOffsetEmTicks * MinPriceIncrement; if Not bModuloAutomacao then begin SellToCoverStop(fPrecoStop,fPrecoStopOffset); if isBought then SellToCoverLimit(fPrecoAlvo); end else begin SellShortStop(fPrecoStop,fPrecoStopOffset); if isBought then SellShortLimit(fPrecoAlvo); end; end; //Código responsável pela manutenção das ordens de stoploss e take profit if isSold then begin if Not bConfigurouRiscoInicial then begin fPrecoStop := SellPrice + cStopEmTicks * MinPriceIncrement; fPrecoAlvo := SellPrice - cAlvoEmTicks * MinPriceIncrement; fPrecoStopOffset := fPrecoStop + cStopOffsetEmTicks * MinPriceIncrement; bConfigurouRiscoInicial := true; end; if ((Close <= (SellPrice - cTraillingStopTrigger * MinPriceIncrement)) or (cTraillingStopTrigger = 0)) and ( not bTrailingStopAtivado) then bTrailingStopAtivado := true; if ((Close + cTrailingStopOffset * MinPriceIncrement) <= fPrecoStop) and bTrailingStopAtivado then fPrecoStop := Close + cTrailingStopOffset * MinPriceIncrement; if (Close <= (SellPrice - cBreakevenEmTicks * MinPriceIncrement)) and ( not bBreakevenAtivado) then begin bBreakevenAtivado := true; if fPrecoStop > SellPrice then fPrecoStop := SellPrice + MinPriceIncrement * 3; end; fPrecoStopOffset := fPrecoStop + cStopOffsetEmTicks * MinPriceIncrement; if Not bModuloAutomacao then begin BuyToCoverStop(fPrecoStop,fPrecoStopOffset); if isSold then BuyToCoverLimit(fPrecoAlvo); end else begin BuyStop(fPrecoStop,fPrecoStopOffset); if isSold then BuyLimit(fPrecoAlvo); end; end; //Encerra posicao if (Time = iHorarioEncerramentoDaytrade) and HasPosition then ClosePosition; if Not hasPosition and bConfigurouRiscoInicial then begin bConfigurouRiscoInicial := false; bBreakevenAtivado := false; bTrailingStopAtivado := false; end; end; end;