1 registered members (AndrewAMD),
1,203
guests, and 9
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Code help: Range of 1st hours trading.
[Re: jcl]
#432855
11/17/13 17:26
11/17/13 17:26
|
Joined: Apr 2013
Posts: 107 UK
Geek
OP
Member
|
OP
Member
Joined: Apr 2013
Posts: 107
UK
|
Better find out the reason of the error 45, and fix it. In your case it's calling dayHigh/Low before the market day is over. You cannot know the high of a day already in the morning. Thus do not call those functions until the EndMarket hour is over.
Thanks for the explanations. I think i have sorted the other problems just still not understanding why the the dayHigh/dayLow is not working? I know you have explained it but my apologise, I am still unable to figure it out, although i know its probably something quite simple.. I thought that if i have set the start and end market for the dayhigh and daylow then when using (UTC,0) then this should work as it reverts to the first hour of trading 8 till 9 am and calculates the high/low of the day(first hour trading)? Could you point out specifically in the code what is incorrect and where am i calling the functions before EndMarket = 900; is over? And possibly if you don't mind the solution this so i can finally "get it". Many Thanks. function run() {
BarPeriod = 15; LookBack = 1000; set(TICKS);
vars Price = series(price()); var DH = dayHigh (UTC,0); var DL = dayLow (UTC,0);
StartMarket = 800; EndMarket = 900;
var Range = DH-DL; var Close = timeOffset(UTC,0,16,30); var Open = timeOffset(UTC,0,8,00); var StartTrade = timeOffset(UTC,0,9,00); var Now = timeOffset(UTC,0,0,00);
if (Now > Close) exitLong();
if (Now > Close) exitShort();
asset("UK100");
Stop = Range;
TakeProfit = Range;
if (Range > 40*PIP) Margin = 0;
if (*Price > DH && NumOpenLong <1 && Now > StartTrade && Now < Close) enterLong();
else if (*Price < DL && NumOpenShort <1 && Now > StartTrade && Now < Close) enterShort();
}
|
|
|
Re: Code help: Range of 1st hours trading.
[Re: jcl]
#432866
11/18/13 10:34
11/18/13 10:34
|
Joined: Apr 2013
Posts: 107 UK
Geek
OP
Member
|
OP
Member
Joined: Apr 2013
Posts: 107
UK
|
Thank you, appreciated. Yes i understand this now, The 0 = current? The Start and End Market give me from 8 till 9am. So, If the current hour is more than 9am the high and low from 8-9am will be Yes or No.. I noticed when the { } have been added it removes the negative price offset error. I've run the script but no trades are entered, just wondering what could be causing this....I think it relates to these lines && NumOpenLong <1 && Now > StartTrade && Now < Close) .. Will try and sort this out now. function run() {
BarPeriod = 15; LookBack = 100; set(TICKS);
vars Price = series(price());
StartMarket = 800; EndMarket = 900;
var DH = 0, DL = 0;
if(lhour(UTC) > EndMarket) { DH = dayHigh (UTC,0); DL = dayLow (UTC,0); } var Range = DH-DL; var Close = timeOffset(UTC,0,16,30); var Open = timeOffset(UTC,0,8,00); var StartTrade = timeOffset(UTC,0,9,00); var Now = timeOffset(UTC,0,0,00);
if (Now > Close) exitLong();
if (Now > Close) exitShort();
asset("UK100");
Stop = Range;
TakeProfit = Range;
if (Range > 40*PIP) Margin = 0;
if (*Price > DH && NumOpenLong <1 && Now > StartTrade && Now < Close) enterLong(1);
else if (*Price < DL && NumOpenShort <1 && Now > StartTrade && Now < Close) enterShort(1);
}
Last edited by Geek; 11/18/13 10:50.
|
|
|
Re: Code help: Range of 1st hours trading.
[Re: jcl]
#432872
11/18/13 11:40
11/18/13 11:40
|
Joined: Apr 2013
Posts: 107 UK
Geek
OP
Member
|
OP
Member
Joined: Apr 2013
Posts: 107
UK
|
Thanks, i have made a few changes and the code seems to work but the results are terrible and much worse than expected. I am not sure if something in my code is wrong ( i think it must be) and is causing such terrible result. i can see many more trades being entered than what i thought. It averages 32 per day when it should be only 1-2 and sometimes 3 per day.. This is my junior coding attempt, i am still very much learning, so apologise if its rubbish, if there is a simpler way or coding the below, or some more mistakes please feel free to point them out. The system is: Find the range of the first hours trading. (8am till 9am) Go long or short after 9am when the price has broken out of the high or low or the first hours trading. Stop and Profit targets are based on the "range" either way. Stop trading at close (4.30pm) if stop or profit target have not been hit Do not trade overnight. If there is a large range > 40 in the first hour its best Not to open a trade for the day and come back the following day. [img=http://s24.postimg.org/hbllwch1t/1st_hour_range_trading_system.jpg]function run() {
BarPeriod = 5; LookBack = 200; set(TICKS);
StartDate = 2013;
vars Price = series(price());
StartMarket = 800; // Daily market opening time (used for calcuating first hours trading range) EndMarket = 900; // Daily market closing time.
var DH = 0, DL = 0;
if(lhour(UTC) > EndMarket/100) { DH = dayHigh (UTC,0); DL = dayLow (UTC,0); } var Range = DH-DL;
var Close = timeOffset(UTC,0,16,30); // 4.30pm Close UTC var Open = timeOffset(UTC,0,8,00); // 8.00am Open UTC var StartTrade = timeOffset(UTC,0,9,00); // 9am UTC var Now = timeOffset(UTC,0,0,00); // Current Time UTC?
if (Range > 40*PIP) // If the first hours trading range is higher than 40 pips do not enter trade. Margin = 0;
if (lhour(UTC) < StartTrade) // If less than 9am, do not enter trade. Margin = 0; if (lhour(UTC) > Close) // if more than 4.30pm do not enter trade. Margin = 0; if (lhour(UTC) > StartTrade) // if more than 9am enter trade. Margin = 50; if (lhour(UTC) < Close) // if less than 4.30pm enter trade. Margin = 50;
if (lhour(UTC) > Close) // Exit Trade at Close (4.30pm) exitLong(); if (lhour(UTC) > Close) // Exit Trade at Close (4.30pm) exitShort();
asset("UK100");
Stop = Range; // Stop Loss based on the first hours trading range.
TakeProfit = Range; // TakeProfit on the first hours trading range.
if (*Price > DH && NumOpenLong <1) // If current price is more than the first hours trading high enter Long enterLong(1);
else if (*Price < DL && NumOpenShort <1) // if current price is less than the first hors trading low enter Short. enterShort(1);
}
|
|
|
Re: Code help: Range of 1st hours trading.
[Re: jcl]
#432874
11/18/13 12:13
11/18/13 12:13
|
Joined: Apr 2013
Posts: 107 UK
Geek
OP
Member
|
OP
Member
Joined: Apr 2013
Posts: 107
UK
|
Thanks again and for your patience One last thing, Is this correct for the current time so i can compare the offset correctly? var Now = timeOffset(UTC,0,0,00); I changed the code, and things just got even worse... I think i will ditch this attempt as you say and start from scratch again..
Last edited by Geek; 11/18/13 12:18.
|
|
|
Re: Code help: Range of 1st hours trading.
[Re: Geek]
#432883
11/18/13 15:00
11/18/13 15:00
|
Joined: May 2013
Posts: 245
swingtraderkk
Member
|
Member
Joined: May 2013
Posts: 245
|
Geek, I've had a little go at this to try to make it simpler for you. I think you might be getting confused by the Day High and Day Low functions, so I'm simply looking at the max & min value of priceHigh & pricelow series over the first hour trading and setting pending orders for those levels. I've commented out a simple One Cancels Other check, I was not sure if it was part of the strategy to take one or both breakouts. I have also commented out some print lines to help you see what is going on. Uncomment them and check your log if you want to see what's going on bar by bar.
function run()
{
set(TICKS+LOGFILE);
BarPeriod = 30;
LookBack = 3;
Hedge = 2;
StartDate = 20100101;
// EndDate = 20100201;
asset("UK100");
int marketstarthour = 8;
int marketstartminute = 0;
int marketendhour = 16;
int marketendminute = 30;
vars hi = series(priceHigh());
vars lo = series(priceLow());
//Find the high low range of first hours trading
vars enthigh = series(MaxVal(hi,2));
vars entlow = series(MinVal(lo,2));
vars Range = series(enthigh[0]-entlow[0]);
// printf("\n%2.0d:%2.0d High %4.1f Low %4.1f enthigh %4.1f entlow %4.1f range %4.1f",hour(),minute(),hi[0],lo[0],enthigh[0],entlow[0],Range[0]);
//
// if (NumPendingTotal == 1) // Take only one trade per day OCO one cancels other Not sure if this was part of strategy or not
// {
// for(open_trades)
// {
// if (TradeIsPending) exitTrade(ThisTrade);
// }
// }
if ((hour() == marketstarthour+1)
and (minute() == marketstartminute))
{
// printf("\nenthigh = %4.1f; entlow = %4.1f; Range = %4.1f; PIP = %4.1f",enthigh[0],entlow[0],Range[0],PIP);
//
if (Range[0] < 40*PIP) // Don’t trade if range is higher than 40 points
{
// printf("\nRange %4.1f < 40 pips, entering pending trades at enthigh %4.1f & entlow %4.1f,price %4.1f",Range[0],enthigh[0],entlow[0],priceClose());
EntryTime = 15; // Expire pending trades at 4:30
enterLong(0,enthigh[0],Range[0],Range[0]);
enterShort(0,entlow[0],Range[0],Range[0]); //Buy on the breakout of the lowest low or the highest high of that first hour
}
}
if (((hour() >= marketendhour)
and (minute() >= marketendminute)) and (NumOpenTotal > 0))
{
// printf("\nExiting Open Trades");
exitLong();
exitShort();
}
}
|
|
|
|