darwinex / dwxconnect

Seamlessly link any Trading Strategy in ANY programming language to Darwinex liquidity via MetaTrader 4 or 5. DWX Connect is your very own, fully customizable Trading API!
BSD 3-Clause "New" or "Revised" License
166 stars 87 forks source link

Anyway to have this code run only once? #47

Open stuald opened 2 months ago

stuald commented 2 months ago

Hi guys,

is there a way to adjust the base python code to run only once.. rather than the continual and constant live update? right now I am running the code, and leaving open for 3 minutes before a "close" exit function.

id like to be able to run... and then it just finishes once all the tasks are complete if possible.

thanks Stuart

elvinex commented 2 months ago

Hi Stuart,

If you use the provided example, which has

while processor.dwx.ACTIVE:
    sleep(1)

at the end of the file, you could just set self.dwx.ACTIVE = false at the point you want to exit.

stuald commented 2 months ago

thanks Elvinex, firstly I should say - great piece of code. I'm loving it. thank you ever so much.

I don't use the base code as a live type interaction method. I use it as a run once only. I currently modify your sleep to 180 - so it has time to process everything within my function. (but 180 isn't guaranteed enough of course depending on what is going on) I run my specific buy/sell/modify and math code lines within the get history function. (I've emptied the other functions, so no live ticker pricing etc. all it does it get history and then execute my lines.) Sometimes, it takes a long time to modify a larger number of trades. 180 of course is not guaranteed, hence looking for a way to exit after my code is done, rather than maxing 180 seconds.

So I added sys.exit() after the sleep 180.

instead, should i keep the sleep(1) and... Should I therefore add your line of code(self.dwx.ACTIVE = false) at the end of the get history function? so that it exits once all this modification is finished? or place this after the sleep(1), or place within the CLASS itself after the #get_history function -as the method to exit. I want the full function to still run of course? :)

thanks for your help Stuart

elvinex commented 2 months ago

Lol, if 180s is not enough, it must be a quite a number of tasks.

Normally, I would place the self.dwx.ACTIVE = False after confirmation that all of the tasks have been done. But it depends on what the tasks actually are. For example, if the task was just to send one order, you could wait until on_order_event will trigger with that order event. If you have multiple orders, you could count how many etc.

Or if you don't care about the feedback that everything was actually done, I would put the sleep(180) and exit() after the last command. This way you at least know that the code before has been executed.

stuald commented 2 months ago

thanks elvinex, I will have a play with this and update the thread before closing.

thanks Stuart