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)
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!
Can you help answer these questions from other members on NexusFi?
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.
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.
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!)...
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 )