Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/25/24 10:20
Trading Journey
by howardR. 04/24/24 20:04
M1 Oversampling
by Petra. 04/24/24 10:34
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
folder management functions
by VoroneTZ. 04/17/24 06:52
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
5 registered members (AndrewAMD, TipmyPip, VoroneTZ, Quad, 1 invisible), 688 guests, and 11 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Reverse / Open new trade out of a TMF #434524
12/20/13 12:28
12/20/13 12:28
Joined: Dec 2013
Posts: 5
Switzerland
U
UPL Offline OP
Newbie
UPL  Offline OP
Newbie
U

Joined: Dec 2013
Posts: 5
Switzerland
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?

Attached Files
Capture.PNG (273 downloads)
This is what I get - but I want a continous flow of Long and Short orders
Last edited by UPL; 12/20/13 13:15.
Re: Reverse / Open new trade out of a TMF [Re: UPL] #434571
12/21/13 08:28
12/21/13 08:28
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
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

Re: Reverse / Open new trade out of a TMF [Re: jcl] #434600
12/22/13 10:17
12/22/13 10:17
Joined: Dec 2013
Posts: 5
Switzerland
U
UPL Offline OP
Newbie
UPL  Offline OP
Newbie
U

Joined: Dec 2013
Posts: 5
Switzerland
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/13 07:17
12/23/13 07:17
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
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/13 10:22
12/23/13 10:22
Joined: Dec 2013
Posts: 5
Switzerland
U
UPL Offline OP
Newbie
UPL  Offline OP
Newbie
U

Joined: Dec 2013
Posts: 5
Switzerland
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

Attached Files
Capture.jpg (9 downloads)
Here are 2 examples where TrailLock = 90 obviously doesn't execute at 90% of intra-bar profit...
Last edited by UPL; 12/23/13 10:30.
Re: Reverse / Open new trade out of a TMF [Re: UPL] #434764
12/26/13 09:34
12/26/13 09:34
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
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.

Re: Reverse / Open new trade out of a TMF [Re: jcl] #434921
12/30/13 10:22
12/30/13 10:22
Joined: Dec 2013
Posts: 5
Switzerland
U
UPL Offline OP
Newbie
UPL  Offline OP
Newbie
U

Joined: Dec 2013
Posts: 5
Switzerland
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

Re: Reverse / Open new trade out of a TMF [Re: UPL] #434938
12/30/13 15:14
12/30/13 15:14
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
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/13 09:27
12/31/13 09:27
Joined: Dec 2013
Posts: 5
Switzerland
U
UPL Offline OP
Newbie
UPL  Offline OP
Newbie
U

Joined: Dec 2013
Posts: 5
Switzerland
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 Files
Capture.PNG (123 downloads)
Re: Reverse / Open new trade out of a TMF [Re: UPL] #434977
12/31/13 13:14
12/31/13 13:14
Joined: Jul 2000
Posts: 27,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
Frankfurt
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.


Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1