One step at a time; may I please have the code/snippet for (1). a trailing_Stop, and (2). how to stop the trailing at breakeven, thanks.
I tried to search the forum ... surely I cannot be the only one in need of a trailing_stop code/snippet; but the search (results) page returns thread names, not pages and in as far as these threads have page numbers in 20s, 50s, 100s its like searching for a pin in a haystack
I wish we could have a glossary of code/snippets for some of these basic "building block" objects ... anyway, assist me with (1). the trailing_stop code/snippet and (2) how to stop the trailing at breakeven, thanks.
How do I edit it to remove 'Time Filter' and its 'Dollar value Stops and Limits.'
regards.
using System;
using Broker.StrategyLanguage.Function;
namespace Broker.StrategyLanguage.Strategy
{
public class EMA2LineWithTimeFilter : BaseStrategyAdvisor
{
private ISeries<Double> m_price;
private int m_fastlength = 9;
private int m_slowlength = 18;
private XAverage m_average1;
private XAverage m_average2;
private SeriesVar<Double> m_fastavg;
private SeriesVar<Double> m_slowavg;
private int m_OpeningHour = 3;
private int m_ClosingHour = 11;
private double m_Stop_Amount = 80;
private double m_Profit_Target_Amount = 145;
private bool checktime;
private double m_shareorposition = 1;
private IMarketOrder m_Order0;
private IMarketOrder m_Order1;
private IMarketOrder m_Order2;
private IMarketOrder m_Order3;
public EMA2LineWithTimeFilter(object ctx) :
base(ctx) {}
public double shareorposition{
get { return m_shareorposition; }
set { m_shareorposition = value; }
}
private ISeries<Double> price{
get { return m_price; }
}
[Input]
public int fastlength{
get { return m_fastlength; }
set { m_fastlength = value; }
}
[Input]
public int slowlength{
get { return m_slowlength; }
set { m_slowlength = value; }
}
[Input]
public int OpeningHour{
get { return m_OpeningHour; }
set { m_OpeningHour = value; }
}
[Input]
public int ClosingHour{
get { return m_ClosingHour; }
set { m_ClosingHour = value; }
}
[Input]
public double Stop_Amount{
get { return m_Stop_Amount; }
set { m_Stop_Amount = value; }
}
[Input]
public double Profit_Target_Amount{
get { return m_Profit_Target_Amount; }
set { m_Profit_Target_Amount = value; }
}
protected override void Construct(){
m_average1 = new XAverage(this);
m_average2 = new XAverage(this);
m_fastavg = new SeriesVar<Double>(this);
m_slowavg = new SeriesVar<Double>(this);
m_Order0 =
OrdersFactory.CreateMarketNextBar(new OrdersCreateParams(Lots.Default, "EMATFCrossLong", OrderAction.Buy));
m_Order1 =
OrdersFactory.CreateMarketNextBar(new OrdersCreateParams(Lots.Default, "EMATFCrossShort",
OrderAction.SellShort));
m_Order2 =
OrdersFactory.CreateMarketNextBar(new OrdersCreateParams(Lots.Default, "L-C", OrderAction.Sell));
m_Order3 =
OrdersFactory.CreateMarketNextBar(new OrdersCreateParams(Lots.Default, "S-C", OrderAction.BuyToCover));
I've raised this complaint before. See attached snapshot: a signal to short the instrument is given followed by a signal to buy; a trade (long) is entered only to be closed immediately. This happens when two opposing signals are given on adjacent bars; I use this strategy with IOG on, also note that the errant module is trailstop - it is the one that gets filled immediately it is generated in the instance of opposing signals on adjacent bars.
As I said I've raised this complaint before so please examine the code, I just missed money today.
While I am not the creator of the stEA, I don't know exactly what is the flow of the trading logic or the reason behind how the code is written.
Upon a quick look at the code, it appears that the variable pTrailPrice might be the caused of this behavior. You might want to track the change of this variable by using the Output.WriteLine method which you can print out the variable to examine how it is reacting with other conditions.
Let us know if you have further questions.
By the way, if you have difficulties in coding your trading strategies, you might want to use our programming service which our programmers could help you in completing the trading strategies. Please visit FXCM for more information.
The pTrailPrice code/module whose stop gets filled immediately it is generated when opposing signals appear on adjacent bars as captured in the snapshot above was provided by Carl A, see post #92.
Would you please have that member look at the problem; perhaps could be better placed to help.
The pTrailPrice code/module whose stop gets filled immediately it is generated when opposing signals appear on adjacent bars as captured in the snapshot above was provided by Carl A, see post #92.
Would you please have that member look at the problem; perhaps could be better placed to help.
Regards.
Hi Ziklag,
Could you please provide the code and a detail description of the problem?
See post #97 the code is attached. The logic is straight forward; when a bar closes below the MA a short signal/order is generated with a trailing-stop, m_initialstop = 40 (IOG on). All is well until adjacent bars generate signals/orders in which case the last/second order, in the instance of post #97 the buy signal/order, has its trailstop filled immediately it is generated thereby exiting the position prematurely. Actually post #97 captures what happened on Wednesday, that move went on to create about 200pips that I missed.
The problem apparently is, instead of m_initialstop = 40 it appears like in this instance one gets m_initialstop = market, that is what has to be investigated.
Actually I've just discovered that it does not necessarily have to be adjacent bars but any/all instances where the previous position is held to the point where a another/new/opposing signal is generated so that the task of discarding the old and initiating the new results in the new/second/last signal/order getting m_initialstop = market instead of m_initialstop = 40
Thanks jpschan but still, no joy ;-( In the code, here attached, I made the correction you suggested but on testing, as is captured in the snapshot, each of the two successful short trades shown is followed by a buy signal/trade that failed - nipped in the bud by TrailStopSell
The task of discarding the preceding trade with its associated stop/limit orders and initiating the new still results in the new/second/last signal/order getting m_initialstop = market instead of m_initialstop = 40.
I'm sorry I didn't have time to look at the whole code, or the discussion. I just looked at the last few comments.
It seems maybe the " re-Initialize pLastPrice and pTrailPrice inside TrailStop() method" suggestion was not quite right.
The logic of the code resets the prices if MarketPosition == 0. But this doesn't take into account the case where the MarketPosition changes from >0 to <0 in one tick, which it will do in the case that a "buy" was triggered when you were already short (it closes the short and opens the long), and vice versa of course.
What you could try is to keep a variable of the last MarketPosition, and then reset the pLastPrice and pTrailPrice when the state changes.
Something like this...
PHP Code:
// In variable definition section private int lastMarketPosition;
// In Initialize() lastMarketPosition = 0;
// In TrailStop or wherever you put the logic { ...
I do something similar to this in my own strategy and it seems to work fine. Of course, I didn't check the rest of your logic, etc. there could be other issues, but this might be better.
Thank you for posting. robocod is correct. You need to keep track of the change in position from >0 to <0 and vice versa. Thanks robocod for pointing out the problem.
Disclaimer: Trading foreign exchange on margin carries a high level of risk, and may not be suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to trade foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with foreign exchange trading, and seek advice from an independent financial advisor if you have any doubts. Any opinions, news, research, analyses, prices, or other information contained on this website is provided as general market commentary and does not constitute investment advice. Forex Capital Markets LLC. will not accept liability for any loss or damage, including without limitation to, any loss of profit, which may arise directly or indirectly from use of or reliance on such information.