Close half script - not working

 
I have a script, which is supposed to close half the position, every time is executed or double clicked. But the script not working as per expected. The script only close's half position only sometimes (but should be always), also sometimes it does not always close half position. Can someone please help me refine this script. Script is attached.
 
bm0899:
I have a script, which is supposed to close half the position, every time is executed or double clicked. But the script not working as per expected. The script only close's half position only sometimes (but should be always), also sometimes it does not always close half position. Can someone please help me refine this script. Script is attached.

Try it out now

 

Thanks mladen. The script is still causing the same problem, which is: when you double click the script to run (or drag to a chart) it does not function/work. Only on some instances it will function/work (when dragging to chart of double clicking). Please check, Many thanks :-)

 
bm0899:

Thanks mladen. The script is still causing the same problem, which is: when you double click the script to run (or drag to a chart) it does not function/work. Only on some instances it will function/work (when dragging to chart of double clicking). Please check, Many thanks :-)

As I told already : it works for me. Check the error that you are getting when you try to execute it since on my demo accounts it is working OK
Reason: