Pages:
Author

Topic: CryptoTrader.org - First Web-based Algorithmic Trading Platform for Bitcoin - page 8. (Read 37927 times)

member
Activity: 90
Merit: 10
Quote
init: (context)->
    context.pair = 'btc_usd'
    context.buy_treshold = 0.24
    context.sell_treshold = 0.19
    context.buy_price = 0
    context.previous_price = 0

handle: (context, data)->
    instrument = data[context.pair]
    short = instrument.ema(10) # calculate EMA value using ta-lib function
    long = instrument.ema(21)       
    diff = 100 * (short - long) / ((short + long) / 2)
    if diff > context.buy_treshold
        buy instrument # Spend all amount of cash for BTC
        if buy instrument #<< how do I check if  a buy was executed this tick?
            context.buy_price = instrument.price
            debug "Buy Threshold"
            debug "#{context.previous_price} #{context.buy_price} #{instrument.price}"
    else if diff < -context.sell_treshold
        sell instrument # Sell BTC position
        if sell instrument
            debug "Sell Threshold"
            debug "#{context.previous_price} #{context.buy_price} #{instrument.price}"
    else if instrument.price < (0.985*context.previous_price)
        sell instrument
        if sell instrument
            debug "Trailing Stop Loss"
            debug "#{context.previous_price} #{context.buy_price} #{instrument.price}"
    else if instrument.price < (0.985*context.buy_price)
        sell instrument
        if sell instrument
            debug "Stop Loss"
            debug "#{context.previous_price} #{context.buy_price} #{instrument.price}"
    context.previous_price = instrument.price
Debug should tell for each buy or sell how it was actually triggered. but the 'If buy instrument' condition doesn't work.
http://cryptotrader.org/backtests/f2wBfWCXHvha7N9ip
full member
Activity: 220
Merit: 100
Please show your code and maybe we can figure out what is wrong.
member
Activity: 90
Merit: 10
Hmm, that still doesn't seem to work?
full member
Activity: 220
Merit: 100
That is a good question. When a buy or a sell occurs, the methods return an order id. Otherwise, undefined value is returned.

Code:
  if buy instrument
    context.buy_price = instrument.price

Thanks, i'm going to update documentation with it.
member
Activity: 90
Merit: 10
Yes, but how can I make sure the context.buy_price only gets overwritten when a buy actually occurs?
full member
Activity: 220
Merit: 100
Thanks pulsecat, that helps a lot!

One more question:
I am trying to add some kind of trailing stop loss to my strategy, therefore I need to somehow store the buy/sell prices for later reference.

Something like:
Quote
if buy_occurs
  buy_price = instrument.price

And then for the following ticks:
Quote
if instrument.price < (buy_price*0.99)
  sell instrument

How exactly would I do that?

Thanks!

Use the context object to store variables:
Code:
 if buy_occurs
    context.buy_price = instrument.price

And then
Code:
 if context.buy_price and instrument.price < (context.buy_price*0.99)
    sell instrument
member
Activity: 90
Merit: 10
Thanks pulsecat, that helps a lot!

One more question:
I am trying to add some kind of stop loss to my strategy, therefore I need to somehow store the buy/sell prices for later reference.

Something like:
Quote
if buy_occurs
  buy_price = instrument.price

And then for the following ticks:
Quote
if instrument.price < (buy_price*0.99)
  sell instrument

How exactly would I do that?

Thanks!
full member
Activity: 220
Merit: 100
It's true, Coffeescript is very sensitive to formatting.
Here is example of usage ADX indicator

