Hi everyone,
I am busy working on a simple exit strategy, using a percent trailing. I have previously been working with Tradestation, but then discovered Metatrader and am currently developing an EA.
Tradestation has a function SetPercentTrailing where I can pass 2 values a floor amount and a percent trailing. I am trying to reconstruct this function in EA.
So far I have put together this code:
Code:
// Trailing stop calculation
if(OrdersTotal() > 0)
{
OrderSelect(ticket, SELECT_BY_TICKET);
CurrentProfit = OrderProfit();
if (CurrentProfit >= HighestProfit)
{
HighestProfit = CurrentProfit;
}
if (HighestProfit >= (FloorAmt*Lots))
{
if ((HighestProfit - CurrentProfit) / HighestProfit) * 100 >= TrailingPct)
{
if(OrderType() == OP_BUY)
{
OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Red);
HighestProfit = 0;
}
if(OrderType() == OP_SELL)
{
OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red);
HighestProfit = 0;
}
//Print("Closing order for Trailing amount");
} }
}
How it works, is that if I am in a trade, it continuasly sets a highest profit. Now if my current profit is higher then a floor amount that I set, and my profit drops below a certain percentage of my highest profit, then I close my trade.
Logically I think that this is correct, but unfortunately it does not give me the same results as in Tradestation, and I am wondering if there is an error in my code, or if Tradestation's Trailing Percent works differently.
Any help appreciated...thanks