I've been trying to figure out how to reverse a position directly out of a TMF, in order to have immediate execution at the Tick instead of having to wait for the next bar(s).
Here is my TMF:
int TMFLong() { var Thresh = Trail*3; bool Cond_LongStop = price(0)<=priceClose(1); if(TradeResult>Trail) TrailLock = 80;
if(Cond_LongStop and TradeResult>Thresh){ exitLong(); return enterShort(0); } return 0; }
Instead of the combination
exitLong(); return enterShort(0);
I tried directly
reverseShort(0)
but it doesnt seem to open a Long position.
What am I doing wrong?
Last edited by UPL; 12/20/1313:15.
Re: Reverse / Open new trade out of a TMF
[Re: UPL]
#434571 12/21/1308:2812/21/1308:28
For seeing why your script does not work, the whole code would be needed. But the TMF already can not work this way. You're using the global Trail variable without setting it before, and you're sometimes setting TrailLock and sometimes not. Trailing won't work anyway without Stop. All this must be properly set up for entering a trailing trade. The TMF must also return a number, like 0 or another integer code, not a TRADE pointer. And you can't get a continous flow of positions when you're using stops or trailing.
If you want to enter a new position immediately when the old one is stopped out, you must handle stop and trailing only in the TMF. You can't use the global Stop/Trail variables for this because they won't enter a new trade.
Look in the manual about an example and return codes of a TMF:
Thanks for clarifying. That means that Stop and Trail can be also set in the TMF alone - I thought I had the TMF only reacting to the likes of TradeEntryLimit, TradeStopLimit, but not to Stop and Trail, i.e. those need to be set before the TMF is called?
Re: Reverse / Open new trade out of a TMF
[Re: UPL]
#434638 12/23/1307:1712/23/1307:17
Stop and Trail variables must be always set accordingly before you enter a trade that is supposed to stop and trail. It does not matter whether in a TMF or elsewhere.
Of course I'm not sure when and how you want to stop and trail, so I can't give a detailed advice. The best way for getting help is describing your algorithm in words and posting the code that you have so far - then people can see what's wrong. If your algorithm is top secret, you can alternatively subscribe a support ticket on the download page and contact Zorro Support for help.
Re: Reverse / Open new trade out of a TMF
[Re: jcl]
#434656 12/23/1310:2212/23/1310:22
I've simplified the strategy so I can publish the script below. I made a few changes already and seems the TMF works better now. However, the TrailLock function remains a mystery to me - it simply doesnt execute at (in my case) 90% of the max profit of a trade. Do you have any advice for that too?
// ============== TRADER SCRIPT UPL, simplified for bug fixing ==============
int TMFLong() { Stop = priceLow(1)-Spread; Trail = 2.9*ATR(1);
For directly manipulating the stop limit in a TMF, you can use the TradeStopLimit variable. And set the TICKS flag. Simulation then takes longer, but without simulating ticks the TMF would make not much sense.
BTW, when posting code, please use the "Code" tag - it's the hash button on the reply screen. Otherwise it's a little hard to read.
Re: Reverse / Open new trade out of a TMF
[Re: jcl]
#434921 12/30/1310:2212/30/1310:22
Thanks. Clear on the TICKS flag (is set), also clear on the TradeStopLimit variable.
What your saying is that within the TMF, I can NOT change Stop, TradeEntryLimit, Trail, TrailLock while the trade is open?
I have in another version of the script set TrailLock = 70 before entering the trade, and still, while having been on 15 points TradeResult, it falls back to below 70% (=10.5 points) of that without exiting the trade and taking the profit. I'm utterly puzzled
Re: Reverse / Open new trade out of a TMF
[Re: UPL]
#434938 12/30/1315:1412/30/1315:14
You can either trail automatically with the "Trail" and "TrailLock" variables, or by script with a TMF that moves the stop limit. But you can not do both at the same time - that would make no sense.
Re: Reverse / Open new trade out of a TMF
[Re: jcl]
#434968 12/31/1309:2712/31/1309:27
Yes, that's clear. I am only using Trail and TrailLock, no changes of stop limit through the TMF. Still it doesn't close the trade at the designated % of profit. Both in "Test" and "Trade" mode. Please see code and picture.
One can not really "lock" a profit in financial trading. Therefore the variable name "TrailLock" is maybe a little misleading. What it does is sending a close command at market when the price drops below 90% of a previous maximum. Your profit then depends on the current price and the slippage. It will often be less than 90%. It can theoretically even close with a loss.
Using an extremely short time frame, such as 2 minutes, makes this even worse in the simulation, as the price history then contains only a few price ticks inside a frame. Your image above looks perfectly ok for a 2-minutes period. In real trading there are more price ticks per minute, so the profit could be a little better because it takes less time to exit. But it normally won't be 90%. For a more or less realistic simulation you normally use time frames of about 30 minutes or more.