Dark Theme
Light Theme
Trading Articles
Article Categories
Article Tools
Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to
register in order to view the content of the threads and start contributing to our community.
It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Help - Code to trade between specified time.
Updated November 25, 2019
Help - Code to trade between specified time.
November 19th, 2019, 09:15 AM
Scotland
Posts: 27 since May 2019
Thanks Given: 11
Thanks Received: 4
I'm trying to set my strategy to trade between 1705 and 1505.
When i put the following code in the backtest results show 0 - the strategy is not running.
if time >= 1705 and time <= 1505 then
begin;
without this line of code the strategy runs as intended. So I'm guessing I am putting it wrong or missing something else?
Help much appreciated, thanks!
Can you help answer these questions from other members on NexusFi?
Best Threads (Most Thanked) in the last 7 days on NexusFi
November 19th, 2019, 09:31 AM
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629
Edwin Lefevre,
welcome to futures.IO. The time can't be later than 1700 and at the same time be earlier than 1500, that's why your condition will never be evaluated to true. Using the logical OR instead of AND will do what you have in mind as the time can be later than 1700 or before 1505. When the time is between 1505 and 1700 neither of the two checks would evaluate to true and the condition would be false.
Regards,
ABCTG
November 19th, 2019, 09:52 AM
Scotland
Posts: 27 since May 2019
Thanks Given: 11
Thanks Received: 4
Very simple answer - I would have kept struggling for hours. Just started coding and although easier than I thought, I still make simple mistakes.
Thanks!
November 19th, 2019, 10:12 AM
Agoura Hills, CA USA
Experience: Advanced
Platform: Tradestation&Multicharts
Broker: TradeStation
Trading: Futures & Equities
Posts: 128 since Apr 2019
Thanks Given: 9
Thanks Received: 102
Edwin Lefevre
Very simple answer - I would have kept struggling for hours. Just started coding and although easier than I thought, I still make simple mistakes.
Thanks!
Edwin you could also tag your first bar and then count bars from it. You would do
If time = 1700 then firstbar = barnumber;
If barnumber < firstbar + (your counter) then begin
Sent using the NexusFi mobile app
November 23rd, 2019, 12:58 PM
Scotland
Posts: 27 since May 2019
Thanks Given: 11
Thanks Received: 4
Hi guys I have a follow up question from this. I have been struglinng with getting my strategy to stop early on specified (holidays) days.
I have the following so far:
if date <= 1191127 and time <= 1455 or time >= 1705 then
if date = 1191128 and time <= 1145 or time >= 1705 then
if date = 1191129 and time <= 1145 or time >= 1705 then
if date >= 1191130 and time <= 1455 or time >= 1705 then
begin;
{entry orders}
....
{Holiday close orders}
If MarketPosition >0 and date = 1191128 and time = 1150 then
sell("Cover Long") next bar at open;
If MarketPosition <0 and date = 1191128 and time = 1150 then
buytocover ("Cover Short") next bar at open;
{Holiday close order}
If MarketPosition >0 and date = 1191129 and time = 1150 then
sell("Cover Long") next bar at open;
If MarketPosition <0 and date = 1191129 and time = 1150 then
buytocover ("Cover Short") next bar at open;
**edit**
Think I potentially solved my problem.
Var :
closing_time (0);
if date = 1191128 then
closing_time = 1155 else
closing_time = 1455;
if date = 1191129 then
closing_time = 1155 else
closing_time = 1455;
if time <= closing_time or time >= 1705 then
begin;
November 25th, 2019, 04:10 AM
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629
Edwin Lefevre,
when you are combining AND and OR in a condition it can make sense to use brackets to ensure that the code is evaluated in the order that you want.
Code
if date <= 1191127 and ( time <= 1455 or time >= 1705 ) then
Regards,
ABCTG
Edwin Lefevre
Hi guys I have a follow up question from this. I have been struglinng with getting my strategy to stop early on specified (holidays) days.
I have the following so far:
if date <= 1191127 and time <= 1455 or time >= 1705 then
if date = 1191128 and time <= 1145 or time >= 1705 then
if date = 1191129 and time <= 1145 or time >= 1705 then
if date >= 1191130 and time <= 1455 or time >= 1705 then
begin;
{entry orders}
....
{Holiday close orders}
If MarketPosition >0 and date = 1191128 and time = 1150 then
sell("
Cover Long") next bar at open;
If MarketPosition <0 and date = 1191128 and time = 1150 then
buytocover ("Cover Short") next bar at open;
{Holiday close order}
If MarketPosition >0 and date = 1191129 and time = 1150 then
sell("Cover Long") next bar at open;
If MarketPosition <0 and date = 1191129 and time = 1150 then
buytocover ("Cover Short") next bar at open;
**edit**
Think I potentially solved my problem.
Var :
closing_time (0);
if date = 1191128 then
closing_time = 1155 else
closing_time = 1455;
if date = 1191129 then
closing_time = 1155 else
closing_time = 1455;
if time <= closing_time or time >= 1705 then
begin;
November 25th, 2019, 05:52 AM
Scotland
Posts: 27 since May 2019
Thanks Given: 11
Thanks Received: 4
Hey Abctg, thanks for the tip.
Can I ask if my code makes sense to you? Using an if, then, else, statement to change a variable.
closing_time (0);
if date = 1191128 then
closing_time = 1155 else
closing_time = 1455;
November 25th, 2019, 06:07 AM
Posts: 2,436 since Apr 2013
Thanks Given: 482
Thanks Received: 1,629
Edwin Lefevre,
using different values for variables according to the outcome of other checks (like dates in your case) is perfectly fine.
Regards,
ABCTG
Edwin Lefevre
Hey Abctg, thanks for the tip.
Can I ask if my code makes sense to you? Using an if, then, else, statement to change a variable.
closing_time (0);
if date = 1191128 then
closing_time = 1155 else
closing_time = 1455;
Last Updated on November 25, 2019