You need to use [IntrabarOrderGeneration = true] at your code. Then "next bar" is "next tick" if the condition is true and there is a tick/trade at the 1021 bar.
That's true, there's no need to add a second data series to a chart for this. With IOG it works fine:
Code: Select all
[IntrabarOrderGeneration = true];
if (LastBarOnChart_s) then begin
Print("Time of last trade: ", Q_Time_s);
end;
Prints:
Code: Select all
Time of last trade: 150901.00
Time of last trade: 150902.00
Time of last trade: 150903.00
Time of last trade: 150903.00
Time of last trade: 150906.00
Time of last trade: 150906.00
Time of last trade: 150907.00
Time of last trade: 150908.00
Time of last trade: 150912.00
Time of last trade: 150912.00
Time of last trade: 150915.00
Time of last trade: 150917.00
Time of last trade: 150921.00
This is on a 30-minute chart by the way, but here we only need to match the time of the last trade with whichever time you want to use in your strategy.
I haven't got a chance to test it yet, but it seems to return my local computer time, right? That'd put a lot of stress on my local time accuracy if you really don't want to hold your positions overnight. That being said, I'd rather rely on the broker's server time rather than my local time.
That may be true, but how much would your computer time differ with your broker time -- perhaps 3 seconds at most? But let's say it's 10 seconds; using `ComputerDateTime` is still the lesser of two evils.
That's because `ComputerDateTime` is updated every second, while the time of the last trade (see code example above) or the closing time of a data series with a lower granularity (as suggested in this thread) updates considerably less often. With CFDs and (midcap and smallcap) stocks it's not uncommon that there's a time gap of 5 minutes of more between trades. So using the current computer time is then a more secure approach than using the last trade (or the time of a bar), I think.
Also: using the time of the last trade has the disadvantage that, during 'slow' market periods, there can be a considerable lag between the current (computer) time and the time of the last trade.
What you could do in your case:
- Use `ComputerDateTime` in your script for the current time
- Optionally: calculate the seconds between the broker time and your computer time to correct for any time difference.
- And use
RecalcLastBarAfter to make sure that the script recalculates every x seconds (to prevent lagging scripts due to slow market conditions).
Code: Select all
if time=1021 then begin
if MP=1 then
sell next bar at market;
if MP=-1 then
buytocover next bar at market;
end;
I think your script will be more robust if you use a time range instead of an exact time (`time=1021`). Because, if there are for whatever reason no ticks between 10:20 and 10:22, then the 10:21 time never happens.
I think this is more robust:
Code: Select all
// Execute in the last 10 minutes prior to 10:30
if (Time >= 1020 and Time < 1030) then begin
// ..
end;