I found a solution...
Lets say that i have $1000 in cash and I want to short a stock that is currently at trading at 90. i want to short at 100 and stop loss at 110 if the trade goes against me (just an example).
I set the following trade:
if this trade goes against me, i should lose $100 and have $900 cash remaining.
BUT when i send these orders to backtrader, I get margin error (order.status == order.Margin) on the stop loss because the engine is thinking that i will need $1100 in order to buy 10 stocks at 110...
I know that until the price will hit the stop loss, i will have more cash from short selling... but the program cant calculate it and returns an error...
What is the right way to solve it?
how can i set stop loss, without getting margin called on me?
Is it possible to plot a subplot indicator y-scale as logarithmic?
At https://www.backtrader.com/docu/induse/#controlling-plotting there is no plotlog variable. I also tried it and there is no such variable for indicators.
anybody was able to solve it without installing older version of mathplotlib? maybe it should be an issue on backtrader.
i have the newest version, and it crashes with the same error ("MemoryError: Unable to allocate 70.8 GiB") only when there was no buy/sell, for example if i don't add any strategy or the strategy did not create a buy/sell order.
OK i found a solution
Observer can access the strategy using self._owner so we can do the following:
in the Observer, we listen to anything inside an self._owner.events array (i just made up the name .events):
def next(self): while len(self._owner.events)>0: event,value = self._owner.events.pop(0) getattr(self.lines,event)=value
and in the Strategy, we just send events:
def __init__(self): self.events= if (CONDITION): self.events.append(["event_x",1])
Note that event_x must be the name of a line defined in the observer.
Thanks, i see that observers can display events like buys/sells. But how do i signal into an observer from the strategy itself? how can i code something like this?
if (condition): myobserver.event1=1 #change value in the observer somehow
is it possible to do so from the strategy?
I don't want to duplicate the entire if-logic to the observer just to display a value.
I will also mention that i do see a specific case that it is possible: in the Observers example i see the OrderObserver that displays information about orders, but it is only because the strategy generates orders and the observer catches them using the next() function. but what about just any events, without generating orders? just generating the event from the strategy?
I developed a strategy, and i would like to visually view specific "events". The logic of determining the events can be found in the strategy. in other words: In the strategy i have all the if statements in order to determine the exact time that i want to see this event on the graph.
Is it possible to add labels on bars, from the strategy?
Alternatively, and this is not so pretty solution but it will work, i think about developing a custom indicator and change it from 0 to 1 for just 1 bar when this event occurs, but i'm not sure how to do it from the strategy itself (i don't want to duplicate the entire logic of the strategy to a new indicator)
How can i do that?
Do you have any code sample for that?