profit and loss for each candle

tommytx

Member
VIP
Code:
def entry = GetDayOfMonth(GetYYYYMMDD()) > 1 and GetMonth() == 12;
def exit = GetDayOfMonth(GetYYYYMMDD()) > 20 and GetMonth() == 12;
AddOrder(OrderType.BUY_TO_OPEN, entry, open);
AddOrder(OrderType.SELL_TO_CLOSE, exit, close);

the 1 would represent day 1 of dec and i suspect around 20 days out of 30 for the entire month so on the average maybe 20 trading days in the month.

Can anyone offer suggestions as to how i can make this work.. Not knowing the exact number of trading days in each month, i just used day 1 and day 20 as there will normally be approx 20 days per month.. Maybe i can figure out how to calculate the actual days of trading in each month later.. But having most of the days will suffice for now. Depending on what timeframe you choose, i want it to calculate the profit on each candle FOR THE MONTH CHOSEN SUCH AS 12 FOR DECEMBER. If an open is started on the opening of the first candle and a close on the close of the same candle the Report will then show the profit or loss for each candle depending on the date like above is 12 for December.. If working the way i hope to get it working that would go thru each candle in the month of december. The TOS autogenerated Report should then show profit and loss by candle for the entire month of December. Actually i would only be using it for timeframe of one yeasr and the number like 12 above would calculate only the month of Dec or you change the 12 to a 4 it would than calculate day by day profit or loss for the month of April and enter it into the TOS report that it automatically calculates with say like 20 dated entries for each candle. It could have the timeframe set to 1year, 1 day and should make 20 entries for the 1 hour candles for them entire month. Or you could set the time frame to 1 day 4 hours and only get 2 entries per day in the log. Hope all this makes sense.

Bottom line is that i am trying to get the profit and loss for each candle for each day of the month.. Month selected by for example 12 above for
December.
if an open is processed on each opening candle a close is done on each candle that is open.. Should place in the Report the profit and loss of each candle in the month that is being processed.
 
Last edited by a moderator:
Solution
Bottom line is that i am trying to get the profit and loss for each candle for each day of the month.

What you’re trying to do is clear: you want a strategy that opens a trade on each candle in a chosen month and closes it on the same candle, so the Strategy Report shows the profit or loss for every candle. The issue is that Thinkorswim strategies don’t operate in a way that allows that behavior, and that’s why your code isn’t producing the results you expect.

The first limitation is how your entry and exit conditions behave. When you use “day > 1” for December, that becomes true on December 2nd and stays true for the rest of the month. The same thing happens with “day > 20.” Strategies only react when a condition changes...
Bottom line is that i am trying to get the profit and loss for each candle for each day of the month.

What you’re trying to do is clear: you want a strategy that opens a trade on each candle in a chosen month and closes it on the same candle, so the Strategy Report shows the profit or loss for every candle. The issue is that Thinkorswim strategies don’t operate in a way that allows that behavior, and that’s why your code isn’t producing the results you expect.

The first limitation is how your entry and exit conditions behave. When you use “day > 1” for December, that becomes true on December 2nd and stays true for the rest of the month. The same thing happens with “day > 20.” Strategies only react when a condition changes from false to true, so you get one entry on the first candle where the condition becomes true and one exit on the first candle where the exit condition becomes true. After that, the conditions remain true, but the strategy won’t fire again. It doesn’t evaluate each candle as a separate trade opportunity.

The second limitation is that Thinkorswim cannot enter and exit on the same candle. Profit and loss are only calculated after a candle completes. Because of that, the platform can’t evaluate “open at the open of this candle and close at the close of this candle,” since the close doesn’t exist yet at the moment the strategy would need it.

That ties directly into the repainting problem. When you reference the current candle’s open or close, those values are still forming. Strategies must use the previous candle’s data — open[-1], close[-1], and so on — because those are the only finalized values. Using the current candle causes the strategy to change its calculations as the bar forms, which makes the Strategy Report unreliable.

Here’s a quick table that summarizes the core roadblocks:

What you wantWhy TOS won’t do it
One trade per candleStrategies only act on condition changes, not per‑candle loops
Enter and exit on the same candleProfit is only calculated after the candle completes
Use current candle’s open/closeCauses repainting; only historical values are valid
A P/L entry for every candle in a monthStrategy engine can’t reset itself each bar

The bottom line is that your idea is understandable, but the Thinkorswim strategy engine isn’t built to generate a candle‑by‑candle profit log. The way conditions trigger, the requirement for completed candles, and the repainting issue all prevent it from working the way you’re imagining.
 
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
1266 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top