Git Product home page Git Product logo

gemini's Introduction

                            Python Build Status Dependencies GitHub Issues Contributions welcome License

Install

pip3 install git+git://github.com/anfederico/gemini.git

https://github.com/anfederico/gemini/issues

Load

from gemini_core import data, engine, helpers

Examples

Input Data (Optional)

If you have your own data that has/hasn't been processed, you should conform to the following structure. Basically, load your data into a Pandas dataframe object and be sure to convert the dates to datetime format and include the following lowercase column titles.

                                  date         high          low         open        close
                0  2017-07-08 11:00:00  2480.186778  2468.319314  2477.279567  2471.314030  
                1  2017-07-08 11:30:00  2471.314030  2455.014057  2471.202796  2458.073602
                2  2017-07-08 12:00:00  2480.000000  2456.000000  2458.073602  2480.000000 
                3  2017-07-08 12:30:00  2489.004639  2476.334333  2479.402768  2481.481258
                4  2017-07-08 13:00:00  2499.000000  2476.621873  2481.458643  2491.990000 
                5  2017-07-08 13:30:00  2503.503479  2490.314610  2492.440289  2496.005562
                6  2017-07-08 14:00:00  2525.000000  2491.062741  2494.449524  2520.775500
                7  2017-07-08 14:30:00  2521.500036  2510.000000  2520.775500  2518.450645
                8  2017-07-08 15:00:00  2519.817394  2506.054360  2518.451000  2514.484009
 4195.81 ┤                                                                                         
 4161.76 ┤                                   ╭─╮                                                   
 4127.72 ┤                                   │ ╰╮                                                  
 4093.67 ┤                                   │  │                                                  
 4059.62 ┤                ╭╮                 │  ╰╮                                                 
 4025.58 ┤              ╭─╯╰╮╭╮              │   ╰─╮                                               
 3991.53 ┤             ╭╯   ╰╯│        ╭╮╭╮  │     │                                               
 3957.48 ┤             │      ╰╮      ╭╯╰╯│╭╮│     │                                               
 3923.44 ┤             │       │      │   ╰╯╰╯     │                                               
 3889.39 ┤             │       │   ╭╮╭╯            │                     ╭───╮                     
 3855.34 ┤             │       │  ╭╯││             │                     │   ╰─╮                   
 3821.30 ┤       ╭╮    │       ╰──╯ ╰╯             │     ╭╮   ╭╮        ╭╯     ╰╮  ╭╮ ╭╮          ╭
 3787.25 ┤       ││    │                           │╭╮  ╭╯╰╮ ╭╯│        │       │  │╰─╯╰─╮        │
 3753.21 ┤     ╭╮│╰─╮  │                           ││╰╮╭╯  ╰╮│ │╭──╮    │       │ ╭╯     ╰─╮      │
 3719.16 ┤     │╰╯  │╭─╯                           ╰╯ ╰╯    ╰╯ ╰╯  │    │       ╰─╯        │  ╭─╮╭╯
 3685.11 ┤    ╭╯    ╰╯                                             │    │                  ╰──╯ ╰╯ 
 3651.07 ┤    │                                                    │   ╭╯                          
 3617.02 ┤    │                                                    ╰╮╭─╯                           
 3582.97 ┤    │                                                     ╰╯                             
 3548.93 ┤╮  ╭╯                                                                                    
 3514.88 ┼╰╮╭╯                                                                                     
 3480.83 ┤ ╰╯                                                                                      

Data Retrieval

If you don't have your own data, we've included useful functions for grabbing low and high timeframe historical data from crypto exchanges. These helper functions will automatically resample your datasets to any desired timeframe and return a Gemini-compatible dataframe.

# Higher timeframes (>= daily)
df = data.get_htf_candles("BTC_USD", "Bitfinex", "3-DAY", "2019-01-12 00:00:00", "2019-02-01 00:00:00")

# Lower timeframes (< daily)
df = data.get_ltf_candles("USDC_BTC", "30-MIN", "2019-01-12 00:00:00", "2019-02-01 00:00:00")

Loading Data into the Backtester

backtest = engine.backtest(df)

Creating your Strategy

In addition to loading the data, you must define the strategy you want to test. To do this, we'll create a logic function that can be passed to the backtester when you start. The backtester will proceed step-wise through the dataset, copying the current/past datapoints into a variable called "Lookback" to prevent lookahead bias. If the data hasn't already been processed, you may process it within the logic function (this makes the simulation more accurate but significantly increases runtime). You can then use the helper class called "Period" to conveniently reference current and past datapoints. With those, you may execute long, sell, short, and cover positions directly on the "Account" class based on your strategy.

