Neo traderBot

Neo traderBot

Você sabia?

Abordamos o tema de automatização de estratégias em NTSL, MQL5 e NinjaScript!

leaf leftleaf right
Notifications
Clear all

Código de automação com stoploss móvel e breakeven - Ajuda

1 Posts
1 Usuários
0 Reactions
87 Visualizações
(@ewersonmansini)
Novo membro
Registrou: 3 meses atrás
Posts: 1
Iniciador do tópico  
const

// Relação de risco/ganho de 2,5 x 1

  cStopEmTicks = 20;
  cAlvoEmTicks = 50;
  cTraillingStopTrigger = 16;
  cTrailingStepEmTicks = 20;
  cTrailingStopOffset = 20;
  cBreakevenEmTicks = 10;
  cStopOffsetEmTicks = 40;

input
  preco(close);
  periodo(0);
  bGestaoRisco(true);
  iHorarioInicioAberturaPosicao(0905);
  iHorarioFimAberturaPosicao(1630);


var

// login para a estratégia funcionar

  CPF             :integer;
  Validade        :integer;

//Variáveis

  bSinalCompra, bSinalVenda, bPosicionado: boolean;
  fPrecoStop, fPrecoAlvo, fPrecoStopOffset: float;
  fFloorTraillingStop: float;
  bBreakevenAtivado: boolean;
  bConfigurouRiscoInicial: boolean;
  bTrailingStopAtivado: boolean;
 

begin

    // ----------------- Inicialização da estratégia -----------------------
    // OBS: Inicialização de variáveis a serem utilizadas na estratégia de
    // execução
    // ---------------------------------------------------------------------
  
    bSinalCompra := false;
    bSinalVenda := false;
    bPosicionado := hasPosition;

    //Condição para abertura de posição

    if Not bPosicionado and (Time >= iHorarioInicioAberturaPosicao) and (Time <= iHorarioFimAberturaPosicao) then
    
    begin

        //Condição para compra

        if (open[2] < close[2]) and 
        (open[1] < close[1]) then
           PaintBar(clBlue);    
           bSinalCompra := true;

        //Condição para compra

        if (open[2] > close[2]) and 
        (open[1] > close[1]) then
           PaintBar(clyellow);
           bSinalVenda := True;

    end;

    //Envio de ordens

    if bSinalCompra then BuyAtMarket;

    if bSinalVenda then SellShortAtMarket;

    if bGestaoRisco then

    begin
        
        //POSIÇÃO COMPRADA
        //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

            begin

                fPrecoStop := Close - cTrailingStopOffset*MinPriceIncrement;
                fPrecoStopOffset := fPrecoStop - cStopOffsetEmTicks*MinPriceIncrement;

            end;

            SellToCoverStop(fPrecoStop,fPrecoStopOffset);
            SellToCoverLimit(fPrecoAlvo);

        end;

        //POSIÇÃO VENDIDA
        //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
      
            begin
        
                fPrecoStop := Close + cTrailingStopOffset*MinPriceIncrement;
                fPrecoStopOffset := fPrecoStop + cStopOffsetEmTicks*MinPriceIncrement;
      
            end;

            BuyToCoverStop(fPrecoStop,fPrecoStopOffset);
            BuyToCoverLimit(fPrecoAlvo);
        
        end;

        //Encerra posicao comprada no horario limite
        if (Time >= iHorarioFimAberturaPosicao)
           and HasPosition
           then ClosePosition;

        if Not hasPosition and bConfigurouRiscoInicial then
    
        begin
      
            bConfigurouRiscoInicial := false;
            bTrailingStopAtivado := false;

        end;

    end;

    // Plot de Stop e Alvo para inspeção visual do código

    bPosicionado := hasPosition;

    if bPosicionado or bPosicionado[1] or bPosicionado[2] then
  
    begin
        
        PlotN(1,fPrecoStop);
        PlotN(2,fPrecoStopOffset);
        PlotN(3,fPrecoAlvo);
        SetPlotColor(1,clRed);
        SetPlotColor(2,clRed);
        SetPlotColor(3,clGreen);
        SetPlotStyle(1,psDash);
        SetPlotStyle(2,psDash);
        SetPlotStyle(3,psDash);
        SetPlotWidth(1,2);
        SetPlotWidth(2,2);
        SetPlotWidth(3,2);

    end;

end;

s


   
Citar