Click here to Login





                                                   Trapping C# script errors?

  0

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


SCREENSHOT:

https://i.imgur.com/WgTfrol.png

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."

https://i.imgur.com/NKR5gBF.png

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.

Thanks,
Josh McCormick



QuantShare
2020-09-14 11:21:15

  0

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

  0

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?
return;

...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.

Thanks,
Josh McCormick



QuantShare
2020-09-25 14:25:07

  0

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
0




Reply:

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"










QuantShare

Trading Items
Identify Spilts Screener Script
Options EOD Quotes
Portfolio vs Stock Return
Vary the Start Date of a Trading System Simulation
Dynamic Position Sizing

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 rename a ticker symbol
How to add a metric in the trading system simulation report

Related Forum Threads
Script Trading Example
Money management script help - Targeted net profit per day
Help With Daily Data Import Script
AMM ATS script
Reuse optimization parameters from Strategy script in Money Mgmt

Blog Posts
QS Trading Software: Global Script
Charting & Scripts - Manage stock charts using the global script
How to filter quotes data from high and low price spikes
QuantShare Trading Software: New Features in the 3.4.3 Version
Custom Shortcuts - Move a Chart to Display the Last Quotes/Data









QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright 2020 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
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.