Dynamic Trailing Stop Loss 0

Posted on 13, May 2014

in Category SID


Description for TrailingStop SID 954

An example on how to implement a dynamic trailing stop. This example is using FXCM and Forex with EURUSD with a 10 pip stop. If the price moves up, the stop moves up with it.

Dynamic Trailing Stop Loss/sid Notes for this Trading Account:

Dynamic Trailing Stop Loss/sid Rules for this Trading Account:

Dynamic Trailing Stop Loss/sid Notes for this Trading System:

Dynamic Trailing Stop Loss/sid Rules for this Trading System:

Dynamic Trailing Stop Loss/sid Begin event logic:

#Enter an order
PlaceBuyMarket('Enter',1000,OnFill => 1);

Dynamic Trailing Stop Loss/sid Bar event logic:

#Dynamic Trailing stop logic.

#Loop round all orders, finding stops, extracting data assocated with them and changing them as needed.
#Rules, we always want to be within 10 pips of the price.

foreach my $order (Orders) {
  if ($order->OrderTypeID == OrderTypeSellStop) {
    #extract user data that is assocated to an order 
    #Although in this example, we dont do anything, just out put it
    my $data = $order->UserData;
    my $pipDiff = (ClosePrice - $order->StopPrice) / PipValue();
    Output "$data $pipDiff";
    if ($data) {
      Output $data->{one};
    }
    #
    if ($pipDiff > 1) {
      Output "Change";
      $order->Change(1000,ClosePrice - (PipValue()*10),0);
    }
  }
}

Dynamic Trailing Stop Loss/sid OrderFill event logic:

Output "In on fill";

#We now place an stop - we also assocate some user data
#to the order. This is useful for assocating ID's and 
#other state information, such as price targets

PlaceOrder (
  Quantity  => 1000, 
  StopPrice => EnterPrice - (PipValue()*10),
  OrderType => OrderTypeSellStop,
  Label     => "Stop",
  UserData  =>  { one => 'a', two => 'b' },
);

0 Comments

Leave a Message