Click here to Login

                                                   Trapping C# script errors?


Josh McCormick
2020-09-10 18:14:42


I usually don't get an error on that function. I want to say the ones that I usually find are on chart.SymbolName and on chart.Update function calls.

Two questions:

1. Are there things I should be doing to prevent these kinds of errors from happening?
2. When these errors do happen, is there some way I can trap them so they don't result in a pop-up?

UPDATE: Here's a more common one that I run into: "Method: get_SymbolName" "Object reference not set to an instance of an object."

UPDATE2: The line "Chart[] charts = Charts.GetAllCharts();" just generated a pop-up error box with an OK button (like the others). This one is on Method: GetAllCharts and the error is "Object reference not set to an instance of an object". There was a chart on-screen and I was using another program at the time.

Josh McCormick

2020-09-14 11:21:15


The error means that you are calling an object method while the object is null

If chart.SymbolName returns that error then this means that chart is null
You can do:
if(chart != null)
// Your code

Josh McCormick
2020-09-21 17:37:01


Okay! Now I see where I need to go with this.

For something like Charts.GetAllCharts(), that's a function that I'd expect (hope) would eventually give a correct response (because if it doesn't, things are really screwed and nothing else it can do will matter). I'm going to solve that one with while loop (with a sleep), and retry until it works.

I'm thinking about the rest of my code and how I'd handle something more simple like a chart.SymbolName failing (like if a chart was removed *after* Charts.GetAllCharts or Charts.GetSelectedChart was called, but *before* chart.SymbolName is called). Since chart.SymbolName returns a string, I'm assuming it'll return a blank string when chart isn't null, but instead it points to an object that no longer exists?

If so, that'll really be situational and I'll have to look at how I'm processing those. From there, two more questions come to mind:

1. The pop-up boxes are still going to happen regardless of whether I check to see if these functions have failed or not, right?

2. If I'm caught somewhere mid-script in a nested look and I decide that my best choice is it to stop what I'm doing and to start the script again from scratch, what's going to be my best way of handling this? I'm thinking...

Global.ExecuteScript ("Scripts/scriptname.azf",true,null); <--- "null" will work if there are no variables?

...or is there a better way for a script to restart itself from scratch? I don't like embedding the script's name and path into the script itself, but if that's my only option, I'll take it.

Josh McCormick

2020-09-25 14:25:07


Best Answer
1/ The popup should be displayed only when there is an error

2/ The script cannot restart itself. You can avoid errors by doing proper check. Just like the example, I sent you above, before running a function of Chart, you could check if Chart is null or not

if(chart != null)
// Your code

No more messages


No html code. URLs turn into links automatically.

Type in the trading objects you want to include: - Add Objects
To add a trading object in your message, type in the object name, select it and then click on "Add Objects"


Trading Items
Identify Spilts Screener Script
Moving Average Ratio
Ignore Orders for Recently Sold Securities
Invest Available Cash In a Security
Cash/Equity Rebalance

How-to Lessons
How to run a screen or a watchlist from a script
How to get stocks for a particular index using the global script
How to execute a script at regular intervals
How to calculate the average of a time series using the money man...
How to add a metric in the trading system simulation report

Related Forum Threads
Script Trading Example
Advance Money Management Script debug.
code for roc of certain script
In a Script is there any way to iterate through a List-of-Symbols...
Viewing source or fixing script

Blog Posts
Charting & Scripts - Manage stock charts using the global script
QS Trading Software: Global Script
Export Trading Orders from a Portfolio to a File
Update Chart Layout based on Active Ticker Symbol - Part 2
5 position sizing techniques you can use in your trading system

Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright 2023
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Follow us on Google+
RSS Trading Items

Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.