Code optimization

 

Hi, I have a function which closes open long positions when my virtual stop losses are hit. I am about to extend this to short positions as well. The function is about 500 lines long. I was already about to copy paste the function and change respective parts to fit the closing of open short positions. But then I thought instead of duplicatio everything I can add more if clauses to check whether it is open long or open short. This means that the function gets more complicated and longer instead of having to shorter functions. On the other hand both functions together would be longer and include a lot of duplicated stuff as if I have only one function and add some more if statements. I am asking myself what's the better way of doing it in terms of coding style and optimaziton (runtime) etc. Or is this more or less just personal prefernce?


thank you!

 
eatrader1231231231231233r5235134:

Hi, I have a function which closes open long positions when my virtual stop losses are hit. I am about to extend this to short positions as well. The function is about 500 lines long. I was already about to copy paste the function and change respective parts to fit the closing of open short positions. But then I thought instead of duplicatio everything I can add more if clauses to check whether it is open long or open short. This means that the function gets more complicated and longer instead of having to shorter functions. On the other hand both functions together would be longer and include a lot of duplicated stuff as if I have only one function and add some more if statements. I am asking myself what's the better way of doing it in terms of coding style and optimaziton (runtime) etc. Or is this more or less just personal prefernce?


thank you!

You should have (at least) 3 functions. Long, Short and Common and you can call the common either from Long or Short. 

 
eatrader1231231231231233r5235134:

Hi, I have a function which closes open long positions when my virtual stop losses are hit. I am about to extend this to short positions as well. The function is about 500 lines long. I was already about to copy paste the function and change respective parts to fit the closing of open short positions. But then I thought instead of duplicatio everything I can add more if clauses to check whether it is open long or open short. This means that the function gets more complicated and longer instead of having to shorter functions. On the other hand both functions together would be longer and include a lot of duplicated stuff as if I have only one function and add some more if statements. I am asking myself what's the better way of doing it in terms of coding style and optimaziton (runtime) etc. Or is this more or less just personal prefernce?


thank you!

there would maybe be more optimism to help you if you provided your code , my suggestion would be to redo it so that it functions properly and when your code is completed and functioning post it back here so that we can see what you did and what you have to work with here are lots of talented coders here ,but they will not take likely to helping especially if you have not even posted any code to work from but like I said complete it first then post it here and they will most probably help you otherwise they will refer you to the Freelance section .

 
eatrader1231231231231233r5235134:

Hi, I have a function which closes open long positions when my virtual stop losses are hit. I am about to extend this to short positions as well. The function is about 500 lines long. I was already about to copy paste the function and change respective parts to fit the closing of open short positions. But then I thought instead of duplicatio everything I can add more if clauses to check whether it is open long or open short. This means that the function gets more complicated and longer instead of having to shorter functions. On the other hand both functions together would be longer and include a lot of duplicated stuff as if I have only one function and add some more if statements. I am asking myself what's the better way of doing it in terms of coding style and optimaziton (runtime) etc. Or is this more or less just personal prefernce?


thank you!

500 lines of code is a lot for what you are doing....

have a single loop that checks the positions both long and short, and then call a long and short close function respectively.

 
Thank you all for your suggestions. It looks like it is recommended to use two separate functions which I did. I am also trying to create a function for common stuff. Thanks!
Reason: