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

Dúvida sobre stop loss móvel?

4 Posts
3 Usuários
1 Reactions
114 Visualizações
(@jasayresbr)
Novo membro
Registrou: 2 anos atrás
Posts: 2
Iniciador do tópico  

Me desculpe minha ignorância, mais com eu insiro o código do snippes, Stoploss móvel (Versão contínua) em minha estratégia sem alterar minha estratégia de execução, trocados em miúdos eu tenho minha estratégia que executa uma compra/venda em determinado sinal de entrada e quero somente adicionar o seu stoploss móvel, mais quando eu colo seu sippes ele dá diferentes entradas na minha automação e eu quero só agregar o seu stoploss móvel, desde já agradeço qualquer ajuda.


   
Citar
masker
(@masker)
Membro confiável
Registrou: 2 anos atrás
Posts: 41
 

Olá amigo,

basicamente a parte do modelo de stoploss móvel que vc irá precisar implementar no seu código é essa:

//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 + cTrailingStopOffset*MinPriceIncrement) <= fPrecoStop)
      and (Close < SellPrice) then
      begin
        fPrecoStop := Close + cTrailingStopOffset*MinPriceIncrement;
        fPrecoStopOffset := fPrecoStop + cStopOffsetEmTicks*MinPriceIncrement;
      end;

      BuyStop(fPrecoStop,fPrecoStopOffset);
      if isSold then BuyLimit(fPrecoAlvo);
    end;

Você pode incluir antes do bloco de abertura das posições. Lembrando que este exemplo está a saída de posições vendidas.

Não se esqueça de incluir as variáveis:

var
  fPrecoStop, fPrecoAlvo, fPrecoStopOffset: float;

e também os parâmetros:

const
 //Relação risco/ganho de 3:1
 cStopEmTicks = 10;
 cAlvoEmTicks = 30;
 cTrailingStopOffset = 15;
 cStopOffsetEmTicks = 50;

Espero que te ajude.

Abs

 


   
Johnathas reacted
ReplyCitar
(@jasayresbr)
Novo membro
Registrou: 2 anos atrás
Posts: 2
Iniciador do tópico  
Inicio
  Se (Fechamento > keltnerCh(3.00,20,1)|0|) então
    SellShortLimit(high);
  Se (Fechamento > KeltnerCh(3.00,20,1)|0|) então
    PaintBar(clYellow);
  Se (Fechamento < keltnerCh(3.00,20,1)|1|) então
    BuyLimit(Low);
  Se (Fechamento < KeltnerCh(3.00,20,1)|1|) então
    PaintBar(clblue);
  Plot(KeltnerCh(3.00,20,1)|0|);
  Plot2(KeltnerCh(3.00,20,1)|1|);
  Fim;
 




   
ReplyCitar
(@admin)
Membro Admin
Registrou: 2 anos atrás
Posts: 216
 

Oi @jasayresbr!

O @masker foi preciso na resposta! Agora que você postou o código só vou fazer o que ele orientou. Seu código deveria ficar assim:

 

const
 //Basta você definir aqui os parametros do stop móvel
 //Imagino que queira deixar a operação rolar sem alvo
 //Você pode colocar o alvo em ticks bem distante ou
 //excluir essa variável e remover todos os comandos que
 //referenciam cAlvoEmTicks

 //Para habilitar o stop movel desde o início basta configurar
 //cTraillingStopTrigger igual a zero 

 cStopEmTicks = 15;
 cAlvoEmTicks = 100;
 cTraillingStopTrigger = 0;
 cTrailingStopOffset = 10;
 cStopOffsetEmTicks = 50;

input
  bGestaoDeRiscoPelaEstrategia(true);
  bModuloAutomacao(false);
  iHorarioEncerramentoDaytrade(1645);

var
  fPrecoStop, fPrecoAlvo, fPrecoStopOffset: float;
  bConfigurouRiscoInicial: boolean;
  bTrailingStopAtivado: boolean;
  bPosicionado: boolean;
begin
  bPosicionado := hasPosition;

  // Primeiro bloco é a sua lógica de compra e venda
  Se (Fechamento > keltnerCh(3.00,20,1)|0|) e not bPosicionado então
    SellShortLimit(high);

  Se (Fechamento > KeltnerCh(3.00,20,1)|0|) então
    PaintBar(clYellow);

  Se (Fechamento < keltnerCh(3.00,20,1)|1|) e not bPosicionado então
    BuyLimit(Low);

  Se (Fechamento < KeltnerCh(3.00,20,1)|1|) então
    PaintBar(clblue);

  // Mudei os plots para 4 e 5 só para não conflitar com as linhas de 
  // stop e alvo
  Plot4(KeltnerCh(3.00,20,1)|0|);
  Plot5(KeltnerCh(3.00,20,1)|1|);

  if bGestaoDeRiscoPelaEstrategia 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;

      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;


    //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;

      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;

    // Se for utilizar encerramento de posição a partir
    // de determinado horário não esqueça também de limitar o 
    // horário de abertura de posição 

    //Encerra posicao comprada no horario limite
    //if (Time >= iHorarioEncerramentoDaytrade)
    //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;

   
ReplyCitar