Trading Concepts Scripts

Posted By: PiptheRipper

Trading Concepts Scripts - 10/09/12 12:07

First of all, I just want say, Zorro is a great program and thank you for all the effort in the development of it.

The idea of this thread is to hopefully help out traders that is new to writing strategy scripts, like myself to successfully code trading concepts and in the process learn new techniques.

The Zorro manual with the tutorials contains lots of great information, but can be hard to understand for people that is new to this.

I hope the more experienced coders can give a hand and help out the new guys.

Peace.
PiptheRipper
Posted By: PiptheRipper

Re: Trading Concepts Scripts - 10/09/12 12:24

I've been writing basic scripts in Zorro for about a week now, but can't manage to do the following:

Creating a var for the previous week High & Low.
I tried using the HH and LL indicators, but I get an error related to them if I test the script. I hope that someone can post an example script in order to return the previous week high and low, whether using the HH&LL indicator or not.

Creating a var for the previous Day High & Low in a smaller than Daily time frame.
When using a BarPeriod = 1440. I can get yesterdays high and low by writing the following:

var daily_high = priceHigh(1);
var daily_low = priceLow(1);

but if I use a smaller BarPeriod, how is it done?

Once again, I tried using the HH & LL indicator but with no luck.
I've managed to get partial success with using the dayHigh and dayLow function, but the Sunday bar causes Monday to return a value of zero.

Any suggestions would be appreciated.

Thank you.
PiptheRipper
Posted By: TankWolf

Re: Trading Concepts Scripts - 10/09/12 14:02

Hi PipRipper,

I dont think I can help with the first one as Im rather still a newbie myself but in regards to your second question I think it might be done like this:

Quote:

BarPeriod = 60; //60 minute bar period.
TimeFrame = 24; // 24 * 60 minute bars.
var *High = series(priceHigh()); //Creates a series of 24 hour highs.
var *Low = series(priceLow()); //Creates a series of 24 hour lows.
var daily_high = High[1]; //Stores the priceHigh() array from 1 day ago in the variable daily_high.
var daily_low = Low[1]; //Stores the priceLow() array from 1 day ago in the variable daily_low.


I havnt tested this and like I say Im only learning but its a suggestion.

Actually thought about your first question again and if my first code works then this should too.

Quote:

BarPeriod = 1440; //1 day bar periods.
TimeFrame = 5; //5 * 1440 bar periods.
var *High = series(priceHigh()); //Creates a series of 5 day highs.
var *Low = series(priceLow()); //Creates a series of 5 day lows.
var weekly_high = High[1]; //Stores the priceHigh() array from 1 week ago in the variable daily_high.
var weekly_low = Low[1]; //Stores the priceLow() array from 1 week ago in the variable daily_low.


Posted By: jcl

Re: Trading Concepts Scripts - 10/09/12 14:51

For the daily high/low on intraday bars, you can use the day() functions for calculating them when the day is over:

Code:
BarPeriod = 60; // one-hour-bars
static var DailyHigh = 0, DailyLow = 0;
if(day(0) != day(1)) { // midnight -> day has changed
  DailyHigh = HH(24);
  DailyLow = LL(24);
}



Note the "static". This keeps the high and low between runs. Otherwise they would be set to 0 again at every run.
Posted By: PiptheRipper

Re: Trading Concepts Scripts - 10/09/12 20:18

Thank you jcl! I appreciate the help and it works just like I wanted it to.
About the weekly high/low. I can't seem to get that to work. I can get monthly high/lows without a problem, by just changing "day" to "month" in you above script, but changing it to "week" returns a zero value.

@TankWolf

Thank you for your contribution. Your code seems to return the daily low and high, but it's delayed by half a day. It doesn't update at midnight. Nway I'll be using the way jcl suggested, but thanx again and plz keep contributing.
Posted By: gfx

Re: Trading Concepts Scripts - 10/09/12 21:50

I'm not set up to test this yet, but how about using