Code:
###
  Example usage of ADX indicator
  The script engine is based on CoffeeScript (http://coffeescript.org)
  Any trading algorithm needs to implement two methods:
    init(context) and handle(context,data)
###  

# Initialization method called before a simulation starts.
# Context object holds script data and will be passed to 'handle' method.
init: (context)->

# This method is called for each tick
handle: (context, data)->
    # data object provides access to the current candle (ex. data['btc_usd'].close)
    instrument = data.btc_usd
    result = talib.ADX
      high: instrument.high
      low: instrument.low
      close: instrument.close
      startIdx: 0
      endIdx: instrument.close.length-1
      optInTimePeriod: 14    
    adx = _.last(result)    
    if adx
        debug "ADX:#{adx} price:#{instrument.price}"    

http://cryptotrader.org/backtests/HQBAMoPXQMtqtskkQ
member
Activity: 90
Merit: 10

Below is the code that calls ADX function
Code:
   instrument = data.btc_usd
    result = talib.ADX
      high: instrument.high
      low: instrument.low
      close: instrument.close
      startIdx: 0
      endIdx: instrument.close.length-1
      optInTimePeriod: 9
    # result is an array of floats
    debug _.last(result) # Prints current value


When I try this I get
"TypeError: Cannot read property 'high' of undefined"

Could you link some working examples of various indicators maybe? Thanks ..
hero member
Activity: 560
Merit: 500
I am the one who knocks
Figured I would post here in case people didn't know, but you can use the Try CoffeeScript tab at http://coffeescript.org/ to get your syntax correct.

Also there is a slight typo in pulsecat's last post, result should not be indented (you can see this at the above website), removing that space and then it compiles.
full member
Activity: 220
Merit: 100

I tried to use the new indicators but even reading the doc in the site it's not much clearer for me

If I just want to access the main value of an indicator with default parameters, what is the code ?

Take ADX as an exemple, is it something like this ?
Code:
instrument = data.btc_usd
indic = instrument.talib.ADX
debug indic.signal
(I know it doesn't work, but I would like to know how far I am)  Grin

To call TA-Lib function directly, you need to determine the data passed to and from the function.
The function index at http://cryptotrader.org/talib provides information on the arguments and return values.
For example, your algorithm needs to use ADX indicator:
ADX - Average Directional Movement Index

talib.ADX(params)

Input parameters:

- high - array of floats
- low - array of floats
- close - array of floats
- startIdx - start index for input data
- endIdx - end index for input data
- optInTimePeriod

Returns:
 - array of floats

Below is the code that calls ADX function
Code:
   instrument = data.btc_usd
    result = talib.ADX
      high: instrument.high
      low: instrument.low
      close: instrument.close
      startIdx: 0
      endIdx: instrument.close.length-1
      optInTimePeriod: 9
    # result is an array of floats
    debug _.last(result) # Prints current value

full member
Activity: 196
Merit: 100
VERY Cool..  Will try this out..
sr. member
Activity: 378
Merit: 250
Since several people requested for 2H bars i'm going to add such option soon. As regards custom intervals, it shouldn't be hard to implement this, since the backend stores all history data from Mtgox.

great  Cheesy

Feature update
- added support for Underscore library (http://underscorejs.org/) - cool utility-belt library for JavaScript that provides support for the usual functional suspects (each, map, reduce, filter...)
- added bindings to Ta-lib library (http://ta-lib.org) - technical analysis library that includes 125+ indicators.

For details check the documentation at the site.

I tried to use the new indicators but even reading the doc in the site it's not much clearer for me

If I just want to access the main value of an indicator with default parameters, what is the code ?

Take ADX as an exemple, is it something like this ?
Code:
instrument = data.btc_usd
indic = instrument.talib.ADX
debug indic.signal
(I know it doesn't work, but I would like to know how far I am)  Grin
full member
Activity: 220
Merit: 100
Feature update
- added support for Underscore library (http://underscorejs.org/) - cool utility-belt library for JavaScript that provides support for the usual functional suspects (each, map, reduce, filter...)
- added bindings to Ta-lib library (http://ta-lib.org) - technical analysis library that includes 125+ indicators. Full functions list http://cryptotrader.org/talib

For details check the documentation at the site.
full member
Activity: 220
Merit: 100
thanks !  Smiley

Nothing happens when I try to register, is it normal ?

also cant seem to register....using safari or firefox, both fail

The user related functionality is disabled until basic scripting functionality is ready. Sorry Smiley

Is it planned to add other time intervals ? like 2h or 6h
(or let a possibility to custom it)

Sometimes there is a huge difference between 2 consecutive intervals and I would be curious to test the middle Smiley

Since several people requested for 2H bars i'm going to add such option soon. As regards custom intervals, it shouldn't be hard to implement this, since the backend stores all history data from Mtgox.



sr. member
Activity: 378
Merit: 250
Is it planned to add other time intervals ? like 2h or 6h
(or let a possibility to custom it)

Sometimes there is a huge difference between 2 consecutive intervals and I would be curious to test the middle Smiley
sr. member
Activity: 350
Merit: 250
at last.....ive been trying to automate this stuff for months.....
thank you thank you thank you

also cant seem to register....using safari or firefox, both fail
sr. member
Activity: 378
Merit: 250
thanks !  Smiley

Nothing happens when I try to register, is it normal ?
full member
Activity: 220
Merit: 100
great tool, thanks Smiley


but I don't understand how to make my own scripts

In your help, there is an exemple
debug data.btc_usd.high[data.btc_usd.length-1] # Displays current High value

If I try it and copy-paste the line in the source code, all I have is
null
null
null
...

What can I change to have the correct values in the backtest field ?

This seems to be a documentation issue. The valid code for accessing current High price is:
Code:
   debug data.btc_usd.high[data.btc_usd.high.length-1]

Thanks for reporting this.


Thank you !
How do you get the high price of the 5 last hours for example (if we are in hourly mode) ? Is there a function or do you have to make a loop ?


Array data can be accessed very easily:
Code:
 debug data.btc_usd.high.slice(-5)

To loop over array:
Code:
 prices = data.btc_usd.high.slice(-5)
  for val in prices
    debug val

If you're interested in writing your own scripts at Cryptotrader, i would recommend you to examine Coffeescript Documentation at first. It is a language with very expressive and clean syntax.
sr. member
Activity: 378
Merit: 250
great tool, thanks Smiley


but I don't understand how to make my own scripts

In your help, there is an exemple
debug data.btc_usd.high[data.btc_usd.length-1] # Displays current High value

If I try it and copy-paste the line in the source code, all I have is
null
null
null
...

What can I change to have the correct values in the backtest field ?

This seems to be a documentation issue. The valid code for accessing current High price is:
Code:
   debug data.btc_usd.high[data.btc_usd.high.length-1]

Thanks for reporting this.


Thank you !
How do you get the high price of the 5 last hours for example (if we are in hourly mode) ? Is there a function or do you have to make a loop ?
Pages:
Jump to: