Trailing Stop

How to create strategies and indicators
Message
Author
southsider
Posts: 1
Joined: Tue Dec 01, 2009 8:50 pm

Trailing Stop

#1 Postby southsider » Tue Dec 01, 2009 8:52 pm

Can someone post code they use for trailing stops

My programming skills are a bit rusty

User avatar
Terranin
Site Admin
Posts: 833
Joined: Sat Oct 21, 2006 4:39 pm

Re: Trailing Stop

#2 Postby Terranin » Sat Dec 05, 2009 12:11 pm

southsider wrote:Can someone post code they use for trailing stops

My programming skills are a bit rusty


Here it is some code in Delphi:

Code: Select all

Procedure TS;
var
  i:integer;
begin
  if OrdersTotal>0 then
  for i:=0  to OrdersTotal do
{1} begin
  OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if (OrderType=tp_Buy) then
      begin
      if (OrderStopLoss=0) and (Bid-OrderOpenPrice>TrailingStop*Point) then  ModifyOrder(OrderTicket,OrderOpenPrice,Bid-TrailingStop*Point,0);
      if (OrderStopLoss>0)  and (RoundTo(Bid-TrailingStop*Point,-4)>RoundTo(OrderStopLoss,-4)) then
        begin
        if Bid-OrderStoploss>(TrailingStop+TrailingStep)*Point then   ModifyOrder(OrderTicket,OrderOpenPrice,Bid-TrailingStop*Point,0);
        end;
      end;
      if  (OrderType=tp_Sell) then
      begin
      if (OrderStopLoss=0) and (OrderOpenPrice-Ask>Trailingstop*Point) then   ModifyOrder(OrderTicket,OrderOpenPrice,Ask+TrailingStop*Point,0);
      if (OrderStopLoss>0)  and (RoundTo(Ask+TrailingStop*Point,-4)< RoundTo(OrderStopLoss,-4)) then
        begin
        if (OrderStopLoss-Ask)>(TrailingStop+TrailingStep)*Point then   ModifyOrder(OrderTicket,OrderOpenPrice,Ask+TrailingStop*Point,0);
        end;
      end;
{1} end;
end;


Uses TrailingStop and TralingStep parameters equal to same parameters in order dialog window.
Hasta la vista
Mike

LeoneDeluca
Posts: 1
Joined: Sat Nov 13, 2010 6:42 pm

trailing stop parameter definition

#3 Postby LeoneDeluca » Sat Nov 13, 2010 6:47 pm

what are the following:

-Trailing Stop, Points
-Step of change, Points

FT Support
Posts: 905
Joined: Sat Jul 11, 2009 10:54 am

#4 Postby FT Support » Sun Nov 14, 2010 5:42 am

Check our other product here:
http://www.forexcopier.com

eriaku
Posts: 5
Joined: Thu Apr 19, 2012 6:27 am
Location: UG

Re: Trailing Stop

#5 Postby eriaku » Tue May 15, 2012 10:04 pm

Terranin wrote:
and (RoundTo(Bid-TrailingStop*Point,-4)>RoundTo(OrderStopLoss,-4)) then
begin



What's the '-4' for? I cant find any code for trailing stops in the FT manual, and have tried to learn RoundTo from google finds, but need the equivalent of above expression in C++ or MQL, please?

FT Support
Posts: 905
Joined: Sat Jul 11, 2009 10:54 am

#6 Postby FT Support » Wed May 16, 2012 1:50 am

-4 means that the result of RoundTo function will be with 4 digits after point, in MQL you can use "NormalizeDouble" function

Code: Select all

NormalizeDouble(X, 4)
Check our other product here:
http://www.forexcopier.com

eriaku
Posts: 5
Joined: Thu Apr 19, 2012 6:27 am
Location: UG

Re: Trailing Stop

#7 Postby eriaku » Thu May 17, 2012 4:58 am

Terranin wrote:
Here it is some code in Delphi:

Code: Select all

Procedure TS;
var
  i:integer;
begin
  if OrdersTotal>0 then
  for i:=0  to OrdersTotal do
{1} begin
  OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if (OrderType=tp_Buy) then
      begin
      if (OrderStopLoss=0) and (Bid-OrderOpenPrice>TrailingStop*Point) then  ModifyOrder(OrderTicket,OrderOpenPrice,Bid-TrailingStop*Point,0);
      if (OrderStopLoss>0)  and (RoundTo(Bid-TrailingStop*Point,-4)>RoundTo(OrderStopLoss,-4)) then
        begin
        if Bid-OrderStoploss>(TrailingStop+TrailingStep)*Point then   ModifyOrder(OrderTicket,OrderOpenPrice,Bid-TrailingStop*Point,0);
        end;
      end;
      if  (OrderType=tp_Sell) then
      begin
      if (OrderStopLoss=0) and (OrderOpenPrice-Ask>Trailingstop*Point) then   ModifyOrder(OrderTicket,OrderOpenPrice,Ask+TrailingStop*Point,0);
      if (OrderStopLoss>0)  and (RoundTo(Ask+TrailingStop*Point,-4)< RoundTo(OrderStopLoss,-4)) then
        begin
        if (OrderStopLoss-Ask)>(TrailingStop+TrailingStep)*Point then   ModifyOrder(OrderTicket,OrderOpenPrice,Ask+TrailingStop*Point,0);
        end;
      end;
{1} end;
end;


Uses TrailingStop and TralingStep parameters equal to same parameters in order dialog window.


Can anyone code this trailing stop code in MQL?


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 18 guests