def logic(account, lookback):
    try:
        # Process dataframe to collect signals
        lookback = helpers.get_signals(lookback)
        
        # Load into period class to simplify indexing
        lookback = helpers.period(lookback)
        
        today = lookback.loc(0) # Current candle
        yesterday = lookback.loc(-1) # Previous candle
        
        if today['signal'] == "down":
            if yesterday['signal'] == "down":
                exit_price = today['close']
                for position in acount.positions:  
                    if position.type == 'long':
                        account.close_position(position, 0.5, exit_price)

        if today['signal'] == "up":
            if yesterday['signal'] == "up":
                risk          = 0.03
                entry_price   = today['close']
                entry_capital = account.buying_power*risk
                if entry_capital >= 0:
                    account.enter_position('long', entry_capital, entry_price)
     
    except ValueError: 
        pass # Handles lookback errors in beginning of dataset

# Start backtesting custom logic with 1000 (BTC) intital capital
backtest.start(1000, logic)

Analyzing your Strategy

After the backtest, you can analyze your strategy by printing the results to console. As of now, these include simple statistics of your run but we plan to implement more complicated metrics for a stronger understanding of performance.

backtest.results()
Buy and Hold : -3.03%
Net Profit   : -30.26
Strategy     : 40.0%
Net Profit   : 400.01
Longs        : 156
Sells        : 137
Shorts       : 0
Covers       : 0
--------------------
Total Trades : 293

Visualizing the Equity Curve

You can visualize the performance of your strategy by comparing the equity curve with a buy and hold baseline. The equity curve simply tracks your account value throughout the backtest and will optionally show where your algorithm made its trades including longs, sells, shorts, and covers.

backtest.chart()

gemini's People

Contributors

anfederico avatar danleepy avatar liamhartley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gemini's Issues

getSignals function missing

Hi,
thanks for the code. I am missing an example of the getSignals function.
As it stand, the current code is not working.

AttributeError: 'module' object has no attribute 'getSignals'

Thanks for your help.

Pivots and helpers.getSignals()

  1. Is pivots a python package you are using through Pip?
  2. The Readme has helpers.getSignals() from helper function while there is no such function in helper.py file.

No lines inside chart.html

User Info

Google Chrome: 87.0.4280.88 (Official Build) (x86_64)
Revision | 89e2380a3e36c3464b5dd1302349b1382549290d-refs/branch-heads/4280@{#1761}
OS | macOS Version 10.15.7 (Build 19H2)
JavaScript | V8 8.7.220.29
Flash | 32.0.0.465 /Users/douglasrudolph/Library/Application Support/Google/Chrome/PepperFlash/32.0.0.465/PepperFlashPlayer.plugin

Description

No lines were rendered inside chart.html after running backtest.chart() on your example backtest algorithm and BTCUSD data

Here's a screenshot of the chart:
image

This was the guide I was following:
https://gemini-docs.readthedocs.io/en/latest/using-gemini.html#basic

This was the data I used:
https://github.com/anfederico/gemini/blob/master/examples/data/BTC_USD.csv

This is the HTML that was generated into chart.html:
https://gist.github.com/11/159820cdf0a7b8dd69bd928add991acd

Cheers 🍻

PS: Thanks for the amazing API -- loving everything about this project

No module named 'gemini_core'

Tried on:
Mac M1 w/ Big Sur and Windows 10, both gave the same error.

Installed via:
pip3 install git+git://github.com/anfederico/gemini.git

Running from gemini_core import data, engine, helpers results in:

from gemini_core import data, engine, helpers
ModuleNotFoundError: No module named 'gemini_core'

When I run pip3 list I can see:
gemini 1.0.6

If I try to import from just gemini I get the error:
cannot import name 'data' from 'gemini' (/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/gemini/__init__.py)

Edit:
I fixed this by looking through the commits by importing like this instead:
from gemini.gemini_core import data, engine, helpers

Maybe the readme is incorrect? Also I see the last commit updated the example, and just fyi the link to that on the readme is a 404. Excited to use Gemini, thanks!

Potential update to loop through different variables for strategy?

First off this project has been very helpful in me finally getting into backtesting different strategies. I am pretty new to writing my own code and am having issues figuring out how to backtest through a list of different variables and also compile the results into some kind of heatmap. For example if the strategy is based on some kind of moving average, I want to be able to have a list of different lead-lag combinations, and have the script cycle through all different combination, generating results for each and also showing a heatmap with the final return. If anyone has an idea of how to go about doing that, it would be much appreciated!

Keep getting error when I ran the reversion example


AttributeError Traceback (most recent call last)
in
----> 1 output = backtest.start(100000, logic)

~/opt/anaconda3/lib/python3.8/site-packages/gemini/gemini_core/engine.py in start(self, initial_capital, logic)
41 """
42 self.tracker = []
---> 43 self.account = exchange.account(initial_capital)
44
45 # Enter backtest ---------------------------------------------

AttributeError: module 'gemini_core.exchange' has no attribute 'account'

Why Logic is function?

Why Logic is function, not class?
What is you need to set init options, like self.invested (true/false), self.sma_frame, self.ticker, etc?

How do I actually get this running?

Hey,

Cloned your Gemini git repo, tried running example.py but getting errors, like name gemini is not defined, so I put it in the same folder as gemini, imported gemini, now helpers has no attribute "getSignals".

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.