Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Friday, January 24, 2020

Straight up market finally pauses; Traderbot is in testing with a Python IB library that works! Minding the gap

Friday's market down move was blamed on the coronavirus now troubling China ...
But: who knows? The market has been going mostly up for almost 11 years, so any change would not be a surprise ...

However, with the Dynamic Covered Strangle, it doesn't make any difference what the market does (with one exception, discussed below).

Once again, we sell a futures put and call, not too far away from the money (.30 delta) and then set our traderbot to watch the two short strikes and buy or sell the underlying future as needed. Then we relax and let the options expire down to near their external value, close the trade and do it again, forever.



The main risk we face is a gap in the futures price, when an external event (especially during the shutdown in trading Friday to Sunday) happens, like this:


I don't have the data to detect the frequency of this sort of thing, but it seems to be rare enough to be not a risk to the overall strategy, especially if (as I plan) I'll be diversified over several different futures underlyings. Oil is vulnerable to this particular shock; soybeans more vulnerable to this one:


These shouldn't happen at the same time ... and my strategy for managing such a thing would be like this:


  • Move the "virtual short strike" to wherever we got the underlying futures price filled at, and run the traderbot as normal, trading back and forth around this value.
  • Wait for most time value of the option to expire ... if the underlying value is still way up beyond the gap, we have to take whatever loss is the value between the original short strike and the new "virtual one." But we also have a chance for the underlying price to snap back, as happened recently during one of the Iranian attacks on a Saudi oil facility; the price spiked over the weekend but came right back to the mid-50's where it started ... no harm done if managed as described above.
Keep calm and carry on, in this situation as in much of life!

Finally a piece of excellent news .. I am testing the traderbot with a Python library that works with Interactive Brokers! 

I should have this ready to deploy by next week ...

Watch this space!


Sunday, February 25, 2018

No trade this week; looking for the next one(s)

I am working with QuantGo data to find my next trade ideas. I made progress on importing the data I'm evaluating into a database using this toolset:



Python comes with a terrific feature in its CSV module: it can "sniff" the format of the CSV file in question and automagically figure out how to parse it:

with open('example.csv', 'rb') as csvfile:
    dialect = csv.Sniffer().sniff(csvfile.read(1024))
    csvfile.seek(0)
    reader = csv.reader(csvfile, dialect)
    # ... process CSV file contents here ..

I found SQLAlchemy to be well documented and a pleasure to use ...

Next more database design for auxiliary tables I will need to store the underlying values and the delta of the options, etc.

Also I found on a StackExchange site for quants a post on backtesting so I am trying Orats, but I'm not sure it can do what I want to do ...

More next week ...

Wednesday, January 18, 2017

The Algorithm: current status

I started out trying to program against a standard technical indicator, the MACD crossover:


For day trading I tried using different settings than the standard (9,12,26); this Python library I found, qtpylib, had (3,10,16) programmed in by default and found that was recommended by a trading guru as good for intraday trading so tried that.

I couldn't get Python to match what the charts (on ThinkOrSwim) were doing at the same time I was reading this book:


The author was going on about TradeStation and its backtesting features. At the same time I found that Zaner supported TradeStation to send signals into its platform, Zaner360. So I opened a TradeStation account as well.

So far my testing with MACD shows that it puts out too many trade signals. I haven't necessarily given up on it yet but I tried another indicator, the RSI (relative strength index) that seemed to work better:

I'm still working on this as the test shows a lovely profit, but with too much 'drawdown' in the middle of the test. If you start with $100,000 and end with $380,000 that's great ... but not if you have a trough in the middle where you're down $104,000.

There are ways around this and I'm still working on it ... more details when I have them.