if (dow(0) < dow(1)) { // new week

The tricky bit is figuring out how many bars there are in a week. It varies on some brokers and some weeks, so instead of the HH(n) approach you might want to use something like:

Code:
var WeeklyHigh, WeeklyLow;
var WkHi = -99999, WkLo = 99999;
if (dow(0) < dow(1)) {  // new week
  WeeklyHigh = WkHi;
  WeeklyLow = WkLo;
  WkHi = -99999;
  WkLo = 99999;
}
WkHi = max(WkHi, priceHigh(0));
WkLo = min(WkLo, priceLow(0));

Posted By: TankWolf

Re: Trading Concepts Scripts - 10/10/12 01:00

Thanks jcl for clarifying that. I do have one question however, if we use the day(), HH() & LL() functions then I remember you saying somewhere you will not be able to preform oversampling on the script due to any strategy that uses certain opening and closing hours of the exchange.

So I guess what Im trying to get at when we use strategies that look at intraday high/lows will this method generally yeild better backtesting results than using the priceHigh() & priceLow() functions and then oversampling the script?
Posted By: jcl

Re: Trading Concepts Scripts - 10/10/12 12:33

For intraday high/low values you can use oversampling without problems. For strategies however that depend on the behavior of markets at certain times or dates, oversampling won't work because it mixes up the bars. This is a different trade method and I can't say if it yields better or worse results than intraday trading. It depends on the strategy.
Posted By: PiptheRipper

Re: Trading Concepts Scripts - 10/10/12 13:41

TankWolf

I tried the script, but could not get it to work.

Thank you.
Posted By: TankWolf

Re: Trading Concepts Scripts - 10/12/12 00:30

Quote:
BarPeriod = 60; // one-hour-bars
static var WeeklyHigh = 0, WeeklyLow = 0;
if(day(0) != day(5)) { // midnight of day 5 -> week has ended?
WeeklyHigh = HH(120); //(24 hours *5 days)
WeeklyLow = LL(120); //(24 hours *5 days)
}


Because there is no week() function is this how we would find the weekly high & low?

Also say if we wanted to get the high and low of a bar during the current day say for example you wanted the high and low of the opening candle of London is this how we would do it?
Quote:

BarPeriod = 60; // One hour bars.
static var LondonOpenLow = 0, LondonOpenHigh = 0;
if(hour(1) == timeOffset(UTC,0,7,0)) //Check to see if last hour was LondonOpen.
LondonOpenLow = priceLow(1); //Get low from last bar?
LondonOpenHigh = priceHigh(1); //Get high from last bar?


OR
Quote:

BarPeriod = 60; // One hour bars.
static var LondonOpenLow = 0, LondonOpenHigh = 0;
if(hour(1) == timeOffset(UTC,0,7,0)) //Check to see if last hour was LondonOpen.
LondonOpenLow = LL(1); //Get LL from 1 bar ago?
LondonOpenHigh = HH(1); //Get HH from 1 bar ago?



Posted By: jcl

Re: Trading Concepts Scripts - 10/12/12 06:58

You can find the start and end of a week with the dow() function.

On daily bars, monday: dow(0) == 1, and friday: dow(0) == 5.

On hourly bars, monday morning: dow(0) == 1 and hour(0) == 0

Friday evening: dow(0) == 5 and hour(0) == 22
Posted By: gfx

Re: Trading Concepts Scripts - 10/12/12 13:47

Originally Posted By: TankWolf
Quote:
BarPeriod = 60; // one-hour-bars
static var WeeklyHigh = 0, WeeklyLow = 0;
if(day(0) != day(5)) { // midnight of day 5 -> week has ended?
WeeklyHigh = HH(120); //(24 hours *5 days)
WeeklyLow = LL(120); //(24 hours *5 days)
}
Because there is no week() function is this how we would find the weekly high & low?

TankWolf, did you see the example I posted? I prefer my method because it's more robust.

* By testing for dow(0) < dow(1), it works even if there are missing days at the start of the week. Or if one broker has Sunday-Friday bars and another has Monday-Saturday bars. (That may not be a problem with Zorro but it is with MT4.)

* Recording the "high and low so far" like I did doesn't rely on there being a specific number of bars per day, or per week. I don't know about Zorro but many platforms can have gaps if your data feed dropped out.
Posted By: TankWolf

Re: Trading Concepts Scripts - 12/06/12 10:55

Okies I have a question.

Say Im using a 60 minute bar period and I want to get the daily open and close of a full daily candle how exactly do I achieve this so it mirrors the open and close of the daily bars on FXCM.

Quote:

BarPeriod = 60;
static var DailyOpen = 0, DailyClose = 0;
if(day(0) != day(1)) { // midnight has passed.
DailyOpen = priceOpen(24); //Get open from 24 bars ago?
DailyClose = priceClose(0); //Get close from last bar?
}


Though it doesn't seem to work right, also Sunday is a puzzle to me. Say if I want the first daily candle of the week to be from the Sunday open candle on FXCM to the close of the Monday candle (because technically that is the first full day of the week) how is that done...

Thanks for any feedback.
Posted By: jcl

Re: Trading Concepts Scripts - 12/07/12 11:44

Your code is theoretically correct, although better use priceClose(1); to get yesterday's close price.

The problem is that the number of bars is not always equal to the number of hours. There are no bars at the weekend, on holidays, or when the market is closed. For this reason, better use

DailyOpen = priceOpen(timeOffset(UTC,1,0,0));
DailyClose = priceClose(timeOffset(UTC,1,23,59));

Have a look at the source code of the dayOpen / dayClose functions in the indicators.c file for getting the idea.
Posted By: TankWolf

Re: Trading Concepts Scripts - 12/09/12 10:32

Thanks for the help I can now get it to work perfectly except for one problem, I can not get Fridays open & close to work no matter what I try...

Quote:

static var DailyOpen = 0, DailyClose = 0;
if(dow(0) > 1 && dow(0) <= 5 && day(0) != day(1)) { // midnight has passed.
DailyOpen = priceOpen(timeOffset(UTC,1,0,0));
DailyClose = priceClose(timeOffset(UTC,1,23,59));
printf("\nDayOpen: %.5f",DailyOpen);
printf("\nDayClose: %.5f",DailyClose);
}
else if(dow(0) == 5 && hour(0) == 0) {
DailyOpen = priceOpen(0);
printf("\nDayOpen: %.5f",DailyOpen);
}
else if(dow(0) == 7 && hour(0) == 23 && minute(0) == 59) {
DailyClose = priceClose(0);
printf("\nDayClose: %.5f",DailyClose);
}


If I just use the original if statement then I get everydays open and close price except Friday like I said above. Now I know because Ive used less than or equal to Friday(5) its not going to work but I was using 6 for Sat too and that didnt seem to make any difference. So I wrote the else if statements to try and get the Friday open and Sunday close a different way. In theory that code to me should work but I can not get data for Friday still... Posting the code for others and if you could help with the Friday problem jcl would be appreciated.

Edit: Using <= 7(Sunday) gives you supposed values for Friday but they are incorrect and dont marry up exactly like the rest of the days just an observation I made.
Posted By: TankWolf

Re: Trading Concepts Scripts - 12/09/12 23:18

All good I figured it out heres the code for others:

Quote:

function run() {
set(TESTNOW);
BarPeriod = 60;

static var DailyOpen = 0, DailyClose = 0;
if(dow(0) > 1 && dow(0) <= 5 && day(0) != day(1)) { // midnight has passed.
DailyOpen = priceOpen(timeOffset(UTC,1,0,0));
DailyClose = priceClose(timeOffset(UTC,1,23,59));
printf("\nDayOpen: %.5f",DailyOpen);
printf("\nDayClose: %.5f",DailyClose);
}
else if(dow(0) == 5 && hour(0) == 1) {
DailyOpen = priceOpen(0);
printf("\nDayOpen: %.5f",DailyOpen);
}
else if(dow(0) == 1 && hour(0) == 0) {
DailyClose = priceClose(0);
printf("\nDayClose: %.5f",DailyClose);
}
Posted By: TankWolf

Re: Trading Concepts Scripts - 12/19/12 11:08

I actually made a slight error in the above code didnt realise it till yesterday but I couldnt edit my last post so here is the correct version:

Quote:

function run() {
BarPeriod = 60;

if(dow(0) > 1 && dow(0) <= 5 && day(0) != day(1)) {
SkillLong[0] = priceOpen(timeOffset(UTC,1,0,0)); // Get Price From Yesterdays Open After Midnight For Monday-Thursday.
SkillLong[1] = priceClose(timeOffset(UTC,1,23,59)); // Get Price From Yesterdays Close Before Midnight For Monday-Thursday.
printf("\nDayOpen: %.5f",SkillLong[0]);
printf("\nDayClose: %.5f",SkillLong[1]);
}
else if(dow(0) == 1 && hour(0) == 0 && day(0) != day(1)) {
SkillLong[0] = priceOpen(23); // Get Price From Fridays Open After Midnight For Friday.
SkillLong[1] = priceClose(0); // Get Price For Fridays Close On Midnight Of Sunday.
printf("\nFDayOpen: %.5f",SkillLong[0]);
printf("\nFDayClose: %.5f",SkillLong[1]);
}
}
Posted By: richieru

Re: Trading Concepts Scripts - 01/12/13 11:30

Hi,
Is there a way to call tick volume for an indicator? Cheers.
Posted By: jcl

Re: Trading Concepts Scripts - 01/12/13 12:35

Sure, you can use the tick counter accessible through g->asset->nCounter. It is set to 1 at the first tick of every bar and then counts the ticks in trade mode.

However, the tick volume, unlike the trade volume, is very broker specific and has no predictive value for indicators. That's why we have not put it in an easy-to-use variable.
© 2024 lite-C Forums