For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Only one trade per day, how?



  • Afternoon,

    I am trying to institute in my strategy a rule of only one closed trade per day. I am trying to access the date in the notify_trade(self, trade) function in the next(self) function; but I can't work out how to access the date value I have created.

    Thoughts?



  • I found a workaround, but I'm not sure if it's the best method.

    I create a blank list and then appended the date of the completed trades into the list. I then added it as apart of the if conditions of the next(self) function.


  • administrators

    You probably want to use Docs - Timers and set a timer which will be called only once a day. In notify_timer you can control a sentinel/flag for the purpose.



  • My fix was a horrible idea, it wasted about 12-15hours of coding time trying to work out other problems it caused down the line.

    I've read the timers documentation, the forums, etc and I can't really seem to get my head around how I would add it in.

    I am not sure how I would add it into the Next(Self): component of the code. I am using intraday minute level data. Would somebody be able to help with what exactly I would write so in it would be:

    If not trade.closed.dtclose == self.data[0].datetime.date()? Since I'm using the optimization feature I am not sure how to either access values from the notify_trade I am just not sure how to add timers to achieve this.


  • administrators

    @elliotp said in Only one trade per day, how?:

    Since I'm using the optimization feature I am not sure how to either access values from the notify_trade I am just not sure how to add timers to achieve this.

    I don't know what you have read or think you have understood, but otpimization has nothing to do and doesn't have an influence on how trades or timers are notified.

    But let me take two steps back:

    • You don't even have a working strategy but are trying to optimize

    Get a working strategy and if you really think optimization is going to help you (which in most cases it won't), change addstrategy into optstrategy

    When you get a notify_timer call (which you probably want at the beginning of the session) you reset a flag. When you make a trade (or close it), set the flag, which will indicate that you cannot do anything else until the flag is reset.



  • @backtrader thanks for the quick response.

    I have a strategy and I feel optimization might provide some interesting insights. I really appreciate the help.

    I'm sorry, I'm lost. How would I do any this? I don't know how to access any of the notify_trade values or even notify_timer values during each run of next(self)


  • administrators

    @elliotp said in Only one trade per day, how?:

    I'm sorry, I'm lost. How would I do any this? I don't know how to access any of the notify_trade values or even notify_timer values during each run of next(self)

    It's not about being sorry. This clearly indicates:

    • You need to work on your programming skills and in your Python skills

    If something simple, such as keeping the reference of values in one method to be used in another, is a challenge for you, it is obvious you should take not one but several steps back before committing to algotrading.



  • @backtrader Ah ok, thank you. I will go and work on my python.



  • What an adventure. A lot of time spent and the solution is quite elegant. @backtrader was right of course; notify_timer was the key.

    Send me a PM if you would like the solution. It's rather simple, but ya, learned from reading the documentation more, reading what backtrader said and then some furthering of my python ability.