Bitcoin Forum

Economy => Trading Discussion => Topic started by: prof7bit on April 22, 2013, 06:38:38 PM



Title: goxtool bot: stop loss (optionally trailing stop)
Post by: prof7bit on April 22, 2013, 06:38:38 PM
Many have asked for it, here is my first attempt:
https://gist.github.com/prof7bit/5437131 (https://gist.github.com/prof7bit/5437131)
(please test and report bugs, will update the code if needed)

STOP_PRICE is the price at which you want to sell (value is in USD or EUR or any other Fiat), if the value is negative it will enable trailing stop mode and it is interpreted as a price difference (enter for example -12.5 here if you want to trail the stop $12.50 below the highest price (highest since strategy (re)loaded).

STOP_VOLUME is the value in BTC to sell when the stop is triggered. It will trigger only once, after it has been triggered you need to reload strategy ("l" key)

as usual: newest version of goxtool is needed, save as _stoploss.py and start gotool with

./goxtool --strategy=_stoploss.py --protocol=websocket --use-http

Messages will be visible in goxtool.log, use tail and grep to follow them in realtime.

Also the usual disclaimer applies: read and understand the code before running it (otherwise don't run it) and since you have read and understood what it will do in every detail before you decided to run it I cannot be held responsible for anything that results from your decisions.



Title: Re: goxtool bot: stop loss (optionally trailing stop)
Post by: c0inbuster on April 22, 2013, 08:13:26 PM
Thanks for your SL / TS strategy.

But I think it will fail when doing this

BTCUSD price is 100
You set SL to 90 and run goxtool
Price falls to 89 so your strategy will sell 0.01
you reboot your computer (I reboot my Raspberry daily)
Price is still 89
goxtool automatically start (this will be my default settings)
You will sell again 0.01 !!!
I think you should store data in a file !

The problem is with flag self.already_executed
it shouldn't be in your strategy code... but in a file (ini, json, yaml...)

when initializing strategy if already_executed is true, send a debug
message to warm user that SL is disable.

I did something similar with Metatrader 4 (using MQL code)
it was a Stop Loss / Trailing Stop / Take Profit on trader side (stealth SL).
I wasn't using a file to store data but "global variables"
http://docs.mql4.com/globals

see for a Metatrader EA for Stealth SL
http://iticsoftware.com/expert-advisor-virtual-stoploss
(it's quite similar to mine but they are storing data, neither in a file,
neither a "global variable" (in MQL4 term) but on a textbox on chart

and a critism of this concept
http://www.onestepremoved.com/stealth-stop-and-take-profit/



I also have a little comment about "BTC" string...
maybe it could be a good idea to hide this.
gox.cur1 instead will be better as MtGox was supposed to also support LTC.
I will also replace gox.currency by gox.cur2

an other comment about "1E8"
In my mind this kind of constant should be in the strategy...
8 is number of digits of symbol BTCUSD or BTCEUR


moreover according to
https://en.bitcoin.it/wiki/Proper_Money_Handling_(JSON-RPC)
I also wont if  int(STOP_VOLUME * 1e8) shouldn't be replaced by
long(round(STOP_VOLUME * 1e8))


Title: Re: goxtool bot: stop loss (optionally trailing stop)
Post by: dutt on April 24, 2013, 06:15:54 AM
Just what I was looking for, thanks.


Title: Re: goxtool bot: stop loss (optionally trailing stop)
Post by: prof7bit on April 24, 2013, 06:59:53 PM
Update regarding streaming api reliability and new server:

Since the reliability of the servers and the different available protocols changes almost daily I need to update my recommendation for how to start goxtool to have the highest reliability, especially for a stop loss this is important. I'm also going to update it in post #1.

Since yesterday the new websocket server went online at mtgox which turns out to work great (much better than socketio) the most reliable way to run the stoploss bot (or any other strategy) without dangerous disconnects is now the following:
Code:
./goxtool.py --protocol=websocket --use-http --strategy=_stoploss.py
And also as usual keep goxtool up to date, git pull at least once a day.


Title: Re: goxtool bot: stop loss (optionally trailing stop)
Post by: supert on April 26, 2013, 12:07:55 PM
Code:
./goxtool.py --protocol=websocket --use-http

Is this the use_plain_old_websocket=True in the ini file or something else?

Can this be put in the ini file?


Title: Re: goxtool bot: stop loss (optionally trailing stop)
Post by: prof7bit on April 26, 2013, 12:15:33 PM
Code:
./goxtool.py --protocol=websocket --use-http

Is this the use_plain_old_websocket=True in the ini file or something else?

Can this be put in the ini file?

Yes, this is the same option. I admit that the name of this option is slightly confusing, it was added before the command line options were added and at one time there even existed a 3rd protocol that was not selecable via ini file at all, eventually I will clean up this mess and make it more intuitive but that has low priority for now.


Title: Re: goxtool bot: stop loss (optionally trailing stop)
Post by: wilfried on April 27, 2013, 10:00:14 AM
hy, i just started goxtool first time, is there a main thread for goxtool?
btw i use centos 6.4, for whom it interests, this howto works perfectly to get the
minimum required python version 2.7 onto centos without ruining your os:
http://toomuchdata.com/2012/06/25/how-to-install-python-2-7-3-on-centos-6-2/
also you need to pip install pccryptp
Thx for goxtools, already love it :)


Title: Re: goxtool bot: stop loss (optionally trailing stop)
Post by: dwolfman on April 30, 2013, 12:45:57 PM
How long does this need to run before it does anything?  Haven't seen it make a trade after just a few minutes running.


Title: Re: goxtool bot: stop loss (optionally trailing stop)
Post by: dwolfman on April 30, 2013, 01:30:17 PM
Never mind, re-read the source code and figured it out.

No trades actually happen until the price hits the trigger.  For some reason I thought this would create a trade that would wait for the price to get there, like the rebalancer does.


Title: Re: goxtool bot: stop loss (optionally trailing stop)
Post by: prof7bit on April 30, 2013, 04:50:42 PM
For some reason I thought this would create a trade that would wait for the price to get there, like the rebalancer does.

Yes, its just a simple stoploss ;-) And it doesn't even have a user interface, the text editor is basically your user interface to this strategy, you edit the constants at the top of the file, save the file and then in goxtool press "l" to reload. Then you have a sell stop at the specified price, waiting there as long as its running and you can sleep well, knowing it will sell immediately once it reaches the stop price.

I'm personally not using any stop oders at the moment myself but a a lot of people have asked for such a thing so I quickly implemented one. The code also serves as a simple example for using the strategy feature of goxtool. A more advanced version of this stop strategy could be made to allow defining multiple stops in both directions (buy or sell) and also persist its state to disk so it would not accidentally fire twice after a restart.


Title: Re: goxtool bot: stop loss (optionally trailing stop)
Post by: DonBit on May 01, 2013, 08:52:40 PM
Keep it up the good work prof7bit! Donating 0.1 BTC, see goxtool's main thread.

I made some changes to this bot (new changes updated on 05/18/2013).
This fork is now located at https://gist.github.com/dkgeorge/5603198

  • Added start gain
  • Added ability to set or change stop loss/start gain orders during runtime. No need to edit files or reload strategy (very useful)! (press s to open stop loss dialog or press g to open start gain dialog)
  • Added autofilling full wallet amount for stop loss (BTC wallet) and start gain (FIAT wallet). Select 0 for volumes to use full wallet amount
  • For easier start gain, volume setting is in FIAT, not BTC (for start gain only)
  • Automatically selects to stop loss when there is at least 0.01 BTC in wallet (minimum Mt. Gox amount for trading) and only selects start gain when BTC wallet is empty and FIAT is filled with any amount
  • Actively informs in goxtools's debug dialog, stop loss or start gain enablement and order filling status. Great to remind you when the bot is enabled and how was the order filled
  • NEW!  Informs Mt.Gox charged fee for Stop loss/Start gain filled order

Note: This fork removed trailing stop feature from the original code






These are just some features that were in my needs that I wanted to share. This is my first attempt to deal with bots, so please feel free for any comments or suggestion.
Use it wisely and enjoy ;D

-DonBit


Title: Re: goxtool bot: stop loss (optionally trailing stop)
Post by: Aido on May 21, 2013, 10:39:51 PM
Hi,

  • Added autofilling full wallet amount for stop loss (BTC wallet) and start gain (FIAT wallet). Select 0 for volumes to use full wallet amount

I took DonBit's idea and made a small change to prof7bit's original _stoploss.py to sell full BTC balance when stoploss is triggered.

Available here: https://gist.github.com/aido/5623833 (https://gist.github.com/aido/5623833)