Can you have a look at the attached script and pic please.
I'm trying to set a buy stop at the high of the last candle, but it does not appear to be hit when it should be mid-candle on the next candle, rather it is opened at the open of the subsequent candle.
What am I doing wrong? I have TICKS set in script.
Code:
function run()
{
set (TICKS);
set (PLOTNOW);
set (LOGFILE);
set (TESTNOW);
BarPeriod = 60;
BarOffset = (ET+17)*60;
LookBack = 100;
StartDate = 20110101;
EndDate = 20110120;
Hedge = 2;
vars op = series(priceOpen());
vars cl = series(priceClose());
vars hi = series(priceHigh());
vars lo = series(priceLow());
vars wt = series((hi[0]+lo[0]+cl[0]+cl[0])/4);
Stoch(14,3,MAType_EMA,3,MAType_EMA);
vars sto5K = series(rSlowK);
vars sto5D = series(rSlowD);
vars signal = series(0);
Stop = 40*PIP;
TakeProfit = 20*PIP;
EntryTime = 10;
if ((sto5K[0] < 30) and (sto5K[0] > sto5K[1]))
{
signal[0] = 1;
}
if (NumOpenLong < 1)
{
if ((NumPendingLong < 1) and (signal[0] > 0))
{
enterLong(0,hi[0]);
plotGraph("Entry",0,hi[0]+5*PIP,MAIN|TRIANGLE4,PURPLE);
}
if ((NumPendingLong > 0) and (signal[0] > 0))
{
exitLong();
enterLong(0,hi[0]);
plotGraph("Entry2",0,hi[0]+5*PIP,MAIN|TRIANGLE4,MAGENTA);
}
if (((NumPendingLong > 0) and (signal[0] < 1)) and (hi[0] < hi[1]))
{
exitLong();
enterLong(0,hi[0]);
plotGraph("Entry3",0,hi[0]+5*PIP,MAIN|TRIANGLE4,MAROON);
}
}
if (is(EXITRUN))
{
exitLong();
exitShort();
}
plot("Sto 5,2,2 K",sto5K[0],NEW,RED);
plot("Sto 5,2,2 D",sto5D[0],0,BLUE);
plot("30",30,0,GREY);
plot("70",70,0,GREY);
PlotWidth = 2400;
PlotHeight1 = 800;
}
The log looks fine to me. You're placing and immediately removing a pending trade at every bar. Only the third entry was hit because the price was already over the limit when you placed the trade.
So, I can not see any problem so far, at least not in the log. Your script does indeed not make much sense to me, maybe it does not do what you intended? For looking into details, just print more data to the log, such as the current price.
The computer just does what you tell it to do.
Re: Pending Entries not being hit
[Re: jcl]
#448403 02/03/1513:2702/03/1513:27
Apologies for clumsy example script, I've tidied it up with printfs to try to explain the logic.
With new code there are still buy stops not being hit when they should.
Code:
function run()
{
set (TICKS);
set (PLOTNOW);
set (LOGFILE);
set (TESTNOW);
BarPeriod = 60;
BarOffset = (ET+17)*60;
LookBack = 100;
StartDate = 20110101;
EndDate = 20110120;
Hedge = 2;
vars op = series(priceOpen());
vars cl = series(priceClose());
vars hi = series(priceHigh());
vars lo = series(priceLow());
vars wt = series((hi[0]+lo[0]+cl[0]+cl[0])/4);
Stoch(14,3,MAType_EMA,3,MAType_EMA);
vars sto5K = series(rSlowK);
vars sto5D = series(rSlowD);
vars signal = series(0);
Stop = 40*PIP;
TakeProfit = 20*PIP;
EntryTime = 10;
if ((sto5K[0] < 30) and (sto5K[0] > sto5K[1]))
{
signal[0] = 1;
printf("\nStoch Signal Fired");
}
if (NumOpenLong < 1) // if open trade do not set pending
{
printf("\nNo Trade Open");
// Set new pending trade if signal fires
if (NumPendingLong < 1)
{
printf("\nNo Pending Trade");
if (signal[0] > 0)
{
enterLong(0,hi[0]);
plotGraph("Entry",0,hi[0]+5*PIP,MAIN|TRIANGLE4,PURPLE);
printf("\nNo Pending Trade, Signal = %1.1f, placing new pending at %1.4f",signal[0],hi[0]);
}
}
// If pending not hit check if signal valid
else
{
printf("\nPending Trade not Hit hi[1] = %1.4f; hi[0] = %1.4f",hi[1],hi[0]);
if (signal[0] > 0)
{
exitLong();
enterLong(0,hi[0]);
plotGraph("Entry2",0,hi[0]+5*PIP,MAIN|TRIANGLE4,GREEN);
printf("\nSignal = %1.1f, exiting old trade placing new pending at %1.4f",signal[0],hi[0]);
}
else
{
if (hi[0] < hi[1])
{
exitLong();
enterLong(0,hi[0]);
plotGraph("Entry3",0,hi[0]+5*PIP,MAIN|TRIANGLE4,YELLOW);
printf("\nSignal = %1.1f, exiting old trade placing new pending at %1.4f",signal[0],hi[0]);
}
}
}
}
else printf("\nTrade Open");
if (is(EXITRUN))
{
exitLong();
exitShort();
}
plot("Sto 5,2,2 K",sto5K[0],NEW,RED);
plot("Sto 5,2,2 D",sto5D[0],0,BLUE);
plot("30",30,0,GREY);
plot("70",70,0,GREY);
PlotWidth = 2400;
PlotHeight1 = 800;
}
Log file extract for region in attached pic:
Code:
[105: Mon 10.01.11 00:00] 1.28963
No Trade Open
No Pending Trade
[106: Mon 10.01.11 01:00] 1.28969
Stoch Signal Fired
No Trade Open
No Pending Trade
(EUR/USD::L) Long 1@1.2907 Entry stop
No Pending Trade, Signal = 1.0, placing new pending at 1.2907
(EUR/USD::L) Entry stop 1.2907 hit at 01:00
[EUR/USD::L0701] Long 1@1.2907 Risk 3$ p at 01:00
[107: Mon 10.01.11 02:00] 0p 1/0
Stoch Signal Fired
Trade Open
[EUR/USD::L0701] Target 1@1.2927: +1.35 at 02:18
[108: Mon 10.01.11 03:00] 17p 1/0
Stoch Signal Fired
No Trade Open
No Pending Trade
(EUR/USD::L) Long 1@1.2931 Entry stop
No Pending Trade, Signal = 1.0, placing new pending at 1.2931
[109: Mon 10.01.11 04:00] 17p 1/0
No Trade Open
Pending Trade not Hit hi[1] = 1.2931; hi[0] = 1.2925
(EUR/USD::L) Missed entry 1.2931 at exit command
(EUR/USD::L) Long 1@1.2925 Entry stop
Signal = 0.0, exiting old trade placing new pending at 1.2925
[110: Mon 10.01.11 05:00] 17p 1/0
No Trade Open
Pending Trade not Hit hi[1] = 1.2925; hi[0] = 1.2912
(EUR/USD::L) Missed entry 1.2925 at exit command
(EUR/USD::L) Long 1@1.2912 Entry stop
Signal = 0.0, exiting old trade placing new pending at 1.2912
[111: Mon 10.01.11 06:00] 17p 1/0
No Trade Open
Pending Trade not Hit hi[1] = 1.2912; hi[0] = 1.2911
(EUR/USD::L) Missed entry 1.2912 at exit command
(EUR/USD::L) Long 1@1.2911 Entry stop
Signal = 0.0, exiting old trade placing new pending at 1.2911
[112: Mon 10.01.11 07:00] 17p 1/0
Stoch Signal Fired
No Trade Open
Pending Trade not Hit hi[1] = 1.2911; hi[0] = 1.2912
(EUR/USD::L) Missed entry 1.2911 at exit command
(EUR/USD::L) Long 1@1.2912 Entry stop
Signal = 1.0, exiting old trade placing new pending at 1.2912
[113: Mon 10.01.11 08:00] 17p 1/0
No Trade Open
Pending Trade not Hit hi[1] = 1.2912; hi[0] = 1.2921
[114: Mon 10.01.11 09:00] 17p 1/0
Stoch Signal Fired
No Trade Open
Pending Trade not Hit hi[1] = 1.2921; hi[0] = 1.2926
(EUR/USD::L) Missed entry 1.2912 at exit command
(EUR/USD::L) Long 1@1.2926 Entry stop
Signal = 1.0, exiting old trade placing new pending at 1.2926
[115: Mon 10.01.11 10:00] 17p 1/0
No Trade Open
Pending Trade not Hit hi[1] = 1.2926; hi[0] = 1.2916
(EUR/USD::L) Missed entry 1.2926 at exit command
(EUR/USD::L) Long 1@1.2916 Entry stop
Signal = 0.0, exiting old trade placing new pending at 1.2916
(EUR/USD::L) Entry stop 1.2916 hit at 10:00
[EUR/USD::L1602] Long 1@1.2916 Risk 3$ p at 10:00
[116: Mon 10.01.11 11:00] 17p 1/7
Trade Open
[EUR/USD::L1602] Target 1@1.2936: +1.30 at 11:14
[117: Mon 10.01.11 12:00] 34p 2/0
No Trade Open
No Pending Trade
[214: Fri 14.01.11 13:00] 34p 2/0
No Trade Open
No Pending Trade
[215: Fri 14.01.11 14:00] 34p 2/0
Stoch Signal Fired
No Trade Open
No Pending Trade
(EUR/USD::L) Long 1@1.3386 Entry stop
No Pending Trade, Signal = 1.0, placing new pending at 1.3386
[216: Fri 14.01.11 15:00] 34p 2/0
No Trade Open
Pending Trade not Hit hi[1] = 1.3386; hi[0] = 1.3376
(EUR/USD::L) Missed entry 1.3386 at exit command
(EUR/USD::L) Long 1@1.3376 Entry stop
Signal = 0.0, exiting old trade placing new pending at 1.3376
(EUR/USD::L) Entry stop 1.3376 hit at 15:00
[EUR/USD::L1703] Long 1@1.3376 Risk 3$ p at 15:00
[EUR/USD::L1703] Target 1@1.3396: +1.29 at 15:06
[217: Fri 14.01.11 16:00] 51p 3/0
No Trade Open
No Pending Trade
[218: Fri 14.01.11 17:00] 51p 3/0
No Trade Open
No Pending Trade
[219: Fri 14.01.11 18:00] 51p 3/0
Stoch Signal Fired
No Trade Open
No Pending Trade
(EUR/USD::L) Long 1@1.3361 Entry stop
No Pending Trade, Signal = 1.0, placing new pending at 1.3361
[220: Fri 14.01.11 19:00] 51p 3/0
Stoch Signal Fired
No Trade Open
Pending Trade not Hit hi[1] = 1.3361; hi[0] = 1.3358
(EUR/USD::L) Missed entry 1.3361 at exit command
(EUR/USD::L) Long 1@1.3358 Entry stop
Signal = 1.0, exiting old trade placing new pending at 1.3358
[221: Sun 16.01.11 23:00] 51p 3/0
No Trade Open
Pending Trade not Hit hi[1] = 1.3358; hi[0] = 1.3392
(EUR/USD::L) Entry stop 1.3358 hit at 23:00
[EUR/USD::L2204] Long 1@1.3375 Risk 3$ p at 23:00
[222: Mon 17.01.11 00:00] 51p 5/0
Trade Open
[223: Mon 17.01.11 01:00] 51p 3/2
Trade Open
[224: Mon 17.01.11 02:00] 51p 3/2
Trade Open
[EUR/USD::L2204] Stop 1@1.3335: -3.28 at 02:40
[225: Mon 17.01.11 03:00] 9p 3/1
No Trade Open
No Pending Trade
[226: Mon 17.01.11 04:00] 9p 3/1
No Trade Open
No Pending Trade
[227: Mon 17.01.11 05:00] 9p 3/1
No Trade Open
No Pending Trade
[228: Mon 17.01.11 06:00] 9p 3/1
No Trade Open
No Pending Trade
[229: Mon 17.01.11 07:00] 9p 3/1
No Trade Open
No Pending Trade
[230: Mon 17.01.11 08:00] 9p 3/1
No Trade Open
No Pending Trade
[231: Mon 17.01.11 09:00] 9p 3/1
No Trade Open
No Pending Trade
[232: Mon 17.01.11 10:00] 9p 3/1
Stoch Signal Fired
No Trade Open
No Pending Trade
(EUR/USD::L) Long 1@1.3277 Entry stop
No Pending Trade, Signal = 1.0, placing new pending at 1.3277
[233: Mon 17.01.11 11:00] 9p 3/1
Stoch Signal Fired
No Trade Open
Pending Trade not Hit hi[1] = 1.3277; hi[0] = 1.3297
(EUR/USD::L) Missed entry 1.3277 at exit command
(EUR/USD::L) Long 1@1.3297 Entry stop
Signal = 1.0, exiting old trade placing new pending at 1.3297
[234: Mon 17.01.11 12:00] 9p 3/1
No Trade Open
Pending Trade not Hit hi[1] = 1.3297; hi[0] = 1.3330
(EUR/USD::L) Entry stop 1.3297 hit at 12:00
[EUR/USD::L3505] Long 1@1.3319 Risk 3$ p at 12:00
[EUR/USD::L3505] Target 1@1.3339: +1.42 at 12:24
[235: Mon 17.01.11 13:00] 27p 4/1
No Trade Open
No Pending Trade
[236: Mon 17.01.11 14:00] 27p 4/1
No Trade Open
No Pending Trade
[237: Mon 17.01.11 15:00] 27p 4/1
No Trade Open
No Pending Trade
Ok, the reason is that pending trades are not opened when their stop or profit limit was hit before the entry limit was hit. This happened with your trade in bar #112; I didn't check the further trades. This would be a bug: it makes no sense to check stop or profit limits before the trade is even entered. Can you check if your script works ok when you do not set Stop or TakeProfit?
Update: It was indeed a bug - this will be fixed in the upcoming update. Thanks for finding it!
Re: Pending Entries not being hit
[Re: jcl]
#448433 02/05/1509:4602/05/1509:46