Reverse / Open new trade out of a TMF

Posted By: UPL

Reverse / Open new trade out of a TMF - 12/20/13 12:28

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?


Description: This is what I get - but I want a continous flow of Long and Short orders
Attached picture Capture.PNG
Posted By: jcl

Re: Reverse / Open new trade out of a TMF - 12/21/13 08: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:

http://manual.zorro-trader.com/trade.htm
Posted By: UPL

Re: Reverse / Open new trade out of a TMF - 12/22/13 10:17

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?
Posted By: jcl

Re: Reverse / Open new trade out of a TMF - 12/23/13 07: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.

Posted By: UPL

Re: Reverse / Open new trade out of a TMF - 12/23/13 10: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);

bool Cond_LongStop = priceHigh(0)<=priceHigh(1)<=priceHigh(2);

if(TradeResult>Trail) TrailLock = 90;

if(Cond_LongStop and TradeResult>Trail){
// exitLong();
return 1;
}

return 0;
}

int TMFShort()
{
Stop = priceHigh(1)+Spread;
Trail = 2.9*ATR(1);

bool Cond_ShortStop = priceLow(0)>=priceLow(1)>=priceLow(2);

if(TradeResult>Trail) TrailLock = 90;

if(Cond_ShortStop and TradeResult>Trail){
// exitShort();
return 1;
}

return 0;
}


// ============== DEFINE GLOBAL VARIABLES ==============

static bool initRun = true;
static int startHour = 8; // 8 is standard
static int endHour = 22; // 22 is standard

static string autoTradesCSV = "C:\\PROJECTS AND COMPANIES\\Project Watchdog\\results\\autoTrades.csv";

set(TICKS);

function run()
{

// ============== DEFINE LOCAL VARIABLES FOR EACH ITERATION ==============

char output[400];
var currEMA,valSRoC,stochDVal,deltaD0,CBand;
bool isLookBackPeriod,noConsolidation,withinVolatility, canTrade,closingHour;
double val;

var* Prices = series(price());
val = Prices[0];


//=================TRADING PARAMETERS ==============

BarPeriod = 2;
BarOffset = 0;

StartDate = 20130730;
EndDate = 20130731;

StartWeek = 10600; // start Monday 6 am
EndWeek = 52200; // end Friday 10 pm
Weekend = 3; // log off during the weekend


// ============== DEFINE STATIC VARIABLES ==============

static bool SRoC_Short_Prev = false;
static bool SRoC_Long_Prev = false;


// ============== INDICATORS ==============

// ============== SRoC ==============

currEMA = EMA(Prices,13);
var* EMAVals = series(currEMA);
valSRoC = (EMAVals[0]- EMAVals[21])*100/EMAVals[21];
var* SRoC = series(valSRoC);


// ============== INDICATOR ANALYSIS ==============

// ============== SRoC Analysis ==============

var DELTASROC_THRESH = 0.00;

var deltaSRoC_Curr = SRoC[0]-SRoC[1];

bool SRoC_Cond_Long = deltaSRoC_Curr>DELTASROC_THRESH;
bool SRoC_Cond_Short = deltaSRoC_Curr<-DELTASROC_THRESH;


// ============== SET TRADING HOURS ==============

isLookBackPeriod = (year()*10000+month()*100+day()<StartDate);

canTrade = workday and lhour(CET,0)>=startHour and lhour(CET,0)<endHour and !(isLookBackPeriod);

bool Cond_LongEntry = priceHigh(0)<priceHigh(1);
bool Cond_ShortEntry = priceLow(0)>priceLow(1);


// ============== PLOT LONG SHORT DOTS AND CALCULATE LONG / SHORT POSITIONS ==============

EntryTime = 1;

if(Cond_LongEntry and canTrade and SRoC_Cond_Long and NumOpenLong==0 and NumPendingLong==0){

Entry = priceHigh(0)+Spread;
enterLong(TMFLong);
}

if(Cond_ShortEntry and canTrade and SRoC_Cond_Short and NumOpenShort==0 and NumPendingShort==0){

Entry = priceLow(0)-Spread;
enterShort(TMFShort);
}


// ============== CLOSE ALL POSITIONS AT END OF THE DAY AND OUTPUT P&L ==============

closingHour = lhour(CET,0)==endHour-1 and minute()==60-2*BarPeriod and (!isLookBackPeriod);

if(closingHour){
closingHour = false;

exitLong();
exitShort();

sound("optimize.wav");
}
}C:\\PROJECTS AND COMPANIES\\Project Watchdog\\results\\autoTrades.csv


Description: Here are 2 examples where TrailLock = 90 obviously doesn't execute at 90% of intra-bar profit...
Attached picture Capture.jpg
Posted By: jcl

Re: Reverse / Open new trade out of a TMF - 12/26/13 09:34

You must set Stop, TrailLock, etc. _before_ you enter a trade, not afterwards. Just as in real life.

http://manual.zorro-trader.com/stop.htm

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.
Posted By: UPL

Re: Reverse / Open new trade out of a TMF - 12/30/13 10: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 frown
Posted By: jcl

Re: Reverse / Open new trade out of a TMF - 12/30/13 15: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.
Posted By: UPL

Re: Reverse / Open new trade out of a TMF - 12/31/13 09: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.

[code][/code]
// ============== Uwe_20131230simple.c ==============

static bool initRun = true;
static int startHour = 8; // 8 is standard
static int endHour = 22; // 22 is standard

set(TICKS);

function run()
{
bool isLookBackPeriod,canTrade,closingHour;

BarPeriod = 2;
BarOffset = 0;

StartDate = 20130730;
EndDate = 20130730;

StartWeek = 10600; // start Monday 6 am
EndWeek = 52200; // end Friday 10 pm
Weekend = 3; // log off during the weekend


// ============== SET TRADING HOURS ==============

isLookBackPeriod = (year()*10000+month()*100+day()<StartDate);

canTrade = workday and lhour(CET,0)>=startHour and lhour(CET,0)<endHour and !(isLookBackPeriod);


// ============== TRADING ==============

bool Cond_LongEntry = priceHigh(0)<priceHigh(1) and rising(price);
bool Cond_ShortEntry = priceLow(0)>priceLow(1) and falling(price);

TrailLock = 90;
Stop = 6;
Trail = Stop;

if(Cond_LongEntry and canTrade and NumOpenLong==0 and NumPendingLong==0){

Entry = priceHigh(0)+Spread;
enterLong();
}

if(Cond_ShortEntry and canTrade and NumOpenShort==0 and NumPendingShort==0){

Entry = priceLow(0)-Spread;
enterShort();
}


// ============== CLOSE ALL POSITIONS AT END OF THE DAY ==============

closingHour = lhour(CET,0)==endHour-1 and minute()==60-2*BarPeriod and (!isLookBackPeriod);

Attached picture Capture.PNG
Posted By: jcl

Re: Reverse / Open new trade out of a TMF - 12/31/13 13:14

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.
© 2024 lite-C Forums