NexusFi: Find Your Edge


Home Menu

 





Help with coding specific entry/exit dates


Discussion in EasyLanguage Programming

Updated
    1. trending_up 5,599 views
    2. thumb_up 5 thanks given
    3. group 1 followers
    1. forum 10 posts
    2. attach_file 1 attachments




 
Search this Thread

Help with coding specific entry/exit dates

  #1 (permalink)
lexiegurl
Wilmington, NC
 
Posts: 7 since Jan 2022
Thanks Given: 4
Thanks Received: 1

Hi everyone!

I was hoping someone can help me with this code as I can't seem to get it right and get mixed results. I am using TradeStation and Portfolio Maestro to try to do some backtesting. I have specific dates I want to enter and exit (ex. enter at open on 1/4/22, exit at close on 1/6/22). I only have these specific dates for a strategy I have worked on in Excel that uses fundamental data. I don't necessarily want to try to code that yet (I'm completely new to using EasyLanguage thus I thought entering and exiting on specific dates would be a good starting point.

With that said, here's the code I have as an example:

inputs:

LECondition ( Date = 1170103) [DisplayName = "LECondition"];

if (LECondition) Then
Buy ("Entry0") next bar on open;

if (date = 1170116) Then
Sell from entry ("Entry0") this bar on close;

if (date = 1170202) Then
Sellshort ("Entry1") next bar on open;

if (date = 1170502) then
Buytocover from Entry ("Entry1") this bar on Close;

if (date = 1170502) Then
Buy ("Entry2") next bar on Open;

if (date = 1170802) then
Sell from Entry ("Entry2") this bar on Close;

if (date = 1170802) Then
Buy ("Entry3") Next bar on Open;

if (date = 1171031) then
Sell from Entry ("Entry3") this bar on Close;

if (date = 1171031) Then
Buy ("Entry4") next bar on Open;

if (date = 1180201) then
Sell from Entry ("Entry4") this bar on Close;

if (date = 1180201) Then
Buy ("Entry5") next bar on Open;

if (date = 1180501) then
Sell from Entry ("Entry5") this bar on Close;

if (date = 1180501) Then
Buy ("Entry6") Next bar on Open;

if (date = 1180801) then
Sell from Entry ("Entry6") this bar on Close;

if (date = 1180801) Then
Buy ("Entry7") next bar on Open;

if (date = 1181030) then
Sell from Entry ("Entry7") this bar on Close;

if (date = 1181030) Then
Buy ("Entry8") next bar on Open;

if (date = 1190201) then
Sell from Entry ("Entry8") this bar on Close;

if (date = 1190201) Then
Sellshort ("Entry9") next bar on Open;

if (date = 1190430) then
Buytocover from Entry ("Entry9") this bar on Close;


I backtest this on a daily chart and it is triggering everything correctly except the first 4 orders/ dates. What am I doing wrong for Entry0 and Entry1 that those orders aren't triggering?

I built the same code above with a date =YYYMMDD and time = HHMM if statement (replacing the date = YYMMDD) and ran that one on a 1 minute chart backtest and all the orders trigger. The problem is when I try to run Portfolio Maestro on my 24 stocks, it just crashes with the 1 min chart/backtest, thus I was trying to change it to the daily bars/backtest.

Any help is very much appreciated!

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Increase in trading performance by 75%
The Elite Circle
Trade idea based off three indicators.
Traders Hideout
Better Renko Gaps
The Elite Circle
How to apply profiles
Traders Hideout
REcommedations for programming help
Sierra Chart
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
34 thanks
Tao te Trade: way of the WLD
24 thanks
My NQ Trading Journal
14 thanks
HumbleTraders next chapter
11 thanks
GFIs1 1 DAX trade per day journal
11 thanks
  #2 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,662 since Jul 2012
Thanks Given: 1,892
Thanks Received: 7,351


lexiegurl View Post
Hi everyone!

I was hoping someone can help me with this code as I can't seem to get it right and get mixed results. I am using TradeStation and Portfolio Maestro to try to do some backtesting. I have specific dates I want to enter and exit (ex. enter at open on 1/4/22, exit at close on 1/6/22). I only have these specific dates for a strategy I have worked on in Excel that uses fundamental data. I don't necessarily want to try to code that yet (I'm completely new to using EasyLanguage thus I thought entering and exiting on specific dates would be a good starting point.

With that said, here's the code I have as an example:

inputs:

LECondition ( Date = 1170103) [DisplayName = "LECondition"];

if (LECondition) Then
Buy ("Entry0") next bar on open;

if (date = 1170116) Then
Sell from entry ("Entry0") this bar on close;

if (date = 1170202) Then
Sellshort ("Entry1") next bar on open;

if (date = 1170502) then
Buytocover from Entry ("Entry1") this bar on Close;

if (date = 1170502) Then
Buy ("Entry2") next bar on Open;

if (date = 1170802) then
Sell from Entry ("Entry2") this bar on Close;

if (date = 1170802) Then
Buy ("Entry3") Next bar on Open;

if (date = 1171031) then
Sell from Entry ("Entry3") this bar on Close;

if (date = 1171031) Then
Buy ("Entry4") next bar on Open;

if (date = 1180201) then
Sell from Entry ("Entry4") this bar on Close;

if (date = 1180201) Then
Buy ("Entry5") next bar on Open;

if (date = 1180501) then
Sell from Entry ("Entry5") this bar on Close;

if (date = 1180501) Then
Buy ("Entry6") Next bar on Open;

if (date = 1180801) then
Sell from Entry ("Entry6") this bar on Close;

if (date = 1180801) Then
Buy ("Entry7") next bar on Open;

if (date = 1181030) then
Sell from Entry ("Entry7") this bar on Close;

if (date = 1181030) Then
Buy ("Entry8") next bar on Open;

if (date = 1190201) then
Sell from Entry ("Entry8") this bar on Close;

if (date = 1190201) Then
Sellshort ("Entry9") next bar on Open;

if (date = 1190430) then
Buytocover from Entry ("Entry9") this bar on Close;


I backtest this on a daily chart and it is triggering everything correctly except the first 4 orders/ dates. What am I doing wrong for Entry0 and Entry1 that those orders aren't triggering?

I built the same code above with a date =YYYMMDD and time = HHMM if statement (replacing the date = YYMMDD) and ran that one on a 1 minute chart backtest and all the orders trigger. The problem is when I try to run Portfolio Maestro on my 24 stocks, it just crashes with the 1 min chart/backtest, thus I was trying to change it to the daily bars/backtest.

Any help is very much appreciated!


Might be a MaxBarsBack issue. Try having the chart start date 50 bars before 1170103 - something like a chart start date of 1161001 should work.

Follow me on Twitter Reply With Quote
Thanked by:
  #3 (permalink)
lexiegurl
Wilmington, NC
 
Posts: 7 since Jan 2022
Thanks Given: 4
Thanks Received: 1


Just out of curiosity how would that be coded in there? Like this? I tried this and it opened the order, but then the orders after didn't trigger and it just shows an open order on 1170104.

inputs:

LECondition ( Date = 1170103 and maxbarsback = 50) [DisplayName = "LECondition"];

if (LECondition) Then
Begin;

If (date = 1170103) then
Buy ("Entry0") next bar on open;

if (date = 1170116) Then
Sell from entry ("Entry0") this bar on close;

if (date = 1170202) Then
Sellshort ("Entry1") next bar on open;

if (date = 1170502) then
Buytocover from Entry ("Entry1") this bar on Close;


I was able to get it to work without the maxbarsback although I am still interested in learning how to properly do it as I don't have much confidence it won't mess up again. For TradeStation charts, I had to do a daily chart starting a year before the first order and in Portfolio Maestro I had to do the backtest with the backtest start date being 2 years before the first order. That seems to work for right now but not sure if that it the final solution.

Reply With Quote
  #4 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,662 since Jul 2012
Thanks Given: 1,892
Thanks Received: 7,351


lexiegurl View Post
Just out of curiosity how would that be coded in there? Like this? I tried this and it opened the order, but then the orders after didn't trigger and it just shows an open order on 1170104.

inputs:

LECondition ( Date = 1170103 and maxbarsback = 50) [DisplayName = "LECondition"];

if (LECondition) Then
Begin;

If (date = 1170103) then
Buy ("Entry0") next bar on open;

if (date = 1170116) Then
Sell from entry ("Entry0") this bar on close;

if (date = 1170202) Then
Sellshort ("Entry1") next bar on open;

if (date = 1170502) then
Buytocover from Entry ("Entry1") this bar on Close;


I was able to get it to work without the maxbarsback although I am still interested in learning how to properly do it as I don't have much confidence it won't mess up again. For TradeStation charts, I had to do a daily chart starting a year before the first order and in Portfolio Maestro I had to do the backtest with the backtest start date being 2 years before the first order. That seems to work for right now but not sure if that it the final solution.

Max Bars Back cannot be programmed in a strategy, so that LECondition is unnecessary.

Here is how to change that:


Follow me on Twitter Reply With Quote
Thanked by:
  #5 (permalink)
lexiegurl
Wilmington, NC
 
Posts: 7 since Jan 2022
Thanks Given: 4
Thanks Received: 1

Ah ok! I thought that was just for look-inside-bar back-testing. I apparently did have that set to 50 bars back like it looks in your screenshot.

That makes me wonder now if it was trying to look 50 bars back when the first bar was less than 50 bars back from the first date (ex. initially before adding the January date I had the first entry on 1170203, but the chart started on 1170107, so less than the 50 bars back). That would explain why the May 2017 entry was triggered since there were more than 50 trading days/bars before that entry date.

Reply With Quote
  #6 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,662 since Jul 2012
Thanks Given: 1,892
Thanks Received: 7,351


lexiegurl View Post
Ah ok! I thought that was just for look-inside-bar back-testing. I apparently did have that set to 50 bars back like it looks in your screenshot.

That makes me wonder now if it was trying to look 50 bars back when the first bar was less than 50 bars back from the first date (ex. initially before adding the January date I had the first entry on 1170203, but the chart started on 1170107, so less than the 50 bars back). That would explain why the May 2017 entry was triggered since there were more than 50 trading days/bars before that entry date.

Yes, when it is 50, the strategy will not place any trades the first 50 bars. In your case, the value could be as small as 1 or 2, since the strategy never needs to look further back in history than that.

Follow me on Twitter Reply With Quote
Thanked by:
  #7 (permalink)
lexiegurl
Wilmington, NC
 
Posts: 7 since Jan 2022
Thanks Given: 4
Thanks Received: 1

Thank you so much for your help! I set all of my lookback bars to 1 and everything has worked the way I wanted it to (well, except for the fact I also didn't realize Portfolio Maestro uses close of the previous bar to calculate how many contracts to buy, not the open price at entry).

I did have one more question on coding exits that I'm not sure if I should add here or start another post. I'll put it here first though just in case it should be. If I wanted to split my exits, what code would I use for that?

Using Portfolio Maestro, my entry # of shares are based on 8% equity at time of entry (using 2:1 margin). Everything I am seeing for splitting exits is to do specify the number of contracts, but I don't know how many contracts there are technically on each entry when coding this entire strategy. Is there any way to code something to the effect of:

"if (date = 1170502 and time = 1600) then sell from entry ("Entry 1") totalshares*0.5 this bar at close"?

Reply With Quote
  #8 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,662 since Jul 2012
Thanks Given: 1,892
Thanks Received: 7,351


lexiegurl View Post
Thank you so much for your help! I set all of my lookback bars to 1 and everything has worked the way I wanted it to (well, except for the fact I also didn't realize Portfolio Maestro uses close of the previous bar to calculate how many contracts to buy, not the open price at entry).

I did have one more question on coding exits that I'm not sure if I should add here or start another post. I'll put it here first though just in case it should be. If I wanted to split my exits, what code would I use for that?

Using Portfolio Maestro, my entry # of shares are based on 8% equity at time of entry (using 2:1 margin). Everything I am seeing for splitting exits is to do specify the number of contracts, but I don't know how many contracts there are technically on each entry when coding this entire strategy. Is there any way to code something to the effect of:

"if (date = 1170502 and time = 1600) then sell from entry ("Entry 1") totalshares*0.5 this bar at close"?


That seems like it should work, I would test it to make sure.

Also, realize that orders with "this bar at close" will work in backtest, but will NOT work in real trading (the order gets sent after the market closes!)...

Follow me on Twitter Reply With Quote
  #9 (permalink)
lexiegurl
Wilmington, NC
 
Posts: 7 since Jan 2022
Thanks Given: 4
Thanks Received: 1

It doesn't work. When I try to verify this code:


if (date = 1170426 and time = 1600) then
Sell from Entry ("Entry1") total shares*0.5 this bar on Close;

it highlights the word total and says "arithmetic (numeric) expression expected here". I also tried shares*0.5 and it just sold 1 share instead of half the position. "totalshares" together was an unknown identifier.

Also, thanks for that heads up! That's a good point I didn't realize but I'm hoping it wouldn't affect my results too drastically. I originally was aiming for like a 9:31am entry and 3:58pm exit (of course in real life, those times may not always perfectly pan out), but when I tried to run a backtest with 24 stocks on the 1 minute charts in Portfolio Maestro it just kept crashing so I gave up. I am using the 30 minute chart to have a 10am entry with a 4pm exit to get a (hopefully!) rough idea of how the strategy can do. With positions being held for weeks, I figure the moves that happen from 9:30-10 and 3:55-4 probably wouldn't drastically affect my strategy in the long run (outlier days like the huge drops last year excluded in that of course )

Reply With Quote
  #10 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,662 since Jul 2012
Thanks Given: 1,892
Thanks Received: 7,351



lexiegurl View Post
It doesn't work. When I try to verify this code:


if (date = 1170426 and time = 1600) then
Sell from Entry ("Entry1") total shares*0.5 this bar on Close;

it highlights the word total and says "arithmetic (numeric) expression expected here". I also tried shares*0.5 and it just sold 1 share instead of half the position. "totalshares" together was an unknown identifier.

Also, thanks for that heads up! That's a good point I didn't realize but I'm hoping it wouldn't affect my results too drastically. I originally was aiming for like a 9:31am entry and 3:58pm exit (of course in real life, those times may not always perfectly pan out), but when I tried to run a backtest with 24 stocks on the 1 minute charts in Portfolio Maestro it just kept crashing so I gave up. I am using the 30 minute chart to have a 10am entry with a 4pm exit to get a (hopefully!) rough idea of how the strategy can do. With positions being held for weeks, I figure the moves that happen from 9:30-10 and 3:55-4 probably wouldn't drastically affect my strategy in the long run (outlier days like the huge drops last year excluded in that of course )

Sorry, try "currentcontracts" instead of total shares

Follow me on Twitter Reply With Quote
Thanked by:




Last Updated on January 13, 2022


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts