How to close all active trades on the last trading day?
Author: GZTRADE
Creation Date: 1/31/2017 7:53 PM
profile picture

GZTRADE

#1
How to make my code close all active trades on the last trading day on a daily chart for backtesting. I'm too new to this can't figure out how to make it work yet. There were a couple other similar threads but it seemed to me they were about intraday charts which got me really confused.

profile picture

Eugene

#2
CODE:
Please log in to see this code.
profile picture

LenMoz

#3
Maybe this (inside the normal trading loop)...
CODE:
Please log in to see this code.
I used "- 2" thinking GZTRADE wanted all trades closed out at the end of the backtest
profile picture

GZTRADE

#4
Thanks a lot got it working perfectly
profile picture

kelvinyip

#5
Eugene

I did something like that but I still see these position in my account tab (paper account 1). what did I do wrong ? I printed out
PrintDebug(IsLastPositionActive) and they are always false but my paperaccount1 shows I have trades there.


CODE:
Please log in to see this code.
profile picture

Eugene

#6
Have you tried suggestion by LenMoz in post #3?
profile picture

kelvinyip

#7
yeah that's what I did... Let me try again this weekend
profile picture

kelvinyip

#8
I have attached how my paperaccount1 look like... should that post back in #3 work?
profile picture

Eugene

#9
It depends on your (unknown) exit order bar. Note that the market is currently closed.
profile picture

kelvinyip

#10
still showing the position... when I check to see if position is active is showing false all the time.. It just came to me.. does it matter if I entered the trade using 30 mins, 5 mins or daily data ? I can't remember what I did
profile picture

Eugene

#11
Did you actually exit that Position with an order? I think you didn't. Either Auto Trading must be enabled for Paper accounts or you right click on an order and click "Place selected order". Then the Accounts tool would reflect the closing of the position.
profile picture

kelvinyip

#12
oh , good hint..let me check that tomorrow
profile picture

kelvinyip

#13
hmm I don't see orders generated either... what else should I check. activeposition is always false.
profile picture

Eugene

#14
profile picture

kelvinyip

#15
I figured that I can just delete my papertradingaccount and add it back on again!!! problem solved.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).