mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
Feature: GoCryptoTrader Backtester (#622)
* Backtester: event handler completed, basic back tester support is working * Backtester: support for ticker data added, general code clean up, start of risk & size manageR * Backtester: WIP * Me: I am going to write tests and comment as I go this time, also me: doesn't write any tests or comments as i go * Backtester: work on orderbook system to track orders, increased test coverage * Backtester: further test coverage, output json, start of js chart output * Backtester: test coverage, output strat name * Backtester: WIP * WIP backtest charts * WIP on template * Backtester: further test coverage added * Backtester: WIP * backtester: attempting easier to read template for backtesting output * comments, and tests * Backtester: end of day WIP started work on risk management for handling leveraged positions * Backtester: WIP * Backtester: started heavy documentation phase for handover * Backtester: started heavy documentation phase for handover * Backtester: further comments, also work on making chart solution modular to allow for usage outside backtester (e.g OHCLV data) * Backtester: CHART LIBRARY * Backtester: move backtester over to new chart library * Backtester: removed old chart templates, template updates * Chart: add advancedintervaldata, convert from stats -> chart * Chart: gctscript hookup to generate chart from OHLCV data * Chart: reworked template to load from generated data if no template path is set * chart: template wip * correclty generate backtester readme, readme generation for charts, chart data generation * Removed old read file methods * Removed chart library from backtest as its now standalone * Remove reference to unfinish TA code. Removes value calculation from order signal. It belongs with portfolio signal * regen * Re-jiggles everything around to not have import cycle issues and makes it look like a normal application * End of day commit creates a new function to setup a backtester from a settings struct. Doesn't work though lol * Builds up more backtest work to allow to be run from the command line * Regen RPC * End of day mind mine field of RSI calculation5 * Finishes basic main.go application * Minor updates while theorising * Rearranging things like the size and data types. Adds portfolio setup like a normal human. Allows positions to be decimal based since this is for CRYPTOCURRENCIES :o * Moves code around to related positions. Adds compatibility to ordermanager to handle order submission. Fails to do things * End of day commit. Adding config based loading for indiviual strats. Attempting to allow for multiple cps per strategy as well as loading fees * End of day commit. Expanding config definition and loading implementation. Attempting to setup backtester wide multi currency support in a strategy. * Moves risk, attempts to revert multi currency, but also supports more in depth multi currency for later...... in the portfolio * End of day commit for realsies. Updates the strat and sets the invalid backtester * No more panics. Finishes config loading. Renames buyandhold to dollarcostaverage * Extends strategy to include a reason why its performing an action. Adds 420blazeit.strat. Expands statistics output. Moves folders around some more. Reduces amount of processing when "DO NOTHING" is the direction * Commit before home time. Looks to expand the order manager to cater to the backtester. Fleshes out risk manager to think about leverage and holdings in other currencies * Some basic expanding of strategy definitions. Changes weird package naming. * Expands size and risk validations. Expands config settings for the validation. Starts looking at loading from live data source * Merge branch 'master' into backscratcher * Work towards having backtester load data * Adds support and tests for all data source loading except for LIVE * Some basic additions looking to append to data streams instead of load all at once, for the purpose of live data analysis * End of day commit where I broke functions * Adds live backtesting * Adds FANCY MATHS to correctly size orders before slippage. Rearranges minmaxing in config and strats * Prints out initial settings. Creates a lame slippage calculator. Ensures that order price/amounts respect OHLCV data. Adds customisable config variables that can influence a strategy * Fixes minor issues with rendering. Fixes portfolio buying and selling now * ALL OVER THE PLACE END OF DAY COMMIT! In order to expand stats, thing must be tracked appropriately, which they arent. Here we add the addition of a compliance.go to track orders specifically. This will allow for the holdings manager to keep track of base stats such as how much we hold versus whats in use along with profits Compliance holds snapshots of every tick and what orders were there across exchanges. Also added a random slippage calculator which will allow a user to set their own slippage rates * Another fun end of day commit where nothing works. In order to have accurate stats, you need accurate positions, to have accurate positions you need to break things down to individual levels and store them. This is part of that process of ensuring that we can have multiple settings and everything processed appropriately. * Finalises multi currency config and support at most levels with exception to data loading. Simplifies some struct property definitions by removing redundancy Allows tracking of entire portfolio snapshots after each interval to track the entire process Lowercases use of exchange names * Sets the different prices to track across time. Attempts to sort out compliance snapshots * end of day commit. Moving compliance to the portfolio to manager and track all transactions at each interval. * Moves compliance calculation to portfolio.go. Adds a nice little decorator on the compliance manager orders to keep track of slippage, cost basis, volume adjusted price and close price. Moves "positions" to "hodlings" to be more accurate. Ensures exchange value calculations are accurate. Begins looking at Statistics and hodlings * Moves statistics to eventhandlers. Removes ticker work as not needed. Redefines hodler properties * hodlings are actually part of the portfolio * Renamed 420blazeit.strat file. Renamed hodlings to holdings. Moved Datahandler to data_types.go. Expanded holdings calculations, doesn't work, but we're getting somewhere. Renamed bad var names in backtest.go. Added new order side types to highlight lack of action reasons * Adds tests for holdings to ensure that holding snapshot calculation is accurate for the length of a strategy. Removes portfolio.Funds because its now handled via the holdings snapshots. Adds helper functions to Holding snapshots to retrieve relevant holdings. Updates sizing calculation to properly handle sell events. Expands holdings definitions to allow for comparison. Expands risk calculations to include holding snapshots so as to analyse all positions simultaneously * Changing the statistics results to consider all datas, with the ultimate goal to replace the current statistics package with this multi currency output * Made "Why" more generic. Expands statistics output. Removes time tying to stats map. Moves order event to correct location. Removes some debug lines. * Adds some raw funky drawdown statistics 🎉 * End of day commit. experimentation leaves little code changes * An attempt at expanding statistics. Need to have ones dedicated to exchange, asset, pair. Early work for having global map to track all the asset things to minimise all the maps throughout the application * 🎉 ADDS MULTI CURRENCY SUPPORT TO FOR THE BACKTESTER 🎉 Can either execute strategies by assessing multiple currencies individually, or as a group and make strategic decisions on what currency to signal in. Adds new strat files to demonstrate * End of day shenanigans. Moving codes around, making more fun stats. Expanding DCA strat to check if DCA is better than the market longer term * Adds sharpe ratio and total stats for final output if more than one currency is considered * Adds sortino ratio and test for validation * Adds information ratio * Adds calmar ratio * Adds CAGR * Slims down the statistics file to only include my work. Updates everything to use interfaces rather than direct code references to make it easier to swap out codes. Begins looking at serialising statistics for reports * More neatening. Removal of old FAKE tests. Can now output a report in JSON * End of day commit. Creation of reporting. Uses tradingview charting library and some basic bootstrap CDN to render content nicely. Will be updating everything to have a special kline item to annotate chart results * Minor formatting changes before all the reviews * End of day commit. Expands reporting to have an enhanced candle. These candles contain metadata on whether an order has been placed and to mark charts appropriately. This will be expanded to have all the stats and make it pretty * Extra code I forgot to commit! * Fixes an issue where data cannot render above 1,100 candles by stopping it from rendering more than that.. * End of day commit. There is no inclusivity with candle requests and I cant figure it out right now. * Fixes issue with missing data by adding events when data isnt present and classifying it. Adds new way for klines to verify data with a bit more clarity * Completes report generation * Improves cagr. removes butts. Replaces old kline function with new supercalc * Adds readme templates and files across whole backtester. Renames 420rsi to more appropriate name. Moves interfaces to common * Some extra documentation * New header * Adds some nice coverage to backtest.go. Updats readmes to use new backtester header template * End of day crappy test commit * Adds report coverage... Somewhat. Adds template path and output path to allow custom properties and easier testing. Fixes interface duplication * Adds some lame tests. * Fixes test * Adds coverage to the exchange event handler * Minor test changes * Fixes slippage calculations based on buying and selling. Adds more tests to compliance and holdings * Rejiggles risk assessment to properly consider leverage if it were ever to be implemented fully. Removes bot dependency and adds coverage to the risk package * Expands coverage to sizing * Rejiggles code to add coverage for the portfolio package and its compatriots. * Adds additional testing to the backtester along with some data gathering tests * Tried and failed attempt to expand testing for the database. * Adds testing for kline, data, statistics * into the 70%s of coverage! Adds tests for base, DCA, statistics * Adds test coverage of strategies * Adds test coerage to statistics. updates template generation to not require CurrencyStatistics to have EAP. Removes EAP from currencystatistics * Adds coverage to currencystatistics.go BUT ITS NOT COMPLETE * 86% coverage wow. Fixes 2 tests * Fixes data races due to engine dependency craziness. Changes order manager to not have a global dependency * Completes currencystatistics test coverage * Some linting fixes * Adds new documentation to the bakctester config. Updates how risk leverage/ratios work with a single map. * Minor documentation changes. Its difficult to describe how it all works * Redefines strats and strat tests. Adds some really light documentation * Updates some basic documentation. * Fixes lazy bugs * Fixes bug in fill event processing. Fixes bug in statistics crashing. Fixes report generation. Fixes multi-currency processing to still process non-errored signals * More documentation. * Fixes ALL LINTING ISSUES * Cuts off unnecessary limbs/interface functions. linting. Adding comments to all functions. Adding ability to use whalebomb to calculate slippage for live orders. Adds testing for it too. Simplifies adding events to statistics. * Removes a weird overlap of holding features that made no sense and the writer of those functions should be ASHAMED. Adds additional documentation * Fixes issue with data being outside ranges. Adds some extra validation to areas where people can mess around. Makes generating configs easier with consistent dates. Adds more documentation. Cleans up okex/okcoin implementation to some functions since people aren't understanding that they share a based okgroup and that anything that is the same between two functions only needs to be written once...................... Also fixes some bad gct script code * Updated image and slight change to readme * Removes unused code. Fixes up verbose and removes old comment * Fixes issues with data validation for other data sources. Fixes bad reference in template * Fixes missing data problem for last candle considered missing. Fixes issue where fill order crashes when sizing error occurs. Adds documentation * Fixes issue with drawdown calculations. Fixes live data usage * Adds some comments for good measure * Default strat fix * Fixes surprise linting issues * gofmt * New linting issue with every commit * Fixes testing. Adds new config setting to set a custom gocryptotrader config path. Updates config tests to use dryrun. Results now include the nickname in the file for easier identification * Fixes live testing bitstamp. Fixes some template issues. Adds comments. * Updates max drawdown calculation to go peak vs trough. Fixes minor return issue. Removes unnecessary Data implementations. Removes weird verbose false. Fixes holdings calculations for boughtvalue. Removes Swingholder and just uses Swing. Fixes time calculation issue in kline * End of day commit that breaks things. Fixes issue with documentation generation only going one space deep. Adds exchange name to warnings of missing candle data. Renames missing candle data function. Adds some testing to kline functions. Adds new ability to size modified orders to portfolio allowance. Addresses defer close and other small nits. Fixes slow loop * End of day commit. There are too many mini changes to list. DateType to int. Default switch case. Returning earlier. Nil returns instead of ok. High low price in data, now used in max drop down. Missing data shown in the report. * End of day commit moving things from stats to maths. * Move the rest to math package and add testing * Ammends slippage calculations for live. Adds sizing funds to order event. Improves CAGR calculation * Mini fix commit for test * End of day mini change for documentation * Fixes in documentation and expanded error messages. Pretties up the report * minor adjustments to sharpe ratio and other ratio calculations * Fixes test by taking it out back. linting * Fixes tests * Fixes some tests, addresses some poor nits * More test and lint fixes * Fixes binance translation issue * Further craziness into reducing the concurrent test issues * lint * Mini fix * Geometric average added and tested. Adjusts application to support it. End of day experiementation with negative geometric mean. Fixes typo in currencystatistics package name * Fixes geometric calculation. Adds sweet CMD logo * fixes geometric mean 😆 can now disable logo output if you hate everything good in life * lint * Should fix test in appveyor by not being nil * Fixes chance of getting no trades error. Maybe making nil events in the test will stop this poorly formed appveyor error * Forgotten Y tail * Check-ch-check-check-check-ch-check it out, minimising stutter is what its all about... Also provides more verbose error messages * de-ooopsies the whoopsie * Attempts to further address race issues when using global logs during start stop process * Includes a copy of the logger itself when logging so that no log.Debug action can create a data race upon being changed globally * Reduces bot usage further * Removes sharpie from b-acktester * comments, renames and bears, oh my! * Fixes git merge issues/tests. Splits average calculation into their own functions. Clarifies math function and sell position comments. Removes taker fee from final report. Adds warning when maker and taker aren't appropriately set. Fixes config testing issue where the config was saved when running exchange_template tests. Adds new test to ensure the testconfig isn't changed unnecessarily * More why to reason * Remove test due to hash discrepancy. * Updates maths to use errors. Updates tests to support it. * Fixes error handling for some packages. Uses position value instead of position size. Fixes leverage ratio work. Removes extra binance windows * Removes references to "multi currency" to shiny new verbiage "simultaneous processing" * Fixes issue with extra data be appended and then declared missing * Removes redundant code via code removal * Does a larger transition to using error types. Addresses math related nits * eat a mint while you lint * Completes err definition sweep * replaces over 80 instances of the same typo! * Renames more properties with Maximum ratios. Adds examples to config readme. Updates config maker takers. Adds cool kline error * Adds 'InclusiveEndDate' config property to API and Database datas. Adds testing for it. Updates readme for it * splint * Minor naming fix. Minor drawdown fix. Attempts to lower the bot usage when heaps of candles are requested. * Large data set processing improvements * Speeds up backtesting processing. Ensures rate limits are set Processing of most events is done in a linear fashion. So functions that relied on checking an events time for example, will now check the latest before processing every interval. The functions will still work normally in the event that someone wishes to use them out of order, but for general backtesting, it greatly speeds up all processing. Further, rather than comparing times all the time, I've introduced offsets for comparisons of ints for events and with candle data tests * Fixes build issue * Adds committed funds stat. Adds config goal Committed funds are calculated as the total amount of money currently in position It allows for a strategist to get the maximum returns for the smallest funds The goal function is to allow a strategist to set a goal description * Fixes data race * Adds unfinished config builder application * End of day broken commit I focussed on too many things at once and there are many things left to resolve * Fixees panics * Finishes config builder * Fixes order manager start/stop. Improves config manager * Fixes writefile reference * Adds some extra readme * Makes a more user friendly config builder. Fixes initial nil. Adds more order size reasons * lint * Adds warnings for when data is missing and ratios will be skewed * bodMISSED bodmas * Does not consider initial entry in performance calculations Adds strategy description field Adds cost basis to chart Fixes time rendering on default configs * Fixes bug in ratio calculations * saveConfig := !(!false != !true) == true * lint * Fixes start end single day drawdowns. Expands cmd drawdown explanation * Comment on rounding, updated report rounding * Addresses readme link issues * Actually fixes readme references * Should truly solve readme links.... * Includes filename for report log * Fixes panics, reduces csv trade candle size, no more science * Removes more science * test123 * Adds extra config validation * Fixes the date validation * Shows smaller fees * Changes perfectly cromulent error message to start >= end Co-authored-by: Andrew Jackson <andrew@disvelop.net>
This commit is contained in:
14
CONTRIBUTORS
14
CONTRIBUTORS
@@ -5,13 +5,13 @@ shazbert | https://github.com/shazbert
|
||||
gloriousCode | https://github.com/gloriousCode
|
||||
dependabot-preview[bot] | https://github.com/apps/dependabot-preview
|
||||
xtda | https://github.com/xtda
|
||||
ermalguni | https://github.com/ermalguni
|
||||
vadimzhukck | https://github.com/vadimzhukck
|
||||
MadCozBadd | https://github.com/MadCozBadd
|
||||
Rots | https://github.com/Rots
|
||||
vazha | https://github.com/vazha
|
||||
ermalguni | https://github.com/ermalguni
|
||||
MadCozBadd | https://github.com/MadCozBadd
|
||||
vadimzhukck | https://github.com/vadimzhukck
|
||||
140am | https://github.com/140am
|
||||
marcofranssen | https://github.com/marcofranssen
|
||||
vazha | https://github.com/vazha
|
||||
dackroyd | https://github.com/dackroyd
|
||||
cranktakular | https://github.com/cranktakular
|
||||
woshidama323 | https://github.com/woshidama323
|
||||
@@ -22,14 +22,14 @@ bretep | https://github.com/bretep
|
||||
Christian-Achilli | https://github.com/Christian-Achilli
|
||||
gam-phon | https://github.com/gam-phon
|
||||
cornelk | https://github.com/cornelk
|
||||
dependabot[bot] | https://github.com/apps/dependabot
|
||||
if1live | https://github.com/if1live
|
||||
lozdog245 | https://github.com/lozdog245
|
||||
soxipy | https://github.com/soxipy
|
||||
mshogin | https://github.com/mshogin
|
||||
herenow | https://github.com/herenow
|
||||
blombard | https://github.com/blombard
|
||||
CodeLingoBot | https://github.com/CodeLingoBot
|
||||
CodeLingoTeam | https://github.com/CodeLingoTeam
|
||||
Daanikus | https://github.com/Daanikus
|
||||
daniel-cohen | https://github.com/daniel-cohen
|
||||
DirectX | https://github.com/DirectX
|
||||
frankzougc | https://github.com/frankzougc
|
||||
@@ -43,3 +43,5 @@ starit | https://github.com/starit
|
||||
Jimexist | https://github.com/Jimexist
|
||||
lookfirst | https://github.com/lookfirst
|
||||
merkeld | https://github.com/merkeld
|
||||
CodeLingoTeam | https://github.com/CodeLingoTeam
|
||||
Daanikus | https://github.com/Daanikus
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014-2020 The GoCryptoTrader Developers
|
||||
Copyright (c) 2014-2021 The GoCryptoTrader Developers
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
21
README.md
21
README.md
@@ -77,6 +77,7 @@ However, we welcome pull requests for any exchange which does not match this cri
|
||||
+ OHLCV/Candle retrieval support. See [OHLCV](/docs/OHLCV.md).
|
||||
+ Scripting support. See [gctscript](/gctscript/README.md).
|
||||
+ Recent and historic trade processing. See [trades](/exchanges/trade/README.md).
|
||||
+ Backtesting application. An event-driven backtesting tool to test and iterate trading strategies using historical or custom data. See [backtester](/backtester/README.md).
|
||||
+ WebGUI (discontinued).
|
||||
|
||||
## Planned Features
|
||||
@@ -142,18 +143,18 @@ Binaries will be published once the codebase reaches a stable condition.
|
||||
|
||||
|User|Contribution Amount|
|
||||
|--|--|
|
||||
| [thrasher-](https://github.com/thrasher-) | 645 |
|
||||
| [shazbert](https://github.com/shazbert) | 199 |
|
||||
| [gloriousCode](https://github.com/gloriousCode) | 173 |
|
||||
| [dependabot-preview[bot]](https://github.com/apps/dependabot-preview) | 70 |
|
||||
| [thrasher-](https://github.com/thrasher-) | 650 |
|
||||
| [shazbert](https://github.com/shazbert) | 202 |
|
||||
| [gloriousCode](https://github.com/gloriousCode) | 176 |
|
||||
| [dependabot-preview[bot]](https://github.com/apps/dependabot-preview) | 87 |
|
||||
| [xtda](https://github.com/xtda) | 47 |
|
||||
| [Rots](https://github.com/Rots) | 15 |
|
||||
| [vazha](https://github.com/vazha) | 15 |
|
||||
| [ermalguni](https://github.com/ermalguni) | 14 |
|
||||
| [MadCozBadd](https://github.com/MadCozBadd) | 10 |
|
||||
| [vadimzhukck](https://github.com/vadimzhukck) | 10 |
|
||||
| [MadCozBadd](https://github.com/MadCozBadd) | 9 |
|
||||
| [Rots](https://github.com/Rots) | 9 |
|
||||
| [140am](https://github.com/140am) | 8 |
|
||||
| [marcofranssen](https://github.com/marcofranssen) | 8 |
|
||||
| [vazha](https://github.com/vazha) | 7 |
|
||||
| [dackroyd](https://github.com/dackroyd) | 5 |
|
||||
| [cranktakular](https://github.com/cranktakular) | 5 |
|
||||
| [woshidama323](https://github.com/woshidama323) | 3 |
|
||||
@@ -164,14 +165,14 @@ Binaries will be published once the codebase reaches a stable condition.
|
||||
| [Christian-Achilli](https://github.com/Christian-Achilli) | 2 |
|
||||
| [gam-phon](https://github.com/gam-phon) | 2 |
|
||||
| [cornelk](https://github.com/cornelk) | 2 |
|
||||
| [dependabot[bot]](https://github.com/apps/dependabot) | 2 |
|
||||
| [if1live](https://github.com/if1live) | 2 |
|
||||
| [lozdog245](https://github.com/lozdog245) | 2 |
|
||||
| [soxipy](https://github.com/soxipy) | 2 |
|
||||
| [mshogin](https://github.com/mshogin) | 2 |
|
||||
| [herenow](https://github.com/herenow) | 2 |
|
||||
| [blombard](https://github.com/blombard) | 1 |
|
||||
| [CodeLingoBot](https://github.com/CodeLingoBot) | 1 |
|
||||
| [CodeLingoTeam](https://github.com/CodeLingoTeam) | 1 |
|
||||
| [Daanikus](https://github.com/Daanikus) | 1 |
|
||||
| [daniel-cohen](https://github.com/daniel-cohen) | 1 |
|
||||
| [DirectX](https://github.com/DirectX) | 1 |
|
||||
| [frankzougc](https://github.com/frankzougc) | 1 |
|
||||
@@ -185,3 +186,5 @@ Binaries will be published once the codebase reaches a stable condition.
|
||||
| [Jimexist](https://github.com/Jimexist) | 1 |
|
||||
| [lookfirst](https://github.com/lookfirst) | 1 |
|
||||
| [merkeld](https://github.com/merkeld) | 1 |
|
||||
| [CodeLingoTeam](https://github.com/CodeLingoTeam) | 1 |
|
||||
| [Daanikus](https://github.com/Daanikus) | 1 |
|
||||
|
||||
106
backtester/README.md
Normal file
106
backtester/README.md
Normal file
@@ -0,0 +1,106 @@
|
||||
# GoCryptoTrader Backtester: Backtester package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This backtester package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
|
||||
# GoCryptoTrader Backtester
|
||||
An event-driven backtesting tool to test and iterate trading strategies using historical or custom data.
|
||||
|
||||
## Features
|
||||
- Works with all GoCryptoTrader exchanges that support trade/candle retrieval. See [candle readme](/docs/OHLCV.md) and [trade readme](/exchanges/trade/README.md) for supported exchanges
|
||||
- CSV data import
|
||||
- Database data import
|
||||
- Proof of concept live data running
|
||||
- Can run strategies against multiple cryptocurrencies
|
||||
- Can run strategies that can assess multiple currencies simultaneously to make complex decisions
|
||||
- Dollar cost strategy implementation
|
||||
- RSI strategy implementation
|
||||
- Rules customisation via config `.strat` files
|
||||
- Strategy customisation without requiring recompilation. For example, customising RSI high, low and length values via config `.strat` files.
|
||||
- Report generation
|
||||
- Portfolio manager to help size orders based on config rules, risk and candle volume
|
||||
- Order manager to place orders with customisable slippage estimator
|
||||
- Helpful statistics to help determine whether a strategy was effective
|
||||
- Compliance manager to keep snapshots of every transaction and their changes at every interval
|
||||
|
||||
## How does it work?
|
||||
- The application will load a `.strat` config file as specified at runtime
|
||||
- The `.strat` config file will contain
|
||||
- Start & end dates
|
||||
- The strategy to run
|
||||
- The candle interval
|
||||
- Where the data is to be sourced ([API](/backtester/data/kline/api/README.md), [CSV](/backtester/data/kline/csv/README.md), [database](/backtester/data/kline/database/README.md), [live](/backtester/data/kline/live/README.md))
|
||||
- Whether to use trade or candle data ([readme](/backtester/data/kline/README.md))
|
||||
- A nickname for the strategy (to help differentiate between runs/configs using the same strategy)
|
||||
- The currency/currencies to use
|
||||
- The exchange(s) to run against
|
||||
- See [readme](/backtester/config/README.md) for a breakdown of all config features
|
||||
- The GoCryptoTrader Backtester will retrieve the data specified in the config ([readme](/backtester/backtest/README.md))
|
||||
- The data is converted into candles and each candle is streamed as a data event.
|
||||
- The data event is analysed by the strategy which will output a purchasing signal such as `BUY`, `SELL` or `DONOTHING` ([readme](/backtester/eventtypes/signal/README.md))
|
||||
- The purchase signal is then processed by the portfolio manager ([readme](/backtester/eventhandlers/portfolio/README.md)) which will size the order ([readme](/backtester/eventhandlers/portfolio/size/README.md)) and assess risk ([readme](/backtester/eventhandlers/portfolio/risk/README.md)) before sending it to the exchange
|
||||
- The exchange order event handler will size to the candle data and run a slippage estimator ([readme](/backtester/eventhandlers/exchange/slippage/README.md)) and place the order ([readme](/backtester/eventhandlers/exchange/README.md))
|
||||
- Upon an order being placed, the order is snapshot for analysis in both the statistics package ([readme](/backtester/eventhandlers/statistics/README.md)) and the report package ([readme](/backtester/report/README.md))
|
||||
|
||||
|
||||
# Cool story, how do I use it?
|
||||
To run the application using the provided dollar cost average strategy, simply run `go run .` from `gocryptotrader/backtester`. An output of the results will be put in the `results` folder.
|
||||
|
||||
# How do I create my own config?
|
||||
There is a config generating helper application under `/backtester/config/configbuilder` to help you create a `.strat` file. Read more about it [here](/backtester/config/configbuilder/README.md). There are also a number of tests under `/config/config_test.go` which generate configs into the `examples` folder, which if you have code knowledge, can write your own configs programmatically.
|
||||
|
||||
# How do I create my own strategy?
|
||||
Creating strategies requires programming skills. [Here](/backtester/eventhandlers/strategies/README.md) is a readme on the subject. After reading the readmes, please review the strategies [here](/backtester/eventhandlers/strategies/) to gain an understanding on how to write your own.
|
||||
|
||||
# How does it work technically?
|
||||
- The readmes linked in the "How does it work" covers the main parts of the application.
|
||||
- If you are still unsure, please raise an issue, ask a question in our Slack or open a pull request
|
||||
- Here is an overview
|
||||

|
||||
|
||||
|
||||
# Important notes
|
||||
- This application is not considered production ready and you may experience issues
|
||||
- If you encounter any issues, you can raise them in our Slack channel or via Github issues
|
||||
- **Past performance is no guarantee of future results**
|
||||
- While an experimental feature, it is **not** recommended to **ever** use live trading and real orders
|
||||
- **Past performance is no guarantee of future results**
|
||||
|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
56
backtester/backtest/README.md
Normal file
56
backtester/backtest/README.md
Normal file
@@ -0,0 +1,56 @@
|
||||
# GoCryptoTrader Backtester: Backtest package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/backtest)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This backtest package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Backtest package overview
|
||||
|
||||
The backtest package is the most important package of the GoCryptoTrader backtester. It is the engine which combines all elements.
|
||||
It is responsible for the following functionality
|
||||
- Loading settings from a provided config file
|
||||
- Retrieving data
|
||||
- Loading the data into assessable chunks
|
||||
- Analysing the data via the `handleEvent` function
|
||||
- Looping through all data
|
||||
- Outputting results into a report
|
||||
|
||||
|
||||
A flow of the application is as follows:
|
||||

|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
980
backtester/backtest/backtest.go
Normal file
980
backtester/backtest/backtest.go
Normal file
@@ -0,0 +1,980 @@
|
||||
package backtest
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/config"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data/kline/api"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data/kline/csv"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data/kline/database"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data/kline/live"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/eventholder"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/exchange/slippage"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/compliance"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/risk"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/settings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/size"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/statistics"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/statistics/currencystatistics"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/strategies"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/strategies/base"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/fill"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/signal"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/report"
|
||||
gctcommon "github.com/thrasher-corp/gocryptotrader/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
gctdatabase "github.com/thrasher-corp/gocryptotrader/database"
|
||||
"github.com/thrasher-corp/gocryptotrader/engine"
|
||||
gctexchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
)
|
||||
|
||||
// New returns a new BackTest instance
|
||||
func New() *BackTest {
|
||||
return &BackTest{
|
||||
shutdown: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
// Reset BackTest values to default
|
||||
func (bt *BackTest) Reset() {
|
||||
bt.EventQueue.Reset()
|
||||
bt.Datas.Reset()
|
||||
bt.Portfolio.Reset()
|
||||
bt.Statistic.Reset()
|
||||
bt.Exchange.Reset()
|
||||
bt.Bot = nil
|
||||
}
|
||||
|
||||
// NewFromConfig takes a strategy config and configures a backtester variable to run
|
||||
func NewFromConfig(cfg *config.Config, templatePath, output string, bot *engine.Engine) (*BackTest, error) {
|
||||
log.Infoln(log.BackTester, "loading config...")
|
||||
if cfg == nil {
|
||||
return nil, errNilConfig
|
||||
}
|
||||
if bot == nil {
|
||||
return nil, errNilBot
|
||||
}
|
||||
bt := New()
|
||||
|
||||
var e exchange.Exchange
|
||||
bt.Datas = &data.HandlerPerCurrency{}
|
||||
bt.EventQueue = &eventholder.Holder{}
|
||||
reports := &report.Data{
|
||||
Config: cfg,
|
||||
TemplatePath: templatePath,
|
||||
OutputPath: output,
|
||||
}
|
||||
bt.Reports = reports
|
||||
|
||||
err := bt.setupBot(cfg, bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
e, err = bt.setupExchangeSettings(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bt.Exchange = &e
|
||||
|
||||
buyRule := config.MinMax{
|
||||
MinimumSize: cfg.PortfolioSettings.BuySide.MinimumSize,
|
||||
MaximumSize: cfg.PortfolioSettings.BuySide.MaximumSize,
|
||||
MaximumTotal: cfg.PortfolioSettings.BuySide.MaximumTotal,
|
||||
}
|
||||
buyRule.Validate()
|
||||
sellRule := config.MinMax{
|
||||
MinimumSize: cfg.PortfolioSettings.SellSide.MinimumSize,
|
||||
MaximumSize: cfg.PortfolioSettings.SellSide.MaximumSize,
|
||||
MaximumTotal: cfg.PortfolioSettings.SellSide.MaximumTotal,
|
||||
}
|
||||
sellRule.Validate()
|
||||
sizeManager := &size.Size{
|
||||
BuySide: buyRule,
|
||||
SellSide: sellRule,
|
||||
}
|
||||
|
||||
portfolioRisk := &risk.Risk{
|
||||
CurrencySettings: make(map[string]map[asset.Item]map[currency.Pair]*risk.CurrencySettings),
|
||||
}
|
||||
for i := range cfg.CurrencySettings {
|
||||
if portfolioRisk.CurrencySettings[cfg.CurrencySettings[i].ExchangeName] == nil {
|
||||
portfolioRisk.CurrencySettings[cfg.CurrencySettings[i].ExchangeName] = make(map[asset.Item]map[currency.Pair]*risk.CurrencySettings)
|
||||
}
|
||||
var a asset.Item
|
||||
a, err = asset.New(cfg.CurrencySettings[i].Asset)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"%w for %v %v %v. Err %v",
|
||||
errInvalidConfigAsset,
|
||||
cfg.CurrencySettings[i].ExchangeName,
|
||||
cfg.CurrencySettings[i].Asset,
|
||||
cfg.CurrencySettings[i].Base+cfg.CurrencySettings[i].Quote,
|
||||
err)
|
||||
}
|
||||
if portfolioRisk.CurrencySettings[cfg.CurrencySettings[i].ExchangeName][a] == nil {
|
||||
portfolioRisk.CurrencySettings[cfg.CurrencySettings[i].ExchangeName][a] = make(map[currency.Pair]*risk.CurrencySettings)
|
||||
}
|
||||
var curr currency.Pair
|
||||
curr, err = currency.NewPairFromString(cfg.CurrencySettings[i].Base + cfg.CurrencySettings[i].Quote)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"%w for %v %v %v. Err %v",
|
||||
errInvalidConfigCurrency,
|
||||
cfg.CurrencySettings[i].ExchangeName,
|
||||
cfg.CurrencySettings[i].Asset,
|
||||
cfg.CurrencySettings[i].Base+cfg.CurrencySettings[i].Quote,
|
||||
err)
|
||||
}
|
||||
portfolioRisk.CurrencySettings[cfg.CurrencySettings[i].ExchangeName][a][curr] = &risk.CurrencySettings{
|
||||
MaximumOrdersWithLeverageRatio: cfg.CurrencySettings[i].Leverage.MaximumOrdersWithLeverageRatio,
|
||||
MaxLeverageRate: cfg.CurrencySettings[i].Leverage.MaximumLeverageRate,
|
||||
MaximumHoldingRatio: cfg.CurrencySettings[i].MaximumHoldingsRatio,
|
||||
}
|
||||
if cfg.CurrencySettings[i].MakerFee > cfg.CurrencySettings[i].TakerFee {
|
||||
log.Warnf(log.BackTester, "maker fee '%v' should not exceed taker fee '%v'. Please review config",
|
||||
cfg.CurrencySettings[i].MakerFee,
|
||||
cfg.CurrencySettings[i].TakerFee)
|
||||
}
|
||||
}
|
||||
var p *portfolio.Portfolio
|
||||
p, err = portfolio.Setup(sizeManager, portfolioRisk, cfg.StatisticSettings.RiskFreeRate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := range e.CurrencySettings {
|
||||
var lookup *settings.Settings
|
||||
lookup, err = p.SetupCurrencySettingsMap(e.CurrencySettings[i].ExchangeName, e.CurrencySettings[i].AssetType, e.CurrencySettings[i].CurrencyPair)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lookup.Fee = e.CurrencySettings[i].TakerFee
|
||||
lookup.Leverage = e.CurrencySettings[i].Leverage
|
||||
lookup.BuySideSizing = e.CurrencySettings[i].BuySide
|
||||
lookup.SellSideSizing = e.CurrencySettings[i].SellSide
|
||||
lookup.InitialFunds = e.CurrencySettings[i].InitialFunds
|
||||
lookup.ComplianceManager = compliance.Manager{
|
||||
Snapshots: []compliance.Snapshot{},
|
||||
}
|
||||
}
|
||||
bt.Portfolio = p
|
||||
|
||||
bt.Strategy, err = strategies.LoadStrategyByName(cfg.StrategySettings.Name, cfg.StrategySettings.SimultaneousSignalProcessing)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bt.Strategy.SetDefaults()
|
||||
if cfg.StrategySettings.CustomSettings != nil {
|
||||
err = bt.Strategy.SetCustomSettings(cfg.StrategySettings.CustomSettings)
|
||||
if err != nil && !errors.Is(err, base.ErrCustomSettingsUnsupported) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
stats := &statistics.Statistic{
|
||||
StrategyName: bt.Strategy.Name(),
|
||||
StrategyNickname: cfg.Nickname,
|
||||
StrategyDescription: bt.Strategy.Description(),
|
||||
StrategyGoal: cfg.Goal,
|
||||
ExchangeAssetPairStatistics: make(map[string]map[asset.Item]map[currency.Pair]*currencystatistics.CurrencyStatistic),
|
||||
RiskFreeRate: cfg.StatisticSettings.RiskFreeRate,
|
||||
}
|
||||
bt.Statistic = stats
|
||||
reports.Statistics = stats
|
||||
|
||||
cfg.PrintSetting()
|
||||
|
||||
return bt, nil
|
||||
}
|
||||
|
||||
func (bt *BackTest) setupExchangeSettings(cfg *config.Config) (exchange.Exchange, error) {
|
||||
log.Infoln(log.BackTester, "setting exchange settings...")
|
||||
resp := exchange.Exchange{}
|
||||
|
||||
for i := range cfg.CurrencySettings {
|
||||
exch, pair, a, err := bt.loadExchangePairAssetBase(
|
||||
cfg.CurrencySettings[i].ExchangeName,
|
||||
cfg.CurrencySettings[i].Base,
|
||||
cfg.CurrencySettings[i].Quote,
|
||||
cfg.CurrencySettings[i].Asset)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
exchangeName := strings.ToLower(exch.GetName())
|
||||
bt.Datas.Setup()
|
||||
klineData, err := bt.loadData(cfg, exch, pair, a)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
bt.Datas.SetDataForCurrency(exchangeName, a, pair, klineData)
|
||||
var makerFee, takerFee float64
|
||||
if cfg.CurrencySettings[i].MakerFee > 0 {
|
||||
makerFee = cfg.CurrencySettings[i].MakerFee
|
||||
}
|
||||
if cfg.CurrencySettings[i].TakerFee > 0 {
|
||||
takerFee = cfg.CurrencySettings[i].TakerFee
|
||||
}
|
||||
if makerFee == 0 || takerFee == 0 {
|
||||
var apiMakerFee, apiTakerFee float64
|
||||
apiMakerFee, apiTakerFee = getFees(exch, pair)
|
||||
if makerFee == 0 {
|
||||
makerFee = apiMakerFee
|
||||
}
|
||||
if takerFee == 0 {
|
||||
takerFee = apiTakerFee
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.CurrencySettings[i].MaximumSlippagePercent < 0 {
|
||||
log.Warnf(log.BackTester, "invalid maximum slippage percent '%v'. Slippage percent is defined as a number, eg '100.00', defaulting to '%v'",
|
||||
cfg.CurrencySettings[i].MaximumSlippagePercent,
|
||||
slippage.DefaultMaximumSlippagePercent)
|
||||
cfg.CurrencySettings[i].MaximumSlippagePercent = slippage.DefaultMaximumSlippagePercent
|
||||
}
|
||||
if cfg.CurrencySettings[i].MaximumSlippagePercent == 0 {
|
||||
cfg.CurrencySettings[i].MaximumSlippagePercent = slippage.DefaultMaximumSlippagePercent
|
||||
}
|
||||
if cfg.CurrencySettings[i].MinimumSlippagePercent < 0 {
|
||||
log.Warnf(log.BackTester, "invalid minimum slippage percent '%v'. Slippage percent is defined as a number, eg '80.00', defaulting to '%v'",
|
||||
cfg.CurrencySettings[i].MinimumSlippagePercent,
|
||||
slippage.DefaultMinimumSlippagePercent)
|
||||
cfg.CurrencySettings[i].MinimumSlippagePercent = slippage.DefaultMinimumSlippagePercent
|
||||
}
|
||||
if cfg.CurrencySettings[i].MinimumSlippagePercent == 0 {
|
||||
cfg.CurrencySettings[i].MinimumSlippagePercent = slippage.DefaultMinimumSlippagePercent
|
||||
}
|
||||
if cfg.CurrencySettings[i].MaximumSlippagePercent < cfg.CurrencySettings[i].MinimumSlippagePercent {
|
||||
cfg.CurrencySettings[i].MaximumSlippagePercent = slippage.DefaultMaximumSlippagePercent
|
||||
}
|
||||
|
||||
realOrders := false
|
||||
if cfg.DataSettings.LiveData != nil {
|
||||
realOrders = cfg.DataSettings.LiveData.RealOrders
|
||||
}
|
||||
|
||||
buyRule := config.MinMax{
|
||||
MinimumSize: cfg.CurrencySettings[i].BuySide.MinimumSize,
|
||||
MaximumSize: cfg.CurrencySettings[i].BuySide.MaximumSize,
|
||||
MaximumTotal: cfg.CurrencySettings[i].BuySide.MaximumTotal,
|
||||
}
|
||||
buyRule.Validate()
|
||||
sellRule := config.MinMax{
|
||||
MinimumSize: cfg.CurrencySettings[i].SellSide.MinimumSize,
|
||||
MaximumSize: cfg.CurrencySettings[i].SellSide.MaximumSize,
|
||||
MaximumTotal: cfg.CurrencySettings[i].SellSide.MaximumTotal,
|
||||
}
|
||||
sellRule.Validate()
|
||||
resp.CurrencySettings = append(resp.CurrencySettings, exchange.Settings{
|
||||
ExchangeName: cfg.CurrencySettings[i].ExchangeName,
|
||||
InitialFunds: cfg.CurrencySettings[i].InitialFunds,
|
||||
MinimumSlippageRate: cfg.CurrencySettings[i].MinimumSlippagePercent,
|
||||
MaximumSlippageRate: cfg.CurrencySettings[i].MaximumSlippagePercent,
|
||||
CurrencyPair: pair,
|
||||
AssetType: a,
|
||||
ExchangeFee: takerFee,
|
||||
MakerFee: takerFee,
|
||||
TakerFee: makerFee,
|
||||
UseRealOrders: realOrders,
|
||||
BuySide: buyRule,
|
||||
SellSide: sellRule,
|
||||
Leverage: config.Leverage{
|
||||
CanUseLeverage: cfg.CurrencySettings[i].Leverage.CanUseLeverage,
|
||||
MaximumLeverageRate: cfg.CurrencySettings[i].Leverage.MaximumLeverageRate,
|
||||
MaximumOrdersWithLeverageRatio: cfg.CurrencySettings[i].Leverage.MaximumOrdersWithLeverageRatio,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (bt *BackTest) loadExchangePairAssetBase(exch, base, quote, ass string) (gctexchange.IBotExchange, currency.Pair, asset.Item, error) {
|
||||
var err error
|
||||
e := bt.Bot.GetExchangeByName(exch)
|
||||
if e == nil {
|
||||
return nil, currency.Pair{}, "", engine.ErrExchangeNotFound
|
||||
}
|
||||
|
||||
var cp, fPair currency.Pair
|
||||
cp, err = currency.NewPairFromStrings(base, quote)
|
||||
if err != nil {
|
||||
return nil, currency.Pair{}, "", err
|
||||
}
|
||||
|
||||
var a asset.Item
|
||||
a, err = asset.New(ass)
|
||||
if err != nil {
|
||||
return nil, currency.Pair{}, "", err
|
||||
}
|
||||
|
||||
exchangeBase := e.GetBase()
|
||||
if !exchangeBase.ValidateAPICredentials() {
|
||||
log.Warnf(log.BackTester, "no credentials set for %v, this is theoretical only", exchangeBase.Name)
|
||||
}
|
||||
|
||||
fPair, err = exchangeBase.FormatExchangeCurrency(cp, a)
|
||||
if err != nil {
|
||||
return nil, currency.Pair{}, "", err
|
||||
}
|
||||
return e, fPair, a, nil
|
||||
}
|
||||
|
||||
// setupBot sets up a basic bot to retrieve exchange data
|
||||
// as well as process orders
|
||||
func (bt *BackTest) setupBot(cfg *config.Config, bot *engine.Engine) error {
|
||||
var err error
|
||||
bt.Bot = bot
|
||||
err = cfg.ValidateCurrencySettings()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := range cfg.CurrencySettings {
|
||||
err = bt.Bot.LoadExchange(cfg.CurrencySettings[i].ExchangeName, false, nil)
|
||||
if err != nil && !errors.Is(err, engine.ErrExchangeAlreadyLoaded) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !bt.Bot.OrderManager.Started() {
|
||||
return bt.Bot.OrderManager.Start(bt.Bot)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getFees will return an exchange's fee rate from GCT's wrapper function
|
||||
func getFees(exch gctexchange.IBotExchange, fPair currency.Pair) (makerFee, takerFee float64) {
|
||||
var err error
|
||||
takerFee, err = exch.GetFeeByType(&gctexchange.FeeBuilder{
|
||||
FeeType: gctexchange.OfflineTradeFee,
|
||||
Pair: fPair,
|
||||
IsMaker: false,
|
||||
PurchasePrice: 1,
|
||||
Amount: 1,
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorf(log.BackTester, "Could not retrieve taker fee for %v. %v", exch.GetName(), err)
|
||||
}
|
||||
|
||||
makerFee, err = exch.GetFeeByType(&gctexchange.FeeBuilder{
|
||||
FeeType: gctexchange.OfflineTradeFee,
|
||||
Pair: fPair,
|
||||
IsMaker: true,
|
||||
PurchasePrice: 1,
|
||||
Amount: 1,
|
||||
})
|
||||
if err != nil {
|
||||
log.Errorf(log.BackTester, "Could not retrieve maker fee for %v. %v", exch.GetName(), err)
|
||||
}
|
||||
|
||||
return makerFee, takerFee
|
||||
}
|
||||
|
||||
// loadData will create kline data from the sources defined in start config files. It can exist from databases, csv or API endpoints
|
||||
// it can also be generated from trade data which will be converted into kline data
|
||||
func (bt *BackTest) loadData(cfg *config.Config, exch gctexchange.IBotExchange, fPair currency.Pair, a asset.Item) (*kline.DataFromKline, error) {
|
||||
log.Infof(log.BackTester, "loading data for %v %v %v...\n", exch.GetName(), a, fPair)
|
||||
if exch == nil {
|
||||
return nil, engine.ErrExchangeNotFound
|
||||
}
|
||||
b := exch.GetBase()
|
||||
if cfg.DataSettings.DatabaseData == nil &&
|
||||
cfg.DataSettings.LiveData == nil &&
|
||||
cfg.DataSettings.APIData == nil &&
|
||||
cfg.DataSettings.CSVData == nil {
|
||||
return nil, errNoDataSource
|
||||
}
|
||||
resp := &kline.DataFromKline{}
|
||||
if (cfg.DataSettings.APIData != nil && cfg.DataSettings.DatabaseData != nil) ||
|
||||
(cfg.DataSettings.APIData != nil && cfg.DataSettings.LiveData != nil) ||
|
||||
(cfg.DataSettings.APIData != nil && cfg.DataSettings.CSVData != nil) ||
|
||||
(cfg.DataSettings.DatabaseData != nil && cfg.DataSettings.LiveData != nil) ||
|
||||
(cfg.DataSettings.CSVData != nil && cfg.DataSettings.LiveData != nil) ||
|
||||
(cfg.DataSettings.CSVData != nil && cfg.DataSettings.DatabaseData != nil) {
|
||||
return nil, errAmbiguousDataSource
|
||||
}
|
||||
|
||||
dataType, err := common.DataTypeToInt(cfg.DataSettings.DataType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch {
|
||||
case cfg.DataSettings.CSVData != nil:
|
||||
if cfg.DataSettings.Interval <= 0 {
|
||||
return nil, errIntervalUnset
|
||||
}
|
||||
resp, err = csv.LoadData(
|
||||
dataType,
|
||||
cfg.DataSettings.CSVData.FullPath,
|
||||
strings.ToLower(exch.GetName()),
|
||||
cfg.DataSettings.Interval,
|
||||
fPair,
|
||||
a)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v. Please check your GoCryptoTrader configuration", err)
|
||||
}
|
||||
resp.Item.RemoveDuplicates()
|
||||
resp.Item.SortCandlesByTimestamp(false)
|
||||
resp.Range = gctkline.CalculateCandleDateRanges(
|
||||
resp.Item.Candles[0].Time,
|
||||
resp.Item.Candles[len(resp.Item.Candles)-1].Time.Add(cfg.DataSettings.Interval),
|
||||
gctkline.Interval(cfg.DataSettings.Interval),
|
||||
0,
|
||||
)
|
||||
err = resp.Range.VerifyResultsHaveData(resp.Item.Candles)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "missing candles data between") {
|
||||
log.Warn(log.BackTester, err.Error())
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
case cfg.DataSettings.DatabaseData != nil:
|
||||
if cfg.DataSettings.DatabaseData.InclusiveEndDate {
|
||||
cfg.DataSettings.DatabaseData.EndDate = cfg.DataSettings.DatabaseData.EndDate.Add(cfg.DataSettings.Interval)
|
||||
}
|
||||
if cfg.DataSettings.DatabaseData.ConfigOverride != nil {
|
||||
bt.Bot.Config.Database = *cfg.DataSettings.DatabaseData.ConfigOverride
|
||||
gctdatabase.DB.DataPath = filepath.Join(gctcommon.GetDefaultDataDir(runtime.GOOS), "database")
|
||||
gctdatabase.DB.Config = cfg.DataSettings.DatabaseData.ConfigOverride
|
||||
err = bt.Bot.DatabaseManager.Start(bt.Bot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() {
|
||||
err = bt.Bot.DatabaseManager.Stop()
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
resp, err = loadDatabaseData(cfg, exch.GetName(), fPair, a, dataType)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to retrieve data from GoCryptoTrader database. Error: %v. Please ensure the database is setup correctly and has data before use", err)
|
||||
}
|
||||
|
||||
resp.Item.RemoveDuplicates()
|
||||
resp.Item.SortCandlesByTimestamp(false)
|
||||
resp.Range = gctkline.CalculateCandleDateRanges(
|
||||
cfg.DataSettings.DatabaseData.StartDate,
|
||||
cfg.DataSettings.DatabaseData.EndDate,
|
||||
gctkline.Interval(cfg.DataSettings.Interval),
|
||||
0,
|
||||
)
|
||||
err = resp.Range.VerifyResultsHaveData(resp.Item.Candles)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "missing candles data between") {
|
||||
log.Warn(log.BackTester, err.Error())
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
case cfg.DataSettings.APIData != nil:
|
||||
if cfg.DataSettings.APIData.InclusiveEndDate {
|
||||
cfg.DataSettings.APIData.EndDate = cfg.DataSettings.APIData.EndDate.Add(cfg.DataSettings.Interval)
|
||||
}
|
||||
resp, err = loadAPIData(
|
||||
cfg,
|
||||
exch,
|
||||
fPair,
|
||||
a,
|
||||
b.Features.Enabled.Kline.ResultLimit,
|
||||
dataType)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
case cfg.DataSettings.LiveData != nil:
|
||||
if len(cfg.CurrencySettings) > 1 {
|
||||
return nil, errors.New("live data simulation only supports one currency")
|
||||
}
|
||||
err = loadLiveData(cfg, b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
go bt.loadLiveDataLoop(
|
||||
resp,
|
||||
cfg,
|
||||
exch,
|
||||
fPair,
|
||||
a,
|
||||
dataType)
|
||||
return resp, nil
|
||||
}
|
||||
if resp == nil {
|
||||
return nil, fmt.Errorf("processing error, response returned nil")
|
||||
}
|
||||
|
||||
err = b.ValidateKline(fPair, a, resp.Item.Interval)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = resp.Load()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bt.Reports.AddKlineItem(&resp.Item)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func loadDatabaseData(cfg *config.Config, name string, fPair currency.Pair, a asset.Item, dataType int64) (*kline.DataFromKline, error) {
|
||||
if cfg == nil || cfg.DataSettings.DatabaseData == nil {
|
||||
return nil, errors.New("nil config data received")
|
||||
}
|
||||
err := cfg.ValidateDate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cfg.DataSettings.Interval <= 0 {
|
||||
return nil, errIntervalUnset
|
||||
}
|
||||
|
||||
return database.LoadData(
|
||||
cfg.DataSettings.DatabaseData.StartDate,
|
||||
cfg.DataSettings.DatabaseData.EndDate,
|
||||
cfg.DataSettings.Interval,
|
||||
strings.ToLower(name),
|
||||
dataType,
|
||||
fPair,
|
||||
a)
|
||||
}
|
||||
|
||||
func loadAPIData(cfg *config.Config, exch gctexchange.IBotExchange, fPair currency.Pair, a asset.Item, resultLimit uint32, dataType int64) (*kline.DataFromKline, error) {
|
||||
err := cfg.ValidateDate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cfg.DataSettings.Interval <= 0 {
|
||||
return nil, errIntervalUnset
|
||||
}
|
||||
dates := gctkline.CalculateCandleDateRanges(
|
||||
cfg.DataSettings.APIData.StartDate,
|
||||
cfg.DataSettings.APIData.EndDate,
|
||||
gctkline.Interval(cfg.DataSettings.Interval),
|
||||
resultLimit)
|
||||
candles, err := api.LoadData(
|
||||
dataType,
|
||||
cfg.DataSettings.APIData.StartDate,
|
||||
cfg.DataSettings.APIData.EndDate,
|
||||
cfg.DataSettings.Interval,
|
||||
exch,
|
||||
fPair,
|
||||
a)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%v. Please check your GoCryptoTrader configuration", err)
|
||||
}
|
||||
err = dates.VerifyResultsHaveData(candles.Candles)
|
||||
if err != nil && errors.Is(err, gctkline.ErrMissingCandleData) {
|
||||
log.Warn(log.BackTester, err.Error())
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
candles.FillMissingDataWithEmptyEntries(&dates)
|
||||
candles.RemoveOutsideRange(cfg.DataSettings.APIData.StartDate, cfg.DataSettings.APIData.EndDate)
|
||||
return &kline.DataFromKline{
|
||||
Item: *candles,
|
||||
Range: dates,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func loadLiveData(cfg *config.Config, base *gctexchange.Base) error {
|
||||
if cfg == nil || base == nil || cfg.DataSettings.LiveData == nil {
|
||||
return common.ErrNilArguments
|
||||
}
|
||||
if cfg.DataSettings.Interval <= 0 {
|
||||
return errIntervalUnset
|
||||
}
|
||||
|
||||
if cfg.DataSettings.LiveData.APIKeyOverride != "" {
|
||||
base.API.Credentials.Key = cfg.DataSettings.LiveData.APIKeyOverride
|
||||
}
|
||||
if cfg.DataSettings.LiveData.APISecretOverride != "" {
|
||||
base.API.Credentials.Secret = cfg.DataSettings.LiveData.APISecretOverride
|
||||
}
|
||||
if cfg.DataSettings.LiveData.APIClientIDOverride != "" {
|
||||
base.API.Credentials.ClientID = cfg.DataSettings.LiveData.APIClientIDOverride
|
||||
}
|
||||
if cfg.DataSettings.LiveData.API2FAOverride != "" {
|
||||
base.API.Credentials.PEMKey = cfg.DataSettings.LiveData.API2FAOverride
|
||||
}
|
||||
validated := base.ValidateAPICredentials()
|
||||
base.API.AuthenticatedSupport = validated
|
||||
if !validated && cfg.DataSettings.LiveData.RealOrders {
|
||||
log.Warn(log.BackTester, "invalid API credentials set, real orders set to false")
|
||||
cfg.DataSettings.LiveData.RealOrders = false
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Run will iterate over loaded data events
|
||||
// save them and then handle the event based on its type
|
||||
func (bt *BackTest) Run() error {
|
||||
log.Info(log.BackTester, "running backtester against pre-defined data")
|
||||
dataLoadingIssue:
|
||||
for ev := bt.EventQueue.NextEvent(); ; ev = bt.EventQueue.NextEvent() {
|
||||
if ev == nil {
|
||||
dataHandlerMap := bt.Datas.GetAllData()
|
||||
for exchangeName, exchangeMap := range dataHandlerMap {
|
||||
for assetItem, assetMap := range exchangeMap {
|
||||
var hasProcessedData bool
|
||||
for currencyPair, dataHandler := range assetMap {
|
||||
d := dataHandler.Next()
|
||||
if d == nil {
|
||||
if !bt.hasHandledEvent {
|
||||
log.Errorf(log.BackTester, "Unable to perform `Next` for %v %v %v", exchangeName, assetItem, currencyPair)
|
||||
}
|
||||
break dataLoadingIssue
|
||||
}
|
||||
if bt.Strategy.UseSimultaneousProcessing() && hasProcessedData {
|
||||
continue
|
||||
}
|
||||
bt.EventQueue.AppendEvent(d)
|
||||
hasProcessedData = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err := bt.handleEvent(ev)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !bt.hasHandledEvent {
|
||||
bt.hasHandledEvent = true
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleEvent is the main processor of data for the backtester
|
||||
// after data has been loaded and Run has appended a data event to the queue,
|
||||
// handle event will process events and add further events to the queue if they
|
||||
// are required
|
||||
func (bt *BackTest) handleEvent(e common.EventHandler) error {
|
||||
switch ev := e.(type) {
|
||||
case common.DataEventHandler:
|
||||
return bt.processDataEvent(ev)
|
||||
case signal.Event:
|
||||
bt.processSignalEvent(ev)
|
||||
case order.Event:
|
||||
bt.processOrderEvent(ev)
|
||||
case fill.Event:
|
||||
bt.processFillEvent(ev)
|
||||
case nil:
|
||||
default:
|
||||
return fmt.Errorf("%w %v received, could not process",
|
||||
errUnhandledDatatype,
|
||||
e)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// processDataEvent determines what signal events are generated and appended
|
||||
// to the event queue based on whether it is running a multi-currency consideration strategy order not
|
||||
//
|
||||
// for multi-currency-consideration it will pass all currency datas to the strategy for it to determine what
|
||||
// currencies to act upon
|
||||
//
|
||||
// for non-multi-currency-consideration strategies, it will simply process every currency individually
|
||||
// against the strategy and generate signals
|
||||
func (bt *BackTest) processDataEvent(e common.DataEventHandler) error {
|
||||
if bt.Strategy.UseSimultaneousProcessing() {
|
||||
var dataEvents []data.Handler
|
||||
dataHandlerMap := bt.Datas.GetAllData()
|
||||
for _, exchangeMap := range dataHandlerMap {
|
||||
for _, assetMap := range exchangeMap {
|
||||
for _, dataHandler := range assetMap {
|
||||
latestData := dataHandler.Latest()
|
||||
bt.updateStatsForDataEvent(latestData)
|
||||
dataEvents = append(dataEvents, dataHandler)
|
||||
}
|
||||
}
|
||||
}
|
||||
signals, err := bt.Strategy.OnSimultaneousSignals(dataEvents, bt.Portfolio)
|
||||
if err != nil {
|
||||
if errors.Is(err, base.ErrTooMuchBadData) {
|
||||
// too much bad data is a severe error and backtesting must cease
|
||||
return err
|
||||
}
|
||||
log.Error(log.BackTester, err)
|
||||
return nil
|
||||
}
|
||||
for i := range signals {
|
||||
err = bt.Statistic.SetEventForOffset(signals[i])
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
}
|
||||
bt.EventQueue.AppendEvent(signals[i])
|
||||
}
|
||||
} else {
|
||||
bt.updateStatsForDataEvent(e)
|
||||
d := bt.Datas.GetDataForCurrency(e.GetExchange(), e.GetAssetType(), e.Pair())
|
||||
|
||||
s, err := bt.Strategy.OnSignal(d, bt.Portfolio)
|
||||
if err != nil {
|
||||
if errors.Is(err, base.ErrTooMuchBadData) {
|
||||
// too much bad data is a severe error and backtesting must cease
|
||||
return err
|
||||
}
|
||||
log.Error(log.BackTester, err)
|
||||
return nil
|
||||
}
|
||||
err = bt.Statistic.SetEventForOffset(s)
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
}
|
||||
bt.EventQueue.AppendEvent(s)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// updateStatsForDataEvent makes various systems aware of price movements from
|
||||
// data events
|
||||
func (bt *BackTest) updateStatsForDataEvent(e common.DataEventHandler) {
|
||||
// update portfolio with latest price
|
||||
err := bt.Portfolio.Update(e)
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
}
|
||||
// update statistics with latest price
|
||||
err = bt.Statistic.SetupEventForTime(e)
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (bt *BackTest) processSignalEvent(ev signal.Event) {
|
||||
cs, err := bt.Exchange.GetCurrencySettings(ev.GetExchange(), ev.GetAssetType(), ev.Pair())
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
return
|
||||
}
|
||||
var o *order.Order
|
||||
o, err = bt.Portfolio.OnSignal(ev, &cs)
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
return
|
||||
}
|
||||
err = bt.Statistic.SetEventForOffset(o)
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
}
|
||||
|
||||
bt.EventQueue.AppendEvent(o)
|
||||
}
|
||||
|
||||
func (bt *BackTest) processOrderEvent(ev order.Event) {
|
||||
d := bt.Datas.GetDataForCurrency(ev.GetExchange(), ev.GetAssetType(), ev.Pair())
|
||||
f, err := bt.Exchange.ExecuteOrder(ev, d, bt.Bot)
|
||||
if err != nil {
|
||||
if f == nil {
|
||||
log.Errorf(log.BackTester, "fill event should always be returned, please fix, %v", err)
|
||||
return
|
||||
}
|
||||
log.Errorf(log.BackTester, "%v %v %v %v", f.GetExchange(), f.GetAssetType(), f.Pair(), err)
|
||||
}
|
||||
err = bt.Statistic.SetEventForOffset(f)
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
}
|
||||
bt.EventQueue.AppendEvent(f)
|
||||
}
|
||||
|
||||
func (bt *BackTest) processFillEvent(ev fill.Event) {
|
||||
t, err := bt.Portfolio.OnFill(ev)
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = bt.Statistic.SetEventForOffset(t)
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
}
|
||||
|
||||
var holding holdings.Holding
|
||||
holding, err = bt.Portfolio.ViewHoldingAtTimePeriod(ev.GetExchange(), ev.GetAssetType(), ev.Pair(), ev.GetTime())
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
}
|
||||
|
||||
err = bt.Statistic.AddHoldingsForTime(&holding)
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
}
|
||||
|
||||
var cp *compliance.Manager
|
||||
cp, err = bt.Portfolio.GetComplianceManager(ev.GetExchange(), ev.GetAssetType(), ev.Pair())
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
}
|
||||
|
||||
snap := cp.GetLatestSnapshot()
|
||||
err = bt.Statistic.AddComplianceSnapshotForTime(snap, ev)
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
}
|
||||
}
|
||||
|
||||
// RunLive is a proof of concept function that does not yet support multi currency usage
|
||||
// It runs by constantly checking for new live datas and running through the list of events
|
||||
// once new data is processed. It will run until application close event has been received
|
||||
func (bt *BackTest) RunLive() error {
|
||||
log.Info(log.BackTester, "running backtester against live data")
|
||||
timeoutTimer := time.NewTimer(time.Minute * 5)
|
||||
// a frequent timer so that when a new candle is released by an exchange
|
||||
// that it can be processed quickly
|
||||
processEventTicker := time.NewTicker(time.Second)
|
||||
doneARun := false
|
||||
for {
|
||||
select {
|
||||
case <-bt.shutdown:
|
||||
return nil
|
||||
case <-timeoutTimer.C:
|
||||
return errLiveDataTimeout
|
||||
case <-processEventTicker.C:
|
||||
for e := bt.EventQueue.NextEvent(); ; e = bt.EventQueue.NextEvent() {
|
||||
if e == nil {
|
||||
// as live only supports singular currency, just get the proper reference manually
|
||||
var d data.Handler
|
||||
dd := bt.Datas.GetAllData()
|
||||
for k1, v1 := range dd {
|
||||
for k2, v2 := range v1 {
|
||||
for k3 := range v2 {
|
||||
d = dd[k1][k2][k3]
|
||||
}
|
||||
}
|
||||
}
|
||||
de := d.Next()
|
||||
if de == nil {
|
||||
break
|
||||
}
|
||||
bt.EventQueue.AppendEvent(de)
|
||||
doneARun = true
|
||||
continue
|
||||
}
|
||||
err := bt.handleEvent(e)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if doneARun {
|
||||
timeoutTimer = time.NewTimer(time.Minute * 5)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// loadLiveDataLoop is an incomplete function to continuously retrieve exchange data on a loop
|
||||
// from live. Its purpose is to be able to perform strategy analysis against current data
|
||||
func (bt *BackTest) loadLiveDataLoop(resp *kline.DataFromKline, cfg *config.Config, exch gctexchange.IBotExchange, fPair currency.Pair, a asset.Item, dataType int64) {
|
||||
startDate := time.Now()
|
||||
candles, err := live.LoadData(
|
||||
exch,
|
||||
dataType,
|
||||
cfg.DataSettings.Interval,
|
||||
fPair,
|
||||
a)
|
||||
if err != nil {
|
||||
log.Errorf(log.BackTester, "%v. Please check your GoCryptoTrader configuration", err)
|
||||
return
|
||||
}
|
||||
resp.Item = *candles
|
||||
err = bt.loadLiveData(resp, cfg, exch, fPair, a, startDate, dataType)
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
return
|
||||
}
|
||||
|
||||
loadNewDataTicker := time.NewTicker(time.Second * 30)
|
||||
for {
|
||||
select {
|
||||
case <-bt.shutdown:
|
||||
return
|
||||
case <-loadNewDataTicker.C:
|
||||
err = bt.loadLiveData(resp, cfg, exch, fPair, a, startDate, dataType)
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (bt *BackTest) loadLiveData(resp *kline.DataFromKline, cfg *config.Config, exch gctexchange.IBotExchange, fPair currency.Pair, a asset.Item, startDate time.Time, dataType int64) error {
|
||||
candles, err := live.LoadData(
|
||||
exch,
|
||||
dataType,
|
||||
cfg.DataSettings.Interval,
|
||||
fPair,
|
||||
a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp.Item.Candles = append(resp.Item.Candles, candles.Candles...)
|
||||
_, err = exch.FetchOrderbook(fPair, a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp.Item.RemoveDuplicates()
|
||||
resp.Item.SortCandlesByTimestamp(false)
|
||||
if len(candles.Candles) == 0 {
|
||||
return nil
|
||||
}
|
||||
endDate := candles.Candles[len(candles.Candles)-1].Time.Add(cfg.DataSettings.Interval)
|
||||
if resp.Range.Ranges == nil {
|
||||
dataRange := gctkline.CalculateCandleDateRanges(
|
||||
startDate,
|
||||
endDate,
|
||||
gctkline.Interval(cfg.DataSettings.Interval),
|
||||
0,
|
||||
)
|
||||
resp.Range = gctkline.IntervalRangeHolder{
|
||||
Start: gctkline.CreateIntervalTime(startDate),
|
||||
End: gctkline.CreateIntervalTime(endDate),
|
||||
Ranges: dataRange.Ranges,
|
||||
}
|
||||
}
|
||||
var intervalData []gctkline.IntervalData
|
||||
for i := range candles.Candles {
|
||||
intervalData = append(intervalData, gctkline.IntervalData{
|
||||
Start: gctkline.CreateIntervalTime(candles.Candles[i].Time),
|
||||
End: gctkline.CreateIntervalTime(candles.Candles[i].Time.Add(cfg.DataSettings.Interval)),
|
||||
HasData: true,
|
||||
})
|
||||
}
|
||||
resp.Range.Ranges[0].Intervals = intervalData
|
||||
if len(intervalData) > 0 {
|
||||
resp.Range.Ranges[0].End = intervalData[len(intervalData)-1].End
|
||||
}
|
||||
|
||||
resp.Append(candles)
|
||||
bt.Reports.AddKlineItem(&resp.Item)
|
||||
log.Info(log.BackTester, "sleeping for 30 seconds before checking for new candle data")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop shuts down the live data loop
|
||||
func (bt *BackTest) Stop() {
|
||||
close(bt.shutdown)
|
||||
}
|
||||
554
backtester/backtest/backtest_test.go
Normal file
554
backtester/backtest/backtest_test.go
Normal file
@@ -0,0 +1,554 @@
|
||||
package backtest
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/config"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/eventholder"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/risk"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/size"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/statistics"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/statistics/currencystatistics"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/strategies"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/strategies/base"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/strategies/dollarcostaverage"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/report"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/engine"
|
||||
gctexchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
)
|
||||
|
||||
const testExchange = "binance"
|
||||
|
||||
func newBotWithExchange() (*engine.Engine, gctexchange.IBotExchange) {
|
||||
bot, err := engine.NewFromSettings(&engine.Settings{
|
||||
ConfigFile: filepath.Join("..", "..", "testdata", "configtest.json"),
|
||||
EnableDryRun: true,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = bot.LoadExchange(testExchange, false, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
exch := bot.GetExchangeByName(testExchange)
|
||||
if exch == nil {
|
||||
log.Fatal("expected not nil")
|
||||
}
|
||||
return bot, exch
|
||||
}
|
||||
|
||||
func TestNewFromConfig(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := NewFromConfig(nil, "", "", nil)
|
||||
if err == nil {
|
||||
t.Error("expected error for nil config")
|
||||
}
|
||||
|
||||
cfg := &config.Config{
|
||||
GoCryptoTraderConfigPath: filepath.Join("..", "..", "testdata", "configtest.json"),
|
||||
}
|
||||
_, err = NewFromConfig(cfg, "", "", nil)
|
||||
if !errors.Is(err, errNilBot) {
|
||||
t.Errorf("expected: %v, received %v", errNilBot, err)
|
||||
}
|
||||
|
||||
bot, _ := newBotWithExchange()
|
||||
_, err = NewFromConfig(cfg, "", "", bot)
|
||||
if !errors.Is(err, config.ErrNoCurrencySettings) {
|
||||
t.Errorf("expected: %v, received %v", config.ErrNoCurrencySettings, err)
|
||||
}
|
||||
|
||||
cfg.CurrencySettings = []config.CurrencySettings{
|
||||
{
|
||||
ExchangeName: "test",
|
||||
Base: "test",
|
||||
Quote: "test",
|
||||
},
|
||||
}
|
||||
_, err = NewFromConfig(cfg, "", "", bot)
|
||||
if !errors.Is(err, config.ErrBadInitialFunds) {
|
||||
t.Errorf("expected: %v, received %v", config.ErrBadInitialFunds, err)
|
||||
}
|
||||
|
||||
cfg.CurrencySettings[0].InitialFunds = 1337
|
||||
_, err = NewFromConfig(cfg, "", "", bot)
|
||||
if !errors.Is(err, config.ErrUnsetAsset) {
|
||||
t.Errorf("expected: %v, received %v", config.ErrUnsetAsset, err)
|
||||
}
|
||||
|
||||
cfg.CurrencySettings[0].Asset = asset.Spot.String()
|
||||
_, err = NewFromConfig(cfg, "", "", bot)
|
||||
if !errors.Is(err, engine.ErrExchangeNotFound) {
|
||||
t.Errorf("expected: %v, received %v", engine.ErrExchangeNotFound, err)
|
||||
}
|
||||
|
||||
cfg.CurrencySettings[0].ExchangeName = testExchange
|
||||
_, err = NewFromConfig(cfg, "", "", bot)
|
||||
if !errors.Is(err, errNoDataSource) {
|
||||
t.Errorf("expected: %v, received %v", errNoDataSource, err)
|
||||
}
|
||||
|
||||
cfg.CurrencySettings[0].Base = "BTC"
|
||||
cfg.CurrencySettings[0].Quote = "USDT"
|
||||
|
||||
cfg.DataSettings.APIData = &config.APIData{
|
||||
StartDate: time.Time{},
|
||||
EndDate: time.Time{},
|
||||
}
|
||||
|
||||
_, err = NewFromConfig(cfg, "", "", bot)
|
||||
if err != nil && !strings.Contains(err.Error(), "unrecognised dataType") {
|
||||
t.Error(err)
|
||||
}
|
||||
cfg.DataSettings.DataType = common.CandleStr
|
||||
_, err = NewFromConfig(cfg, "", "", bot)
|
||||
if !errors.Is(err, config.ErrStartEndUnset) {
|
||||
t.Errorf("expected: %v, received %v", config.ErrStartEndUnset, err)
|
||||
}
|
||||
|
||||
cfg.DataSettings.APIData.StartDate = time.Now().Add(-time.Hour)
|
||||
cfg.DataSettings.APIData.EndDate = time.Now()
|
||||
cfg.DataSettings.APIData.InclusiveEndDate = true
|
||||
_, err = NewFromConfig(cfg, "", "", bot)
|
||||
if !errors.Is(err, errIntervalUnset) {
|
||||
t.Errorf("expected: %v, received %v", errIntervalUnset, err)
|
||||
}
|
||||
|
||||
cfg.DataSettings.Interval = gctkline.FifteenMin.Duration()
|
||||
|
||||
_, err = NewFromConfig(cfg, "", "", bot)
|
||||
if !errors.Is(err, base.ErrStrategyNotFound) {
|
||||
t.Errorf("expected: %v, received %v", base.ErrStrategyNotFound, err)
|
||||
}
|
||||
|
||||
cfg.StrategySettings = config.StrategySettings{
|
||||
Name: dollarcostaverage.Name,
|
||||
CustomSettings: map[string]interface{}{
|
||||
"hello": "moto",
|
||||
},
|
||||
}
|
||||
cfg.CurrencySettings[0].MakerFee = 1337
|
||||
cfg.CurrencySettings[0].TakerFee = 1337
|
||||
_, err = NewFromConfig(cfg, "", "", bot)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadData(t *testing.T) {
|
||||
t.Parallel()
|
||||
cfg := &config.Config{
|
||||
GoCryptoTraderConfigPath: filepath.Join("..", "..", "testdata", "configtest.json"),
|
||||
}
|
||||
cfg.CurrencySettings = []config.CurrencySettings{
|
||||
{
|
||||
ExchangeName: "test",
|
||||
Asset: "test",
|
||||
Base: "test",
|
||||
Quote: "test",
|
||||
},
|
||||
}
|
||||
cfg.CurrencySettings[0].ExchangeName = testExchange
|
||||
cfg.CurrencySettings[0].Asset = asset.Spot.String()
|
||||
cfg.CurrencySettings[0].Base = "BTC"
|
||||
cfg.CurrencySettings[0].Quote = "USDT"
|
||||
cfg.CurrencySettings[0].InitialFunds = 1337
|
||||
cfg.DataSettings.APIData = &config.APIData{
|
||||
StartDate: time.Time{},
|
||||
EndDate: time.Time{},
|
||||
}
|
||||
cfg.DataSettings.APIData.StartDate = time.Now().Add(-time.Hour)
|
||||
cfg.DataSettings.APIData.EndDate = time.Now()
|
||||
cfg.DataSettings.Interval = gctkline.FifteenMin.Duration()
|
||||
cfg.DataSettings.DataType = common.CandleStr
|
||||
cfg.StrategySettings = config.StrategySettings{
|
||||
Name: dollarcostaverage.Name,
|
||||
CustomSettings: map[string]interface{}{
|
||||
"hello": "moto",
|
||||
},
|
||||
}
|
||||
cfg.CurrencySettings[0].MakerFee = 1337
|
||||
cfg.CurrencySettings[0].TakerFee = 1337
|
||||
bot, exch := newBotWithExchange()
|
||||
|
||||
_, err := NewFromConfig(cfg, "", "", bot)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
bt := BackTest{
|
||||
Reports: &report.Data{},
|
||||
}
|
||||
|
||||
cp := currency.NewPair(currency.BTC, currency.USDT)
|
||||
_, err = bt.loadData(cfg, exch, cp, asset.Spot)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
cfg.DataSettings.APIData = nil
|
||||
cfg.DataSettings.DatabaseData = &config.DatabaseData{
|
||||
StartDate: time.Now().Add(-time.Hour),
|
||||
EndDate: time.Now(),
|
||||
ConfigOverride: nil,
|
||||
InclusiveEndDate: true,
|
||||
}
|
||||
cfg.DataSettings.DataType = common.CandleStr
|
||||
cfg.DataSettings.Interval = gctkline.FifteenMin.Duration()
|
||||
|
||||
bt.Bot = bot
|
||||
_, err = bt.loadData(cfg, exch, cp, asset.Spot)
|
||||
if err != nil && !strings.Contains(err.Error(), "unable to retrieve data from GoCryptoTrader database") {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
cfg.DataSettings.DatabaseData = nil
|
||||
cfg.DataSettings.CSVData = &config.CSVData{
|
||||
FullPath: "test",
|
||||
}
|
||||
_, err = bt.loadData(cfg, exch, cp, asset.Spot)
|
||||
if err != nil && !strings.Contains(err.Error(), "The system cannot find the file specified.") {
|
||||
t.Error(err)
|
||||
}
|
||||
cfg.DataSettings.CSVData = nil
|
||||
cfg.DataSettings.LiveData = &config.LiveData{
|
||||
APIKeyOverride: "test",
|
||||
APISecretOverride: "test",
|
||||
APIClientIDOverride: "test",
|
||||
API2FAOverride: "test",
|
||||
RealOrders: true,
|
||||
}
|
||||
_, err = bt.loadData(cfg, exch, cp, asset.Spot)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDatabaseData(t *testing.T) {
|
||||
t.Parallel()
|
||||
cp := currency.NewPair(currency.BTC, currency.USDT)
|
||||
_, err := loadDatabaseData(nil, "", cp, "", -1)
|
||||
if err != nil && !strings.Contains(err.Error(), "nil config data received") {
|
||||
t.Error(err)
|
||||
}
|
||||
cfg := &config.Config{
|
||||
DataSettings: config.DataSettings{
|
||||
DatabaseData: &config.DatabaseData{
|
||||
StartDate: time.Time{},
|
||||
EndDate: time.Time{},
|
||||
ConfigOverride: nil,
|
||||
},
|
||||
},
|
||||
GoCryptoTraderConfigPath: filepath.Join("..", "..", "testdata", "configtest.json"),
|
||||
}
|
||||
_, err = loadDatabaseData(cfg, "", cp, "", -1)
|
||||
if !errors.Is(err, config.ErrStartEndUnset) {
|
||||
t.Errorf("expected %v, received %v", config.ErrStartEndUnset, err)
|
||||
}
|
||||
cfg.DataSettings.DatabaseData.StartDate = time.Now().Add(-time.Hour)
|
||||
cfg.DataSettings.DatabaseData.EndDate = time.Now()
|
||||
_, err = loadDatabaseData(cfg, "", cp, "", -1)
|
||||
if !errors.Is(err, errIntervalUnset) {
|
||||
t.Errorf("expected %v, received %v", errIntervalUnset, err)
|
||||
}
|
||||
|
||||
cfg.DataSettings.Interval = gctkline.OneDay.Duration()
|
||||
_, err = loadDatabaseData(cfg, "", cp, "", -1)
|
||||
if err != nil && !strings.Contains(err.Error(), "could not retrieve database data") {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
cfg.DataSettings.DataType = common.CandleStr
|
||||
_, err = loadDatabaseData(cfg, "", cp, "", common.DataCandle)
|
||||
if err != nil && !strings.Contains(err.Error(), "exchange, base, quote, asset, interval, start & end cannot be empty") {
|
||||
t.Error(err)
|
||||
}
|
||||
_, err = loadDatabaseData(cfg, testExchange, cp, asset.Spot, common.DataCandle)
|
||||
if err != nil && !strings.Contains(err.Error(), "database support is disabled") {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadLiveData(t *testing.T) {
|
||||
t.Parallel()
|
||||
err := loadLiveData(nil, nil)
|
||||
if !errors.Is(err, common.ErrNilArguments) {
|
||||
t.Error(err)
|
||||
}
|
||||
cfg := &config.Config{
|
||||
GoCryptoTraderConfigPath: filepath.Join("..", "..", "testdata", "configtest.json"),
|
||||
}
|
||||
err = loadLiveData(cfg, nil)
|
||||
if !errors.Is(err, common.ErrNilArguments) {
|
||||
t.Error(err)
|
||||
}
|
||||
b := &gctexchange.Base{
|
||||
Name: testExchange,
|
||||
API: gctexchange.API{
|
||||
AuthenticatedSupport: false,
|
||||
AuthenticatedWebsocketSupport: false,
|
||||
PEMKeySupport: false,
|
||||
Credentials: struct {
|
||||
Key string
|
||||
Secret string
|
||||
ClientID string
|
||||
PEMKey string
|
||||
}{},
|
||||
CredentialsValidator: struct {
|
||||
RequiresPEM bool
|
||||
RequiresKey bool
|
||||
RequiresSecret bool
|
||||
RequiresClientID bool
|
||||
RequiresBase64DecodeSecret bool
|
||||
}{
|
||||
RequiresPEM: true,
|
||||
RequiresKey: true,
|
||||
RequiresSecret: true,
|
||||
RequiresClientID: true,
|
||||
RequiresBase64DecodeSecret: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
err = loadLiveData(cfg, b)
|
||||
if !errors.Is(err, common.ErrNilArguments) {
|
||||
t.Error(err)
|
||||
}
|
||||
cfg.DataSettings.LiveData = &config.LiveData{
|
||||
|
||||
RealOrders: true,
|
||||
}
|
||||
cfg.DataSettings.Interval = gctkline.OneDay.Duration()
|
||||
cfg.DataSettings.DataType = common.CandleStr
|
||||
err = loadLiveData(cfg, b)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
cfg.DataSettings.LiveData.APIKeyOverride = "1234"
|
||||
cfg.DataSettings.LiveData.APISecretOverride = "1234"
|
||||
cfg.DataSettings.LiveData.APIClientIDOverride = "1234"
|
||||
cfg.DataSettings.LiveData.API2FAOverride = "1234"
|
||||
err = loadLiveData(cfg, b)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReset(t *testing.T) {
|
||||
t.Parallel()
|
||||
bt := BackTest{
|
||||
Bot: &engine.Engine{},
|
||||
shutdown: make(chan struct{}),
|
||||
Datas: &data.HandlerPerCurrency{},
|
||||
Strategy: &dollarcostaverage.Strategy{},
|
||||
Portfolio: &portfolio.Portfolio{},
|
||||
Exchange: &exchange.Exchange{},
|
||||
Statistic: &statistics.Statistic{},
|
||||
EventQueue: &eventholder.Holder{},
|
||||
Reports: &report.Data{},
|
||||
}
|
||||
bt.Reset()
|
||||
if bt.Bot != nil {
|
||||
t.Error("expected nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFullCycle(t *testing.T) {
|
||||
t.Parallel()
|
||||
ex := testExchange
|
||||
cp := currency.NewPair(currency.BTC, currency.USD)
|
||||
a := asset.Spot
|
||||
tt := time.Now()
|
||||
|
||||
stats := &statistics.Statistic{}
|
||||
stats.ExchangeAssetPairStatistics = make(map[string]map[asset.Item]map[currency.Pair]*currencystatistics.CurrencyStatistic)
|
||||
stats.ExchangeAssetPairStatistics[ex] = make(map[asset.Item]map[currency.Pair]*currencystatistics.CurrencyStatistic)
|
||||
stats.ExchangeAssetPairStatistics[ex][a] = make(map[currency.Pair]*currencystatistics.CurrencyStatistic)
|
||||
|
||||
port, err := portfolio.Setup(&size.Size{
|
||||
BuySide: config.MinMax{},
|
||||
SellSide: config.MinMax{},
|
||||
}, &risk.Risk{}, 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
_, err = port.SetupCurrencySettingsMap(ex, a, cp)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = port.SetInitialFunds(ex, a, cp, 1333337)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
bot, _ := newBotWithExchange()
|
||||
|
||||
bt := BackTest{
|
||||
Bot: bot,
|
||||
shutdown: nil,
|
||||
Datas: &data.HandlerPerCurrency{},
|
||||
Strategy: &dollarcostaverage.Strategy{},
|
||||
Portfolio: port,
|
||||
Exchange: &exchange.Exchange{},
|
||||
Statistic: stats,
|
||||
EventQueue: &eventholder.Holder{},
|
||||
Reports: &report.Data{},
|
||||
}
|
||||
|
||||
bt.Datas.Setup()
|
||||
k := kline.DataFromKline{
|
||||
Item: gctkline.Item{
|
||||
Exchange: ex,
|
||||
Pair: cp,
|
||||
Asset: a,
|
||||
Interval: gctkline.FifteenMin,
|
||||
Candles: []gctkline.Candle{{
|
||||
Time: tt,
|
||||
Open: 1337,
|
||||
High: 1337,
|
||||
Low: 1337,
|
||||
Close: 1337,
|
||||
Volume: 1337,
|
||||
}},
|
||||
},
|
||||
Base: data.Base{},
|
||||
Range: gctkline.IntervalRangeHolder{
|
||||
Start: gctkline.CreateIntervalTime(tt),
|
||||
End: gctkline.CreateIntervalTime(tt.Add(gctkline.FifteenMin.Duration())),
|
||||
Ranges: []gctkline.IntervalRange{
|
||||
{
|
||||
Start: gctkline.CreateIntervalTime(tt),
|
||||
End: gctkline.CreateIntervalTime(tt.Add(gctkline.FifteenMin.Duration())),
|
||||
Intervals: []gctkline.IntervalData{
|
||||
{
|
||||
Start: gctkline.CreateIntervalTime(tt),
|
||||
End: gctkline.CreateIntervalTime(tt.Add(gctkline.FifteenMin.Duration())),
|
||||
HasData: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
err = k.Load()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
bt.Datas.SetDataForCurrency(ex, a, cp, &k)
|
||||
|
||||
err = bt.Run()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStop(t *testing.T) {
|
||||
t.Parallel()
|
||||
bt := BackTest{shutdown: make(chan struct{})}
|
||||
bt.Stop()
|
||||
}
|
||||
|
||||
func TestFullCycleMulti(t *testing.T) {
|
||||
t.Parallel()
|
||||
ex := testExchange
|
||||
cp := currency.NewPair(currency.BTC, currency.USD)
|
||||
a := asset.Spot
|
||||
tt := time.Now()
|
||||
|
||||
stats := &statistics.Statistic{}
|
||||
stats.ExchangeAssetPairStatistics = make(map[string]map[asset.Item]map[currency.Pair]*currencystatistics.CurrencyStatistic)
|
||||
stats.ExchangeAssetPairStatistics[ex] = make(map[asset.Item]map[currency.Pair]*currencystatistics.CurrencyStatistic)
|
||||
stats.ExchangeAssetPairStatistics[ex][a] = make(map[currency.Pair]*currencystatistics.CurrencyStatistic)
|
||||
|
||||
port, err := portfolio.Setup(&size.Size{
|
||||
BuySide: config.MinMax{},
|
||||
SellSide: config.MinMax{},
|
||||
}, &risk.Risk{}, 0)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
_, err = port.SetupCurrencySettingsMap(ex, a, cp)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = port.SetInitialFunds(ex, a, cp, 1333337)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
bot, _ := newBotWithExchange()
|
||||
|
||||
bt := BackTest{
|
||||
Bot: bot,
|
||||
shutdown: nil,
|
||||
Datas: &data.HandlerPerCurrency{},
|
||||
Portfolio: port,
|
||||
Exchange: &exchange.Exchange{},
|
||||
Statistic: stats,
|
||||
EventQueue: &eventholder.Holder{},
|
||||
Reports: &report.Data{},
|
||||
}
|
||||
|
||||
bt.Strategy, err = strategies.LoadStrategyByName(dollarcostaverage.Name, true)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
bt.Datas.Setup()
|
||||
k := kline.DataFromKline{
|
||||
Item: gctkline.Item{
|
||||
Exchange: ex,
|
||||
Pair: cp,
|
||||
Asset: a,
|
||||
Interval: gctkline.FifteenMin,
|
||||
Candles: []gctkline.Candle{{
|
||||
Time: tt,
|
||||
Open: 1337,
|
||||
High: 1337,
|
||||
Low: 1337,
|
||||
Close: 1337,
|
||||
Volume: 1337,
|
||||
}},
|
||||
},
|
||||
Base: data.Base{},
|
||||
Range: gctkline.IntervalRangeHolder{
|
||||
Start: gctkline.CreateIntervalTime(tt),
|
||||
End: gctkline.CreateIntervalTime(tt.Add(gctkline.FifteenMin.Duration())),
|
||||
Ranges: []gctkline.IntervalRange{
|
||||
{
|
||||
Start: gctkline.CreateIntervalTime(tt),
|
||||
End: gctkline.CreateIntervalTime(tt.Add(gctkline.FifteenMin.Duration())),
|
||||
Intervals: []gctkline.IntervalData{
|
||||
{
|
||||
Start: gctkline.CreateIntervalTime(tt),
|
||||
End: gctkline.CreateIntervalTime(tt.Add(gctkline.FifteenMin.Duration())),
|
||||
HasData: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
err = k.Load()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
bt.Datas.SetDataForCurrency(ex, a, cp, &k)
|
||||
|
||||
err = bt.Run()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
40
backtester/backtest/backtest_types.go
Normal file
40
backtester/backtest/backtest_types.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package backtest
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/eventholder"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/statistics"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/strategies"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/report"
|
||||
"github.com/thrasher-corp/gocryptotrader/engine"
|
||||
)
|
||||
|
||||
var (
|
||||
errNilConfig = errors.New("unable to setup backtester with nil config")
|
||||
errNilBot = errors.New("unable to setup backtester without a loaded GoCryptoTrader bot")
|
||||
errInvalidConfigAsset = errors.New("invalid asset in config")
|
||||
errInvalidConfigCurrency = errors.New("invalid currency in config")
|
||||
errAmbiguousDataSource = errors.New("ambiguous settings received. Only one data type can be set")
|
||||
errNoDataSource = errors.New("no data settings set in config")
|
||||
errIntervalUnset = errors.New("candle interval unset")
|
||||
errUnhandledDatatype = errors.New("unhandled datatype")
|
||||
errLiveDataTimeout = errors.New("no data returned in 5 minutes, shutting down")
|
||||
)
|
||||
|
||||
// BackTest is the main holder of all backtesting functionality
|
||||
type BackTest struct {
|
||||
Bot *engine.Engine
|
||||
hasHandledEvent bool
|
||||
shutdown chan struct{}
|
||||
Datas data.Holder
|
||||
Strategy strategies.Handler
|
||||
Portfolio portfolio.Handler
|
||||
Exchange exchange.ExecutionHandler
|
||||
Statistic statistics.Handler
|
||||
EventQueue eventholder.EventHolder
|
||||
Reports report.Handler
|
||||
}
|
||||
44
backtester/common/README.md
Normal file
44
backtester/common/README.md
Normal file
@@ -0,0 +1,44 @@
|
||||
# GoCryptoTrader Backtester: Common package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/common)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This common package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Common package overview
|
||||
|
||||
Common contains some basic data types which are used throughout.
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
BIN
backtester/common/backtester.png
Normal file
BIN
backtester/common/backtester.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 84 KiB |
15
backtester/common/common.go
Normal file
15
backtester/common/common.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package common
|
||||
|
||||
import "fmt"
|
||||
|
||||
// DataTypeToInt converts the config string value into an int
|
||||
func DataTypeToInt(dataType string) (int64, error) {
|
||||
switch dataType {
|
||||
case CandleStr:
|
||||
return DataCandle, nil
|
||||
case TradeStr:
|
||||
return DataTrade, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("unrecognised dataType '%v'", dataType)
|
||||
}
|
||||
}
|
||||
107
backtester/common/common_types.go
Normal file
107
backtester/common/common_types.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
const (
|
||||
// DoNothing is an explicit signal for the backtester to not perform an action
|
||||
// based upon indicator results
|
||||
DoNothing order.Side = "DO NOTHING"
|
||||
// CouldNotBuy is flagged when a BUY signal is raised in the strategy/signal phase, but the
|
||||
// portfolio manager or exchange cannot place an order
|
||||
CouldNotBuy order.Side = "COULD NOT BUY"
|
||||
// CouldNotSell is flagged when a SELL signal is raised in the strategy/signal phase, but the
|
||||
// portfolio manager or exchange cannot place an order
|
||||
CouldNotSell order.Side = "COULD NOT SELL"
|
||||
// MissingData is signalled during the strategy/signal phase when data has been identified as missing
|
||||
// No buy or sell events can occur
|
||||
MissingData order.Side = "MISSING DATA"
|
||||
// CandleStr is a config readable data type to tell the backtester to retrieve candle data
|
||||
CandleStr = "candle"
|
||||
// TradeStr is a config readable data type to tell the backtester to retrieve trade data
|
||||
TradeStr = "trade"
|
||||
)
|
||||
|
||||
// DataCandle is an int64 representation of a candle data type
|
||||
const (
|
||||
DataCandle = iota
|
||||
DataTrade
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrNilArguments is a common error response to highlight that nils were passed in
|
||||
// when they should not have been
|
||||
ErrNilArguments = errors.New("received nil argument(s)")
|
||||
ErrNilEvent = errors.New("nil event received")
|
||||
)
|
||||
|
||||
// EventHandler interface implements required GetTime() & Pair() return
|
||||
type EventHandler interface {
|
||||
GetOffset() int64
|
||||
SetOffset(int64)
|
||||
IsEvent() bool
|
||||
GetTime() time.Time
|
||||
Pair() currency.Pair
|
||||
GetExchange() string
|
||||
GetInterval() kline.Interval
|
||||
GetAssetType() asset.Item
|
||||
|
||||
GetReason() string
|
||||
AppendReason(string)
|
||||
}
|
||||
|
||||
// DataEventHandler interface used for loading and interacting with Data
|
||||
type DataEventHandler interface {
|
||||
EventHandler
|
||||
ClosePrice() float64
|
||||
HighPrice() float64
|
||||
LowPrice() float64
|
||||
OpenPrice() float64
|
||||
}
|
||||
|
||||
// Directioner dictates the side of an order
|
||||
type Directioner interface {
|
||||
SetDirection(side order.Side)
|
||||
GetDirection() order.Side
|
||||
}
|
||||
|
||||
// ASCIILogo is a sweet logo that is optionally printed to the command line window
|
||||
const ASCIILogo = `
|
||||
|
||||
@@@@@@@@@@@@@@@@@
|
||||
@@@@@@@@@@@@@@@@@@@@@@@ ,,,,,,
|
||||
@@@@@@@@,,,,, @@@@@@@@@,,,,,,,,
|
||||
@@@@@@@@,,,,,,, @@@@@@@,,,,,,,
|
||||
@@@@@@(,,,,,,,, ,,@@@@@@@,,,,,,
|
||||
,,@@@@@@,,,,,,,,, #,,,,,,,,,,,,,,,,,
|
||||
,,,,*@@@@@@,,,,,,,,,,,,,,,,,,,,,,,,,,%%%%%%%
|
||||
,,,,,,,*@@@@@@,,,,,,,,,,,,,,%%%%%,,,,,,%%%%%%%%
|
||||
,,,,,,,,*@@@@@@,,,,,,,,,,,%%%%%%%%%%%%%%%%%%#%%
|
||||
,,,,,,*@@@@@@,,,,,,,,,%%%,,,,,%%%%%%%%,,,,,
|
||||
,,,*@@@@@@,,,,,,%%, ,,,,,,,@*%%,@,,,,,,
|
||||
*@@@@@@,,,,,,,,, ,,,,@@@@@@,,,,,,
|
||||
@@@@@@,,,,,,,,, @@@@@@@,,,,,,
|
||||
@@@@@@@@,,,,,,, @@@@@@@,,,,,,,
|
||||
@@@@@@@@@,,,, @@@@@@@@@#,,,,,,,
|
||||
@@@@@@@@@@@@@@@@@@@@@@@ *,,,,
|
||||
@@@@@@@@@@@@@@@@
|
||||
|
||||
______ ______ __ ______ __
|
||||
/ ____/___ / ____/______ ______ / /_____/_ __/________ _____/ /__ _____
|
||||
/ / __/ __ \/ / / ___/ / / / __ \/ __/ __ \/ / / ___/ __ / __ / _ \/ ___/
|
||||
/ /_/ / /_/ / /___/ / / /_/ / /_/ / /_/ /_/ / / / / / /_/ / /_/ / __/ /
|
||||
\____/\____/\____/_/ \__, / .___/\__/\____/_/ /_/ \__,_/\__,_/\___/_/
|
||||
/___/
|
||||
____ __ __ __
|
||||
/ __ )____ ______/ /__/ /____ _____/ /____ _____
|
||||
/ __ / __ / ___/ //_/ __/ _ \/ ___/ __/ _ \/ ___/
|
||||
/ /_/ / /_/ / /__/ ,< / /_/ __(__ ) /_/ __/ /
|
||||
/_____/\__,_/\___/_/|_|\__/\___/____/\__/\___/_/
|
||||
`
|
||||
167
backtester/config/README.md
Normal file
167
backtester/config/README.md
Normal file
@@ -0,0 +1,167 @@
|
||||
# GoCryptoTrader Backtester: Config package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/config)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This config package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Config package overview
|
||||
|
||||
### What does the config package do?
|
||||
The config package contains a set of structs which allow for the customisation of the GoCryptoTrader Backtester when running.
|
||||
The GoCryptoTrader Backtester runs from reading config files (`.strat` files by default under `/examples`).
|
||||
|
||||
|
||||
### What does Simultaneous Processing mean?
|
||||
GoCryptoTrader Backtester config files may contain multiple `ExchangeSettings` which defined exchange, asset and currency pairs to iterate through a period of time.
|
||||
|
||||
If there are multiple entries to `ExchangeSettings` and SimultaneousProcessing is disabled, then each individual exchange, asset and currency pair candle event is evaluated individually and does not know about other exchange, asset and currency pair data events. It is a way to test a singular strategy against multiple assets simultaneously. But it isn't defined as Simultaneous Processing
|
||||
Simultaneous Signal Processing is a setting which allows multiple `ExchangeSettings` data events for a candle event to be considered simultaneously. This means that you can check if the price of BTC-USDT is 5% greater on Binance than it is on Kraken and choose to make signal a BUY event for Kraken and not Binance.
|
||||
|
||||
It allows for complex strategical decisions to be made when you consider the scope of the entire market at a given time, rather than in a vacuum when SimultaneousSignalProcessing is disabled.
|
||||
|
||||
### How do I customise the GoCryptoTrader Backtester?
|
||||
See below for a set of tables and fields, expected values and what they can do
|
||||
|
||||
#### Config
|
||||
|
||||
| Key | Description |
|
||||
| --- | ------|
|
||||
| Nickname | A nickname for the specific config. When running multiple variants of the same strategy, use the nickname to help differentiate between runs |
|
||||
| Goal | A description of what you would hope the outcome to be. When verifying output, you can review and confirm whether the strategy met that goal |
|
||||
| CurrencySettings | Currency settings is an array of settings for each individual currency you wish to run the strategy against. |
|
||||
| StrategySettings | Select which strategy to run, what custom settings to load and whether the strategy can assess multiple currencies at once to make more in-depth decisions |
|
||||
| PortfolioSettings | Contains a list of global rules for the portfolio manager. CurrencySettings contain their own rules on things like how big a position is allowable, the portfolio manager rules are the same, but override any individual currency's settings |
|
||||
| StatisticSettings | Contains settings that impact statistics calculation. Such as the risk-free rate for the sharpe ratio |
|
||||
| GoCryptoTraderConfigPath | The filepath for the location of GoCryptoTrader's config path. The Backtester utilises settings from GoCryptoTrader. If unset, will utilise the default filepath via `config.DefaultFilePath`, implemented [here](/config/config.go#L1460) |
|
||||
|
||||
#### Currency Settings
|
||||
|
||||
| Key | Description | Example |
|
||||
| --- | ------- | ----- |
|
||||
| ExchangeName | The exchange to load. See [here](https://github.com/thrasher-corp/gocryptotrader/blob/master/README.md) for a list of supported exchanges | `Binance` |
|
||||
| Asset | The asset type. Typically, this will be `spot`, however, see [this package](https://github.com/thrasher-corp/gocryptotrader/blob/master/exchanges/asset/asset.go) for the various asset types GoCryptoTrader supports| `spot` |
|
||||
| Base | The base of a currency | `BTC` |
|
||||
| Quote | The quote of a currency | `USDT` |
|
||||
| InitialFunds | The funds that the GoCryptoTraderBacktester has for the specific currency | `10000` |
|
||||
| Leverage | This struct defines the leverage rules that this specific currency setting must abide by | `1` |
|
||||
| BuySide | This struct defines the buying side rules this specific currency setting must abide by such as maximum purchase amount | - |
|
||||
| SellSide | This struct defines the selling side rules this specific currency setting must abide by such as maximum selling amount | - |
|
||||
| MinimumSlippagePercent | Is the lower bounds in a random number generated that make purchases more expensive, or sell events less valuable. If this value is 90, then the most a price can be affected is 10% | `90` |
|
||||
| MaximumSlippagePercent | Is the upper bounds in a random number generated that make purchases more expensive, or sell events less valuable. If this value is 99, then the least a price can be affected is 1%. Set both upper and lower to 100 to have no randomness applied to purchase events | `100` |
|
||||
| MakerFee | The fee to use when sizing and purchasing currency | `0.001` |
|
||||
| TakerFee | Unused fee for when an order is placed in the orderbook, rather than taken from the orderbook | `0.002` |
|
||||
| MaximumHoldingsRatio | When multiple currency settings are used, you may set a maximum holdings ratio to prevent having too large a stake in a single currency | `0.5` |
|
||||
|
||||
#### Strategy Settings
|
||||
|
||||
| Key | Description | Example |
|
||||
| --- | ------- | --- |
|
||||
| Name | The strategy to use. | `rsi` |
|
||||
| UsesSimultaneousProcessing | This denotes whether multiple currencies are processed simultaneously with the strategy function `OnSimultaneousSignals`. Eg If you have multiple CurrencySettings and only wish to purchase BTC-USDT when XRP-DOGE is 1337, this setting is useful as you can analyse both signal events to output a purchase call for BTC. | `true` |
|
||||
| CustomSettings | This is a map where you can enter custom settings for a strategy. The RSI strategy allows for customisation of the upper, lower and length variables to allow you to change them from 70, 30 and 14 respectively to 69, 36, 12 | `"custom-settings": { "rsi-high": 70, "rsi-low": 30, "rsi-period": 14 } ` |
|
||||
|
||||
#### PortfolioSettings
|
||||
|
||||
| Key | Description |
|
||||
| --- | ------- |
|
||||
| Leverage | This struct defines the leverage rules that this specific currency setting must abide by |
|
||||
| BuySide | This struct defines the buying side rules this specific currency setting must abide by such as maximum purchase amount |
|
||||
| SellSide | This struct defines the selling side rules this specific currency setting must abide by such as maximum selling amount |
|
||||
|
||||
#### StatisticsSettings
|
||||
|
||||
| Key | Description | Example |
|
||||
| --- | ----------- | ------- |
|
||||
| RiskFreeRate | The risk free rate used in the calculation of sharpe and sortino ratios | `0.03` |
|
||||
|
||||
#### APIData
|
||||
|
||||
| Key | Description | Example |
|
||||
| --- | ----------- | ------- |
|
||||
| DataType | Choose whether `candle` or `trade` data is used. If trades are used, they will be converted to candles | `trade` |
|
||||
| Interval | The candle interval in `time.Duration` format eg set as`15000000000` for a value of `time.Second * 15` | `15000000000` |
|
||||
| StartDate | The start date to retrieve data | `2021-01-23T11:00:00+11:00` |
|
||||
| EndDate | The end date to retrieve data | `2021-01-24T11:00:00+11:00` |
|
||||
| InclusiveEndDate | When enabled, the end date's candle is included in the results. ie `2021-01-24T11:00:00+11:00` with a one hour candle, the final candle will be `2021-01-24T11:00:00+11:00` to `2021-01-24T12:00:00+11:00` | `false` |
|
||||
|
||||
#### CSVData
|
||||
|
||||
| Key | Description | Example |
|
||||
| --- | ----------- | ------- |
|
||||
| DataType | Choose whether `candle` or `trade` data is used. If trades are used, they will be converted to candles | `candle` |
|
||||
| Interval | The candle interval in `time.Duration` format eg set as`15000000000` for a value of `time.Second * 15` | `15000000000` |
|
||||
| FullPath | The file to load | `/data/exchangelist.csv` |
|
||||
|
||||
#### DatabaseData
|
||||
|
||||
| Key | Description | Example |
|
||||
| --- | ----------- | ------- |
|
||||
| DataType | Choose whether `candle` or `trade` data is used. If trades are used, they will be converted to candles | `trade` |
|
||||
| Interval | The candle interval in `time.Duration` format eg set as`15000000000` for a value of `time.Second * 15` | `15000000000` |
|
||||
| StartDate | The start date to retrieve data | `2021-01-23T11:00:00+11:00` |
|
||||
| EndDate | The end date to retrieve data | `2021-01-24T11:00:00+11:00` |
|
||||
| ConfigOverride | Override GoCryptoTrader's config database data with custom settings | `true` |
|
||||
| InclusiveEndDate | When enabled, the end date's candle is included in the results. ie `2021-01-24T11:00:00+11:00` with a one hour candle, the final candle will be `2021-01-24T11:00:00+11:00` to `2021-01-24T12:00:00+11:00` | `false` |
|
||||
|
||||
#### LiveData
|
||||
|
||||
| Key | Description | Example |
|
||||
| --- | ----------- | ------- |
|
||||
| DataType | Choose whether `candle` or `trade` data is used. If trades are used, they will be converted to candles | `candle` |
|
||||
| Interval | The candle interval in `time.Duration` format eg set as`15000000000` for a value of `time.Second * 15` | `15000000000` |
|
||||
| APIKeyOverride | Will set the GoCryptoTrader exchange to use the following API Key | `1234` |
|
||||
| APISecretOverride | Will set the GoCryptoTrader exchange to use the following API Secret | `5678` |
|
||||
| APIClientIDOverride | Will set the GoCryptoTrader exchange to use the following API Client ID | `9012` |
|
||||
| API2FAOverride | Will set the GoCryptoTrader exchange to use the following 2FA seed | `hello-moto` |
|
||||
| RealOrders | Whether to place real orders. You really should never consider using this. Ever ever. | `true` |
|
||||
|
||||
##### Leverage Settings
|
||||
|
||||
| Key | Description | Example |
|
||||
| --- | ----------- | ------- |
|
||||
| CanUseLeverage | Allows the use of leverage | `false` |
|
||||
| MaximumOrdersWithLeverageRatio | If the ratio of leveraged orders for a currency exceeds this, the order cannot be placed | `0.5` |
|
||||
| MaximumLeverageRate | Orders cannot be placed with leverage over this amount | `100` |
|
||||
|
||||
##### Buy/Sell Settings
|
||||
|
||||
| Key | Description | Example |
|
||||
| --- | ----------- | ------- |
|
||||
| MinimumSize | If the order's quantity is below this, the order cannot be placed | `0.1` |
|
||||
| MaximumSize | If the order's quantity is over this amount, it cannot be placed and will be reduced to the maximum amount | `10` |
|
||||
| MaximumTotal | If the order's price * amount exceeds this number, the order cannot be placed and will be reduced to this figure | `1337` |
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
183
backtester/config/config.go
Normal file
183
backtester/config/config.go
Normal file
@@ -0,0 +1,183 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
gctcommon "github.com/thrasher-corp/gocryptotrader/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/common/file"
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
)
|
||||
|
||||
// ReadConfigFromFile will take a config from a path
|
||||
func ReadConfigFromFile(path string) (*Config, error) {
|
||||
if !file.Exists(path) {
|
||||
return nil, errors.New("file not found")
|
||||
}
|
||||
|
||||
fileData, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return LoadConfig(fileData)
|
||||
}
|
||||
|
||||
// LoadConfig unmarshalls byte data into a config struct
|
||||
func LoadConfig(data []byte) (resp *Config, err error) {
|
||||
err = json.Unmarshal(data, &resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// PrintSetting prints relevant settings to the console for easy reading
|
||||
func (c *Config) PrintSetting() {
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------")
|
||||
log.Info(log.BackTester, "------------------Backtester Settings------------------------")
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------")
|
||||
log.Info(log.BackTester, "------------------Strategy Settings--------------------------")
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------")
|
||||
log.Infof(log.BackTester, "Strategy: %s", c.StrategySettings.Name)
|
||||
if len(c.StrategySettings.CustomSettings) > 0 {
|
||||
log.Info(log.BackTester, "Custom strategy variables:")
|
||||
for k, v := range c.StrategySettings.CustomSettings {
|
||||
log.Infof(log.BackTester, "%s: %v", k, v)
|
||||
}
|
||||
} else {
|
||||
log.Info(log.BackTester, "Custom strategy variables: unset")
|
||||
}
|
||||
log.Infof(log.BackTester, "Simultaneous Signal Processing: %v", c.StrategySettings.SimultaneousSignalProcessing)
|
||||
for i := range c.CurrencySettings {
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------")
|
||||
currStr := fmt.Sprintf("------------------%v %v-%v Settings---------------------------------------------------------",
|
||||
c.CurrencySettings[i].Asset,
|
||||
c.CurrencySettings[i].Base,
|
||||
c.CurrencySettings[i].Quote)
|
||||
log.Infof(log.BackTester, currStr[:61])
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------")
|
||||
log.Infof(log.BackTester, "Exchange: %v", c.CurrencySettings[i].ExchangeName)
|
||||
log.Infof(log.BackTester, "Initial funds: %.4f", c.CurrencySettings[i].InitialFunds)
|
||||
log.Infof(log.BackTester, "Maker fee: %.2f", c.CurrencySettings[i].TakerFee)
|
||||
log.Infof(log.BackTester, "Taker fee: %.2f", c.CurrencySettings[i].MakerFee)
|
||||
log.Infof(log.BackTester, "Minimum slippage percent %.2f", c.CurrencySettings[i].MinimumSlippagePercent)
|
||||
log.Infof(log.BackTester, "Maximum slippage percent: %.2f", c.CurrencySettings[i].MaximumSlippagePercent)
|
||||
log.Infof(log.BackTester, "Buy rules: %+v", c.CurrencySettings[i].BuySide)
|
||||
log.Infof(log.BackTester, "Sell rules: %+v", c.CurrencySettings[i].SellSide)
|
||||
log.Infof(log.BackTester, "Leverage rules: %+v", c.CurrencySettings[i].Leverage)
|
||||
}
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------")
|
||||
log.Info(log.BackTester, "------------------Portfolio Settings-------------------------")
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------")
|
||||
log.Infof(log.BackTester, "Buy rules: %+v", c.PortfolioSettings.BuySide)
|
||||
log.Infof(log.BackTester, "Sell rules: %+v", c.PortfolioSettings.SellSide)
|
||||
log.Infof(log.BackTester, "Leverage rules: %+v", c.PortfolioSettings.Leverage)
|
||||
if c.DataSettings.LiveData != nil {
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------")
|
||||
log.Info(log.BackTester, "------------------Live Settings------------------------------")
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------")
|
||||
log.Infof(log.BackTester, "Data type: %v", c.DataSettings.DataType)
|
||||
log.Infof(log.BackTester, "Interval: %v", c.DataSettings.Interval)
|
||||
log.Infof(log.BackTester, "REAL ORDERS: %v", c.DataSettings.LiveData.RealOrders)
|
||||
log.Infof(log.BackTester, "Overriding GCT API settings: %v", c.DataSettings.LiveData.APIClientIDOverride != "")
|
||||
}
|
||||
if c.DataSettings.APIData != nil {
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------")
|
||||
log.Info(log.BackTester, "------------------API Settings-------------------------------")
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------")
|
||||
log.Infof(log.BackTester, "Data type: %v", c.DataSettings.DataType)
|
||||
log.Infof(log.BackTester, "Interval: %v", c.DataSettings.Interval)
|
||||
log.Infof(log.BackTester, "Start date: %v", c.DataSettings.APIData.StartDate.Format(gctcommon.SimpleTimeFormat))
|
||||
log.Infof(log.BackTester, "End date: %v", c.DataSettings.APIData.EndDate.Format(gctcommon.SimpleTimeFormat))
|
||||
}
|
||||
if c.DataSettings.CSVData != nil {
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------")
|
||||
log.Info(log.BackTester, "------------------CSV Settings-------------------------------")
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------")
|
||||
log.Infof(log.BackTester, "Data type: %v", c.DataSettings.DataType)
|
||||
log.Infof(log.BackTester, "Interval: %v", c.DataSettings.Interval)
|
||||
log.Infof(log.BackTester, "CSV file: %v", c.DataSettings.CSVData.FullPath)
|
||||
}
|
||||
if c.DataSettings.DatabaseData != nil {
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------")
|
||||
log.Info(log.BackTester, "------------------Database Settings--------------------------")
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------")
|
||||
log.Infof(log.BackTester, "Data type: %v", c.DataSettings.DataType)
|
||||
log.Infof(log.BackTester, "Interval: %v", c.DataSettings.Interval)
|
||||
log.Infof(log.BackTester, "Start date: %v", c.DataSettings.DatabaseData.StartDate.Format(gctcommon.SimpleTimeFormat))
|
||||
log.Infof(log.BackTester, "End date: %v", c.DataSettings.DatabaseData.EndDate.Format(gctcommon.SimpleTimeFormat))
|
||||
}
|
||||
log.Info(log.BackTester, "-------------------------------------------------------------\n\n")
|
||||
}
|
||||
|
||||
// Validate ensures no one sets bad config values on purpose
|
||||
func (m *MinMax) Validate() {
|
||||
if m.MaximumSize < 0 {
|
||||
m.MaximumSize *= -1
|
||||
log.Warnf(log.BackTester, "invalid maximum size set to %v", m.MaximumSize)
|
||||
}
|
||||
if m.MinimumSize < 0 {
|
||||
m.MinimumSize *= -1
|
||||
log.Warnf(log.BackTester, "invalid minimum size set to %v", m.MinimumSize)
|
||||
}
|
||||
if m.MaximumSize <= m.MinimumSize && m.MinimumSize != 0 && m.MaximumSize != 0 {
|
||||
m.MaximumSize = m.MinimumSize + 1
|
||||
log.Warnf(log.BackTester, "invalid maximum size set to %v", m.MaximumSize)
|
||||
}
|
||||
if m.MaximumTotal < 0 {
|
||||
m.MaximumTotal *= -1
|
||||
log.Warnf(log.BackTester, "invalid maximum total set to %v", m.MaximumTotal)
|
||||
}
|
||||
}
|
||||
|
||||
// ValidateDate checks whether someone has set a date poorly in their config
|
||||
func (c *Config) ValidateDate() error {
|
||||
if c.DataSettings.DatabaseData != nil {
|
||||
if c.DataSettings.DatabaseData.StartDate.IsZero() ||
|
||||
c.DataSettings.DatabaseData.EndDate.IsZero() {
|
||||
return ErrStartEndUnset
|
||||
}
|
||||
if c.DataSettings.DatabaseData.StartDate.After(c.DataSettings.DatabaseData.EndDate) ||
|
||||
c.DataSettings.DatabaseData.StartDate.Equal(c.DataSettings.DatabaseData.EndDate) {
|
||||
return ErrBadDate
|
||||
}
|
||||
}
|
||||
if c.DataSettings.APIData != nil {
|
||||
if c.DataSettings.APIData.StartDate.IsZero() ||
|
||||
c.DataSettings.APIData.EndDate.IsZero() {
|
||||
return ErrStartEndUnset
|
||||
}
|
||||
if c.DataSettings.APIData.StartDate.After(c.DataSettings.APIData.EndDate) ||
|
||||
c.DataSettings.APIData.StartDate.Equal(c.DataSettings.APIData.EndDate) {
|
||||
return ErrBadDate
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateCurrencySettings checks whether someone has set invalid currency setting data in their config
|
||||
func (c *Config) ValidateCurrencySettings() error {
|
||||
if len(c.CurrencySettings) == 0 {
|
||||
return ErrNoCurrencySettings
|
||||
}
|
||||
for i := range c.CurrencySettings {
|
||||
if c.CurrencySettings[i].InitialFunds <= 0 {
|
||||
return ErrBadInitialFunds
|
||||
}
|
||||
if c.CurrencySettings[i].Base == "" {
|
||||
return ErrUnsetCurrency
|
||||
}
|
||||
if c.CurrencySettings[i].Asset == "" {
|
||||
return ErrUnsetAsset
|
||||
}
|
||||
if c.CurrencySettings[i].ExchangeName == "" {
|
||||
return ErrUnsetExchange
|
||||
}
|
||||
if c.CurrencySettings[i].MinimumSlippagePercent < 0 ||
|
||||
c.CurrencySettings[i].MaximumSlippagePercent < 0 ||
|
||||
c.CurrencySettings[i].MinimumSlippagePercent > c.CurrencySettings[i].MaximumSlippagePercent {
|
||||
return ErrBadSlippageRates
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
1005
backtester/config/config_test.go
Normal file
1005
backtester/config/config_test.go
Normal file
File diff suppressed because it is too large
Load Diff
136
backtester/config/config_types.go
Normal file
136
backtester/config/config_types.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
)
|
||||
|
||||
// Errors for config validation
|
||||
var (
|
||||
ErrBadDate = errors.New("start date >= end date, please check your config")
|
||||
ErrNoCurrencySettings = errors.New("no currency settings set in the config")
|
||||
ErrBadInitialFunds = errors.New("initial funds set with invalid data, please check your config")
|
||||
ErrUnsetExchange = errors.New("exchange name unset for currency settings, please check your config")
|
||||
ErrUnsetAsset = errors.New("asset unset for currency settings, please check your config")
|
||||
ErrUnsetCurrency = errors.New("currency unset for currency settings, please check your config")
|
||||
ErrBadSlippageRates = errors.New("invalid slippage rates in currency settings, please check your config")
|
||||
ErrStartEndUnset = errors.New("data start and end dates are invalid, please check your config")
|
||||
)
|
||||
|
||||
// Config defines what is in an individual strategy config
|
||||
type Config struct {
|
||||
Nickname string `json:"nickname"`
|
||||
Goal string `json:"goal"`
|
||||
StrategySettings StrategySettings `json:"strategy-settings"`
|
||||
CurrencySettings []CurrencySettings `json:"currency-settings"`
|
||||
DataSettings DataSettings `json:"data-settings"`
|
||||
PortfolioSettings PortfolioSettings `json:"portfolio-settings"`
|
||||
StatisticSettings StatisticSettings `json:"statistic-settings"`
|
||||
GoCryptoTraderConfigPath string `json:"gocryptotrader-config-path"`
|
||||
}
|
||||
|
||||
// DataSettings is a container for each type of data retrieval setting.
|
||||
// Only ONE can be populated per config
|
||||
type DataSettings struct {
|
||||
Interval time.Duration `json:"interval"`
|
||||
DataType string `json:"data-type"`
|
||||
APIData *APIData `json:"api-data,omitempty"`
|
||||
DatabaseData *DatabaseData `json:"database-data,omitempty"`
|
||||
LiveData *LiveData `json:"live-data,omitempty"`
|
||||
CSVData *CSVData `json:"csv-data,omitempty"`
|
||||
}
|
||||
|
||||
// StrategySettings contains what strategy to load, along with custom settings map
|
||||
// (variables defined per strategy)
|
||||
// along with defining whether the strategy will assess all currencies at once, or individually
|
||||
type StrategySettings struct {
|
||||
Name string `json:"name"`
|
||||
SimultaneousSignalProcessing bool `json:"use-simultaneous-signal-processing"`
|
||||
CustomSettings map[string]interface{} `json:"custom-settings"`
|
||||
}
|
||||
|
||||
// StatisticSettings holds configurable varialbes to adjust ratios where
|
||||
// proper data is currently lacking
|
||||
type StatisticSettings struct {
|
||||
RiskFreeRate float64 `json:"risk-free-rate"`
|
||||
}
|
||||
|
||||
// PortfolioSettings act as a global protector for strategies
|
||||
// these settings will override ExchangeSettings that go against it
|
||||
// and assess the bigger picture
|
||||
type PortfolioSettings struct {
|
||||
Leverage Leverage `json:"leverage"`
|
||||
BuySide MinMax `json:"buy-side"`
|
||||
SellSide MinMax `json:"sell-side"`
|
||||
}
|
||||
|
||||
// Leverage rules are used to allow or limit the use of leverage in orders
|
||||
// when supported
|
||||
type Leverage struct {
|
||||
CanUseLeverage bool `json:"can-use-leverage"`
|
||||
MaximumOrdersWithLeverageRatio float64 `json:"maximum-orders-with-leverage-ratio"`
|
||||
MaximumLeverageRate float64 `json:"maximum-leverage-rate"`
|
||||
}
|
||||
|
||||
// MinMax are the rules which limit the placement of orders.
|
||||
type MinMax struct {
|
||||
MinimumSize float64 `json:"minimum-size"` // will not place an order if under this amount
|
||||
MaximumSize float64 `json:"maximum-size"` // can only place an order up to this amount
|
||||
MaximumTotal float64 `json:"maximum-total"`
|
||||
}
|
||||
|
||||
// CurrencySettings stores pair based variables
|
||||
// It contains rules about the specific currency pair
|
||||
// you wish to trade with
|
||||
// Backtester will load the data of the currencies specified here
|
||||
type CurrencySettings struct {
|
||||
ExchangeName string `json:"exchange-name"`
|
||||
Asset string `json:"asset"`
|
||||
Base string `json:"base"`
|
||||
Quote string `json:"quote"`
|
||||
|
||||
InitialFunds float64 `json:"initial-funds"`
|
||||
|
||||
Leverage Leverage `json:"leverage"`
|
||||
BuySide MinMax `json:"buy-side"`
|
||||
SellSide MinMax `json:"sell-side"`
|
||||
|
||||
MinimumSlippagePercent float64 `json:"min-slippage-percent"`
|
||||
MaximumSlippagePercent float64 `json:"max-slippage-percent"`
|
||||
|
||||
MakerFee float64 `json:"maker-fee-override"`
|
||||
TakerFee float64 `json:"taker-fee-override"`
|
||||
|
||||
MaximumHoldingsRatio float64 `json:"maximum-holdings-ratio"`
|
||||
}
|
||||
|
||||
// APIData defines all fields to configure API based data
|
||||
type APIData struct {
|
||||
StartDate time.Time `json:"start-date"`
|
||||
EndDate time.Time `json:"end-date"`
|
||||
InclusiveEndDate bool `json:"inclusive-end-date"`
|
||||
}
|
||||
|
||||
// CSVData defines all fields to configure CSV based data
|
||||
type CSVData struct {
|
||||
FullPath string `json:"full-path"`
|
||||
}
|
||||
|
||||
// DatabaseData defines all fields to configure database based data
|
||||
type DatabaseData struct {
|
||||
StartDate time.Time `json:"start-date"`
|
||||
EndDate time.Time `json:"end-date"`
|
||||
ConfigOverride *database.Config `json:"config-override"`
|
||||
InclusiveEndDate bool `json:"inclusive-end-date"`
|
||||
}
|
||||
|
||||
// LiveData defines all fields to configure live data
|
||||
type LiveData struct {
|
||||
APIKeyOverride string `json:"api-key-override"`
|
||||
APISecretOverride string `json:"api-secret-override"`
|
||||
APIClientIDOverride string `json:"api-client-id-override"`
|
||||
API2FAOverride string `json:"api-2fa-override"`
|
||||
RealOrders bool `json:"fake-orders"`
|
||||
}
|
||||
53
backtester/config/configbuilder/README.md
Normal file
53
backtester/config/configbuilder/README.md
Normal file
@@ -0,0 +1,53 @@
|
||||
# GoCryptoTrader Backtester: Configbuilder package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/config/configbuilder)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This configbuilder package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Configbuilder package overview
|
||||
|
||||
### What does the config builder do?
|
||||
The config builder runs you through the process of creating a strategy config (`.strat`) file. Configs can also be generated via test code under `config_test.go`.
|
||||
Once the config is created, when running the backtester, you can reference it via `go run . -configPath=(path-to-strat-file)`
|
||||
|
||||
### How do I run it?
|
||||
`go run .`
|
||||
|
||||
### Anything else?
|
||||
The config builder will ask you all the necessary questions required to create a config file. If there is anything confusing, feel free to ask a question in our Slack group or open an issue!
|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
652
backtester/config/configbuilder/main.go
Normal file
652
backtester/config/configbuilder/main.go
Normal file
@@ -0,0 +1,652 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/config"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/strategies"
|
||||
gctcommon "github.com/thrasher-corp/gocryptotrader/common"
|
||||
gctconfig "github.com/thrasher-corp/gocryptotrader/config"
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
dbPSQL "github.com/thrasher-corp/gocryptotrader/database/drivers/postgres"
|
||||
dbsqlite3 "github.com/thrasher-corp/gocryptotrader/database/drivers/sqlite3"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
)
|
||||
|
||||
const (
|
||||
yes = "yes"
|
||||
y = "y"
|
||||
)
|
||||
|
||||
var dataOptions = []string{
|
||||
"API",
|
||||
"CSV",
|
||||
"Database",
|
||||
"Live",
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Print(common.ASCIILogo)
|
||||
fmt.Println("Welcome to the config generator!")
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
cfg := config.Config{
|
||||
StrategySettings: config.StrategySettings{
|
||||
Name: "",
|
||||
SimultaneousSignalProcessing: false,
|
||||
CustomSettings: nil,
|
||||
},
|
||||
CurrencySettings: []config.CurrencySettings{},
|
||||
DataSettings: config.DataSettings{
|
||||
Interval: 0,
|
||||
DataType: "",
|
||||
APIData: nil,
|
||||
DatabaseData: nil,
|
||||
LiveData: nil,
|
||||
CSVData: nil,
|
||||
},
|
||||
PortfolioSettings: config.PortfolioSettings{
|
||||
Leverage: config.Leverage{},
|
||||
BuySide: config.MinMax{},
|
||||
SellSide: config.MinMax{},
|
||||
},
|
||||
StatisticSettings: config.StatisticSettings{},
|
||||
GoCryptoTraderConfigPath: "",
|
||||
}
|
||||
fmt.Println("-----Strategy Settings-----")
|
||||
var err error
|
||||
var strats []strategies.Handler
|
||||
firstRun := true
|
||||
for err != nil || firstRun {
|
||||
firstRun = false
|
||||
strats, err = parseStrategySettings(&cfg, reader)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("-----Exchange Settings-----")
|
||||
firstRun = true
|
||||
for err != nil || firstRun {
|
||||
firstRun = false
|
||||
err = parseExchangeSettings(reader, &cfg, strats)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("-----Portfolio Settings-----")
|
||||
firstRun = true
|
||||
for err != nil || firstRun {
|
||||
firstRun = false
|
||||
err = parsePortfolioSettings(reader, &cfg)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("-----Data Settings-----")
|
||||
firstRun = true
|
||||
for err != nil || firstRun {
|
||||
firstRun = false
|
||||
err = parseDataSettings(&cfg, reader)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("-----Statistics Settings-----")
|
||||
firstRun = true
|
||||
for err != nil || firstRun {
|
||||
firstRun = false
|
||||
err = parseStatisticsSettings(&cfg, reader)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("-----GoCryptoTrader config Settings-----")
|
||||
firstRun = true
|
||||
for err != nil || firstRun {
|
||||
firstRun = false
|
||||
fmt.Printf("Enter the path to the GoCryptoTrader config you wish to use. Leave blank to use \"%v\"\n", gctconfig.DefaultFilePath())
|
||||
path := quickParse(reader)
|
||||
if path != "" {
|
||||
cfg.GoCryptoTraderConfigPath = path
|
||||
} else {
|
||||
cfg.GoCryptoTraderConfigPath = gctconfig.DefaultFilePath()
|
||||
}
|
||||
_, err = os.Stat(cfg.GoCryptoTraderConfigPath)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
var resp []byte
|
||||
resp, err = json.MarshalIndent(cfg, "", " ")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("Write strategy config to file? If no, the output will be on screen y/n")
|
||||
yn := quickParse(reader)
|
||||
if yn == y || yn == yes {
|
||||
var wd string
|
||||
wd, err = os.Getwd()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fn := cfg.StrategySettings.Name
|
||||
if cfg.Nickname != "" {
|
||||
fn += "-" + cfg.Nickname
|
||||
}
|
||||
fn += ".strat" // nolint:misspell // its shorthand for strategy
|
||||
wd = filepath.Join(wd, fn)
|
||||
fmt.Printf("Enter output file. If blank, will output to \"%v\"\n", wd)
|
||||
path := quickParse(reader)
|
||||
if path == "" {
|
||||
path = wd
|
||||
}
|
||||
err = ioutil.WriteFile(path, resp, 0770)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
log.Print(string(resp))
|
||||
}
|
||||
log.Println("Config creation complete!")
|
||||
}
|
||||
|
||||
func parseStatisticsSettings(cfg *config.Config, reader *bufio.Reader) error {
|
||||
fmt.Println("Enter the risk free rate. eg 0.03")
|
||||
var err error
|
||||
cfg.StatisticSettings.RiskFreeRate, err = strconv.ParseFloat(quickParse(reader), 64)
|
||||
return err
|
||||
}
|
||||
|
||||
func parseDataSettings(cfg *config.Config, reader *bufio.Reader) error {
|
||||
var err error
|
||||
fmt.Println("Will you be using \"candle\" or \"trade\" data?")
|
||||
cfg.DataSettings.DataType = quickParse(reader)
|
||||
if cfg.DataSettings.DataType == common.TradeStr {
|
||||
fmt.Println("Trade data will be converted into candles")
|
||||
}
|
||||
fmt.Println("What candle time interval will you use?")
|
||||
cfg.DataSettings.Interval, err = parseKlineInterval(reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("Where will this data be sourced?")
|
||||
var choice string
|
||||
choice, err = parseDataChoice(reader, len(cfg.CurrencySettings) > 1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch choice {
|
||||
case "API":
|
||||
err = parseAPI(reader, cfg)
|
||||
case "Database":
|
||||
err = parseDatabase(reader, cfg)
|
||||
case "CSV":
|
||||
parseCSV(reader, cfg)
|
||||
case "Live":
|
||||
parseLive(reader, cfg)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func parsePortfolioSettings(reader *bufio.Reader, cfg *config.Config) error {
|
||||
var err error
|
||||
fmt.Println("Will there be global portfolio buy-side limits? y/n")
|
||||
yn := quickParse(reader)
|
||||
if yn == y || yn == yes {
|
||||
cfg.PortfolioSettings.BuySide, err = minMaxParse("buy", reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
fmt.Println("Will there be global portfolio sell-side limits? y/n")
|
||||
yn = quickParse(reader)
|
||||
if yn == y || yn == yes {
|
||||
cfg.PortfolioSettings.SellSide, err = minMaxParse("sell", reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseExchangeSettings(reader *bufio.Reader, cfg *config.Config, strats []strategies.Handler) error {
|
||||
var err error
|
||||
addCurrency := y
|
||||
for strings.Contains(addCurrency, y) {
|
||||
var currencySetting *config.CurrencySettings
|
||||
currencySetting, err = addCurrencySetting(reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg.CurrencySettings = append(cfg.CurrencySettings, *currencySetting)
|
||||
fmt.Println("Add another exchange currency setting? y/n")
|
||||
addCurrency = quickParse(reader)
|
||||
}
|
||||
|
||||
if len(cfg.CurrencySettings) > 1 {
|
||||
for i := range strats {
|
||||
if strats[i].Name() == cfg.StrategySettings.Name &&
|
||||
strats[i].SupportsSimultaneousProcessing() {
|
||||
fmt.Println("Will this strategy use simultaneous processing? y/n")
|
||||
yn := quickParse(reader)
|
||||
if yn == y || yn == yes {
|
||||
cfg.StrategySettings.SimultaneousSignalProcessing = true
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseStrategySettings(cfg *config.Config, reader *bufio.Reader) ([]strategies.Handler, error) {
|
||||
fmt.Println("Firstly, please select which strategy you wish to use")
|
||||
strats := strategies.GetStrategies()
|
||||
var strategiesToUse []string
|
||||
for i := range strats {
|
||||
fmt.Printf("%v. %s\n", i+1, strats[i].Name())
|
||||
strategiesToUse = append(strategiesToUse, strats[i].Name())
|
||||
}
|
||||
var err error
|
||||
cfg.StrategySettings.Name, err = parseStratName(quickParse(reader), strategiesToUse)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Println("What is the goal of your strategy?")
|
||||
cfg.Goal = quickParse(reader)
|
||||
fmt.Println("Enter a nickname, it can help distinguish between different configs using the same strategy")
|
||||
cfg.Nickname = quickParse(reader)
|
||||
fmt.Println("Does this strategy have custom settings? y/n")
|
||||
customSettings := quickParse(reader)
|
||||
if strings.Contains(customSettings, y) {
|
||||
cfg.StrategySettings.CustomSettings = customSettingsLoop(reader)
|
||||
}
|
||||
return strats, nil
|
||||
}
|
||||
|
||||
func parseAPI(reader *bufio.Reader, cfg *config.Config) error {
|
||||
cfg.DataSettings.APIData = &config.APIData{}
|
||||
var startDate, endDate, inclusive string
|
||||
var err error
|
||||
defaultStart := time.Now().Add(-time.Hour * 24 * 365)
|
||||
defaultEnd := time.Now()
|
||||
fmt.Printf("What is the start date? Leave blank for \"%v\"\n", defaultStart.Format(gctcommon.SimpleTimeFormat))
|
||||
startDate = quickParse(reader)
|
||||
if startDate != "" {
|
||||
cfg.DataSettings.APIData.StartDate, err = time.Parse(startDate, gctcommon.SimpleTimeFormat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
cfg.DataSettings.APIData.StartDate = defaultStart
|
||||
}
|
||||
|
||||
fmt.Printf("What is the end date? Leave blank for \"%v\"\n", defaultStart.Format(gctcommon.SimpleTimeFormat))
|
||||
endDate = quickParse(reader)
|
||||
if endDate != "" {
|
||||
cfg.DataSettings.APIData.EndDate, err = time.Parse(endDate, gctcommon.SimpleTimeFormat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
cfg.DataSettings.APIData.EndDate = defaultEnd
|
||||
}
|
||||
fmt.Println("Is the end date inclusive? y/n")
|
||||
inclusive = quickParse(reader)
|
||||
cfg.DataSettings.APIData.InclusiveEndDate = inclusive == y || inclusive == yes
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseCSV(reader *bufio.Reader, cfg *config.Config) {
|
||||
cfg.DataSettings.CSVData = &config.CSVData{}
|
||||
fmt.Println("What is path of the CSV file to read?")
|
||||
cfg.DataSettings.CSVData.FullPath = quickParse(reader)
|
||||
}
|
||||
|
||||
func parseDatabase(reader *bufio.Reader, cfg *config.Config) error {
|
||||
cfg.DataSettings.DatabaseData = &config.DatabaseData{}
|
||||
var input string
|
||||
var err error
|
||||
defaultStart := time.Now().Add(-time.Hour * 24 * 365)
|
||||
defaultEnd := time.Now()
|
||||
fmt.Printf("What is the start date? Leave blank for \"%v\"\n", defaultStart.Format(gctcommon.SimpleTimeFormat))
|
||||
startDate := quickParse(reader)
|
||||
if startDate != "" {
|
||||
cfg.DataSettings.DatabaseData.StartDate, err = time.Parse(startDate, gctcommon.SimpleTimeFormat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
cfg.DataSettings.DatabaseData.StartDate = defaultStart
|
||||
}
|
||||
|
||||
fmt.Printf("What is the end date? Leave blank for \"%v\"\n", defaultStart.Format(gctcommon.SimpleTimeFormat))
|
||||
endDate := quickParse(reader)
|
||||
if endDate != "" {
|
||||
cfg.DataSettings.DatabaseData.EndDate, err = time.Parse(endDate, gctcommon.SimpleTimeFormat)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
cfg.DataSettings.DatabaseData.EndDate = defaultEnd
|
||||
}
|
||||
fmt.Println("Is the end date inclusive? y/n")
|
||||
input = quickParse(reader)
|
||||
cfg.DataSettings.DatabaseData.InclusiveEndDate = input == y || input == yes
|
||||
|
||||
fmt.Println("Do you wish to override GoCryptoTrader's database config? y/n")
|
||||
input = quickParse(reader)
|
||||
if input == y || input == yes {
|
||||
cfg.DataSettings.DatabaseData.ConfigOverride = &database.Config{
|
||||
Enabled: true,
|
||||
}
|
||||
fmt.Println("Do you want database verbose output? y/n")
|
||||
input = quickParse(reader)
|
||||
cfg.DataSettings.DatabaseData.ConfigOverride.Verbose = input == y || input == yes
|
||||
|
||||
fmt.Printf("What database driver to use? %v %v or %v\n", database.DBPostgreSQL, database.DBSQLite, database.DBSQLite3)
|
||||
cfg.DataSettings.DatabaseData.ConfigOverride.Driver = quickParse(reader)
|
||||
|
||||
fmt.Println("What is the database host?")
|
||||
cfg.DataSettings.DatabaseData.ConfigOverride.Host = quickParse(reader)
|
||||
|
||||
fmt.Println("What is the database username?")
|
||||
cfg.DataSettings.DatabaseData.ConfigOverride.Username = quickParse(reader)
|
||||
|
||||
fmt.Println("What is the database password? eg 1234")
|
||||
cfg.DataSettings.DatabaseData.ConfigOverride.Password = quickParse(reader)
|
||||
|
||||
fmt.Println("What is the database? eg database.db")
|
||||
cfg.DataSettings.DatabaseData.ConfigOverride.Database = quickParse(reader)
|
||||
|
||||
if cfg.DataSettings.DatabaseData.ConfigOverride.Driver == database.DBPostgreSQL {
|
||||
fmt.Println("What is the database SSLMode? eg disable")
|
||||
cfg.DataSettings.DatabaseData.ConfigOverride.SSLMode = quickParse(reader)
|
||||
}
|
||||
fmt.Println("What is the database Port? eg 1337")
|
||||
input = quickParse(reader)
|
||||
var port float64
|
||||
if input != "" {
|
||||
port, err = strconv.ParseFloat(input, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
cfg.DataSettings.DatabaseData.ConfigOverride.Port = uint16(port)
|
||||
database.DB.Config = cfg.DataSettings.DatabaseData.ConfigOverride
|
||||
if cfg.DataSettings.DatabaseData.ConfigOverride.Driver == database.DBPostgreSQL {
|
||||
_, err = dbPSQL.Connect()
|
||||
if err != nil {
|
||||
return fmt.Errorf("database failed to connect: %v", err)
|
||||
}
|
||||
} else if cfg.DataSettings.DatabaseData.ConfigOverride.Driver == database.DBSQLite ||
|
||||
cfg.DataSettings.DatabaseData.ConfigOverride.Driver == database.DBSQLite3 {
|
||||
_, err = dbsqlite3.Connect()
|
||||
if err != nil {
|
||||
return fmt.Errorf("database failed to connect: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseLive(reader *bufio.Reader, cfg *config.Config) {
|
||||
cfg.DataSettings.LiveData = &config.LiveData{}
|
||||
fmt.Println("Do you wish to use live trading? It's highly recommended that you do not. y/n")
|
||||
input := quickParse(reader)
|
||||
cfg.DataSettings.LiveData.RealOrders = input == y || input == yes
|
||||
if cfg.DataSettings.LiveData.RealOrders {
|
||||
fmt.Printf("Do you want to override GoCryptoTrader's API credentials for %s? y/n\n", cfg.CurrencySettings[0].ExchangeName)
|
||||
input = quickParse(reader)
|
||||
if input == y || input == yes {
|
||||
fmt.Println("What is the API key?")
|
||||
cfg.DataSettings.DatabaseData.ConfigOverride.Database = quickParse(reader)
|
||||
fmt.Println("What is the API secret?")
|
||||
cfg.DataSettings.DatabaseData.ConfigOverride.Database = quickParse(reader)
|
||||
fmt.Println("What is the Client ID?")
|
||||
cfg.DataSettings.DatabaseData.ConfigOverride.Database = quickParse(reader)
|
||||
fmt.Println("What is the 2FA seed?")
|
||||
cfg.DataSettings.DatabaseData.ConfigOverride.Database = quickParse(reader)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func parseDataChoice(reader *bufio.Reader, multiCurrency bool) (string, error) {
|
||||
if multiCurrency {
|
||||
// live trading does not support multiple currencies
|
||||
dataOptions = dataOptions[:3]
|
||||
}
|
||||
for i := range dataOptions {
|
||||
fmt.Printf("%v. %s\n", i+1, dataOptions[i])
|
||||
}
|
||||
response := quickParse(reader)
|
||||
num, err := strconv.ParseFloat(response, 64)
|
||||
if err == nil {
|
||||
intNum := int(num)
|
||||
if intNum > len(dataOptions) || intNum <= 0 {
|
||||
return "", errors.New("unknown option")
|
||||
}
|
||||
return dataOptions[intNum-1], nil
|
||||
}
|
||||
for i := range dataOptions {
|
||||
if strings.EqualFold(response, dataOptions[i]) {
|
||||
return dataOptions[i], nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("unrecognised data option")
|
||||
}
|
||||
|
||||
func parseKlineInterval(reader *bufio.Reader) (time.Duration, error) {
|
||||
allCandles := gctkline.SupportedIntervals
|
||||
for i := range allCandles {
|
||||
fmt.Printf("%v. %s\n", i+1, allCandles[i].Word())
|
||||
}
|
||||
response := quickParse(reader)
|
||||
num, err := strconv.ParseFloat(response, 64)
|
||||
if err == nil {
|
||||
intNum := int(num)
|
||||
if intNum > len(allCandles) || intNum <= 0 {
|
||||
return 0, errors.New("unknown option")
|
||||
}
|
||||
return allCandles[intNum-1].Duration(), nil
|
||||
}
|
||||
for i := range allCandles {
|
||||
if strings.EqualFold(response, allCandles[i].Word()) {
|
||||
return allCandles[i].Duration(), nil
|
||||
}
|
||||
}
|
||||
return 0, errors.New("unrecognised interval")
|
||||
}
|
||||
|
||||
func parseStratName(name string, strategiesToUse []string) (string, error) {
|
||||
num, err := strconv.ParseFloat(name, 64)
|
||||
if err == nil {
|
||||
intNum := int(num)
|
||||
if intNum > len(strategiesToUse) || intNum <= 0 {
|
||||
return "", errors.New("unknown option")
|
||||
}
|
||||
return strategiesToUse[intNum-1], nil
|
||||
}
|
||||
for i := range strategiesToUse {
|
||||
if strings.EqualFold(name, strategiesToUse[i]) {
|
||||
return strategiesToUse[i], nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("unrecognised strategy")
|
||||
}
|
||||
|
||||
func customSettingsLoop(reader *bufio.Reader) map[string]interface{} {
|
||||
resp := make(map[string]interface{})
|
||||
customSettingField := "loopTime!"
|
||||
for customSettingField != "" {
|
||||
fmt.Println("Enter a custom setting name. Enter nothing to stop")
|
||||
customSettingField = quickParse(reader)
|
||||
if customSettingField != "" {
|
||||
fmt.Println("Enter a custom setting value")
|
||||
resp[customSettingField] = quickParse(reader)
|
||||
}
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
func addCurrencySetting(reader *bufio.Reader) (*config.CurrencySettings, error) {
|
||||
setting := config.CurrencySettings{
|
||||
BuySide: config.MinMax{},
|
||||
SellSide: config.MinMax{},
|
||||
}
|
||||
fmt.Println("Enter the exchange name. eg Binance")
|
||||
setting.ExchangeName = quickParse(reader)
|
||||
|
||||
fmt.Println("Please select an asset")
|
||||
supported := asset.Supported()
|
||||
for i := range supported {
|
||||
fmt.Printf("%v. %s\n", i+1, supported[i])
|
||||
}
|
||||
response := quickParse(reader)
|
||||
num, err := strconv.ParseFloat(response, 64)
|
||||
if err == nil {
|
||||
intNum := int(num)
|
||||
if intNum > len(supported) || intNum <= 0 {
|
||||
return nil, errors.New("unknown option")
|
||||
}
|
||||
setting.Asset = supported[intNum-1].String()
|
||||
}
|
||||
for i := range supported {
|
||||
if strings.EqualFold(response, supported[i].String()) {
|
||||
setting.Asset = supported[i].String()
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("Enter the currency base. eg BTC")
|
||||
setting.Base = quickParse(reader)
|
||||
|
||||
fmt.Println("Enter the currency quote. eg USDT")
|
||||
setting.Quote = quickParse(reader)
|
||||
|
||||
fmt.Println("Enter the initial funds. eg 10000")
|
||||
parseNum := quickParse(reader)
|
||||
if parseNum != "" {
|
||||
setting.InitialFunds, err = strconv.ParseFloat(parseNum, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("Enter the maker-fee. eg 0.001")
|
||||
parseNum = quickParse(reader)
|
||||
if parseNum != "" {
|
||||
setting.MakerFee, err = strconv.ParseFloat(parseNum, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
fmt.Println("Enter the taker-fee. eg 0.01")
|
||||
parseNum = quickParse(reader)
|
||||
if parseNum != "" {
|
||||
setting.TakerFee, err = strconv.ParseFloat(parseNum, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("Will there be buy-side limits? y/n")
|
||||
yn := quickParse(reader)
|
||||
if yn == y || yn == yes {
|
||||
setting.BuySide, err = minMaxParse("buy", reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
fmt.Println("Will there be sell-side limits? y/n")
|
||||
yn = quickParse(reader)
|
||||
if yn == y || yn == yes {
|
||||
setting.SellSide, err = minMaxParse("sell", reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
fmt.Println("Do you wish to include slippage? y/n")
|
||||
yn = quickParse(reader)
|
||||
if yn == y || yn == yes {
|
||||
fmt.Println("Slippage is randomly determined between the lower and upper bounds.")
|
||||
fmt.Println("If the lower bound is 80, then the price can change up to 80% of itself. eg if the price is 100 and the lower bound is 80, then the lowest slipped price is $80")
|
||||
fmt.Println("If the upper bound is 100, then the price can be unaffected. A minimum of 80 and a maximum of 100 means that the price will randomly be set between those bounds as a way of emulating slippage")
|
||||
|
||||
fmt.Println("What is the lower bounds of slippage? eg 80")
|
||||
setting.MinimumSlippagePercent, err = strconv.ParseFloat(quickParse(reader), 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Println("What is the upper bounds of slippage? eg 100")
|
||||
setting.MaximumSlippagePercent, err = strconv.ParseFloat(quickParse(reader), 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &setting, nil
|
||||
}
|
||||
|
||||
func minMaxParse(buySell string, reader *bufio.Reader) (config.MinMax, error) {
|
||||
resp := config.MinMax{}
|
||||
var err error
|
||||
fmt.Printf("What is the maximum %s size? eg 1\n", buySell)
|
||||
parseNum := quickParse(reader)
|
||||
if parseNum != "" {
|
||||
resp.MaximumSize, err = strconv.ParseFloat(parseNum, 64)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
}
|
||||
fmt.Printf("What is the minimum %s size? eg 0.1\n", buySell)
|
||||
parseNum = quickParse(reader)
|
||||
if parseNum != "" {
|
||||
resp.MinimumSize, err = strconv.ParseFloat(parseNum, 64)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
}
|
||||
fmt.Printf("What is the maximum spend %s buy? eg 12000\n", buySell)
|
||||
parseNum = quickParse(reader)
|
||||
if parseNum != "" {
|
||||
resp.MaximumTotal, err = strconv.ParseFloat(parseNum, 64)
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func quickParse(reader *bufio.Reader) string {
|
||||
customSettingField, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
customSettingField = strings.Replace(customSettingField, "\r", "", -1)
|
||||
return strings.Replace(customSettingField, "\n", "", -1)
|
||||
}
|
||||
52
backtester/config/examples/README.md
Normal file
52
backtester/config/examples/README.md
Normal file
@@ -0,0 +1,52 @@
|
||||
# GoCryptoTrader Backtester: Examples package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/config/examples)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This examples package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Examples package overview
|
||||
|
||||
Current Config Examples:
|
||||
|
||||
| Config | Description |
|
||||
| --- | ------ |
|
||||
| dollar-cost-average.strat | A simple dollar cost average strategy which makes a purchase on every candle. |
|
||||
| dollar-cost-average-live.strat | Using the same dollar cost average strategy, but runs the analysis against live candles |
|
||||
| dollar-cost-average-multi-currency-assessment.strat | This strategy will assess multiple currencies in the one `OnSignals` function, however, it also just simply makes a purchase on every candle |
|
||||
| dollar-cost-average-multiple-currencies.strat | This runs the same strategy against multiple currencies independently |
|
||||
| rsi.strat | Runs a strategy using rsi figures to make buy or sell orders based on market figures |
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
@@ -0,0 +1,95 @@
|
||||
{
|
||||
"nickname": "TestGenerateConfigForDCAAPICandlesMultipleCurrencies",
|
||||
"goal": "To demonstrate running the DCA strategy using the API against multiple currencies candle data",
|
||||
"strategy-settings": {
|
||||
"name": "dollarcostaverage",
|
||||
"use-simultaneous-signal-processing": false,
|
||||
"custom-settings": null
|
||||
},
|
||||
"currency-settings": [
|
||||
{
|
||||
"exchange-name": "binance",
|
||||
"asset": "spot",
|
||||
"base": "BTC",
|
||||
"quote": "USDT",
|
||||
"initial-funds": 100000,
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"min-slippage-percent": 0,
|
||||
"max-slippage-percent": 0,
|
||||
"maker-fee-override": 0.001,
|
||||
"taker-fee-override": 0.002,
|
||||
"maximum-holdings-ratio": 0
|
||||
},
|
||||
{
|
||||
"exchange-name": "binance",
|
||||
"asset": "spot",
|
||||
"base": "ETH",
|
||||
"quote": "USDT",
|
||||
"initial-funds": 100000,
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"min-slippage-percent": 0,
|
||||
"max-slippage-percent": 0,
|
||||
"maker-fee-override": 0.001,
|
||||
"taker-fee-override": 0.002,
|
||||
"maximum-holdings-ratio": 0
|
||||
}
|
||||
],
|
||||
"data-settings": {
|
||||
"interval": 86400000000000,
|
||||
"data-type": "candle",
|
||||
"api-data": {
|
||||
"start-date": "2020-11-01T00:00:00+11:00",
|
||||
"end-date": "2020-12-01T00:00:00+11:00",
|
||||
"inclusive-end-date": false
|
||||
}
|
||||
},
|
||||
"portfolio-settings": {
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
}
|
||||
},
|
||||
"statistic-settings": {
|
||||
"risk-free-rate": 0.03
|
||||
},
|
||||
"gocryptotrader-config-path": ""
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
{
|
||||
"nickname": "TestGenerateConfigForDCAAPICandlesSimultaneousProcessing",
|
||||
"goal": "To demonstrate how simultaneous processing can work",
|
||||
"strategy-settings": {
|
||||
"name": "dollarcostaverage",
|
||||
"use-simultaneous-signal-processing": true,
|
||||
"custom-settings": null
|
||||
},
|
||||
"currency-settings": [
|
||||
{
|
||||
"exchange-name": "binance",
|
||||
"asset": "spot",
|
||||
"base": "BTC",
|
||||
"quote": "USDT",
|
||||
"initial-funds": 1000000,
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0,
|
||||
"maximum-size": 0,
|
||||
"maximum-total": 1000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0,
|
||||
"maximum-size": 0,
|
||||
"maximum-total": 1000
|
||||
},
|
||||
"min-slippage-percent": 0,
|
||||
"max-slippage-percent": 0,
|
||||
"maker-fee-override": 0.001,
|
||||
"taker-fee-override": 0.002,
|
||||
"maximum-holdings-ratio": 0
|
||||
},
|
||||
{
|
||||
"exchange-name": "binance",
|
||||
"asset": "spot",
|
||||
"base": "ETH",
|
||||
"quote": "USDT",
|
||||
"initial-funds": 100000,
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"min-slippage-percent": 0,
|
||||
"max-slippage-percent": 0,
|
||||
"maker-fee-override": 0.001,
|
||||
"taker-fee-override": 0.002,
|
||||
"maximum-holdings-ratio": 0
|
||||
}
|
||||
],
|
||||
"data-settings": {
|
||||
"interval": 86400000000000,
|
||||
"data-type": "candle",
|
||||
"api-data": {
|
||||
"start-date": "2020-11-01T00:00:00+11:00",
|
||||
"end-date": "2020-12-01T00:00:00+11:00",
|
||||
"inclusive-end-date": false
|
||||
}
|
||||
},
|
||||
"portfolio-settings": {
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
}
|
||||
},
|
||||
"statistic-settings": {
|
||||
"risk-free-rate": 0.03
|
||||
},
|
||||
"gocryptotrader-config-path": ""
|
||||
}
|
||||
68
backtester/config/examples/dca-api-candles.strat
Normal file
68
backtester/config/examples/dca-api-candles.strat
Normal file
@@ -0,0 +1,68 @@
|
||||
{
|
||||
"nickname": "TestGenerateConfigForDCAAPICandles",
|
||||
"goal": "To demonstrate DCA strategy using API candles",
|
||||
"strategy-settings": {
|
||||
"name": "dollarcostaverage",
|
||||
"use-simultaneous-signal-processing": false,
|
||||
"custom-settings": null
|
||||
},
|
||||
"currency-settings": [
|
||||
{
|
||||
"exchange-name": "binance",
|
||||
"asset": "spot",
|
||||
"base": "BTC",
|
||||
"quote": "USDT",
|
||||
"initial-funds": 100000,
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"min-slippage-percent": 0,
|
||||
"max-slippage-percent": 0,
|
||||
"maker-fee-override": 0.001,
|
||||
"taker-fee-override": 0.002,
|
||||
"maximum-holdings-ratio": 0
|
||||
}
|
||||
],
|
||||
"data-settings": {
|
||||
"interval": 86400000000000,
|
||||
"data-type": "candle",
|
||||
"api-data": {
|
||||
"start-date": "2020-11-01T00:00:00+11:00",
|
||||
"end-date": "2020-12-01T00:00:00+11:00",
|
||||
"inclusive-end-date": false
|
||||
}
|
||||
},
|
||||
"portfolio-settings": {
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
}
|
||||
},
|
||||
"statistic-settings": {
|
||||
"risk-free-rate": 0.03
|
||||
},
|
||||
"gocryptotrader-config-path": ""
|
||||
}
|
||||
68
backtester/config/examples/dca-api-trades.strat
Normal file
68
backtester/config/examples/dca-api-trades.strat
Normal file
@@ -0,0 +1,68 @@
|
||||
{
|
||||
"nickname": "TestGenerateConfigForDCAAPITrades",
|
||||
"goal": "To demonstrate running the DCA strategy using API trade data",
|
||||
"strategy-settings": {
|
||||
"name": "dollarcostaverage",
|
||||
"use-simultaneous-signal-processing": false,
|
||||
"custom-settings": null
|
||||
},
|
||||
"currency-settings": [
|
||||
{
|
||||
"exchange-name": "binance",
|
||||
"asset": "spot",
|
||||
"base": "BTC",
|
||||
"quote": "USDT",
|
||||
"initial-funds": 100000,
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"min-slippage-percent": 0,
|
||||
"max-slippage-percent": 0,
|
||||
"maker-fee-override": 0.001,
|
||||
"taker-fee-override": 0.002,
|
||||
"maximum-holdings-ratio": 0
|
||||
}
|
||||
],
|
||||
"data-settings": {
|
||||
"interval": 86400000000000,
|
||||
"data-type": "trade",
|
||||
"api-data": {
|
||||
"start-date": "2020-11-01T00:00:00+11:00",
|
||||
"end-date": "2020-12-01T00:00:00+11:00",
|
||||
"inclusive-end-date": false
|
||||
}
|
||||
},
|
||||
"portfolio-settings": {
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
}
|
||||
},
|
||||
"statistic-settings": {
|
||||
"risk-free-rate": 0.03
|
||||
},
|
||||
"gocryptotrader-config-path": ""
|
||||
}
|
||||
70
backtester/config/examples/dca-candles-live.strat
Normal file
70
backtester/config/examples/dca-candles-live.strat
Normal file
@@ -0,0 +1,70 @@
|
||||
{
|
||||
"nickname": "TestGenerateConfigForDCALiveCandles",
|
||||
"goal": "To demonstrate live trading proof of concept against candle data",
|
||||
"strategy-settings": {
|
||||
"name": "dollarcostaverage",
|
||||
"use-simultaneous-signal-processing": false,
|
||||
"custom-settings": null
|
||||
},
|
||||
"currency-settings": [
|
||||
{
|
||||
"exchange-name": "binance",
|
||||
"asset": "spot",
|
||||
"base": "BTC",
|
||||
"quote": "USDT",
|
||||
"initial-funds": 100000,
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"min-slippage-percent": 0,
|
||||
"max-slippage-percent": 0,
|
||||
"maker-fee-override": 0.001,
|
||||
"taker-fee-override": 0.002,
|
||||
"maximum-holdings-ratio": 0
|
||||
}
|
||||
],
|
||||
"data-settings": {
|
||||
"interval": 3600000000000,
|
||||
"data-type": "candle",
|
||||
"live-data": {
|
||||
"api-key-override": "",
|
||||
"api-secret-override": "",
|
||||
"api-client-id-override": "",
|
||||
"api-2fa-override": "",
|
||||
"fake-orders": false
|
||||
}
|
||||
},
|
||||
"portfolio-settings": {
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
}
|
||||
},
|
||||
"statistic-settings": {
|
||||
"risk-free-rate": 0.03
|
||||
},
|
||||
"gocryptotrader-config-path": ""
|
||||
}
|
||||
66
backtester/config/examples/dca-csv-candles.strat
Normal file
66
backtester/config/examples/dca-csv-candles.strat
Normal file
@@ -0,0 +1,66 @@
|
||||
{
|
||||
"nickname": "TestGenerateConfigForDCACSVCandles",
|
||||
"goal": "To demonstrate the DCA strategy using CSV candle data",
|
||||
"strategy-settings": {
|
||||
"name": "dollarcostaverage",
|
||||
"use-simultaneous-signal-processing": false,
|
||||
"custom-settings": null
|
||||
},
|
||||
"currency-settings": [
|
||||
{
|
||||
"exchange-name": "binance",
|
||||
"asset": "spot",
|
||||
"base": "BTC",
|
||||
"quote": "USDT",
|
||||
"initial-funds": 100000,
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"min-slippage-percent": 0,
|
||||
"max-slippage-percent": 0,
|
||||
"maker-fee-override": 0.001,
|
||||
"taker-fee-override": 0.002,
|
||||
"maximum-holdings-ratio": 0
|
||||
}
|
||||
],
|
||||
"data-settings": {
|
||||
"interval": 86400000000000,
|
||||
"data-type": "candle",
|
||||
"csv-data": {
|
||||
"full-path": "..\\testdata\\binance_BTCUSDT_24h_2019_01_01_2020_01_01.csv"
|
||||
}
|
||||
},
|
||||
"portfolio-settings": {
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
}
|
||||
},
|
||||
"statistic-settings": {
|
||||
"risk-free-rate": 0.03
|
||||
},
|
||||
"gocryptotrader-config-path": ""
|
||||
}
|
||||
66
backtester/config/examples/dca-csv-trades.strat
Normal file
66
backtester/config/examples/dca-csv-trades.strat
Normal file
@@ -0,0 +1,66 @@
|
||||
{
|
||||
"nickname": "TestGenerateConfigForDCACSVTrades",
|
||||
"goal": "To demonstrate the DCA strategy using CSV trade data",
|
||||
"strategy-settings": {
|
||||
"name": "dollarcostaverage",
|
||||
"use-simultaneous-signal-processing": false,
|
||||
"custom-settings": null
|
||||
},
|
||||
"currency-settings": [
|
||||
{
|
||||
"exchange-name": "binance",
|
||||
"asset": "spot",
|
||||
"base": "BTC",
|
||||
"quote": "USDT",
|
||||
"initial-funds": 100000,
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"min-slippage-percent": 0,
|
||||
"max-slippage-percent": 0,
|
||||
"maker-fee-override": 0.001,
|
||||
"taker-fee-override": 0.002,
|
||||
"maximum-holdings-ratio": 0
|
||||
}
|
||||
],
|
||||
"data-settings": {
|
||||
"interval": 60000000000,
|
||||
"data-type": "trade",
|
||||
"csv-data": {
|
||||
"full-path": "..\\testdata\\binance_BTCUSDT_24h-trades_2020_11_16.csv"
|
||||
}
|
||||
},
|
||||
"portfolio-settings": {
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
}
|
||||
},
|
||||
"statistic-settings": {
|
||||
"risk-free-rate": 0.03
|
||||
},
|
||||
"gocryptotrader-config-path": ""
|
||||
}
|
||||
81
backtester/config/examples/dca-database-candles.strat
Normal file
81
backtester/config/examples/dca-database-candles.strat
Normal file
@@ -0,0 +1,81 @@
|
||||
{
|
||||
"nickname": "TestGenerateConfigForDCADatabaseCandles",
|
||||
"goal": "To demonstrate the DCA strategy using database candle data",
|
||||
"strategy-settings": {
|
||||
"name": "dollarcostaverage",
|
||||
"use-simultaneous-signal-processing": false,
|
||||
"custom-settings": null
|
||||
},
|
||||
"currency-settings": [
|
||||
{
|
||||
"exchange-name": "binance",
|
||||
"asset": "spot",
|
||||
"base": "BTC",
|
||||
"quote": "USDT",
|
||||
"initial-funds": 100000,
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"min-slippage-percent": 0,
|
||||
"max-slippage-percent": 0,
|
||||
"maker-fee-override": 0.001,
|
||||
"taker-fee-override": 0.002,
|
||||
"maximum-holdings-ratio": 0
|
||||
}
|
||||
],
|
||||
"data-settings": {
|
||||
"interval": 86400000000000,
|
||||
"data-type": "candle",
|
||||
"database-data": {
|
||||
"start-date": "2020-11-01T00:00:00+11:00",
|
||||
"end-date": "2020-12-01T00:00:00+11:00",
|
||||
"config-override": {
|
||||
"enabled": true,
|
||||
"verbose": false,
|
||||
"driver": "sqlite",
|
||||
"connectionDetails": {
|
||||
"host": "localhost",
|
||||
"port": 0,
|
||||
"username": "",
|
||||
"password": "",
|
||||
"database": "testsqlite.db",
|
||||
"sslmode": ""
|
||||
}
|
||||
},
|
||||
"inclusive-end-date": false
|
||||
}
|
||||
},
|
||||
"portfolio-settings": {
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
}
|
||||
},
|
||||
"statistic-settings": {
|
||||
"risk-free-rate": 0.03
|
||||
},
|
||||
"gocryptotrader-config-path": ""
|
||||
}
|
||||
99
backtester/config/examples/rsi-api-candles.strat
Normal file
99
backtester/config/examples/rsi-api-candles.strat
Normal file
@@ -0,0 +1,99 @@
|
||||
{
|
||||
"nickname": "TestGenerateRSICandleAPICustomSettingsStrat",
|
||||
"goal": "To demonstrate the RSI strategy using API candle data and custom settings",
|
||||
"strategy-settings": {
|
||||
"name": "rsi",
|
||||
"use-simultaneous-signal-processing": false,
|
||||
"custom-settings": {
|
||||
"rsi-high": 70,
|
||||
"rsi-low": 30,
|
||||
"rsi-period": 14
|
||||
}
|
||||
},
|
||||
"currency-settings": [
|
||||
{
|
||||
"exchange-name": "binance",
|
||||
"asset": "spot",
|
||||
"base": "BTC",
|
||||
"quote": "USDT",
|
||||
"initial-funds": 1000000,
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"min-slippage-percent": 0,
|
||||
"max-slippage-percent": 0,
|
||||
"maker-fee-override": 0.001,
|
||||
"taker-fee-override": 0.002,
|
||||
"maximum-holdings-ratio": 0
|
||||
},
|
||||
{
|
||||
"exchange-name": "binance",
|
||||
"asset": "spot",
|
||||
"base": "ETH",
|
||||
"quote": "USDT",
|
||||
"initial-funds": 100000,
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"min-slippage-percent": 0,
|
||||
"max-slippage-percent": 0,
|
||||
"maker-fee-override": 0.001,
|
||||
"taker-fee-override": 0.002,
|
||||
"maximum-holdings-ratio": 0
|
||||
}
|
||||
],
|
||||
"data-settings": {
|
||||
"interval": 86400000000000,
|
||||
"data-type": "candle",
|
||||
"api-data": {
|
||||
"start-date": "2020-11-01T00:00:00+11:00",
|
||||
"end-date": "2020-12-01T00:00:00+11:00",
|
||||
"inclusive-end-date": false
|
||||
}
|
||||
},
|
||||
"portfolio-settings": {
|
||||
"leverage": {
|
||||
"can-use-leverage": false,
|
||||
"maximum-orders-with-leverage-ratio": 0,
|
||||
"maximum-leverage-rate": 0
|
||||
},
|
||||
"buy-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
},
|
||||
"sell-side": {
|
||||
"minimum-size": 0.1,
|
||||
"maximum-size": 1,
|
||||
"maximum-total": 10000
|
||||
}
|
||||
},
|
||||
"statistic-settings": {
|
||||
"risk-free-rate": 0.03
|
||||
},
|
||||
"gocryptotrader-config-path": ""
|
||||
}
|
||||
50
backtester/data/README.md
Normal file
50
backtester/data/README.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# GoCryptoTrader Backtester: Data package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/data)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This data package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Data package overview
|
||||
|
||||
The data package defines and implements a base version of the `Streamer` interface which is part of the `Handler` interface. These interfaces allow for the translation of data into individual intervals to be accessed and assessed as part of the `backtest` package.
|
||||
This is a base implementation, the more proper implementation that is used throughout the backtester is under `./kline`
|
||||
|
||||
This can also be used to implement other means to load data for the backtester to process, however kline is currently the only supported method.
|
||||
|
||||
|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
118
backtester/data/data.go
Normal file
118
backtester/data/data.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
// Setup creates a basic map
|
||||
func (h *HandlerPerCurrency) Setup() {
|
||||
if h.data == nil {
|
||||
h.data = make(map[string]map[asset.Item]map[currency.Pair]Handler)
|
||||
}
|
||||
}
|
||||
|
||||
// SetDataForCurrency assigns a data Handler to the data map by exchange, asset and currency
|
||||
func (h *HandlerPerCurrency) SetDataForCurrency(e string, a asset.Item, p currency.Pair, k Handler) {
|
||||
if h.data == nil {
|
||||
h.Setup()
|
||||
}
|
||||
e = strings.ToLower(e)
|
||||
if h.data[e] == nil {
|
||||
h.data[e] = make(map[asset.Item]map[currency.Pair]Handler)
|
||||
}
|
||||
if h.data[e][a] == nil {
|
||||
h.data[e][a] = make(map[currency.Pair]Handler)
|
||||
}
|
||||
h.data[e][a][p] = k
|
||||
}
|
||||
|
||||
// GetAllData returns all set data in the data map
|
||||
func (h *HandlerPerCurrency) GetAllData() map[string]map[asset.Item]map[currency.Pair]Handler {
|
||||
return h.data
|
||||
}
|
||||
|
||||
// GetDataForCurrency returns the Handler for a specific exchange, asset, currency
|
||||
func (h *HandlerPerCurrency) GetDataForCurrency(e string, a asset.Item, p currency.Pair) Handler {
|
||||
return h.data[e][a][p]
|
||||
}
|
||||
|
||||
// Reset returns the struct to defaults
|
||||
func (h *HandlerPerCurrency) Reset() {
|
||||
h.data = nil
|
||||
}
|
||||
|
||||
// Reset loaded data to blank state
|
||||
func (b *Base) Reset() {
|
||||
b.latest = nil
|
||||
b.offset = 0
|
||||
b.stream = nil
|
||||
}
|
||||
|
||||
// GetStream will return entire data list
|
||||
func (b *Base) GetStream() []common.DataEventHandler {
|
||||
return b.stream
|
||||
}
|
||||
|
||||
// Offset returns the current iteration of candle data the backtester is assessing
|
||||
func (b *Base) Offset() int {
|
||||
return b.offset
|
||||
}
|
||||
|
||||
// SetStream sets the data stream for candle analysis
|
||||
func (b *Base) SetStream(s []common.DataEventHandler) {
|
||||
b.stream = s
|
||||
}
|
||||
|
||||
// AppendStream appends new datas onto the stream, however, will not
|
||||
// add duplicates. Used for live analysis
|
||||
func (b *Base) AppendStream(s ...common.DataEventHandler) {
|
||||
for i := range s {
|
||||
if s[i] == nil {
|
||||
continue
|
||||
}
|
||||
b.stream = append(b.stream, s[i])
|
||||
}
|
||||
}
|
||||
|
||||
// Next will return the next event in the list and also shift the offset one
|
||||
func (b *Base) Next() (dh common.DataEventHandler) {
|
||||
if len(b.stream) <= b.offset {
|
||||
return nil
|
||||
}
|
||||
|
||||
ret := b.stream[b.offset]
|
||||
b.offset++
|
||||
b.latest = ret
|
||||
return ret
|
||||
}
|
||||
|
||||
// History will return all previous data events that have happened
|
||||
func (b *Base) History() []common.DataEventHandler {
|
||||
return b.stream[:b.offset]
|
||||
}
|
||||
|
||||
// Latest will return latest data event
|
||||
func (b *Base) Latest() common.DataEventHandler {
|
||||
return b.latest
|
||||
}
|
||||
|
||||
// List returns all future data events from the current iteration
|
||||
// ill-advised to use this in strategies because you don't know the future in real life
|
||||
func (b *Base) List() []common.DataEventHandler {
|
||||
return b.stream[b.offset:]
|
||||
}
|
||||
|
||||
// SortStream sorts the stream by timestamp
|
||||
func (b *Base) SortStream() {
|
||||
sort.Slice(b.stream, func(i, j int) bool {
|
||||
b1 := b.stream[i]
|
||||
b2 := b.stream[j]
|
||||
|
||||
return b1.GetTime().Before(b2.GetTime())
|
||||
})
|
||||
}
|
||||
109
backtester/data/data_test.go
Normal file
109
backtester/data/data_test.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
const testExchange = "binance"
|
||||
|
||||
func TestBaseDataFunctions(t *testing.T) {
|
||||
t.Parallel()
|
||||
var d Base
|
||||
d.Latest()
|
||||
d.Next()
|
||||
o := d.Offset()
|
||||
if o != 0 {
|
||||
t.Error("expected 0")
|
||||
}
|
||||
d.AppendStream(nil)
|
||||
d.AppendStream(nil)
|
||||
d.AppendStream(nil)
|
||||
|
||||
d.Next()
|
||||
o = d.Offset()
|
||||
if o != 0 {
|
||||
t.Error("expected 0")
|
||||
}
|
||||
d.List()
|
||||
d.History()
|
||||
d.SetStream(nil)
|
||||
st := d.GetStream()
|
||||
if st != nil {
|
||||
t.Error("expected nil")
|
||||
}
|
||||
d.Reset()
|
||||
d.GetStream()
|
||||
d.SortStream()
|
||||
}
|
||||
|
||||
func TestSetup(t *testing.T) {
|
||||
t.Parallel()
|
||||
d := HandlerPerCurrency{}
|
||||
d.Setup()
|
||||
if d.data == nil {
|
||||
t.Error("expected not nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDataForCurrency(t *testing.T) {
|
||||
t.Parallel()
|
||||
d := HandlerPerCurrency{}
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
d.SetDataForCurrency(exch, a, p, nil)
|
||||
if d.data == nil {
|
||||
t.Error("expected not nil")
|
||||
}
|
||||
if d.data[exch][a][p] != nil {
|
||||
t.Error("expected nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAllData(t *testing.T) {
|
||||
t.Parallel()
|
||||
d := HandlerPerCurrency{}
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
d.SetDataForCurrency(exch, a, p, nil)
|
||||
d.SetDataForCurrency(exch, a, currency.NewPair(currency.BTC, currency.DOGE), nil)
|
||||
result := d.GetAllData()
|
||||
if len(result) != 1 {
|
||||
t.Error("expected 1")
|
||||
}
|
||||
if len(result[exch][a]) != 2 {
|
||||
t.Error("expected 2")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetDataForCurrency(t *testing.T) {
|
||||
t.Parallel()
|
||||
d := HandlerPerCurrency{}
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
d.SetDataForCurrency(exch, a, p, nil)
|
||||
d.SetDataForCurrency(exch, a, currency.NewPair(currency.BTC, currency.DOGE), nil)
|
||||
result := d.GetDataForCurrency(exch, a, p)
|
||||
if result != nil {
|
||||
t.Error("expected nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReset(t *testing.T) {
|
||||
t.Parallel()
|
||||
d := HandlerPerCurrency{}
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
d.SetDataForCurrency(exch, a, p, nil)
|
||||
d.SetDataForCurrency(exch, a, currency.NewPair(currency.BTC, currency.DOGE), nil)
|
||||
d.Reset()
|
||||
if d.data != nil {
|
||||
t.Error("expected nil")
|
||||
}
|
||||
}
|
||||
61
backtester/data/data_types.go
Normal file
61
backtester/data/data_types.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
// HandlerPerCurrency stores an event handler per exchange asset pair
|
||||
type HandlerPerCurrency struct {
|
||||
data map[string]map[asset.Item]map[currency.Pair]Handler
|
||||
}
|
||||
|
||||
// Holder interface dictates what a data holder is expected to do
|
||||
type Holder interface {
|
||||
Setup()
|
||||
SetDataForCurrency(string, asset.Item, currency.Pair, Handler)
|
||||
GetAllData() map[string]map[asset.Item]map[currency.Pair]Handler
|
||||
GetDataForCurrency(string, asset.Item, currency.Pair) Handler
|
||||
Reset()
|
||||
}
|
||||
|
||||
// Base is the base implementation of some interface functions
|
||||
// where further specific functions are implmented in DataFromKline
|
||||
type Base struct {
|
||||
latest common.DataEventHandler
|
||||
stream []common.DataEventHandler
|
||||
offset int
|
||||
}
|
||||
|
||||
// Handler interface for Loading and Streaming data
|
||||
type Handler interface {
|
||||
Loader
|
||||
Streamer
|
||||
Reset()
|
||||
}
|
||||
|
||||
// Loader interface for Loading data into backtest supported format
|
||||
type Loader interface {
|
||||
Load() error
|
||||
}
|
||||
|
||||
// Streamer interface handles loading, parsing, distributing BackTest data
|
||||
type Streamer interface {
|
||||
Next() common.DataEventHandler
|
||||
GetStream() []common.DataEventHandler
|
||||
History() []common.DataEventHandler
|
||||
Latest() common.DataEventHandler
|
||||
List() []common.DataEventHandler
|
||||
Offset() int
|
||||
|
||||
StreamOpen() []float64
|
||||
StreamHigh() []float64
|
||||
StreamLow() []float64
|
||||
StreamClose() []float64
|
||||
StreamVol() []float64
|
||||
|
||||
HasDataAtTime(time.Time) bool
|
||||
}
|
||||
49
backtester/data/kline/README.md
Normal file
49
backtester/data/kline/README.md
Normal file
@@ -0,0 +1,49 @@
|
||||
# GoCryptoTrader Backtester: Kline package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/data/kline)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This kline package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Kline package overview
|
||||
|
||||
When loading data for the kline, it can come from two sources: candles or trades. In the config they are represented as `common.CandleStr` or `common.TradeStr` respectively.
|
||||
|
||||
Candle data represents the opening, closing, highest, lowest prices of a given timespan (interval) along with the volume (amount traded) during that same period. You can read more about candles [here](https://www.investopedia.com/terms/c/candlestick.asp). This data is utilised throughout the GoCryptoTrader Backtester in order to make informed strategic decisions.
|
||||
|
||||
Trade data represents the raw trading data on an exchange. Every buy or sell action for the given currency. When trading data is used for the GoCryptoTrader Backtester, it is converted into candle data at the interval you specify. This allows for custom candle intervals not provided by an exchange's API and thus has a greater amount of flexibility in backtesting strategies.
|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
47
backtester/data/kline/api/README.md
Normal file
47
backtester/data/kline/api/README.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# GoCryptoTrader Backtester: Api package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/data/kline/api)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This api package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Api package overview
|
||||
|
||||
This package is responsible for the loading of kline data via the API. It can retrieve candle data or trade data which is converted into candle data.
|
||||
This package uses existing GoCryptoTrader exchange implementations.
|
||||
|
||||
See individual exchange implementations [here](/exchanges) and the interface used [here](/exchanges/interfaces.go)
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
52
backtester/data/kline/api/api.go
Normal file
52
backtester/data/kline/api/api.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
|
||||
)
|
||||
|
||||
// LoadData retrieves data from a GoCryptoTrader exchange wrapper which calls the exchange's API
|
||||
func LoadData(dataType int64, startDate, endDate time.Time, interval time.Duration, exch exchange.IBotExchange, fPair currency.Pair, a asset.Item) (*kline.Item, error) {
|
||||
var candles kline.Item
|
||||
var err error
|
||||
switch dataType {
|
||||
case common.DataCandle:
|
||||
candles, err = exch.GetHistoricCandlesExtended(
|
||||
fPair,
|
||||
a,
|
||||
startDate,
|
||||
endDate,
|
||||
kline.Interval(interval))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not retrieve candle data for %v %v %v, %v", exch.GetName(), a, fPair, err)
|
||||
}
|
||||
case common.DataTrade:
|
||||
var trades []trade.Data
|
||||
trades, err = exch.GetHistoricTrades(
|
||||
fPair,
|
||||
a,
|
||||
startDate,
|
||||
endDate)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not retrieve trade data for %v %v %v, %v", exch.GetName(), a, fPair, err)
|
||||
}
|
||||
|
||||
candles, err = trade.ConvertTradesToCandles(kline.Interval(interval), trades...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not convert trade data to candles for %v %v %v, %v", exch.GetName(), a, fPair, err)
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("could not retrieve data for %v %v %v, invalid data type received", exch.GetName(), a, fPair)
|
||||
}
|
||||
candles.Exchange = strings.ToLower(candles.Exchange)
|
||||
|
||||
return &candles, nil
|
||||
}
|
||||
86
backtester/data/kline/api/api_test.go
Normal file
86
backtester/data/kline/api/api_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/engine"
|
||||
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
)
|
||||
|
||||
const testExchange = "binance"
|
||||
|
||||
var (
|
||||
bot *engine.Engine
|
||||
exch exchange.IBotExchange
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
var err error
|
||||
bot, err = engine.NewFromSettings(&engine.Settings{
|
||||
ConfigFile: filepath.Join("..", "..", "..", "..", "testdata", "configtest.json"),
|
||||
EnableDryRun: true,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = bot.LoadExchange(testExchange, false, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
exch = bot.GetExchangeByName(testExchange)
|
||||
if exch == nil {
|
||||
log.Fatal("expected binance")
|
||||
}
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func TestLoadCandles(t *testing.T) {
|
||||
t.Parallel()
|
||||
tt1 := time.Now().Add(-time.Hour).Round(gctkline.OneHour.Duration())
|
||||
tt2 := time.Now().Round(gctkline.OneHour.Duration())
|
||||
interval := gctkline.OneHour
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
var data *gctkline.Item
|
||||
var err error
|
||||
data, err = LoadData(common.DataCandle, tt1, tt2, interval.Duration(), exch, p, a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(data.Candles) == 0 {
|
||||
t.Error("expected candles")
|
||||
}
|
||||
|
||||
_, err = LoadData(-1, tt1, tt2, interval.Duration(), exch, p, a)
|
||||
if err != nil && !strings.Contains(err.Error(), "could not retrieve data for Binance spot BTCUSDT, invalid data type received") {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadTrades(t *testing.T) {
|
||||
t.Parallel()
|
||||
interval := gctkline.FifteenMin
|
||||
tt1 := time.Now().Add(-time.Minute * 60).Round(interval.Duration())
|
||||
tt2 := time.Now().Round(interval.Duration())
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
var err error
|
||||
var data *gctkline.Item
|
||||
data, err = LoadData(common.DataTrade, tt1, tt2, interval.Duration(), exch, p, a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(data.Candles) == 0 {
|
||||
t.Error("expected candles")
|
||||
}
|
||||
}
|
||||
69
backtester/data/kline/csv/README.md
Normal file
69
backtester/data/kline/csv/README.md
Normal file
@@ -0,0 +1,69 @@
|
||||
# GoCryptoTrader Backtester: Csv package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/data/kline/csv)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This csv package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Csv package overview
|
||||
|
||||
This package is responsible for the loading of kline data via a CSV file. It can retrieve candle data or trade data which is converted into candle data.
|
||||
|
||||
### CSV Format
|
||||
#### Candle based CSV
|
||||
|
||||
| Field | Example |
|
||||
| ----- | -------- |
|
||||
| Timestamp | 1546300800 |
|
||||
| Volume | 3 |
|
||||
| Open | 1335 |
|
||||
| High | 1338 |
|
||||
| Low | 1336 |
|
||||
| Close | 1337 |
|
||||
|
||||
Additionally, you can view an example under `./testdata/binance_BTCUSDT_24h_2019_01_01_2020_01_01.csv`
|
||||
|
||||
#### Trade based CSV
|
||||
|
||||
| Field | Example |
|
||||
| ----- | -------- |
|
||||
| Timestamp | 1546300800 |
|
||||
| Price | 1337 |
|
||||
| Amount | 420.69 |
|
||||
|
||||
Additionally, you can view an example under `./testdata/binance_BTCUSDT_24h-trades_2020_11_16.csv`
|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
160
backtester/data/kline/csv/csv.go
Normal file
160
backtester/data/kline/csv/csv.go
Normal file
@@ -0,0 +1,160 @@
|
||||
package csv
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/backtester/data/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
)
|
||||
|
||||
// LoadData is a basic csv reader which converts the found CSV file into a kline item
|
||||
func LoadData(dataType int64, filepath, exchangeName string, interval time.Duration, fPair currency.Pair, a asset.Item) (*gctkline.DataFromKline, error) {
|
||||
resp := &gctkline.DataFromKline{}
|
||||
csvFile, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err = csvFile.Close()
|
||||
if err != nil {
|
||||
log.Errorln(log.BackTester, err)
|
||||
}
|
||||
}()
|
||||
|
||||
csvData := csv.NewReader(csvFile)
|
||||
|
||||
switch dataType {
|
||||
case common.DataCandle:
|
||||
candles := kline.Item{
|
||||
Exchange: exchangeName,
|
||||
Pair: fPair,
|
||||
Asset: a,
|
||||
Interval: kline.Interval(interval),
|
||||
}
|
||||
|
||||
for {
|
||||
row, errCSV := csvData.Read()
|
||||
if errCSV != nil {
|
||||
if errCSV == io.EOF {
|
||||
break
|
||||
}
|
||||
return nil, fmt.Errorf("could not read csv data for %v %v %v, %v", exchangeName, a, fPair, errCSV)
|
||||
}
|
||||
|
||||
candle := kline.Candle{}
|
||||
v, errParse := strconv.ParseInt(row[0], 10, 32)
|
||||
if errParse != nil {
|
||||
return nil, errParse
|
||||
}
|
||||
candle.Time = time.Unix(v, 0).UTC()
|
||||
if candle.Time.IsZero() {
|
||||
err = fmt.Errorf("invalid timestamp received on row %v %v", row[0], err)
|
||||
break
|
||||
}
|
||||
|
||||
candle.Volume, err = strconv.ParseFloat(row[1], 64)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not process candle volume %v %v", row[1], err)
|
||||
break
|
||||
}
|
||||
|
||||
candle.Open, err = strconv.ParseFloat(row[2], 64)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not process candle volume %v %v", row[2], err)
|
||||
break
|
||||
}
|
||||
|
||||
candle.High, err = strconv.ParseFloat(row[3], 64)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not process candle high %v %v", row[3], err)
|
||||
break
|
||||
}
|
||||
|
||||
candle.Low, err = strconv.ParseFloat(row[4], 64)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not process candle low %v %v", row[4], err)
|
||||
break
|
||||
}
|
||||
|
||||
candle.Close, err = strconv.ParseFloat(row[5], 64)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not process candle close %v %v", row[5], err)
|
||||
break
|
||||
}
|
||||
|
||||
candles.Candles = append(candles.Candles, candle)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not read csv candle data for %v %v %v, %v", exchangeName, a, fPair, err)
|
||||
}
|
||||
|
||||
resp.Item = candles
|
||||
case common.DataTrade:
|
||||
var trades []trade.Data
|
||||
for {
|
||||
row, errCSV := csvData.Read()
|
||||
if errCSV != nil {
|
||||
if errCSV == io.EOF {
|
||||
break
|
||||
}
|
||||
return nil, errCSV
|
||||
}
|
||||
|
||||
t := trade.Data{}
|
||||
v, errParse := strconv.ParseInt(row[0], 10, 32)
|
||||
if errParse != nil {
|
||||
return nil, errParse
|
||||
}
|
||||
t.Timestamp = time.Unix(v, 0).UTC()
|
||||
if t.Timestamp.IsZero() {
|
||||
err = fmt.Errorf("invalid timestamp received on row %v", row)
|
||||
break
|
||||
}
|
||||
|
||||
t.Price, err = strconv.ParseFloat(row[1], 64)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not process trade price %v, %v", row[1], err)
|
||||
break
|
||||
}
|
||||
|
||||
t.Amount, err = strconv.ParseFloat(row[2], 64)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not process trade amount %v, %v", row[2], err)
|
||||
break
|
||||
}
|
||||
|
||||
t.Side, err = order.StringToOrderSide(row[3])
|
||||
if err != nil {
|
||||
err = fmt.Errorf("could not process trade side %v, %v", row[3], err)
|
||||
break
|
||||
}
|
||||
|
||||
trades = append(trades, t)
|
||||
}
|
||||
resp.Item, err = trade.ConvertTradesToCandles(kline.Interval(interval), trades...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not read csv trade data for %v %v %v, %v", exchangeName, a, fPair, err)
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("could not process csv data for %v %v %v, invalid data type received", exchangeName, a, fPair)
|
||||
}
|
||||
resp.Item.Exchange = strings.ToLower(exchangeName)
|
||||
resp.Item.Pair = fPair
|
||||
resp.Item.Asset = a
|
||||
resp.Item.Interval = kline.Interval(interval)
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
62
backtester/data/kline/csv/csv_test.go
Normal file
62
backtester/data/kline/csv/csv_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package csv
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
)
|
||||
|
||||
const testExchange = "binance"
|
||||
|
||||
func TestLoadDataCandles(t *testing.T) {
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
_, err := LoadData(
|
||||
common.DataCandle,
|
||||
filepath.Join("..", "..", "..", "..", "testdata", "binance_BTCUSDT_24h_2019_01_01_2020_01_01.csv"),
|
||||
exch,
|
||||
gctkline.FifteenMin.Duration(),
|
||||
p,
|
||||
a)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDataTrades(t *testing.T) {
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
_, err := LoadData(
|
||||
common.DataTrade,
|
||||
filepath.Join("..", "..", "..", "..", "testdata", "binance_BTCUSDT_24h-trades_2020_11_16.csv"),
|
||||
exch,
|
||||
gctkline.FifteenMin.Duration(),
|
||||
p,
|
||||
a)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDataInvalid(t *testing.T) {
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
_, err := LoadData(
|
||||
-1,
|
||||
filepath.Join("..", "..", "..", "..", "testdata", "binance_BTCUSDT_24h-trades_2020_11_16.csv"),
|
||||
exch,
|
||||
gctkline.FifteenMin.Duration(),
|
||||
p,
|
||||
a)
|
||||
if err != nil && !strings.Contains(err.Error(), "could not process csv data for binance spot BTCUSDT, invalid data type received") {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
53
backtester/data/kline/database/README.md
Normal file
53
backtester/data/kline/database/README.md
Normal file
@@ -0,0 +1,53 @@
|
||||
# GoCryptoTrader Backtester: Database package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/data/kline/database)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This database package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Database package overview
|
||||
|
||||
This package is responsible for the loading of kline data via a user's existing GoCryptoTrader database. It can load existing data from the `candles` and `trades` tables.
|
||||
For more information on the GoCryptoTrader database, read [this readme](/database/README.md).
|
||||
Ensure that your database has data and has been seeded with exchanges. For more information on this, please see [this readme](/cmd/dbseed/README.md).
|
||||
|
||||
### Database credentials
|
||||
#### Defaults
|
||||
The default database will be loaded from your GoCryptoTrader config. See [this](/database) for database configuration and implementation.
|
||||
|
||||
#### Overriding the GoCryptoTrader config
|
||||
Database configuration details can be overridden in the `.strat` config file to allow other sources to be used and not rely on existing GoCryptoTrader configuration. See [this readme](/backtester/config/README.md) for details on config customisation
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
66
backtester/data/kline/database/database.go
Normal file
66
backtester/data/kline/database/database.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
|
||||
)
|
||||
|
||||
// LoadData retrieves data from an existing database using GoCryptoTrader's database handling implementation
|
||||
func LoadData(startDate, endDate time.Time, interval time.Duration, exchangeName string, dataType int64, fPair currency.Pair, a asset.Item) (*kline.DataFromKline, error) {
|
||||
resp := &kline.DataFromKline{}
|
||||
switch dataType {
|
||||
case common.DataCandle:
|
||||
klineItem, err := getCandleDatabaseData(
|
||||
startDate,
|
||||
endDate,
|
||||
interval,
|
||||
exchangeName,
|
||||
fPair,
|
||||
a)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not retrieve database candle data for %v %v %v, %v", exchangeName, a, fPair, err)
|
||||
}
|
||||
resp.Item = klineItem
|
||||
case common.DataTrade:
|
||||
trades, err := trade.GetTradesInRange(
|
||||
exchangeName,
|
||||
a.String(),
|
||||
fPair.Base.String(),
|
||||
fPair.Quote.String(),
|
||||
startDate,
|
||||
endDate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
klineItem, err := trade.ConvertTradesToCandles(
|
||||
gctkline.Interval(interval),
|
||||
trades...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not retrieve database trade data for %v %v %v, %v", exchangeName, a, fPair, err)
|
||||
}
|
||||
resp.Item = klineItem
|
||||
default:
|
||||
return nil, fmt.Errorf("could not retrieve database data for %v %v %v, invalid data type received", exchangeName, a, fPair)
|
||||
}
|
||||
resp.Item.Exchange = strings.ToLower(resp.Item.Exchange)
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func getCandleDatabaseData(startDate, endDate time.Time, interval time.Duration, exchangeName string, fPair currency.Pair, a asset.Item) (gctkline.Item, error) {
|
||||
return gctkline.LoadFromDatabase(
|
||||
exchangeName,
|
||||
fPair,
|
||||
a,
|
||||
gctkline.Interval(interval),
|
||||
startDate,
|
||||
endDate)
|
||||
}
|
||||
208
backtester/data/kline/database/database_test.go
Normal file
208
backtester/data/kline/database/database_test.go
Normal file
@@ -0,0 +1,208 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/config"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/database"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/drivers"
|
||||
exchangeDB "github.com/thrasher-corp/gocryptotrader/database/repository/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/repository/trade"
|
||||
"github.com/thrasher-corp/gocryptotrader/database/testhelpers"
|
||||
"github.com/thrasher-corp/gocryptotrader/engine"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
const (
|
||||
verbose = false
|
||||
testExchange = "binance"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
if verbose {
|
||||
testhelpers.EnableVerboseTestOutput()
|
||||
}
|
||||
var err error
|
||||
testhelpers.PostgresTestDatabase = testhelpers.GetConnectionDetails()
|
||||
testhelpers.GetConnectionDetails()
|
||||
testhelpers.TempDir, err = ioutil.TempDir("", "gct-temp")
|
||||
if err != nil {
|
||||
fmt.Printf("failed to create temp file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
t := m.Run()
|
||||
|
||||
err = os.RemoveAll(testhelpers.TempDir)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to remove temp db file: %v", err)
|
||||
}
|
||||
|
||||
os.Exit(t)
|
||||
}
|
||||
|
||||
func TestLoadDataCandles(t *testing.T) {
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
var err error
|
||||
bot := &engine.Engine{}
|
||||
dbConfg := database.Config{
|
||||
Enabled: true,
|
||||
Verbose: false,
|
||||
Driver: "sqlite",
|
||||
ConnectionDetails: drivers.ConnectionDetails{
|
||||
Host: "localhost",
|
||||
Database: "test",
|
||||
},
|
||||
}
|
||||
bot.Config = &config.Config{
|
||||
Database: dbConfg,
|
||||
}
|
||||
|
||||
err = bot.Config.CheckConfig()
|
||||
if err != nil && verbose {
|
||||
// this loads the database config to the global database
|
||||
// the errors are unrelated and likely prone to change for reasons that
|
||||
// this test does not need to care about
|
||||
|
||||
// so we only log the error if verbose
|
||||
t.Log(err)
|
||||
}
|
||||
database.MigrationDir = filepath.Join("..", "..", "..", "..", "database", "migrations")
|
||||
testhelpers.MigrationDir = filepath.Join("..", "..", "..", "..", "database", "migrations")
|
||||
_, err = testhelpers.ConnectToDatabase(&dbConfg)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = bot.DatabaseManager.Start(bot)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = exchangeDB.InsertMany([]exchangeDB.Details{{Name: testExchange}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dStart := time.Date(2020, 1, 0, 0, 0, 0, 0, time.UTC)
|
||||
dInsert := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
dEnd := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
data := &gctkline.Item{
|
||||
Exchange: exch,
|
||||
Pair: p,
|
||||
Asset: a,
|
||||
Interval: gctkline.FifteenMin,
|
||||
Candles: []gctkline.Candle{
|
||||
{
|
||||
Time: dInsert,
|
||||
Open: 1337,
|
||||
High: 1337,
|
||||
Low: 1337,
|
||||
Close: 1337,
|
||||
Volume: 1337,
|
||||
},
|
||||
},
|
||||
}
|
||||
_, err = gctkline.StoreInDatabase(data, true)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = LoadData(dStart, dEnd, gctkline.FifteenMin.Duration(), exch, common.DataCandle, p, a)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDataTrades(t *testing.T) {
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
var err error
|
||||
bot := &engine.Engine{}
|
||||
dbConfg := database.Config{
|
||||
Enabled: true,
|
||||
Verbose: false,
|
||||
Driver: "sqlite",
|
||||
ConnectionDetails: drivers.ConnectionDetails{
|
||||
Host: "localhost",
|
||||
Database: "test",
|
||||
},
|
||||
}
|
||||
bot.Config = &config.Config{
|
||||
Database: dbConfg,
|
||||
}
|
||||
|
||||
err = bot.Config.CheckConfig()
|
||||
if err != nil && verbose {
|
||||
// this loads the database config to the global database
|
||||
// the errors are unrelated and likely prone to change for reasons that
|
||||
// this test does not need to care about
|
||||
|
||||
// so we only log the error if verbose
|
||||
t.Log(err)
|
||||
}
|
||||
database.MigrationDir = filepath.Join("..", "..", "..", "..", "database", "migrations")
|
||||
testhelpers.MigrationDir = filepath.Join("..", "..", "..", "..", "database", "migrations")
|
||||
_, err = testhelpers.ConnectToDatabase(&dbConfg)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = bot.DatabaseManager.Start(bot)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = exchangeDB.InsertMany([]exchangeDB.Details{{Name: testExchange}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dStart := time.Date(2020, 1, 0, 0, 0, 0, 0, time.UTC)
|
||||
dInsert := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
dEnd := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
|
||||
err = trade.Insert(trade.Data{
|
||||
ID: "123",
|
||||
TID: "123",
|
||||
Exchange: exch,
|
||||
Base: p.Base.String(),
|
||||
Quote: p.Quote.String(),
|
||||
AssetType: a.String(),
|
||||
Price: 1337,
|
||||
Amount: 1337,
|
||||
Side: gctorder.Buy.String(),
|
||||
Timestamp: dInsert,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = LoadData(dStart, dEnd, gctkline.FifteenMin.Duration(), exch, common.DataTrade, p, a)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDataInvalid(t *testing.T) {
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
dStart := time.Date(2020, 1, 0, 0, 0, 0, 0, time.UTC)
|
||||
dEnd := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
_, err := LoadData(dStart, dEnd, gctkline.FifteenMin.Duration(), exch, -1, p, a)
|
||||
if err != nil && !strings.Contains(err.Error(), "could not retrieve database data for binance spot BTCUSDT, invalid data type received") {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
146
backtester/data/kline/kline.go
Normal file
146
backtester/data/kline/kline.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package kline
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/event"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/kline"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
)
|
||||
|
||||
// HasDataAtTime verifies checks the underlying range data
|
||||
// To determine whether there is any candle data present at the time provided
|
||||
func (d *DataFromKline) HasDataAtTime(t time.Time) bool {
|
||||
return d.Range.HasDataAtDate(t)
|
||||
}
|
||||
|
||||
// Load sets the candle data to the stream for processing
|
||||
func (d *DataFromKline) Load() error {
|
||||
d.addedTimes = make(map[time.Time]bool)
|
||||
if len(d.Item.Candles) == 0 {
|
||||
return errNoCandleData
|
||||
}
|
||||
|
||||
klineData := make([]common.DataEventHandler, len(d.Item.Candles))
|
||||
for i := range d.Item.Candles {
|
||||
klineData[i] = &kline.Kline{
|
||||
Base: event.Base{
|
||||
Offset: int64(i + 1),
|
||||
Exchange: d.Item.Exchange,
|
||||
Time: d.Item.Candles[i].Time,
|
||||
Interval: d.Item.Interval,
|
||||
CurrencyPair: d.Item.Pair,
|
||||
AssetType: d.Item.Asset,
|
||||
},
|
||||
Open: d.Item.Candles[i].Open,
|
||||
High: d.Item.Candles[i].High,
|
||||
Low: d.Item.Candles[i].Low,
|
||||
Close: d.Item.Candles[i].Close,
|
||||
Volume: d.Item.Candles[i].Volume,
|
||||
}
|
||||
d.addedTimes[d.Item.Candles[i].Time] = true
|
||||
}
|
||||
d.SetStream(klineData)
|
||||
d.SortStream()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Append adds a candle item to the data stream and sorts it to ensure it is all in order
|
||||
func (d *DataFromKline) Append(ki *gctkline.Item) {
|
||||
if d.addedTimes == nil {
|
||||
d.addedTimes = make(map[time.Time]bool)
|
||||
}
|
||||
var klineData []common.DataEventHandler
|
||||
var gctCandles []gctkline.Candle
|
||||
for i := range ki.Candles {
|
||||
if _, ok := d.addedTimes[ki.Candles[i].Time]; !ok {
|
||||
gctCandles = append(gctCandles, ki.Candles[i])
|
||||
d.addedTimes[ki.Candles[i].Time] = true
|
||||
}
|
||||
}
|
||||
var candleTimes []time.Time
|
||||
|
||||
for i := range gctCandles {
|
||||
klineData = append(klineData, &kline.Kline{
|
||||
Base: event.Base{
|
||||
Offset: int64(i + 1),
|
||||
Exchange: ki.Exchange,
|
||||
Time: gctCandles[i].Time,
|
||||
Interval: ki.Interval,
|
||||
CurrencyPair: ki.Pair,
|
||||
AssetType: ki.Asset,
|
||||
},
|
||||
Open: gctCandles[i].Open,
|
||||
High: gctCandles[i].High,
|
||||
Low: gctCandles[i].Low,
|
||||
Close: gctCandles[i].Close,
|
||||
Volume: gctCandles[i].Volume,
|
||||
})
|
||||
candleTimes = append(candleTimes, gctCandles[i].Time)
|
||||
}
|
||||
log.Debugf(log.BackTester, "appending %v candle intervals: %v", len(gctCandles), candleTimes)
|
||||
d.AppendStream(klineData...)
|
||||
d.SortStream()
|
||||
}
|
||||
|
||||
// StreamOpen returns all Open prices from the beginning until the current iteration
|
||||
func (d *DataFromKline) StreamOpen() []float64 {
|
||||
s := d.GetStream()
|
||||
o := d.Offset()
|
||||
|
||||
ret := make([]float64, o)
|
||||
for x := range s[:o] {
|
||||
ret[x] = s[x].(*kline.Kline).Open
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// StreamHigh returns all High prices from the beginning until the current iteration
|
||||
func (d *DataFromKline) StreamHigh() []float64 {
|
||||
s := d.GetStream()
|
||||
o := d.Offset()
|
||||
|
||||
ret := make([]float64, o)
|
||||
for x := range s[:o] {
|
||||
ret[x] = s[x].(*kline.Kline).High
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// StreamLow returns all Low prices from the beginning until the current iteration
|
||||
func (d *DataFromKline) StreamLow() []float64 {
|
||||
s := d.GetStream()
|
||||
o := d.Offset()
|
||||
|
||||
ret := make([]float64, o)
|
||||
for x := range s[:o] {
|
||||
ret[x] = s[x].(*kline.Kline).Low
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// StreamClose returns all Close prices from the beginning until the current iteration
|
||||
func (d *DataFromKline) StreamClose() []float64 {
|
||||
s := d.GetStream()
|
||||
o := d.Offset()
|
||||
|
||||
ret := make([]float64, o)
|
||||
for x := range s[:o] {
|
||||
ret[x] = s[x].(*kline.Kline).Close
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// StreamVol returns all Volume prices from the beginning until the current iteration
|
||||
func (d *DataFromKline) StreamVol() []float64 {
|
||||
s := d.GetStream()
|
||||
o := d.Offset()
|
||||
|
||||
ret := make([]float64, o)
|
||||
for x := range s[:o] {
|
||||
ret[x] = s[x].(*kline.Kline).Volume
|
||||
}
|
||||
return ret
|
||||
}
|
||||
288
backtester/data/kline/kline_test.go
Normal file
288
backtester/data/kline/kline_test.go
Normal file
@@ -0,0 +1,288 @@
|
||||
package kline
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/event"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
)
|
||||
|
||||
const testExchange = "binance"
|
||||
|
||||
func TestLoad(t *testing.T) {
|
||||
t.Parallel()
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
tt := time.Now()
|
||||
d := DataFromKline{}
|
||||
err := d.Load()
|
||||
if !errors.Is(err, errNoCandleData) {
|
||||
t.Errorf("expected: %v, received %v", errNoCandleData, err)
|
||||
}
|
||||
d.Item = gctkline.Item{
|
||||
Exchange: exch,
|
||||
Pair: p,
|
||||
Asset: a,
|
||||
Interval: gctkline.FifteenMin,
|
||||
Candles: []gctkline.Candle{
|
||||
{
|
||||
Time: tt,
|
||||
Open: 1337,
|
||||
High: 1337,
|
||||
Low: 1337,
|
||||
Close: 1337,
|
||||
Volume: 1337,
|
||||
},
|
||||
},
|
||||
}
|
||||
err = d.Load()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasDataAtTime(t *testing.T) {
|
||||
t.Parallel()
|
||||
dStart := time.Date(2020, 1, 0, 0, 0, 0, 0, time.UTC)
|
||||
dInsert := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
dEnd := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
d := DataFromKline{}
|
||||
has := d.HasDataAtTime(time.Now())
|
||||
if has {
|
||||
t.Error("expected false")
|
||||
}
|
||||
|
||||
d.Item = gctkline.Item{
|
||||
Exchange: exch,
|
||||
Pair: p,
|
||||
Asset: a,
|
||||
Interval: gctkline.OneDay,
|
||||
Candles: []gctkline.Candle{
|
||||
{
|
||||
Time: dInsert,
|
||||
Open: 1337,
|
||||
High: 1337,
|
||||
Low: 1337,
|
||||
Close: 1337,
|
||||
Volume: 1337,
|
||||
},
|
||||
},
|
||||
}
|
||||
err := d.Load()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
has = d.HasDataAtTime(dInsert)
|
||||
if has {
|
||||
t.Error("expected false")
|
||||
}
|
||||
|
||||
ranger := gctkline.CalculateCandleDateRanges(dStart, dEnd, gctkline.OneDay, 100000)
|
||||
d.Range = ranger
|
||||
_ = d.Range.VerifyResultsHaveData(d.Item.Candles)
|
||||
has = d.HasDataAtTime(dInsert)
|
||||
if !has {
|
||||
t.Error("expected true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppend(t *testing.T) {
|
||||
t.Parallel()
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
d := DataFromKline{}
|
||||
item := gctkline.Item{
|
||||
Exchange: exch,
|
||||
Pair: p,
|
||||
Asset: a,
|
||||
Interval: gctkline.OneDay,
|
||||
Candles: []gctkline.Candle{
|
||||
{
|
||||
Time: time.Now(),
|
||||
Open: 1337,
|
||||
High: 1337,
|
||||
Low: 1337,
|
||||
Close: 1337,
|
||||
Volume: 1337,
|
||||
},
|
||||
},
|
||||
}
|
||||
d.Append(&item)
|
||||
}
|
||||
|
||||
func TestStreamOpen(t *testing.T) {
|
||||
t.Parallel()
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
d := DataFromKline{}
|
||||
bad := d.StreamOpen()
|
||||
if len(bad) > 0 {
|
||||
t.Error("expected no stream")
|
||||
}
|
||||
d.SetStream([]common.DataEventHandler{
|
||||
&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: time.Now(),
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
High: 1337,
|
||||
Low: 1337,
|
||||
Close: 1337,
|
||||
Volume: 1337,
|
||||
},
|
||||
})
|
||||
d.Next()
|
||||
open := d.StreamOpen()
|
||||
if len(open) == 0 {
|
||||
t.Error("expected open")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStreamVolume(t *testing.T) {
|
||||
t.Parallel()
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
d := DataFromKline{}
|
||||
bad := d.StreamVol()
|
||||
if len(bad) > 0 {
|
||||
t.Error("expected no stream")
|
||||
}
|
||||
d.SetStream([]common.DataEventHandler{
|
||||
&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: time.Now(),
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
High: 1337,
|
||||
Low: 1337,
|
||||
Close: 1337,
|
||||
Volume: 1337,
|
||||
},
|
||||
})
|
||||
d.Next()
|
||||
open := d.StreamVol()
|
||||
if len(open) == 0 {
|
||||
t.Error("expected volume")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStreamClose(t *testing.T) {
|
||||
t.Parallel()
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
d := DataFromKline{}
|
||||
bad := d.StreamClose()
|
||||
if len(bad) > 0 {
|
||||
t.Error("expected no stream")
|
||||
}
|
||||
d.SetStream([]common.DataEventHandler{
|
||||
&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: time.Now(),
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
High: 1337,
|
||||
Low: 1337,
|
||||
Close: 1337,
|
||||
Volume: 1337,
|
||||
},
|
||||
})
|
||||
d.Next()
|
||||
open := d.StreamClose()
|
||||
if len(open) == 0 {
|
||||
t.Error("expected close")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStreamHigh(t *testing.T) {
|
||||
t.Parallel()
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
d := DataFromKline{}
|
||||
bad := d.StreamHigh()
|
||||
if len(bad) > 0 {
|
||||
t.Error("expected no stream")
|
||||
}
|
||||
d.SetStream([]common.DataEventHandler{
|
||||
&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: time.Now(),
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
High: 1337,
|
||||
Low: 1337,
|
||||
Close: 1337,
|
||||
Volume: 1337,
|
||||
},
|
||||
})
|
||||
d.Next()
|
||||
open := d.StreamHigh()
|
||||
if len(open) == 0 {
|
||||
t.Error("expected high")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStreamLow(t *testing.T) {
|
||||
t.Parallel()
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
d := DataFromKline{}
|
||||
bad := d.StreamLow()
|
||||
if len(bad) > 0 {
|
||||
t.Error("expected no stream")
|
||||
}
|
||||
d.SetStream([]common.DataEventHandler{
|
||||
&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: time.Now(),
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
High: 1337,
|
||||
Low: 1337,
|
||||
Close: 1337,
|
||||
Volume: 1337,
|
||||
},
|
||||
})
|
||||
d.Next()
|
||||
open := d.StreamLow()
|
||||
if len(open) == 0 {
|
||||
t.Error("expected low")
|
||||
}
|
||||
}
|
||||
21
backtester/data/kline/kline_types.go
Normal file
21
backtester/data/kline/kline_types.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package kline
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
)
|
||||
|
||||
var errNoCandleData = errors.New("no candle data provided")
|
||||
|
||||
// DataFromKline is a struct which implements the data.Streamer interface
|
||||
// It holds candle data for a specified range with helper functions
|
||||
type DataFromKline struct {
|
||||
Item gctkline.Item
|
||||
data.Base
|
||||
Range gctkline.IntervalRangeHolder
|
||||
|
||||
addedTimes map[time.Time]bool
|
||||
}
|
||||
47
backtester/data/kline/live/README.md
Normal file
47
backtester/data/kline/live/README.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# GoCryptoTrader Backtester: Live package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/data/kline/live)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This live package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Live package overview
|
||||
|
||||
This package will retrieve data for the backtester via continuous requests to live endpoints
|
||||
|
||||
## Important notice
|
||||
Live trading is not fully implemented and you should never consider setting `RealOrders` to `true` in a config. *Past performance is no guarantee of future results*
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
65
backtester/data/kline/live/live.go
Normal file
65
backtester/data/kline/live/live.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package live
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
|
||||
)
|
||||
|
||||
// LoadData retrieves data from a GoCryptoTrader exchange wrapper which calls the exchange's API for the latest interval
|
||||
func LoadData(exch exchange.IBotExchange, dataType int64, interval time.Duration, fPair currency.Pair, a asset.Item) (*kline.Item, error) {
|
||||
var candles kline.Item
|
||||
var err error
|
||||
switch dataType {
|
||||
case common.DataCandle:
|
||||
candles, err = exch.GetHistoricCandles(
|
||||
fPair,
|
||||
a,
|
||||
time.Now().Add(-interval), // multiplied by 2 to ensure the latest candle is always included
|
||||
time.Now(),
|
||||
kline.Interval(interval))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not retrieve live candle data for %v %v %v, %v", exch.GetName(), a, fPair, err)
|
||||
}
|
||||
case common.DataTrade:
|
||||
var trades []trade.Data
|
||||
trades, err = exch.GetRecentTrades(
|
||||
fPair,
|
||||
a)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
candles, err = trade.ConvertTradesToCandles(kline.Interval(interval), trades...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
base := exch.GetBase()
|
||||
if len(candles.Candles) <= 1 && base.GetSupportedFeatures().RESTCapabilities.TradeHistory {
|
||||
trades, err = exch.GetHistoricTrades(
|
||||
fPair,
|
||||
a,
|
||||
time.Now().Add(-interval), // multiplied by 2 to ensure the latest candle is always included
|
||||
time.Now())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not retrieve live trade data for %v %v %v, %v", exch.GetName(), a, fPair, err)
|
||||
}
|
||||
|
||||
candles, err = trade.ConvertTradesToCandles(kline.Interval(interval), trades...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not convert live trade data to candles for %v %v %v, %v", exch.GetName(), a, fPair, err)
|
||||
}
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("could not retrieve live data for %v %v %v, invalid data type received", exch.GetName(), a, fPair)
|
||||
}
|
||||
candles.Exchange = strings.ToLower(exch.GetName())
|
||||
return &candles, nil
|
||||
}
|
||||
82
backtester/data/kline/live/live_test.go
Normal file
82
backtester/data/kline/live/live_test.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package live
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/engine"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
)
|
||||
|
||||
const testExchange = "binance"
|
||||
|
||||
func TestLoadCandles(t *testing.T) {
|
||||
t.Parallel()
|
||||
interval := gctkline.FifteenMin
|
||||
bot, err := engine.NewFromSettings(&engine.Settings{
|
||||
ConfigFile: filepath.Join("..", "..", "..", "..", "testdata", "configtest.json"),
|
||||
EnableDryRun: true,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = bot.LoadExchange(testExchange, false, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
exch := bot.GetExchangeByName(testExchange)
|
||||
if exch == nil {
|
||||
t.Fatal("expected binance")
|
||||
}
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
var data *gctkline.Item
|
||||
data, err = LoadData(exch, common.DataCandle, interval.Duration(), p, a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(data.Candles) == 0 {
|
||||
t.Error("expected candles")
|
||||
}
|
||||
|
||||
_, err = LoadData(exch, -1, interval.Duration(), p, a)
|
||||
if err != nil && !strings.Contains(err.Error(), "could not retrieve live data for Binance spot BTCUSDT, invalid data type received") {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadTrades(t *testing.T) {
|
||||
t.Parallel()
|
||||
interval := gctkline.FifteenMin
|
||||
bot, err := engine.NewFromSettings(&engine.Settings{
|
||||
ConfigFile: filepath.Join("..", "..", "..", "..", "testdata", "configtest.json"),
|
||||
EnableDryRun: true,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = bot.LoadExchange(testExchange, false, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
exch := bot.GetExchangeByName(testExchange)
|
||||
if exch == nil {
|
||||
t.Fatal("expected binance")
|
||||
}
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
var data *gctkline.Item
|
||||
data, err = LoadData(exch, common.DataTrade, interval.Duration(), p, a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(data.Candles) == 0 {
|
||||
t.Error("expected candles")
|
||||
}
|
||||
}
|
||||
47
backtester/eventhandlers/README.md
Normal file
47
backtester/eventhandlers/README.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# GoCryptoTrader Backtester: Eventhandlers package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This eventhandlers package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Eventhandlers overview
|
||||
|
||||
Event handlers are responsible for taking in an event, analysing its contents and outputting another event to be handled. An individual candle is turned into a data event which handled via the strategy event handler. The strategy handler outputs a signal event, which the portfolio eventhandler will size and risk analyse before raising an order event. The event is then sent to the portfolio manager to determine whether there is appropriate funding, adequate risk and proper order sizing before raising an order event. The order event is taken to the exchange handler which will place the order and create a fill event.
|
||||
Below is an overview of how event handlers are used
|
||||

|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
46
backtester/eventhandlers/eventholder/README.md
Normal file
46
backtester/eventhandlers/eventholder/README.md
Normal file
@@ -0,0 +1,46 @@
|
||||
# GoCryptoTrader Backtester: Eventholder package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/eventholder)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This eventholder package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Eventholder package overview
|
||||
|
||||
The event holder is a simple interface implementation which allows the backtester to iterate over the event queue.
|
||||
The event holder is based on the `EventHolder` interface and is implemented by `Holder`.
|
||||
It is used by `backtest.Backtester` and it accepts appending any struct which implements the `common.EventHandler` interface, eg `order.Order`
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
27
backtester/eventhandlers/eventholder/eventholder.go
Normal file
27
backtester/eventhandlers/eventholder/eventholder.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package eventholder
|
||||
|
||||
import (
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
)
|
||||
|
||||
// Reset returns struct to defaults
|
||||
func (e *Holder) Reset() {
|
||||
e.Queue = nil
|
||||
}
|
||||
|
||||
// AppendEvent adds and event to the queue
|
||||
func (e *Holder) AppendEvent(i common.EventHandler) {
|
||||
e.Queue = append(e.Queue, i)
|
||||
}
|
||||
|
||||
// NextEvent removes the current event and returns the next event in the queue
|
||||
func (e *Holder) NextEvent() (i common.EventHandler) {
|
||||
if len(e.Queue) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
i = e.Queue[0]
|
||||
e.Queue = e.Queue[1:]
|
||||
|
||||
return i
|
||||
}
|
||||
48
backtester/eventhandlers/eventholder/eventholder_test.go
Normal file
48
backtester/eventhandlers/eventholder/eventholder_test.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package eventholder
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
)
|
||||
|
||||
func TestReset(t *testing.T) {
|
||||
t.Parallel()
|
||||
e := Holder{Queue: []common.EventHandler{}}
|
||||
e.Reset()
|
||||
if e.Queue != nil {
|
||||
t.Error("expected nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendEvent(t *testing.T) {
|
||||
t.Parallel()
|
||||
e := Holder{Queue: []common.EventHandler{}}
|
||||
e.AppendEvent(&order.Order{})
|
||||
if len(e.Queue) != 1 {
|
||||
t.Error("expected 1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNextEvent(t *testing.T) {
|
||||
t.Parallel()
|
||||
e := Holder{Queue: []common.EventHandler{}}
|
||||
ev := e.NextEvent()
|
||||
if ev != nil {
|
||||
t.Error("expected not ok")
|
||||
}
|
||||
|
||||
e = Holder{Queue: []common.EventHandler{
|
||||
&order.Order{},
|
||||
&order.Order{},
|
||||
&order.Order{},
|
||||
}}
|
||||
if len(e.Queue) != 3 {
|
||||
t.Error("expected 3")
|
||||
}
|
||||
e.NextEvent()
|
||||
if len(e.Queue) != 2 {
|
||||
t.Error("expected 2")
|
||||
}
|
||||
}
|
||||
17
backtester/eventhandlers/eventholder/eventholder_types.go
Normal file
17
backtester/eventhandlers/eventholder/eventholder_types.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package eventholder
|
||||
|
||||
import (
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
)
|
||||
|
||||
// Holder contains the event queue for backtester processing
|
||||
type Holder struct {
|
||||
Queue []common.EventHandler
|
||||
}
|
||||
|
||||
// EventHolder interface details what is expected of an event holder to perform
|
||||
type EventHolder interface {
|
||||
Reset()
|
||||
AppendEvent(common.EventHandler)
|
||||
NextEvent() (e common.EventHandler)
|
||||
}
|
||||
58
backtester/eventhandlers/exchange/README.md
Normal file
58
backtester/eventhandlers/exchange/README.md
Normal file
@@ -0,0 +1,58 @@
|
||||
# GoCryptoTrader Backtester: Exchange package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/exchange)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This exchange package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Exchange package overview
|
||||
|
||||
The exchange eventhandler is responsible for calling the `engine` package's `ordermanager` to place either a fake, or real order on the exchange via API.
|
||||
|
||||
The following steps are taken for the `ExecuteOrder` function:
|
||||
|
||||
- Calculate slippage. If the order is a sell order, it will reduce the price by a random percentage between the two values. If it is a buy order, it will raise the price by a random percentage between the two values
|
||||
- If `RealOrders` is set to `false`:
|
||||
- It will estimate the slippage based on what is in the config file under `min-slippage-percent` and `max-slippage-percent`.
|
||||
- It will be sized within the constraints of the current candles OHLCV values
|
||||
- It will generate the exchange fee based on what is stored in the config for the exchange asset currency pair
|
||||
- If `RealOrders` is set to `true`, it will use the latest orderbook data to calculate slippage by simulating the order
|
||||
- Place the order with the engine order manager
|
||||
- If `RealOrders` is set to `false` it will submit the order with no calls to the exchange's API, use no API credentials and it will always pass
|
||||
- If `RealOrders` is set to `true` it will submit the order via the exchange's API and if successful, will be stored in the order manager
|
||||
- If an order is successfully placed, a snapshot of all existing orders in the run will be captured and store for statistical purposes
|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
269
backtester/eventhandlers/exchange/exchange.go
Normal file
269
backtester/eventhandlers/exchange/exchange.go
Normal file
@@ -0,0 +1,269 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/exchange/slippage"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/event"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/fill"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/engine"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
|
||||
)
|
||||
|
||||
// Reset returns the exchange to initial settings
|
||||
func (e *Exchange) Reset() {
|
||||
*e = Exchange{}
|
||||
}
|
||||
|
||||
// ExecuteOrder assesses the portfolio manager's order event and if it passes validation
|
||||
// will send an order to the exchange/fake order manager to be stored and raise a fill event
|
||||
func (e *Exchange) ExecuteOrder(o order.Event, data data.Handler, bot *engine.Engine) (*fill.Fill, error) {
|
||||
f := &fill.Fill{
|
||||
Base: event.Base{
|
||||
Offset: o.GetOffset(),
|
||||
Exchange: o.GetExchange(),
|
||||
Time: o.GetTime(),
|
||||
CurrencyPair: o.Pair(),
|
||||
AssetType: o.GetAssetType(),
|
||||
Interval: o.GetInterval(),
|
||||
Reason: o.GetReason(),
|
||||
},
|
||||
Direction: o.GetDirection(),
|
||||
Amount: o.GetAmount(),
|
||||
|
||||
ClosePrice: data.Latest().ClosePrice(),
|
||||
}
|
||||
|
||||
cs, err := e.GetCurrencySettings(o.GetExchange(), o.GetAssetType(), o.Pair())
|
||||
if err != nil {
|
||||
return f, err
|
||||
}
|
||||
|
||||
f.ExchangeFee = cs.ExchangeFee // defaulting to just using taker fee right now without orderbook
|
||||
f.Direction = o.GetDirection()
|
||||
if o.GetDirection() != gctorder.Buy && o.GetDirection() != gctorder.Sell {
|
||||
return f, nil
|
||||
}
|
||||
highStr := data.StreamHigh()
|
||||
high := highStr[len(highStr)-1]
|
||||
|
||||
lowStr := data.StreamLow()
|
||||
low := lowStr[len(lowStr)-1]
|
||||
|
||||
volStr := data.StreamVol()
|
||||
volume := volStr[len(volStr)-1]
|
||||
var adjustedPrice, amount float64
|
||||
if cs.UseRealOrders {
|
||||
// get current orderbook
|
||||
var ob *orderbook.Base
|
||||
ob, err = orderbook.Get(f.Exchange, f.CurrencyPair, f.AssetType)
|
||||
if err != nil {
|
||||
return f, err
|
||||
}
|
||||
// calculate an estimated slippage rate
|
||||
adjustedPrice, amount = slippage.CalculateSlippageByOrderbook(ob, o.GetDirection(), o.GetFunds(), f.ExchangeFee)
|
||||
f.Slippage = ((adjustedPrice - f.ClosePrice) / f.ClosePrice) * 100
|
||||
} else {
|
||||
adjustedPrice, amount, err = e.sizeOfflineOrder(high, low, volume, &cs, f)
|
||||
if err != nil {
|
||||
switch f.GetDirection() {
|
||||
case gctorder.Buy:
|
||||
f.SetDirection(common.CouldNotBuy)
|
||||
case gctorder.Sell:
|
||||
f.SetDirection(common.CouldNotSell)
|
||||
default:
|
||||
f.SetDirection(common.DoNothing)
|
||||
}
|
||||
f.AppendReason(err.Error())
|
||||
return f, err
|
||||
}
|
||||
}
|
||||
reducedAmount := reduceAmountToFitPortfolioLimit(adjustedPrice, amount, o.GetFunds())
|
||||
if reducedAmount != amount {
|
||||
f.AppendReason(fmt.Sprintf("Order size shrunk from %v to %v to remain within portfolio limits", amount, reducedAmount))
|
||||
}
|
||||
|
||||
var orderID string
|
||||
orderID, err = e.placeOrder(adjustedPrice, reducedAmount, cs.UseRealOrders, f, bot)
|
||||
if err != nil {
|
||||
return f, err
|
||||
}
|
||||
ords, _ := bot.OrderManager.GetOrdersSnapshot("")
|
||||
for i := range ords {
|
||||
if ords[i].ID != orderID {
|
||||
continue
|
||||
}
|
||||
ords[i].Date = o.GetTime()
|
||||
ords[i].LastUpdated = o.GetTime()
|
||||
ords[i].CloseTime = o.GetTime()
|
||||
f.Order = &ords[i]
|
||||
f.PurchasePrice = ords[i].Price
|
||||
f.Total = (f.PurchasePrice * reducedAmount) + f.ExchangeFee
|
||||
}
|
||||
|
||||
if f.Order == nil {
|
||||
return nil, fmt.Errorf("placed order %v not found in order manager", orderID)
|
||||
}
|
||||
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func reduceAmountToFitPortfolioLimit(adjustedPrice, amount, sizedPortfolioTotal float64) float64 {
|
||||
if adjustedPrice*amount > sizedPortfolioTotal {
|
||||
// adjusted amounts exceeds portfolio manager's allowed funds
|
||||
// the amount has to be reduced to equal the sizedPortfolioTotal
|
||||
amount = sizedPortfolioTotal / adjustedPrice
|
||||
}
|
||||
return amount
|
||||
}
|
||||
|
||||
func (e *Exchange) placeOrder(price, amount float64, useRealOrders bool, f *fill.Fill, bot *engine.Engine) (string, error) {
|
||||
if f == nil {
|
||||
return "", common.ErrNilEvent
|
||||
}
|
||||
u, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var orderID string
|
||||
o := &gctorder.Submit{
|
||||
Price: price,
|
||||
Amount: amount,
|
||||
Fee: f.ExchangeFee,
|
||||
Exchange: f.Exchange,
|
||||
ID: u.String(),
|
||||
Side: f.Direction,
|
||||
AssetType: f.AssetType,
|
||||
Date: f.GetTime(),
|
||||
LastUpdated: f.GetTime(),
|
||||
Pair: f.Pair(),
|
||||
Type: gctorder.Market,
|
||||
}
|
||||
|
||||
if useRealOrders {
|
||||
resp, err := bot.OrderManager.Submit(o)
|
||||
if resp != nil {
|
||||
orderID = resp.OrderID
|
||||
}
|
||||
if err != nil {
|
||||
return orderID, err
|
||||
}
|
||||
} else {
|
||||
submitResponse := gctorder.SubmitResponse{
|
||||
IsOrderPlaced: true,
|
||||
OrderID: u.String(),
|
||||
Rate: f.Amount,
|
||||
Fee: f.ExchangeFee,
|
||||
Cost: price,
|
||||
FullyMatched: true,
|
||||
}
|
||||
resp, err := bot.OrderManager.SubmitFakeOrder(o, submitResponse)
|
||||
if resp != nil {
|
||||
orderID = resp.OrderID
|
||||
}
|
||||
if err != nil {
|
||||
return orderID, err
|
||||
}
|
||||
}
|
||||
return orderID, nil
|
||||
}
|
||||
|
||||
func (e *Exchange) sizeOfflineOrder(high, low, volume float64, cs *Settings, f *fill.Fill) (adjustedPrice, adjustedAmount float64, err error) {
|
||||
if cs == nil || f == nil {
|
||||
return 0, 0, common.ErrNilArguments
|
||||
}
|
||||
// provide history and estimate volatility
|
||||
slippageRate := slippage.EstimateSlippagePercentage(cs.MinimumSlippageRate, cs.MaximumSlippageRate)
|
||||
f.VolumeAdjustedPrice, adjustedAmount = ensureOrderFitsWithinHLV(f.ClosePrice, f.Amount, high, low, volume)
|
||||
if adjustedAmount != f.Amount {
|
||||
f.AppendReason(fmt.Sprintf("Order size shrunk from %v to %v to fit candle", f.Amount, adjustedAmount))
|
||||
}
|
||||
|
||||
if adjustedAmount <= 0 && f.Amount > 0 {
|
||||
return 0, 0, fmt.Errorf("amount set to 0, %w", errDataMayBeIncorrect)
|
||||
}
|
||||
adjustedPrice = applySlippageToPrice(f.GetDirection(), f.GetVolumeAdjustedPrice(), slippageRate)
|
||||
|
||||
f.Slippage = (slippageRate * 100) - 100
|
||||
f.ExchangeFee = calculateExchangeFee(adjustedPrice, adjustedAmount, cs.ExchangeFee)
|
||||
return adjustedPrice, adjustedAmount, nil
|
||||
}
|
||||
|
||||
func applySlippageToPrice(direction gctorder.Side, price, slippageRate float64) float64 {
|
||||
adjustedPrice := price
|
||||
if direction == gctorder.Buy {
|
||||
adjustedPrice = price + (price * (1 - slippageRate))
|
||||
} else if direction == gctorder.Sell {
|
||||
adjustedPrice = price * slippageRate
|
||||
}
|
||||
return adjustedPrice
|
||||
}
|
||||
|
||||
// SetExchangeAssetCurrencySettings sets the settings for an exchange, asset, currency
|
||||
func (e *Exchange) SetExchangeAssetCurrencySettings(exch string, a asset.Item, cp currency.Pair, c *Settings) {
|
||||
if c.ExchangeName == "" ||
|
||||
c.AssetType == "" ||
|
||||
c.CurrencyPair.IsEmpty() {
|
||||
return
|
||||
}
|
||||
|
||||
for i := range e.CurrencySettings {
|
||||
if e.CurrencySettings[i].CurrencyPair == cp &&
|
||||
e.CurrencySettings[i].AssetType == a &&
|
||||
exch == e.CurrencySettings[i].ExchangeName {
|
||||
e.CurrencySettings[i] = *c
|
||||
return
|
||||
}
|
||||
}
|
||||
e.CurrencySettings = append(e.CurrencySettings, *c)
|
||||
}
|
||||
|
||||
// GetCurrencySettings returns the settings for an exchange, asset currency
|
||||
func (e *Exchange) GetCurrencySettings(exch string, a asset.Item, cp currency.Pair) (Settings, error) {
|
||||
for i := range e.CurrencySettings {
|
||||
if e.CurrencySettings[i].CurrencyPair == cp {
|
||||
if e.CurrencySettings[i].AssetType == a {
|
||||
if exch == e.CurrencySettings[i].ExchangeName {
|
||||
return e.CurrencySettings[i], nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Settings{}, fmt.Errorf("no currency settings found for %v %v %v", exch, a, cp)
|
||||
}
|
||||
|
||||
func ensureOrderFitsWithinHLV(slippagePrice, amount, high, low, volume float64) (adjustedPrice, adjustedAmount float64) {
|
||||
adjustedPrice = slippagePrice
|
||||
if adjustedPrice < low {
|
||||
adjustedPrice = low
|
||||
}
|
||||
if adjustedPrice > high {
|
||||
adjustedPrice = high
|
||||
}
|
||||
if volume <= 0 {
|
||||
return adjustedPrice, adjustedAmount
|
||||
}
|
||||
currentVolume := amount * adjustedPrice
|
||||
if currentVolume > volume {
|
||||
// reduce the volume to not exceed the total volume of the candle
|
||||
// it is slightly less than the total to still allow for the illusion
|
||||
// that open high low close values are valid with the remaining volume
|
||||
// this is very opinionated
|
||||
currentVolume = volume * 0.99999999
|
||||
}
|
||||
// extract the amount from the adjusted volume
|
||||
adjustedAmount = currentVolume / adjustedPrice
|
||||
|
||||
return adjustedPrice, adjustedAmount
|
||||
}
|
||||
|
||||
func calculateExchangeFee(price, amount, fee float64) float64 {
|
||||
return fee * price * amount
|
||||
}
|
||||
294
backtester/eventhandlers/exchange/exchange_test.go
Normal file
294
backtester/eventhandlers/exchange/exchange_test.go
Normal file
@@ -0,0 +1,294 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/config"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/event"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/fill"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/engine"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
const testExchange = "binance"
|
||||
|
||||
func TestReset(t *testing.T) {
|
||||
t.Parallel()
|
||||
e := Exchange{
|
||||
CurrencySettings: []Settings{},
|
||||
}
|
||||
e.Reset()
|
||||
if e.CurrencySettings != nil {
|
||||
t.Error("expected nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetCurrency(t *testing.T) {
|
||||
t.Parallel()
|
||||
e := Exchange{}
|
||||
e.SetExchangeAssetCurrencySettings("", "", currency.Pair{}, &Settings{})
|
||||
if len(e.CurrencySettings) != 0 {
|
||||
t.Error("expected 0")
|
||||
}
|
||||
cs := &Settings{
|
||||
ExchangeName: testExchange,
|
||||
UseRealOrders: false,
|
||||
InitialFunds: 1337,
|
||||
CurrencyPair: currency.NewPair(currency.BTC, currency.USDT),
|
||||
AssetType: asset.Spot,
|
||||
ExchangeFee: 0,
|
||||
MakerFee: 0,
|
||||
TakerFee: 0,
|
||||
BuySide: config.MinMax{},
|
||||
SellSide: config.MinMax{},
|
||||
Leverage: config.Leverage{},
|
||||
MinimumSlippageRate: 0,
|
||||
MaximumSlippageRate: 0,
|
||||
}
|
||||
e.SetExchangeAssetCurrencySettings(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.USDT), cs)
|
||||
result, err := e.GetCurrencySettings(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.USDT))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if result.InitialFunds != 1337 {
|
||||
t.Errorf("expected 1337, received %v", result.InitialFunds)
|
||||
}
|
||||
|
||||
e.SetExchangeAssetCurrencySettings(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.USDT), cs)
|
||||
if len(e.CurrencySettings) != 1 {
|
||||
t.Error("expected 1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureOrderFitsWithinHLV(t *testing.T) {
|
||||
t.Parallel()
|
||||
adjustedPrice, adjustedAmount := ensureOrderFitsWithinHLV(123, 1, 100, 99, 100)
|
||||
if adjustedAmount != 1 {
|
||||
t.Error("expected 1")
|
||||
}
|
||||
if adjustedPrice != 100 {
|
||||
t.Error("expected 100")
|
||||
}
|
||||
|
||||
adjustedPrice, adjustedAmount = ensureOrderFitsWithinHLV(123, 1, 100, 99, 80)
|
||||
if adjustedAmount != 0.7999999919999999 {
|
||||
t.Errorf("expected %v received %v", 0.7999999919999999, adjustedAmount)
|
||||
}
|
||||
if adjustedPrice != 100 {
|
||||
t.Error("expected 100")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalculateExchangeFee(t *testing.T) {
|
||||
t.Parallel()
|
||||
fee := calculateExchangeFee(1, 1, 0.1)
|
||||
if fee != 0.1 {
|
||||
t.Error("expected 0.1")
|
||||
}
|
||||
fee = calculateExchangeFee(2, 1, 0.005)
|
||||
if fee != 0.01 {
|
||||
t.Error("expected 0.01")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSizeOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
e := Exchange{}
|
||||
_, _, err := e.sizeOfflineOrder(0, 0, 0, nil, nil)
|
||||
if !errors.Is(err, common.ErrNilArguments) {
|
||||
t.Error(err)
|
||||
}
|
||||
cs := &Settings{}
|
||||
f := &fill.Fill{
|
||||
ClosePrice: 1337,
|
||||
Amount: 1,
|
||||
}
|
||||
_, _, err = e.sizeOfflineOrder(0, 0, 0, cs, f)
|
||||
if !errors.Is(err, errDataMayBeIncorrect) {
|
||||
t.Errorf("expected: %v, received %v", errDataMayBeIncorrect, err)
|
||||
}
|
||||
var p, a float64
|
||||
p, a, err = e.sizeOfflineOrder(10, 2, 10, cs, f)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if p != 10 {
|
||||
t.Error("expected 10")
|
||||
}
|
||||
if a != 1 {
|
||||
t.Error("expected 1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlaceOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
bot, err := engine.NewFromSettings(&engine.Settings{
|
||||
ConfigFile: filepath.Join("..", "..", "..", "testdata", "configtest.json"),
|
||||
EnableDryRun: true,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = bot.OrderManager.Start(bot)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = bot.LoadExchange(testExchange, false, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
e := Exchange{}
|
||||
_, err = e.placeOrder(1, 1, false, nil, nil)
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilEvent, err)
|
||||
}
|
||||
f := &fill.Fill{}
|
||||
_, err = e.placeOrder(1, 1, false, f, bot)
|
||||
if err != nil && err.Error() != "order exchange name must be specified" {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
f.Exchange = testExchange
|
||||
_, err = e.placeOrder(1, 1, false, f, bot)
|
||||
if err != nil && err.Error() != "order pair is empty" {
|
||||
t.Error(err)
|
||||
}
|
||||
f.CurrencyPair = currency.NewPair(currency.BTC, currency.USDT)
|
||||
f.AssetType = asset.Spot
|
||||
f.Direction = gctorder.Buy
|
||||
_, err = e.placeOrder(1, 1, false, f, bot)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
_, err = e.placeOrder(1, 1, true, f, bot)
|
||||
if err != nil && !strings.Contains(err.Error(), "unset/default API keys") {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
a := asset.Spot
|
||||
cs := Settings{
|
||||
ExchangeName: testExchange,
|
||||
UseRealOrders: false,
|
||||
InitialFunds: 1337,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
ExchangeFee: 0.01,
|
||||
MakerFee: 0.01,
|
||||
TakerFee: 0.01,
|
||||
BuySide: config.MinMax{},
|
||||
SellSide: config.MinMax{},
|
||||
Leverage: config.Leverage{},
|
||||
MinimumSlippageRate: 0,
|
||||
MaximumSlippageRate: 1,
|
||||
}
|
||||
e := Exchange{
|
||||
CurrencySettings: []Settings{cs},
|
||||
}
|
||||
ev := event.Base{
|
||||
Exchange: testExchange,
|
||||
Time: time.Now(),
|
||||
Interval: gctkline.FifteenMin,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
}
|
||||
o := &order.Order{
|
||||
Base: ev,
|
||||
Direction: gctorder.Buy,
|
||||
Amount: 1,
|
||||
Funds: 1337,
|
||||
}
|
||||
|
||||
bot, err := engine.NewFromSettings(&engine.Settings{
|
||||
ConfigFile: filepath.Join("..", "..", "..", "testdata", "configtest.json"),
|
||||
EnableDryRun: true,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = bot.OrderManager.Start(bot)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = bot.LoadExchange(testExchange, false, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
b := bot.GetExchangeByName(testExchange)
|
||||
_, err = b.FetchOrderbook(p, a)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
d := &kline.DataFromKline{
|
||||
Item: gctkline.Item{
|
||||
Exchange: "",
|
||||
Pair: currency.Pair{},
|
||||
Asset: "",
|
||||
Interval: 0,
|
||||
Candles: []gctkline.Candle{
|
||||
{
|
||||
Close: 1,
|
||||
High: 1,
|
||||
Low: 1,
|
||||
Volume: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
err = d.Load()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
d.Next()
|
||||
_, err = e.ExecuteOrder(o, d, bot)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
cs.UseRealOrders = true
|
||||
o.Direction = gctorder.Sell
|
||||
e.CurrencySettings = []Settings{cs}
|
||||
_, err = e.ExecuteOrder(o, d, bot)
|
||||
if err != nil && !strings.Contains(err.Error(), "unset/default API keys") {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplySlippageToPrice(t *testing.T) {
|
||||
t.Parallel()
|
||||
resp := applySlippageToPrice(gctorder.Buy, 1, 0.9)
|
||||
if resp != 1.1 {
|
||||
t.Errorf("expected 1.1, received %v", resp)
|
||||
}
|
||||
resp = applySlippageToPrice(gctorder.Sell, 1, 0.9)
|
||||
if resp != 0.9 {
|
||||
t.Errorf("expected 0.9, received %v", resp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReduceAmountToFitPortfolioLimit(t *testing.T) {
|
||||
t.Parallel()
|
||||
initialPrice := 1003.37
|
||||
initialAmount := 1337 / initialPrice
|
||||
portfolioAdjustedTotal := initialAmount * initialPrice
|
||||
adjustedPrice := 1000.0
|
||||
amount := 2.0
|
||||
finalAmount := reduceAmountToFitPortfolioLimit(adjustedPrice, amount, portfolioAdjustedTotal)
|
||||
if finalAmount*adjustedPrice != portfolioAdjustedTotal {
|
||||
t.Errorf("expected value %v to match portfolio total %v", finalAmount*adjustedPrice, portfolioAdjustedTotal)
|
||||
}
|
||||
}
|
||||
54
backtester/eventhandlers/exchange/exchange_types.go
Normal file
54
backtester/eventhandlers/exchange/exchange_types.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/config"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/fill"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/engine"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
var (
|
||||
errDataMayBeIncorrect = errors.New("data may be incorrect")
|
||||
errExchangeUnset = errors.New("exchange unset")
|
||||
)
|
||||
|
||||
// ExecutionHandler interface dictates what functions are required to submit an order
|
||||
type ExecutionHandler interface {
|
||||
SetExchangeAssetCurrencySettings(string, asset.Item, currency.Pair, *Settings)
|
||||
GetCurrencySettings(string, asset.Item, currency.Pair) (Settings, error)
|
||||
ExecuteOrder(order.Event, data.Handler, *engine.Engine) (*fill.Fill, error)
|
||||
Reset()
|
||||
}
|
||||
|
||||
// Exchange contains all the currency settings
|
||||
type Exchange struct {
|
||||
CurrencySettings []Settings
|
||||
}
|
||||
|
||||
// Settings allow the eventhandler to size an order within the limitations set by the config file
|
||||
type Settings struct {
|
||||
ExchangeName string
|
||||
UseRealOrders bool
|
||||
|
||||
InitialFunds float64
|
||||
|
||||
CurrencyPair currency.Pair
|
||||
AssetType asset.Item
|
||||
|
||||
ExchangeFee float64
|
||||
MakerFee float64
|
||||
TakerFee float64
|
||||
|
||||
BuySide config.MinMax
|
||||
SellSide config.MinMax
|
||||
|
||||
Leverage config.Leverage
|
||||
|
||||
MinimumSlippageRate float64
|
||||
MaximumSlippageRate float64
|
||||
}
|
||||
54
backtester/eventhandlers/exchange/slippage/README.md
Normal file
54
backtester/eventhandlers/exchange/slippage/README.md
Normal file
@@ -0,0 +1,54 @@
|
||||
# GoCryptoTrader Backtester: Slippage package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/exchange/slippage)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This slippage package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Slippage package overview
|
||||
|
||||
Slippage refers to the difference between the expected price of a trade and the price at which the trade is executed. Slippage is used here to simulate what would occur if trading was live as no perfect conditions exist for placing orders.
|
||||
Slippage is calculated in two ways in the GoCryptoTrader Backtester
|
||||
|
||||
### If `RealOrders` is `true`
|
||||
- The orderbook is frequently requested during live cycle candle retrieval
|
||||
- When the order is being calculated in the `ExecuteOrder` eventhandler, it will use the orderbook to simulate placing the order and adjust the order price
|
||||
|
||||
### If `RealOrders` is `false`
|
||||
- The `min-slippage-percent` and `max-slippage-percent` values for the specific exchange, asset and currency pair will be used as bounds to simulate an orderbook using a random number
|
||||
- If it is a buy order, it will raise the price by a random percentage between the two values
|
||||
- If the order is a sell order, it will reduce the price by a random percentage between the two values
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
38
backtester/eventhandlers/exchange/slippage/slippage.go
Normal file
38
backtester/eventhandlers/exchange/slippage/slippage.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package slippage
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
|
||||
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
|
||||
)
|
||||
|
||||
// EstimateSlippagePercentage takes in an int range of numbers
|
||||
// turns it into a percentage
|
||||
func EstimateSlippagePercentage(maximumSlippageRate, minimumSlippageRate float64) float64 {
|
||||
if minimumSlippageRate < 1 || minimumSlippageRate > 100 {
|
||||
return 1
|
||||
}
|
||||
if maximumSlippageRate < 1 || maximumSlippageRate > 100 {
|
||||
return 1
|
||||
}
|
||||
|
||||
// the language here is confusing. The maximum slippage rate is the lower bounds of the number,
|
||||
// eg 80 means for every dollar, keep 80%
|
||||
randSeed := int(minimumSlippageRate) - int(maximumSlippageRate)
|
||||
if randSeed > 0 {
|
||||
result := float64(rand.Intn(randSeed)) // nolint:gosec // basic number generation required, no need for crypto/rand
|
||||
return (result + maximumSlippageRate) / 100
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
// CalculateSlippageByOrderbook will analyse a provided orderbook and return the result of attempting to
|
||||
// place the order on there
|
||||
func CalculateSlippageByOrderbook(ob *orderbook.Base, side gctorder.Side, amountOfFunds, feeRate float64) (price, amount float64) {
|
||||
result := ob.SimulateOrder(amountOfFunds, side == gctorder.Buy)
|
||||
rate := (result.MinimumPrice - result.MaximumPrice) / result.MaximumPrice
|
||||
price = result.MinimumPrice * (rate + 1)
|
||||
amount = result.Amount * (1 - feeRate)
|
||||
return
|
||||
}
|
||||
35
backtester/eventhandlers/exchange/slippage/slippage_test.go
Normal file
35
backtester/eventhandlers/exchange/slippage/slippage_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package slippage
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/bitstamp"
|
||||
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
func TestRandomSlippage(t *testing.T) {
|
||||
t.Parallel()
|
||||
resp := EstimateSlippagePercentage(80, 100)
|
||||
if resp < 0.8 || resp > 1 {
|
||||
t.Error("expected result > 0.8 and < 100")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalculateSlippageByOrderbook(t *testing.T) {
|
||||
t.Parallel()
|
||||
b := bitstamp.Bitstamp{}
|
||||
b.SetDefaults()
|
||||
cp := currency.NewPair(currency.BTC, currency.USD)
|
||||
ob, err := b.FetchOrderbook(cp, asset.Spot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
amountOfFunds := 1000.0
|
||||
feeRate := 0.03
|
||||
price, amount := CalculateSlippageByOrderbook(ob, gctorder.Buy, amountOfFunds, feeRate)
|
||||
if price*amount+(price*amount*feeRate) > amountOfFunds {
|
||||
t.Error("order size must be less than funds")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package slippage
|
||||
|
||||
// Default slippage rates. It works on a percentage basis
|
||||
// 100 means unaffected, 95 would mean 95%
|
||||
const (
|
||||
DefaultMaximumSlippagePercent = 100
|
||||
DefaultMinimumSlippagePercent = 100
|
||||
)
|
||||
68
backtester/eventhandlers/portfolio/README.md
Normal file
68
backtester/eventhandlers/portfolio/README.md
Normal file
@@ -0,0 +1,68 @@
|
||||
# GoCryptoTrader Backtester: Portfolio package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This portfolio package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Portfolio package overview
|
||||
|
||||
The portfolio is one of the most critical packages in the GoCryptoTrader Backtester. It is responsible for making sure that all orders, simulated or otherwise are within all defined risk and sizing rules defined in the config.
|
||||
The portfolio receives three kinds of events to be processed: `OnSignal`, `OnFill` and `Update`
|
||||
|
||||
The following steps are taken for the `OnSignal` function:
|
||||
- Retrieve previous iteration's holdings data
|
||||
- If a buy order signal is received, ensure there are enough funds
|
||||
- If a sell order signal is received, ensure there are any holdings to sell
|
||||
- If any other direction, return
|
||||
- The portfolio manager will then size the order according to the exchange asset currency pair's settings along with the portfolio manager's own sizing rules
|
||||
- In the event that the order is to large, the sizing package will reduce the order until it fits that limit, inclusive of fees.
|
||||
- When an order is sized under the limits, an order event cannot be raised an no order will be submitted by the exchange
|
||||
- The portfolio manager's sizing rules override any CurrencySettings' rules if the sizing is outside the portfolio manager's
|
||||
- The portfolio manager will then assess the risk of the order, it will compare existing holdings and ensures that if an order is placed, it will not go beyond risk rules as defined in the config
|
||||
- If the risk is too high, the order signal will be changed to `CouldNotBuy`, `CouldNotSell` or `DoNothing`
|
||||
- If the order is deemed appropriate, the order event will be returned and appended to the event queue for the exchange event handler to run and place the order
|
||||
|
||||
The following steps are taken for the `OnFill` function:
|
||||
- Previous holdings are retrieved and amended with new order information.
|
||||
- The stats for the exchange asset currency pair will be updated to reflect the order and pricing
|
||||
- The order will be added to the compliance manager for analysis in future events or the statistics package
|
||||
|
||||
The following steps are taken for the `Update` function:
|
||||
- The `Update` function is called when orders are not placed, this allows for the portfolio manager to still keep track of pricing and holding statistics, while not needing to process any orders
|
||||
|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
45
backtester/eventhandlers/portfolio/compliance/README.md
Normal file
45
backtester/eventhandlers/portfolio/compliance/README.md
Normal file
@@ -0,0 +1,45 @@
|
||||
# GoCryptoTrader Backtester: Compliance package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/compliance)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This compliance package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Compliance package overview
|
||||
|
||||
The compliance manager is used to store all events at each time interval. When debugging the backtester or wanting to audit backtesting results, you can inspect every single action that has occurred during the backtesting run
|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
51
backtester/eventhandlers/portfolio/compliance/compliance.go
Normal file
51
backtester/eventhandlers/portfolio/compliance/compliance.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package compliance
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// AddSnapshot creates a snapshot in time of the orders placed to allow for finer detail tracking
|
||||
// and to protect against anything modifying order details elsewhere
|
||||
func (m *Manager) AddSnapshot(orders []SnapshotOrder, t time.Time, offset int64, overwriteExisting bool) error {
|
||||
if overwriteExisting {
|
||||
if len(m.Snapshots) == 0 {
|
||||
return errSnapshotNotFound
|
||||
}
|
||||
for i := len(m.Snapshots) - 1; i >= 0; i-- {
|
||||
if offset == m.Snapshots[i].Offset {
|
||||
m.Snapshots[i].Orders = orders
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("%w at %v", errSnapshotNotFound, offset)
|
||||
}
|
||||
m.Snapshots = append(m.Snapshots, Snapshot{
|
||||
Orders: orders,
|
||||
Timestamp: t,
|
||||
Offset: offset,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSnapshotAtTime returns the snapshot of orders a t time
|
||||
func (m *Manager) GetSnapshotAtTime(t time.Time) (Snapshot, error) {
|
||||
for i := len(m.Snapshots) - 1; i >= 0; i-- {
|
||||
if t.Equal(m.Snapshots[i].Timestamp) {
|
||||
return m.Snapshots[i], nil
|
||||
}
|
||||
}
|
||||
return Snapshot{}, fmt.Errorf("%w at %v", errSnapshotNotFound, t)
|
||||
}
|
||||
|
||||
// GetLatestSnapshot returns the snapshot of t - 1 interval
|
||||
func (m *Manager) GetLatestSnapshot() Snapshot {
|
||||
if len(m.Snapshots) == 0 {
|
||||
return Snapshot{
|
||||
Offset: 1,
|
||||
}
|
||||
}
|
||||
|
||||
return m.Snapshots[len(m.Snapshots)-1]
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package compliance
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAddSnapshot(t *testing.T) {
|
||||
t.Parallel()
|
||||
m := Manager{}
|
||||
tt := time.Now()
|
||||
err := m.AddSnapshot([]SnapshotOrder{}, tt, 1, true)
|
||||
if !errors.Is(err, errSnapshotNotFound) {
|
||||
t.Errorf("expected: %v, received %v", errSnapshotNotFound, err)
|
||||
}
|
||||
|
||||
err = m.AddSnapshot([]SnapshotOrder{}, tt, 1, false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = m.AddSnapshot([]SnapshotOrder{}, tt, 1, true)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSnapshotAtTime(t *testing.T) {
|
||||
t.Parallel()
|
||||
m := Manager{}
|
||||
tt := time.Now()
|
||||
err := m.AddSnapshot([]SnapshotOrder{
|
||||
{
|
||||
ClosePrice: 1337,
|
||||
},
|
||||
}, tt, 1, false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
var snappySnap Snapshot
|
||||
snappySnap, err = m.GetSnapshotAtTime(tt)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(snappySnap.Orders) == 0 {
|
||||
t.Fatal("expected an order")
|
||||
}
|
||||
if snappySnap.Orders[0].ClosePrice != 1337 {
|
||||
t.Error("expected 1337")
|
||||
}
|
||||
if !snappySnap.Timestamp.Equal(tt) {
|
||||
t.Errorf("expected %v, received %v", tt, snappySnap.Timestamp)
|
||||
}
|
||||
|
||||
_, err = m.GetSnapshotAtTime(time.Now().Add(time.Hour))
|
||||
if !errors.Is(err, errSnapshotNotFound) {
|
||||
t.Errorf("expected: %v, received %v", errSnapshotNotFound, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetLatestSnapshot(t *testing.T) {
|
||||
t.Parallel()
|
||||
m := Manager{}
|
||||
snappySnap := m.GetLatestSnapshot()
|
||||
if !snappySnap.Timestamp.IsZero() {
|
||||
t.Error("expected blank snapshot")
|
||||
}
|
||||
tt := time.Now()
|
||||
err := m.AddSnapshot([]SnapshotOrder{
|
||||
{
|
||||
ClosePrice: 1337,
|
||||
},
|
||||
}, tt, 1, false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = m.AddSnapshot([]SnapshotOrder{
|
||||
{
|
||||
ClosePrice: 1337,
|
||||
},
|
||||
}, tt.Add(time.Hour), 1, false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
snappySnap = m.GetLatestSnapshot()
|
||||
if snappySnap.Timestamp.Equal(tt) {
|
||||
t.Errorf("expected %v", tt.Add(time.Hour))
|
||||
}
|
||||
if !snappySnap.Timestamp.Equal(tt.Add(time.Hour)) {
|
||||
t.Errorf("expected %v", tt.Add(time.Hour))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package compliance
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
var (
|
||||
errSnapshotNotFound = errors.New("snapshot not found")
|
||||
)
|
||||
|
||||
// Manager holds a snapshot of all orders at each timeperiod, allowing
|
||||
// study of all changes across time
|
||||
type Manager struct {
|
||||
Snapshots []Snapshot
|
||||
}
|
||||
|
||||
// Snapshot consists of the timestamp the snapshot is from, along with all orders made
|
||||
// up until that time
|
||||
type Snapshot struct {
|
||||
Orders []SnapshotOrder `json:"orders"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Offset int64 `json:"offset"`
|
||||
}
|
||||
|
||||
// SnapshotOrder adds some additional data that's only relevant for backtesting
|
||||
// to the order.Detail without adding to order.Detail
|
||||
type SnapshotOrder struct {
|
||||
ClosePrice float64 `json:"close-price"`
|
||||
VolumeAdjustedPrice float64 `json:"volume-adjusted-price"`
|
||||
SlippageRate float64 `json:"slippage-rate"`
|
||||
CostBasis float64 `json:"cost-basis"`
|
||||
*order.Detail `json:"order-detail"`
|
||||
}
|
||||
46
backtester/eventhandlers/portfolio/holdings/README.md
Normal file
46
backtester/eventhandlers/portfolio/holdings/README.md
Normal file
@@ -0,0 +1,46 @@
|
||||
# GoCryptoTrader Backtester: Holdings package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This holdings package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Holdings package overview
|
||||
|
||||
Holdings are used to calculate the holdings at any given time for a given exchange, asset, currency pair. If an order is placed, funds are removed from funding and placed under assets.
|
||||
Every data event will update and calculate holdings value based on the new price. This will allow for statistics to be easily calculated at the end of a backtesting run
|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
92
backtester/eventhandlers/portfolio/holdings/holdings.go
Normal file
92
backtester/eventhandlers/portfolio/holdings/holdings.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package holdings
|
||||
|
||||
import (
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/fill"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
// Create takes a fill event and creates a new holding for the exchange, asset, pair
|
||||
func Create(f fill.Event, initialFunds, riskFreeRate float64) (Holding, error) {
|
||||
if f == nil {
|
||||
return Holding{}, common.ErrNilEvent
|
||||
}
|
||||
if initialFunds <= 0 {
|
||||
return Holding{}, ErrInitialFundsZero
|
||||
}
|
||||
h := Holding{
|
||||
Offset: f.GetOffset(),
|
||||
Pair: f.Pair(),
|
||||
Asset: f.GetAssetType(),
|
||||
Exchange: f.GetExchange(),
|
||||
Timestamp: f.GetTime(),
|
||||
InitialFunds: initialFunds,
|
||||
RemainingFunds: initialFunds,
|
||||
RiskFreeRate: riskFreeRate,
|
||||
}
|
||||
h.update(f)
|
||||
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// Update calculates holding statistics for the events time
|
||||
func (h *Holding) Update(f fill.Event) {
|
||||
h.Timestamp = f.GetTime()
|
||||
h.Offset = f.GetOffset()
|
||||
h.update(f)
|
||||
}
|
||||
|
||||
// UpdateValue calculates the holding's value for a data event's time and price
|
||||
func (h *Holding) UpdateValue(d common.DataEventHandler) {
|
||||
h.Timestamp = d.GetTime()
|
||||
latest := d.ClosePrice()
|
||||
h.Offset = d.GetOffset()
|
||||
h.updateValue(latest)
|
||||
}
|
||||
|
||||
func (h *Holding) update(f fill.Event) {
|
||||
direction := f.GetDirection()
|
||||
o := f.GetOrder()
|
||||
switch direction {
|
||||
case order.Buy:
|
||||
h.CommittedFunds += (o.Amount * o.Price) + o.Fee
|
||||
h.PositionsSize += o.Amount
|
||||
h.PositionsValue += o.Amount * o.Price
|
||||
h.RemainingFunds -= (o.Amount * o.Price) + o.Fee
|
||||
h.TotalFees += o.Fee
|
||||
h.BoughtAmount += o.Amount
|
||||
h.BoughtValue += o.Amount * o.Price
|
||||
case order.Sell:
|
||||
h.CommittedFunds -= (o.Amount * o.Price) + o.Fee
|
||||
h.PositionsSize -= o.Amount
|
||||
h.PositionsValue -= o.Amount * o.Price
|
||||
h.RemainingFunds += (o.Amount * o.Price) - o.Fee
|
||||
h.TotalFees += o.Fee
|
||||
h.SoldAmount += o.Amount
|
||||
h.SoldValue += o.Amount * o.Price
|
||||
case common.DoNothing, common.CouldNotSell, common.CouldNotBuy, common.MissingData, "":
|
||||
}
|
||||
h.TotalValueLostToVolumeSizing += (f.GetClosePrice() - f.GetVolumeAdjustedPrice()) * f.GetAmount()
|
||||
h.TotalValueLostToSlippage += (f.GetVolumeAdjustedPrice() - f.GetPurchasePrice()) * f.GetAmount()
|
||||
h.updateValue(f.GetClosePrice())
|
||||
}
|
||||
|
||||
func (h *Holding) updateValue(l float64) {
|
||||
origPosValue := h.PositionsValue
|
||||
origBoughtValue := h.BoughtValue
|
||||
origSoldValue := h.SoldValue
|
||||
origTotalValue := h.TotalValue
|
||||
h.PositionsValue = h.PositionsSize * l
|
||||
h.BoughtValue = h.BoughtAmount * l
|
||||
h.SoldValue = h.SoldAmount * l
|
||||
h.TotalValue = h.PositionsValue + h.RemainingFunds
|
||||
|
||||
h.TotalValueDifference = h.TotalValue - origTotalValue
|
||||
h.BoughtValueDifference = h.BoughtValue - origBoughtValue
|
||||
h.PositionsValueDifference = h.PositionsValue - origPosValue
|
||||
h.SoldValueDifference = h.SoldValue - origSoldValue
|
||||
|
||||
if origTotalValue != 0 {
|
||||
h.ChangeInTotalValuePercent = (h.TotalValue - origTotalValue) / origTotalValue
|
||||
}
|
||||
}
|
||||
343
backtester/eventhandlers/portfolio/holdings/holdings_test.go
Normal file
343
backtester/eventhandlers/portfolio/holdings/holdings_test.go
Normal file
@@ -0,0 +1,343 @@
|
||||
package holdings
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/event"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/fill"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
const (
|
||||
testExchange = "binance"
|
||||
riskFreeRate = 0.03
|
||||
)
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := Create(nil, -1, riskFreeRate)
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", ErrInitialFundsZero, err)
|
||||
}
|
||||
|
||||
_, err = Create(&fill.Fill{}, -1, riskFreeRate)
|
||||
if !errors.Is(err, ErrInitialFundsZero) {
|
||||
t.Errorf("expected: %v, received %v", ErrInitialFundsZero, err)
|
||||
}
|
||||
|
||||
_, err = Create(nil, 1, riskFreeRate)
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilEvent, err)
|
||||
}
|
||||
|
||||
h, err := Create(&fill.Fill{}, 1, riskFreeRate)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if h.InitialFunds != 1 {
|
||||
t.Errorf("expected 1, received '%v'", h.InitialFunds)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
h, err := Create(&fill.Fill{}, 1, riskFreeRate)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t1 := h.Timestamp
|
||||
h.Update(&fill.Fill{
|
||||
Base: event.Base{
|
||||
Time: time.Now(),
|
||||
},
|
||||
})
|
||||
if t1.Equal(h.Timestamp) {
|
||||
t.Errorf("expected '%v' received '%v'", h.Timestamp, t1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateValue(t *testing.T) {
|
||||
t.Parallel()
|
||||
h, err := Create(&fill.Fill{}, 1, riskFreeRate)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
h.PositionsSize = 1
|
||||
h.UpdateValue(&kline.Kline{
|
||||
Close: 1337,
|
||||
})
|
||||
if h.PositionsValue != 1337 {
|
||||
t.Errorf("expected '%v' received '%v'", h.PositionsValue, 1337)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateBuyStats(t *testing.T) {
|
||||
t.Parallel()
|
||||
h, err := Create(&fill.Fill{}, 1000, riskFreeRate)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
h.update(&fill.Fill{
|
||||
Base: event.Base{
|
||||
Exchange: testExchange,
|
||||
Time: time.Now(),
|
||||
Interval: gctkline.OneHour,
|
||||
CurrencyPair: currency.NewPair(currency.BTC, currency.USDT),
|
||||
AssetType: asset.Spot,
|
||||
},
|
||||
Direction: order.Buy,
|
||||
Amount: 1,
|
||||
ClosePrice: 500,
|
||||
VolumeAdjustedPrice: 500,
|
||||
PurchasePrice: 500,
|
||||
ExchangeFee: 0,
|
||||
Slippage: 0,
|
||||
Order: &order.Detail{
|
||||
Price: 500,
|
||||
Amount: 1,
|
||||
Exchange: testExchange,
|
||||
ID: "1337",
|
||||
Type: order.Limit,
|
||||
Side: order.Buy,
|
||||
Status: order.New,
|
||||
AssetType: asset.Spot,
|
||||
Date: time.Now(),
|
||||
CloseTime: time.Now(),
|
||||
LastUpdated: time.Now(),
|
||||
Pair: currency.NewPair(currency.BTC, currency.USDT),
|
||||
Trades: nil,
|
||||
Fee: 1,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if h.PositionsSize != 1 {
|
||||
t.Errorf("expected '%v' received '%v'", 1, h.PositionsSize)
|
||||
}
|
||||
if h.PositionsValue != 500 {
|
||||
t.Errorf("expected '%v' received '%v'", 500, h.PositionsValue)
|
||||
}
|
||||
if h.InitialFunds != 1000 {
|
||||
t.Errorf("expected '%v' received '%v'", 1000, h.InitialFunds)
|
||||
}
|
||||
if h.RemainingFunds != 499 {
|
||||
t.Errorf("expected '%v' received '%v'", 499, h.RemainingFunds)
|
||||
}
|
||||
if h.TotalValue != 999 {
|
||||
t.Errorf("expected '%v' received '%v'", 999, h.TotalValue)
|
||||
}
|
||||
if h.BoughtAmount != 1 {
|
||||
t.Errorf("expected '%v' received '%v'", 1, h.BoughtAmount)
|
||||
}
|
||||
if h.BoughtValue != 500 {
|
||||
t.Errorf("expected '%v' received '%v'", 500, h.BoughtValue)
|
||||
}
|
||||
if h.SoldAmount != 0 {
|
||||
t.Errorf("expected '%v' received '%v'", 0, h.SoldAmount)
|
||||
}
|
||||
if h.TotalFees != 1 {
|
||||
t.Errorf("expected '%v' received '%v'", 1, h.TotalFees)
|
||||
}
|
||||
|
||||
h.update(&fill.Fill{
|
||||
Base: event.Base{
|
||||
Exchange: testExchange,
|
||||
Time: time.Now(),
|
||||
Interval: gctkline.OneHour,
|
||||
CurrencyPair: currency.NewPair(currency.BTC, currency.USDT),
|
||||
AssetType: asset.Spot,
|
||||
},
|
||||
Direction: order.Buy,
|
||||
Amount: 0.5,
|
||||
ClosePrice: 500,
|
||||
VolumeAdjustedPrice: 500,
|
||||
PurchasePrice: 500,
|
||||
ExchangeFee: 0,
|
||||
Slippage: 0,
|
||||
Order: &order.Detail{
|
||||
Price: 500,
|
||||
Amount: 0.5,
|
||||
Exchange: testExchange,
|
||||
ID: "1337",
|
||||
Type: order.Limit,
|
||||
Side: order.Buy,
|
||||
Status: order.New,
|
||||
AssetType: asset.Spot,
|
||||
Date: time.Now(),
|
||||
CloseTime: time.Now(),
|
||||
LastUpdated: time.Now(),
|
||||
Pair: currency.NewPair(currency.BTC, currency.USDT),
|
||||
Trades: nil,
|
||||
Fee: 0.5,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if h.PositionsSize != 1.5 {
|
||||
t.Errorf("expected '%v' received '%v'", 1, h.PositionsSize)
|
||||
}
|
||||
if h.PositionsValue != 750 {
|
||||
t.Errorf("expected '%v' received '%v'", 750, h.PositionsValue)
|
||||
}
|
||||
if h.InitialFunds != 1000 {
|
||||
t.Errorf("expected '%v' received '%v'", 1000, h.InitialFunds)
|
||||
}
|
||||
if h.RemainingFunds != 248.5 {
|
||||
t.Errorf("expected '%v' received '%v'", 248.5, h.RemainingFunds)
|
||||
}
|
||||
if h.TotalValue != 998.5 {
|
||||
t.Errorf("expected '%v' received '%v'", 998.5, h.TotalValue)
|
||||
}
|
||||
if h.BoughtAmount != 1.5 {
|
||||
t.Errorf("expected '%v' received '%v'", 1, h.BoughtAmount)
|
||||
}
|
||||
if h.BoughtValue != 750 {
|
||||
t.Errorf("expected '%v' received '%v'", 750, h.BoughtValue)
|
||||
}
|
||||
if h.SoldAmount != 0 {
|
||||
t.Errorf("expected '%v' received '%v'", 0, h.SoldAmount)
|
||||
}
|
||||
if h.TotalFees != 1.5 {
|
||||
t.Errorf("expected '%v' received '%v'", 1.5, h.TotalFees)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateSellStats(t *testing.T) {
|
||||
t.Parallel()
|
||||
h, err := Create(&fill.Fill{}, 1000, riskFreeRate)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
h.update(&fill.Fill{
|
||||
Base: event.Base{
|
||||
Exchange: testExchange,
|
||||
Time: time.Now(),
|
||||
Interval: gctkline.OneHour,
|
||||
CurrencyPair: currency.NewPair(currency.BTC, currency.USDT),
|
||||
AssetType: asset.Spot,
|
||||
},
|
||||
Direction: order.Buy,
|
||||
Amount: 1,
|
||||
ClosePrice: 500,
|
||||
VolumeAdjustedPrice: 500,
|
||||
PurchasePrice: 500,
|
||||
ExchangeFee: 0,
|
||||
Slippage: 0,
|
||||
Order: &order.Detail{
|
||||
Price: 500,
|
||||
Amount: 1,
|
||||
Exchange: testExchange,
|
||||
ID: "1337",
|
||||
Type: order.Limit,
|
||||
Side: order.Buy,
|
||||
Status: order.New,
|
||||
AssetType: asset.Spot,
|
||||
Date: time.Now(),
|
||||
CloseTime: time.Now(),
|
||||
LastUpdated: time.Now(),
|
||||
Pair: currency.NewPair(currency.BTC, currency.USDT),
|
||||
Trades: nil,
|
||||
Fee: 1,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if h.PositionsSize != 1 {
|
||||
t.Errorf("expected '%v' received '%v'", 1, h.PositionsSize)
|
||||
}
|
||||
if h.PositionsValue != 500 {
|
||||
t.Errorf("expected '%v' received '%v'", 500, h.PositionsValue)
|
||||
}
|
||||
if h.InitialFunds != 1000 {
|
||||
t.Errorf("expected '%v' received '%v'", 1000, h.InitialFunds)
|
||||
}
|
||||
if h.RemainingFunds != 499 {
|
||||
t.Errorf("expected '%v' received '%v'", 499, h.RemainingFunds)
|
||||
}
|
||||
if h.TotalValue != 999 {
|
||||
t.Errorf("expected '%v' received '%v'", 999, h.TotalValue)
|
||||
}
|
||||
if h.BoughtAmount != 1 {
|
||||
t.Errorf("expected '%v' received '%v'", 1, h.BoughtAmount)
|
||||
}
|
||||
if h.BoughtValue != 500 {
|
||||
t.Errorf("expected '%v' received '%v'", 500, h.BoughtValue)
|
||||
}
|
||||
if h.SoldAmount != 0 {
|
||||
t.Errorf("expected '%v' received '%v'", 0, h.SoldAmount)
|
||||
}
|
||||
if h.TotalFees != 1 {
|
||||
t.Errorf("expected '%v' received '%v'", 1, h.TotalFees)
|
||||
}
|
||||
|
||||
h.update(&fill.Fill{
|
||||
Base: event.Base{
|
||||
Exchange: testExchange,
|
||||
Time: time.Now(),
|
||||
Interval: gctkline.OneHour,
|
||||
CurrencyPair: currency.NewPair(currency.BTC, currency.USDT),
|
||||
AssetType: asset.Spot,
|
||||
},
|
||||
Direction: order.Sell,
|
||||
Amount: 1,
|
||||
ClosePrice: 500,
|
||||
VolumeAdjustedPrice: 500,
|
||||
PurchasePrice: 500,
|
||||
ExchangeFee: 0,
|
||||
Slippage: 0,
|
||||
Order: &order.Detail{
|
||||
Price: 500,
|
||||
Amount: 1,
|
||||
Exchange: testExchange,
|
||||
ID: "1337",
|
||||
Type: order.Limit,
|
||||
Side: order.Sell,
|
||||
Status: order.New,
|
||||
AssetType: asset.Spot,
|
||||
Date: time.Now(),
|
||||
CloseTime: time.Now(),
|
||||
LastUpdated: time.Now(),
|
||||
Pair: currency.NewPair(currency.BTC, currency.USDT),
|
||||
Trades: nil,
|
||||
Fee: 1,
|
||||
},
|
||||
})
|
||||
|
||||
if h.PositionsSize != 0 {
|
||||
t.Errorf("expected '%v' received '%v'", 0, h.PositionsSize)
|
||||
}
|
||||
if h.PositionsValue != 0 {
|
||||
t.Errorf("expected '%v' received '%v'", 0, h.PositionsValue)
|
||||
}
|
||||
if h.InitialFunds != 1000 {
|
||||
t.Errorf("expected '%v' received '%v'", 1000, h.InitialFunds)
|
||||
}
|
||||
if h.RemainingFunds != 998 {
|
||||
t.Errorf("expected '%v' received '%v'", 998, h.RemainingFunds)
|
||||
}
|
||||
if h.TotalValue != 998 {
|
||||
t.Errorf("expected '%v' received '%v'", 998, h.TotalValue)
|
||||
}
|
||||
if h.BoughtAmount != 1 {
|
||||
t.Errorf("expected '%v' received '%v'", 1, h.BoughtAmount)
|
||||
}
|
||||
if h.BoughtValue != 500 {
|
||||
t.Errorf("expected '%v' received '%v'", 500, h.BoughtValue)
|
||||
}
|
||||
if h.SoldAmount != 1 {
|
||||
t.Errorf("expected '%v' received '%v'", 1, h.SoldAmount)
|
||||
}
|
||||
if h.TotalFees != 2 {
|
||||
t.Errorf("expected '%v' received '%v'", 2, h.TotalFees)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package holdings
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
// ErrInitialFundsZero is an error when initial funds are zero or less
|
||||
var ErrInitialFundsZero = errors.New("initial funds <= 0")
|
||||
|
||||
// Holding contains pricing statistics for a given time
|
||||
// for a given exchange asset pair
|
||||
type Holding struct {
|
||||
Offset int64
|
||||
Pair currency.Pair `json:"pair"`
|
||||
Asset asset.Item `json:"asset"`
|
||||
Exchange string `json:"exchange"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
InitialFunds float64 `json:"initial-funds"`
|
||||
PositionsSize float64 `json:"positions-size"`
|
||||
PositionsValue float64 `json:"postions-value"`
|
||||
SoldAmount float64 `json:"sold-amount"`
|
||||
SoldValue float64 `json:"sold-value"`
|
||||
BoughtAmount float64 `json:"bought-amount"`
|
||||
BoughtValue float64 `json:"bought-value"`
|
||||
RemainingFunds float64 `json:"remaining-funds"`
|
||||
CommittedFunds float64 `json:"committed-funds"`
|
||||
|
||||
TotalValueDifference float64
|
||||
ChangeInTotalValuePercent float64
|
||||
BoughtValueDifference float64
|
||||
SoldValueDifference float64
|
||||
PositionsValueDifference float64
|
||||
|
||||
TotalValue float64 `json:"total-value"`
|
||||
TotalFees float64 `json:"total-fees"`
|
||||
TotalValueLostToVolumeSizing float64 `json:"total-value-lost-to-volume-sizing"`
|
||||
TotalValueLostToSlippage float64 `json:"total-value-lost-to-slippage"`
|
||||
TotalValueLost float64 `json:"total-value-lost"`
|
||||
|
||||
RiskFreeRate float64 `json:"risk-free-rate"`
|
||||
}
|
||||
449
backtester/eventhandlers/portfolio/portfolio.go
Normal file
449
backtester/eventhandlers/portfolio/portfolio.go
Normal file
@@ -0,0 +1,449 @@
|
||||
package portfolio
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/compliance"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/risk"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/settings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/event"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/fill"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/signal"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
)
|
||||
|
||||
// Setup creates a portfolio manager instance and sets private fields
|
||||
func Setup(sh SizeHandler, r risk.Handler, riskFreeRate float64) (*Portfolio, error) {
|
||||
if sh == nil {
|
||||
return nil, errSizeManagerUnset
|
||||
}
|
||||
if riskFreeRate < 0 {
|
||||
return nil, errNegativeRiskFreeRate
|
||||
}
|
||||
if r == nil {
|
||||
return nil, errRiskManagerUnset
|
||||
}
|
||||
p := &Portfolio{}
|
||||
p.sizeManager = sh
|
||||
p.riskManager = r
|
||||
p.riskFreeRate = riskFreeRate
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Reset returns the portfolio manager to its default state
|
||||
func (p *Portfolio) Reset() {
|
||||
p.exchangeAssetPairSettings = nil
|
||||
}
|
||||
|
||||
// OnSignal receives the event from the strategy on whether it has signalled to buy, do nothing or sell
|
||||
// on buy/sell, the portfolio manager will size the order and assess the risk of the order
|
||||
// if successful, it will pass on an order.Order to be used by the exchange event handler to place an order based on
|
||||
// the portfolio manager's recommendations
|
||||
func (p *Portfolio) OnSignal(signal signal.Event, cs *exchange.Settings) (*order.Order, error) {
|
||||
if signal == nil || cs == nil {
|
||||
return nil, common.ErrNilArguments
|
||||
}
|
||||
if p.sizeManager == nil {
|
||||
return nil, errSizeManagerUnset
|
||||
}
|
||||
if p.riskManager == nil {
|
||||
return nil, errRiskManagerUnset
|
||||
}
|
||||
|
||||
o := &order.Order{
|
||||
Base: event.Base{
|
||||
Offset: signal.GetOffset(),
|
||||
Exchange: signal.GetExchange(),
|
||||
Time: signal.GetTime(),
|
||||
CurrencyPair: signal.Pair(),
|
||||
AssetType: signal.GetAssetType(),
|
||||
Interval: signal.GetInterval(),
|
||||
Reason: signal.GetReason(),
|
||||
},
|
||||
Direction: signal.GetDirection(),
|
||||
}
|
||||
if signal.GetDirection() == "" {
|
||||
return o, errInvalidDirection
|
||||
}
|
||||
|
||||
lookup := p.exchangeAssetPairSettings[signal.GetExchange()][signal.GetAssetType()][signal.Pair()]
|
||||
if lookup == nil {
|
||||
return nil, fmt.Errorf("%w for %v %v %v",
|
||||
errNoPortfolioSettings,
|
||||
signal.GetExchange(),
|
||||
signal.GetAssetType(),
|
||||
signal.Pair())
|
||||
}
|
||||
prevHolding := lookup.GetLatestHoldings()
|
||||
if p.iteration == 0 {
|
||||
prevHolding.InitialFunds = lookup.InitialFunds
|
||||
prevHolding.RemainingFunds = lookup.InitialFunds
|
||||
prevHolding.Exchange = signal.GetExchange()
|
||||
prevHolding.Pair = signal.Pair()
|
||||
prevHolding.Asset = signal.GetAssetType()
|
||||
prevHolding.Timestamp = signal.GetTime()
|
||||
}
|
||||
p.iteration++
|
||||
|
||||
if signal.GetDirection() == common.DoNothing || signal.GetDirection() == common.MissingData || signal.GetDirection() == "" {
|
||||
return o, nil
|
||||
}
|
||||
|
||||
if signal.GetDirection() == gctorder.Sell && prevHolding.PositionsSize == 0 {
|
||||
o.AppendReason("no holdings to sell")
|
||||
o.SetDirection(common.CouldNotSell)
|
||||
signal.SetDirection(o.Direction)
|
||||
return o, nil
|
||||
}
|
||||
|
||||
// for simplicity, the backtester will round to 8 decimal places
|
||||
remainingFundsRounded := math.Floor(prevHolding.RemainingFunds*100000000) / 100000000
|
||||
if signal.GetDirection() == gctorder.Buy && remainingFundsRounded <= 0 {
|
||||
o.AppendReason("not enough funds to buy")
|
||||
o.SetDirection(common.CouldNotBuy)
|
||||
signal.SetDirection(o.Direction)
|
||||
return o, nil
|
||||
}
|
||||
|
||||
o.Price = signal.GetPrice()
|
||||
o.OrderType = gctorder.Market
|
||||
sizingFunds := prevHolding.RemainingFunds
|
||||
if signal.GetDirection() == gctorder.Sell {
|
||||
sizingFunds = prevHolding.PositionsSize
|
||||
}
|
||||
|
||||
sizedOrder := p.sizeOrder(signal, cs, o, sizingFunds)
|
||||
o.Funds = sizingFunds
|
||||
sizedAmountRounded := math.Floor(sizedOrder.Amount*100000000) / 100000000
|
||||
if sizedAmountRounded <= 0 {
|
||||
o.AppendReason("sized amount is zero")
|
||||
if o.Direction == gctorder.Buy {
|
||||
o.SetDirection(common.CouldNotBuy)
|
||||
} else if o.Direction == gctorder.Sell {
|
||||
o.SetDirection(common.CouldNotSell)
|
||||
}
|
||||
return o, nil
|
||||
}
|
||||
|
||||
return p.evaluateOrder(signal, o, sizedOrder)
|
||||
}
|
||||
|
||||
func (p *Portfolio) evaluateOrder(d common.Directioner, originalOrderSignal, sizedOrder *order.Order) (*order.Order, error) {
|
||||
var evaluatedOrder *order.Order
|
||||
cm, err := p.GetComplianceManager(originalOrderSignal.GetExchange(), originalOrderSignal.GetAssetType(), originalOrderSignal.Pair())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
evaluatedOrder, err = p.riskManager.EvaluateOrder(sizedOrder, p.GetLatestHoldingsForAllCurrencies(), cm.GetLatestSnapshot())
|
||||
if err != nil {
|
||||
originalOrderSignal.AppendReason(err.Error())
|
||||
switch d.GetDirection() {
|
||||
case gctorder.Buy:
|
||||
originalOrderSignal.Direction = common.CouldNotBuy
|
||||
case gctorder.Sell:
|
||||
originalOrderSignal.Direction = common.CouldNotSell
|
||||
case common.CouldNotBuy, common.CouldNotSell:
|
||||
default:
|
||||
originalOrderSignal.Direction = common.DoNothing
|
||||
}
|
||||
d.SetDirection(originalOrderSignal.Direction)
|
||||
return originalOrderSignal, nil
|
||||
}
|
||||
|
||||
return evaluatedOrder, nil
|
||||
}
|
||||
|
||||
func (p *Portfolio) sizeOrder(d common.Directioner, cs *exchange.Settings, originalOrderSignal *order.Order, sizingFunds float64) *order.Order {
|
||||
sizedOrder, err := p.sizeManager.SizeOrder(originalOrderSignal, sizingFunds, cs)
|
||||
if err != nil {
|
||||
originalOrderSignal.AppendReason(err.Error())
|
||||
switch originalOrderSignal.Direction {
|
||||
case gctorder.Buy:
|
||||
originalOrderSignal.Direction = common.CouldNotBuy
|
||||
case gctorder.Sell:
|
||||
originalOrderSignal.Direction = common.CouldNotSell
|
||||
default:
|
||||
originalOrderSignal.Direction = common.DoNothing
|
||||
}
|
||||
d.SetDirection(originalOrderSignal.Direction)
|
||||
return originalOrderSignal
|
||||
}
|
||||
|
||||
if sizedOrder.Amount == 0 {
|
||||
switch originalOrderSignal.Direction {
|
||||
case gctorder.Buy:
|
||||
originalOrderSignal.Direction = common.CouldNotBuy
|
||||
case gctorder.Sell:
|
||||
originalOrderSignal.Direction = common.CouldNotSell
|
||||
default:
|
||||
originalOrderSignal.Direction = common.DoNothing
|
||||
}
|
||||
d.SetDirection(originalOrderSignal.Direction)
|
||||
originalOrderSignal.AppendReason("sized order to 0")
|
||||
}
|
||||
|
||||
return sizedOrder
|
||||
}
|
||||
|
||||
// OnFill processes the event after an order has been placed by the exchange. Its purpose is to track holdings for future portfolio decisions.
|
||||
func (p *Portfolio) OnFill(fillEvent fill.Event) (*fill.Fill, error) {
|
||||
if fillEvent == nil {
|
||||
return nil, common.ErrNilEvent
|
||||
}
|
||||
lookup := p.exchangeAssetPairSettings[fillEvent.GetExchange()][fillEvent.GetAssetType()][fillEvent.Pair()]
|
||||
if lookup == nil {
|
||||
return nil, fmt.Errorf("%w for %v %v %v", errNoPortfolioSettings, fillEvent.GetExchange(), fillEvent.GetAssetType(), fillEvent.Pair())
|
||||
}
|
||||
var err error
|
||||
// Get the holding from the previous iteration, create it if it doesn't yet have a timestamp
|
||||
h := lookup.GetHoldingsForTime(fillEvent.GetTime().Add(-fillEvent.GetInterval().Duration()))
|
||||
if !h.Timestamp.IsZero() {
|
||||
h.Update(fillEvent)
|
||||
} else {
|
||||
h = lookup.GetLatestHoldings()
|
||||
if !h.Timestamp.IsZero() {
|
||||
h.Update(fillEvent)
|
||||
} else {
|
||||
h, err = holdings.Create(fillEvent, lookup.InitialFunds, p.riskFreeRate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
err = p.setHoldingsForOffset(fillEvent.GetExchange(), fillEvent.GetAssetType(), fillEvent.Pair(), &h, true)
|
||||
if errors.Is(err, errNoHoldings) {
|
||||
err = p.setHoldingsForOffset(fillEvent.GetExchange(), fillEvent.GetAssetType(), fillEvent.Pair(), &h, false)
|
||||
}
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
}
|
||||
|
||||
err = p.addComplianceSnapshot(fillEvent)
|
||||
if err != nil {
|
||||
log.Error(log.BackTester, err)
|
||||
}
|
||||
|
||||
direction := fillEvent.GetDirection()
|
||||
if direction == common.DoNothing ||
|
||||
direction == common.CouldNotBuy ||
|
||||
direction == common.CouldNotSell ||
|
||||
direction == common.MissingData ||
|
||||
direction == "" {
|
||||
fe := fillEvent.(*fill.Fill)
|
||||
fe.ExchangeFee = 0
|
||||
return fe, nil
|
||||
}
|
||||
|
||||
return fillEvent.(*fill.Fill), nil
|
||||
}
|
||||
|
||||
// addComplianceSnapshot gets the previous snapshot of compliance events, updates with the latest fillevent
|
||||
// then saves the snapshot to the c
|
||||
func (p *Portfolio) addComplianceSnapshot(fillEvent fill.Event) error {
|
||||
if fillEvent == nil {
|
||||
return common.ErrNilEvent
|
||||
}
|
||||
complianceManager, err := p.GetComplianceManager(fillEvent.GetExchange(), fillEvent.GetAssetType(), fillEvent.Pair())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
prevSnap := complianceManager.GetLatestSnapshot()
|
||||
fo := fillEvent.GetOrder()
|
||||
if fo != nil {
|
||||
snapOrder := compliance.SnapshotOrder{
|
||||
ClosePrice: fillEvent.GetClosePrice(),
|
||||
VolumeAdjustedPrice: fillEvent.GetVolumeAdjustedPrice(),
|
||||
SlippageRate: fillEvent.GetSlippageRate(),
|
||||
Detail: fo,
|
||||
CostBasis: (fo.Price * fo.Amount) + fo.Fee,
|
||||
}
|
||||
prevSnap.Orders = append(prevSnap.Orders, snapOrder)
|
||||
}
|
||||
return complianceManager.AddSnapshot(prevSnap.Orders, fillEvent.GetTime(), fillEvent.GetOffset(), false)
|
||||
}
|
||||
|
||||
// GetComplianceManager returns the order snapshots for a given exchange, asset, pair
|
||||
func (p *Portfolio) GetComplianceManager(exchangeName string, a asset.Item, cp currency.Pair) (*compliance.Manager, error) {
|
||||
lookup := p.exchangeAssetPairSettings[exchangeName][a][cp]
|
||||
if lookup == nil {
|
||||
return nil, fmt.Errorf("%w for %v %v %v could not retrieve compliance manager", errNoPortfolioSettings, exchangeName, a, cp)
|
||||
}
|
||||
return &lookup.ComplianceManager, nil
|
||||
}
|
||||
|
||||
// SetFee sets the fee rate
|
||||
func (p *Portfolio) SetFee(exch string, a asset.Item, cp currency.Pair, fee float64) {
|
||||
lookup := p.exchangeAssetPairSettings[exch][a][cp]
|
||||
lookup.Fee = fee
|
||||
}
|
||||
|
||||
// GetFee can panic for bad requests, but why are you getting things that don't exist?
|
||||
func (p *Portfolio) GetFee(exchangeName string, a asset.Item, cp currency.Pair) float64 {
|
||||
if p.exchangeAssetPairSettings == nil {
|
||||
return 0
|
||||
}
|
||||
lookup := p.exchangeAssetPairSettings[exchangeName][a][cp]
|
||||
if lookup == nil {
|
||||
return 0
|
||||
}
|
||||
return lookup.Fee
|
||||
}
|
||||
|
||||
// IsInvested determines if there are any holdings for a given exchange, asset, pair
|
||||
func (p *Portfolio) IsInvested(exchangeName string, a asset.Item, cp currency.Pair) (holdings.Holding, bool) {
|
||||
s := p.exchangeAssetPairSettings[exchangeName][a][cp]
|
||||
if s == nil {
|
||||
return holdings.Holding{}, false
|
||||
}
|
||||
h := s.GetLatestHoldings()
|
||||
if h.PositionsSize > 0 {
|
||||
return h, true
|
||||
}
|
||||
return h, false
|
||||
}
|
||||
|
||||
// Update updates the portfolio holdings for the data event
|
||||
func (p *Portfolio) Update(d common.DataEventHandler) error {
|
||||
if d == nil {
|
||||
return common.ErrNilEvent
|
||||
}
|
||||
h, ok := p.IsInvested(d.GetExchange(), d.GetAssetType(), d.Pair())
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
h.UpdateValue(d)
|
||||
err := p.setHoldingsForOffset(d.GetExchange(), d.GetAssetType(), d.Pair(), &h, true)
|
||||
if errors.Is(err, errNoHoldings) {
|
||||
err = p.setHoldingsForOffset(d.GetExchange(), d.GetAssetType(), d.Pair(), &h, false)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// SetInitialFunds sets the initial funds
|
||||
func (p *Portfolio) SetInitialFunds(exch string, a asset.Item, cp currency.Pair, funds float64) error {
|
||||
lookup, ok := p.exchangeAssetPairSettings[exch][a][cp]
|
||||
if !ok {
|
||||
var err error
|
||||
lookup, err = p.SetupCurrencySettingsMap(exch, a, cp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
lookup.InitialFunds = funds
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetInitialFunds returns the initial funds
|
||||
func (p *Portfolio) GetInitialFunds(exch string, a asset.Item, cp currency.Pair) float64 {
|
||||
lookup, ok := p.exchangeAssetPairSettings[exch][a][cp]
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
return lookup.InitialFunds
|
||||
}
|
||||
|
||||
// GetLatestHoldingsForAllCurrencies will return the current holdings for all loaded currencies
|
||||
// this is useful to assess the position of your entire portfolio in order to help with risk decisions
|
||||
func (p *Portfolio) GetLatestHoldingsForAllCurrencies() []holdings.Holding {
|
||||
var resp []holdings.Holding
|
||||
for _, x := range p.exchangeAssetPairSettings {
|
||||
for _, y := range x {
|
||||
for _, z := range y {
|
||||
holds := z.GetLatestHoldings()
|
||||
if holds.Offset != 0 {
|
||||
resp = append(resp, holds)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
func (p *Portfolio) setHoldingsForOffset(exch string, a asset.Item, cp currency.Pair, h *holdings.Holding, overwriteExisting bool) error {
|
||||
if h.Timestamp.IsZero() {
|
||||
return errHoldingsNoTimestamp
|
||||
}
|
||||
lookup := p.exchangeAssetPairSettings[exch][a][cp]
|
||||
if lookup == nil {
|
||||
var err error
|
||||
lookup, err = p.SetupCurrencySettingsMap(exch, a, cp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if overwriteExisting && len(lookup.HoldingsSnapshots) == 0 {
|
||||
return errNoHoldings
|
||||
}
|
||||
for i := len(lookup.HoldingsSnapshots) - 1; i >= 0; i-- {
|
||||
if lookup.HoldingsSnapshots[i].Offset == h.Offset {
|
||||
if overwriteExisting {
|
||||
lookup.HoldingsSnapshots[i] = *h
|
||||
return nil
|
||||
}
|
||||
return errHoldingsAlreadySet
|
||||
}
|
||||
}
|
||||
if overwriteExisting {
|
||||
return fmt.Errorf("%w at %v", errNoHoldings, h.Timestamp)
|
||||
}
|
||||
|
||||
lookup.HoldingsSnapshots = append(lookup.HoldingsSnapshots, *h)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ViewHoldingAtTimePeriod retrieves a snapshot of holdings at a specific time period,
|
||||
// returning empty when not found
|
||||
func (p *Portfolio) ViewHoldingAtTimePeriod(exch string, a asset.Item, cp currency.Pair, t time.Time) (holdings.Holding, error) {
|
||||
exchangeAssetPairSettings := p.exchangeAssetPairSettings[exch][a][cp]
|
||||
if exchangeAssetPairSettings == nil {
|
||||
return holdings.Holding{}, fmt.Errorf("%w for %v %v %v", errNoHoldings, exch, a, cp)
|
||||
}
|
||||
|
||||
for i := len(exchangeAssetPairSettings.HoldingsSnapshots) - 1; i >= 0; i-- {
|
||||
if t.Equal(exchangeAssetPairSettings.HoldingsSnapshots[i].Timestamp) {
|
||||
return exchangeAssetPairSettings.HoldingsSnapshots[i], nil
|
||||
}
|
||||
}
|
||||
|
||||
return holdings.Holding{}, fmt.Errorf("%w for %v %v %v at %v", errNoHoldings, exch, a, cp, t)
|
||||
}
|
||||
|
||||
// SetupCurrencySettingsMap ensures a map is created and no panics happen
|
||||
func (p *Portfolio) SetupCurrencySettingsMap(exch string, a asset.Item, cp currency.Pair) (*settings.Settings, error) {
|
||||
if exch == "" {
|
||||
return nil, errExchangeUnset
|
||||
}
|
||||
if a == "" {
|
||||
return nil, errAssetUnset
|
||||
}
|
||||
if cp.IsEmpty() {
|
||||
return nil, errCurrencyPairUnset
|
||||
}
|
||||
if p.exchangeAssetPairSettings == nil {
|
||||
p.exchangeAssetPairSettings = make(map[string]map[asset.Item]map[currency.Pair]*settings.Settings)
|
||||
}
|
||||
if p.exchangeAssetPairSettings[exch] == nil {
|
||||
p.exchangeAssetPairSettings[exch] = make(map[asset.Item]map[currency.Pair]*settings.Settings)
|
||||
}
|
||||
if p.exchangeAssetPairSettings[exch][a] == nil {
|
||||
p.exchangeAssetPairSettings[exch][a] = make(map[currency.Pair]*settings.Settings)
|
||||
}
|
||||
if _, ok := p.exchangeAssetPairSettings[exch][a][cp]; !ok {
|
||||
p.exchangeAssetPairSettings[exch][a][cp] = &settings.Settings{}
|
||||
}
|
||||
|
||||
return p.exchangeAssetPairSettings[exch][a][cp], nil
|
||||
}
|
||||
469
backtester/eventhandlers/portfolio/portfolio_test.go
Normal file
469
backtester/eventhandlers/portfolio/portfolio_test.go
Normal file
@@ -0,0 +1,469 @@
|
||||
package portfolio
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/compliance"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/risk"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/settings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/size"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/event"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/fill"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/signal"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
const testExchange = "binance"
|
||||
|
||||
func TestReset(t *testing.T) {
|
||||
t.Parallel()
|
||||
p := Portfolio{
|
||||
exchangeAssetPairSettings: make(map[string]map[asset.Item]map[currency.Pair]*settings.Settings),
|
||||
}
|
||||
p.Reset()
|
||||
if p.exchangeAssetPairSettings != nil {
|
||||
t.Error("expected nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetup(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := Setup(nil, nil, -1)
|
||||
if !errors.Is(err, errSizeManagerUnset) {
|
||||
t.Errorf("expected: %v, received %v", errSizeManagerUnset, err)
|
||||
}
|
||||
|
||||
_, err = Setup(&size.Size{}, nil, -1)
|
||||
if !errors.Is(err, errNegativeRiskFreeRate) {
|
||||
t.Errorf("expected: %v, received %v", errNegativeRiskFreeRate, err)
|
||||
}
|
||||
|
||||
_, err = Setup(&size.Size{}, nil, 1)
|
||||
if !errors.Is(err, errRiskManagerUnset) {
|
||||
t.Errorf("expected: %v, received %v", errRiskManagerUnset, err)
|
||||
}
|
||||
var p *Portfolio
|
||||
p, err = Setup(&size.Size{}, &risk.Risk{}, 1)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if p.riskFreeRate != 1 {
|
||||
t.Error("expected 1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupCurrencySettingsMap(t *testing.T) {
|
||||
t.Parallel()
|
||||
p := &Portfolio{}
|
||||
_, err := p.SetupCurrencySettingsMap("", "", currency.Pair{})
|
||||
if !errors.Is(err, errExchangeUnset) {
|
||||
t.Errorf("expected: %v, received %v", errExchangeUnset, err)
|
||||
}
|
||||
|
||||
_, err = p.SetupCurrencySettingsMap("hi", "", currency.Pair{})
|
||||
if !errors.Is(err, errAssetUnset) {
|
||||
t.Errorf("expected: %v, received %v", errAssetUnset, err)
|
||||
}
|
||||
|
||||
_, err = p.SetupCurrencySettingsMap("hi", asset.Spot, currency.Pair{})
|
||||
if !errors.Is(err, errCurrencyPairUnset) {
|
||||
t.Errorf("expected: %v, received %v", errCurrencyPairUnset, err)
|
||||
}
|
||||
|
||||
_, err = p.SetupCurrencySettingsMap("hi", asset.Spot, currency.NewPair(currency.BTC, currency.USD))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetHoldings(t *testing.T) {
|
||||
t.Parallel()
|
||||
p := &Portfolio{}
|
||||
|
||||
err := p.setHoldingsForOffset("", "", currency.Pair{}, &holdings.Holding{}, false)
|
||||
if !errors.Is(err, errHoldingsNoTimestamp) {
|
||||
t.Errorf("expected: %v, received %v", errHoldingsNoTimestamp, err)
|
||||
}
|
||||
tt := time.Now()
|
||||
|
||||
err = p.setHoldingsForOffset("", "", currency.Pair{}, &holdings.Holding{Timestamp: tt}, false)
|
||||
if !errors.Is(err, errExchangeUnset) {
|
||||
t.Errorf("expected: %v, received %v", errExchangeUnset, err)
|
||||
}
|
||||
|
||||
err = p.setHoldingsForOffset(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.USD), &holdings.Holding{Timestamp: tt}, false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = p.setHoldingsForOffset(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.USD), &holdings.Holding{Timestamp: tt}, true)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetLatestHoldingsForAllCurrencies(t *testing.T) {
|
||||
t.Parallel()
|
||||
p := &Portfolio{}
|
||||
h := p.GetLatestHoldingsForAllCurrencies()
|
||||
if len(h) != 0 {
|
||||
t.Error("expected 0")
|
||||
}
|
||||
tt := time.Now()
|
||||
err := p.setHoldingsForOffset(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.USD), &holdings.Holding{Timestamp: tt}, true)
|
||||
if !errors.Is(err, errNoHoldings) {
|
||||
t.Errorf("expected: %v, received %v", errNoHoldings, err)
|
||||
}
|
||||
h = p.GetLatestHoldingsForAllCurrencies()
|
||||
if len(h) != 1 {
|
||||
t.Error("expected 1")
|
||||
}
|
||||
if !h[0].Timestamp.IsZero() {
|
||||
t.Error("expected unset holding")
|
||||
}
|
||||
err = p.setHoldingsForOffset(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.DOGE), &holdings.Holding{Offset: 1, Timestamp: tt}, false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
h = p.GetLatestHoldingsForAllCurrencies()
|
||||
if len(h) != 2 {
|
||||
t.Error("expected 2")
|
||||
}
|
||||
err = p.setHoldingsForOffset(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.DOGE), &holdings.Holding{Offset: 1, Timestamp: tt}, false)
|
||||
if !errors.Is(err, errHoldingsAlreadySet) {
|
||||
t.Errorf("expected: %v, received %v", errHoldingsAlreadySet, err)
|
||||
}
|
||||
|
||||
err = p.setHoldingsForOffset(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.DOGE), &holdings.Holding{Offset: 2, Timestamp: tt.Add(time.Minute)}, true)
|
||||
if !errors.Is(err, errNoHoldings) {
|
||||
t.Errorf("expected: %v, received %v", errNoHoldings, err)
|
||||
}
|
||||
h = p.GetLatestHoldingsForAllCurrencies()
|
||||
if len(h) != 2 {
|
||||
t.Error("expected 2")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetInitialFunds(t *testing.T) {
|
||||
t.Parallel()
|
||||
p := Portfolio{}
|
||||
f := p.GetInitialFunds("", "", currency.Pair{})
|
||||
if f != 0 {
|
||||
t.Error("expected zero")
|
||||
}
|
||||
|
||||
err := p.SetInitialFunds("", "", currency.Pair{}, 1)
|
||||
if !errors.Is(err, errExchangeUnset) {
|
||||
t.Errorf("expected: %v, received %v", errExchangeUnset, err)
|
||||
}
|
||||
|
||||
err = p.SetInitialFunds(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.DOGE), 1)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
f = p.GetInitialFunds(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.DOGE))
|
||||
if f != 1 {
|
||||
t.Error("expected 1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestViewHoldingAtTimePeriod(t *testing.T) {
|
||||
t.Parallel()
|
||||
p := Portfolio{}
|
||||
tt := time.Now()
|
||||
_, err := p.ViewHoldingAtTimePeriod("", "", currency.Pair{}, tt)
|
||||
if !errors.Is(err, errNoHoldings) {
|
||||
t.Errorf("expected: %v, received %v", errNoHoldings, err)
|
||||
}
|
||||
|
||||
err = p.setHoldingsForOffset(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.USD), &holdings.Holding{Offset: 1, Timestamp: tt}, false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = p.setHoldingsForOffset(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.USD), &holdings.Holding{Offset: 2, Timestamp: tt.Add(time.Hour)}, false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
_, err = p.ViewHoldingAtTimePeriod(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.USD), tt)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
var h holdings.Holding
|
||||
h, err = p.ViewHoldingAtTimePeriod(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.USD), tt)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !h.Timestamp.Equal(tt) {
|
||||
t.Errorf("expected %v received %v", tt, h.Timestamp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
p := Portfolio{}
|
||||
err := p.Update(nil)
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilEvent, err)
|
||||
}
|
||||
|
||||
err = p.Update(&kline.Kline{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = p.Update(&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: testExchange,
|
||||
CurrencyPair: currency.NewPair(currency.BTC, currency.USD),
|
||||
AssetType: asset.Spot,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
tt := time.Now()
|
||||
err = p.setHoldingsForOffset(testExchange, asset.Spot, currency.NewPair(currency.BTC, currency.USD), &holdings.Holding{Timestamp: tt, PositionsSize: 1337}, false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = p.Update(&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: testExchange,
|
||||
CurrencyPair: currency.NewPair(currency.BTC, currency.USD),
|
||||
AssetType: asset.Spot,
|
||||
Time: tt,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetFee(t *testing.T) {
|
||||
t.Parallel()
|
||||
p := Portfolio{}
|
||||
f := p.GetFee("", "", currency.Pair{})
|
||||
if f != 0 {
|
||||
t.Error("expected 0")
|
||||
}
|
||||
|
||||
_, err := p.SetupCurrencySettingsMap("hi", asset.Spot, currency.NewPair(currency.BTC, currency.USD))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
p.SetFee("hi", asset.Spot, currency.NewPair(currency.BTC, currency.USD), 1337)
|
||||
f = p.GetFee("hi", asset.Spot, currency.NewPair(currency.BTC, currency.USD))
|
||||
if f != 1337 {
|
||||
t.Error("expected 1337")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetComplianceManager(t *testing.T) {
|
||||
t.Parallel()
|
||||
p := Portfolio{}
|
||||
_, err := p.GetComplianceManager("", "", currency.Pair{})
|
||||
if !errors.Is(err, errNoPortfolioSettings) {
|
||||
t.Errorf("expected: %v, received %v", errNoPortfolioSettings, err)
|
||||
}
|
||||
|
||||
_, err = p.SetupCurrencySettingsMap("hi", asset.Spot, currency.NewPair(currency.BTC, currency.USD))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
var cm *compliance.Manager
|
||||
cm, err = p.GetComplianceManager("hi", asset.Spot, currency.NewPair(currency.BTC, currency.USD))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if cm == nil {
|
||||
t.Error("expected not nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddComplianceSnapshot(t *testing.T) {
|
||||
t.Parallel()
|
||||
p := Portfolio{}
|
||||
err := p.addComplianceSnapshot(nil)
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilEvent, err)
|
||||
}
|
||||
|
||||
err = p.addComplianceSnapshot(&fill.Fill{})
|
||||
if !errors.Is(err, errNoPortfolioSettings) {
|
||||
t.Errorf("expected: %v, received %v", errNoPortfolioSettings, err)
|
||||
}
|
||||
|
||||
_, err = p.SetupCurrencySettingsMap("hi", asset.Spot, currency.NewPair(currency.BTC, currency.USD))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = p.addComplianceSnapshot(&fill.Fill{
|
||||
Base: event.Base{
|
||||
Exchange: "hi",
|
||||
CurrencyPair: currency.NewPair(currency.BTC, currency.USD),
|
||||
AssetType: asset.Spot,
|
||||
},
|
||||
Order: &gctorder.Detail{
|
||||
Exchange: "hi",
|
||||
Pair: currency.NewPair(currency.BTC, currency.USD),
|
||||
AssetType: asset.Spot,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnFill(t *testing.T) {
|
||||
t.Parallel()
|
||||
p := Portfolio{}
|
||||
_, err := p.OnFill(nil)
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilEvent, err)
|
||||
}
|
||||
|
||||
f := &fill.Fill{
|
||||
Base: event.Base{
|
||||
Exchange: "hi",
|
||||
CurrencyPair: currency.NewPair(currency.BTC, currency.USD),
|
||||
AssetType: asset.Spot,
|
||||
},
|
||||
Order: &gctorder.Detail{
|
||||
Exchange: "hi",
|
||||
Pair: currency.NewPair(currency.BTC, currency.USD),
|
||||
AssetType: asset.Spot,
|
||||
},
|
||||
}
|
||||
_, err = p.OnFill(f)
|
||||
if !errors.Is(err, errNoPortfolioSettings) {
|
||||
t.Errorf("expected: %v, received %v", errNoPortfolioSettings, err)
|
||||
}
|
||||
var s *settings.Settings
|
||||
s, err = p.SetupCurrencySettingsMap("hi", asset.Spot, currency.NewPair(currency.BTC, currency.USD))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
_, err = p.OnFill(f)
|
||||
if !errors.Is(err, holdings.ErrInitialFundsZero) {
|
||||
t.Errorf("expected: %v, received %v", holdings.ErrInitialFundsZero, err)
|
||||
}
|
||||
|
||||
s.InitialFunds = 1337
|
||||
_, err = p.OnFill(f)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
f.Direction = gctorder.Buy
|
||||
_, err = p.OnFill(f)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnSignal(t *testing.T) {
|
||||
t.Parallel()
|
||||
p := Portfolio{}
|
||||
_, err := p.OnSignal(nil, nil)
|
||||
if !errors.Is(err, common.ErrNilArguments) {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
s := &signal.Signal{}
|
||||
_, err = p.OnSignal(s, &exchange.Settings{})
|
||||
if !errors.Is(err, errSizeManagerUnset) {
|
||||
t.Errorf("expected: %v, received %v", errSizeManagerUnset, err)
|
||||
}
|
||||
p.sizeManager = &size.Size{}
|
||||
|
||||
_, err = p.OnSignal(s, &exchange.Settings{})
|
||||
if !errors.Is(err, errRiskManagerUnset) {
|
||||
t.Errorf("expected: %v, received %v", errRiskManagerUnset, err)
|
||||
}
|
||||
|
||||
p.riskManager = &risk.Risk{}
|
||||
|
||||
_, err = p.OnSignal(s, &exchange.Settings{})
|
||||
if !errors.Is(err, errInvalidDirection) {
|
||||
t.Errorf("expected: %v, received %v", errInvalidDirection, err)
|
||||
}
|
||||
|
||||
s.Direction = gctorder.Buy
|
||||
_, err = p.OnSignal(s, &exchange.Settings{})
|
||||
if !errors.Is(err, errNoPortfolioSettings) {
|
||||
t.Errorf("expected: %v, received %v", errNoPortfolioSettings, err)
|
||||
}
|
||||
_, err = p.SetupCurrencySettingsMap("hi", asset.Spot, currency.NewPair(currency.BTC, currency.USD))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
s = &signal.Signal{
|
||||
Base: event.Base{
|
||||
Exchange: "hi",
|
||||
CurrencyPair: currency.NewPair(currency.BTC, currency.USD),
|
||||
AssetType: asset.Spot,
|
||||
},
|
||||
Direction: gctorder.Buy,
|
||||
}
|
||||
var resp *order.Order
|
||||
resp, err = p.OnSignal(s, &exchange.Settings{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp.Reason == "" {
|
||||
t.Error("expected issue")
|
||||
}
|
||||
|
||||
s.Direction = gctorder.Sell
|
||||
_, err = p.OnSignal(s, &exchange.Settings{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp.Reason == "" {
|
||||
t.Error("expected issue")
|
||||
}
|
||||
|
||||
s.Direction = common.MissingData
|
||||
_, err = p.OnSignal(s, &exchange.Settings{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
s.Direction = gctorder.Buy
|
||||
err = p.setHoldingsForOffset("hi", asset.Spot, currency.NewPair(currency.BTC, currency.USD), &holdings.Holding{Timestamp: time.Now(), RemainingFunds: 1337}, false)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
resp, err = p.OnSignal(s, &exchange.Settings{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp.Direction != common.CouldNotBuy {
|
||||
t.Errorf("expected common.CouldNotBuy, received %v", resp.Direction)
|
||||
}
|
||||
|
||||
s.ClosePrice = 10
|
||||
s.Direction = gctorder.Buy
|
||||
resp, err = p.OnSignal(s, &exchange.Settings{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp.Amount == 0 {
|
||||
t.Error("expected an amount to be sized")
|
||||
}
|
||||
}
|
||||
65
backtester/eventhandlers/portfolio/portfolio_types.go
Normal file
65
backtester/eventhandlers/portfolio/portfolio_types.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package portfolio
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/compliance"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/risk"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/settings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/fill"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/signal"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
var (
|
||||
errInvalidDirection = errors.New("invalid direction")
|
||||
errRiskManagerUnset = errors.New("risk manager unset")
|
||||
errSizeManagerUnset = errors.New("size manager unset")
|
||||
errAssetUnset = errors.New("asset unset")
|
||||
errCurrencyPairUnset = errors.New("currency pair unset")
|
||||
errExchangeUnset = errors.New("exchange unset")
|
||||
errNegativeRiskFreeRate = errors.New("received negative risk free rate")
|
||||
errNoPortfolioSettings = errors.New("no portfolio settings")
|
||||
errNoHoldings = errors.New("no holdings found")
|
||||
errHoldingsNoTimestamp = errors.New("holding with unset timestamp received")
|
||||
errHoldingsAlreadySet = errors.New("holding already set")
|
||||
)
|
||||
|
||||
// Portfolio stores all holdings and rules to assess orders, allowing the portfolio manager to
|
||||
// modify, accept or reject strategy signals
|
||||
type Portfolio struct {
|
||||
iteration float64
|
||||
riskFreeRate float64
|
||||
sizeManager SizeHandler
|
||||
riskManager risk.Handler
|
||||
exchangeAssetPairSettings map[string]map[asset.Item]map[currency.Pair]*settings.Settings
|
||||
}
|
||||
|
||||
// Handler contains all functions expected to operate a portfolio manager
|
||||
type Handler interface {
|
||||
OnSignal(signal.Event, *exchange.Settings) (*order.Order, error)
|
||||
OnFill(fill.Event) (*fill.Fill, error)
|
||||
Update(common.DataEventHandler) error
|
||||
|
||||
SetInitialFunds(string, asset.Item, currency.Pair, float64) error
|
||||
GetInitialFunds(string, asset.Item, currency.Pair) float64
|
||||
|
||||
GetComplianceManager(string, asset.Item, currency.Pair) (*compliance.Manager, error)
|
||||
|
||||
setHoldingsForOffset(string, asset.Item, currency.Pair, *holdings.Holding, bool) error
|
||||
ViewHoldingAtTimePeriod(string, asset.Item, currency.Pair, time.Time) (holdings.Holding, error)
|
||||
SetFee(string, asset.Item, currency.Pair, float64)
|
||||
GetFee(string, asset.Item, currency.Pair) float64
|
||||
Reset()
|
||||
}
|
||||
|
||||
// SizeHandler is the interface to help size orders
|
||||
type SizeHandler interface {
|
||||
SizeOrder(order.Event, float64, *exchange.Settings) (*order.Order, error)
|
||||
}
|
||||
48
backtester/eventhandlers/portfolio/risk/README.md
Normal file
48
backtester/eventhandlers/portfolio/risk/README.md
Normal file
@@ -0,0 +1,48 @@
|
||||
# GoCryptoTrader Backtester: Risk package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/risk)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This risk package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Risk package overview
|
||||
|
||||
The risk manager is responsible for ensuring that no order can be made if it is deemed too risky.
|
||||
Risk is currently defined by ensuring that orders cannot have too much leverage for the individual order, overall with all orders in the portfolio as well as whether there are too many orders for an individual currency
|
||||
|
||||
See config package [readme](/backtester/config/README.md) to view the risk related fields to customise
|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
79
backtester/eventhandlers/portfolio/risk/risk.go
Normal file
79
backtester/eventhandlers/portfolio/risk/risk.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package risk
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/compliance"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
)
|
||||
|
||||
// EvaluateOrder goes through a standard list of evaluations to make to ensure that
|
||||
// we are in a position to follow through with an order
|
||||
func (r *Risk) EvaluateOrder(o order.Event, latestHoldings []holdings.Holding, s compliance.Snapshot) (*order.Order, error) {
|
||||
if o == nil || latestHoldings == nil {
|
||||
return nil, common.ErrNilArguments
|
||||
}
|
||||
retOrder := o.(*order.Order)
|
||||
ex := o.GetExchange()
|
||||
a := o.GetAssetType()
|
||||
p := o.Pair()
|
||||
lookup, ok := r.CurrencySettings[ex][a][p]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%v %v %v %w", ex, a, p, errNoCurrencySettings)
|
||||
}
|
||||
|
||||
if o.IsLeveraged() {
|
||||
if !r.CanUseLeverage {
|
||||
return nil, errLeverageNotAllowed
|
||||
}
|
||||
ratio := existingLeverageRatio(s)
|
||||
if ratio > lookup.MaximumOrdersWithLeverageRatio && lookup.MaximumOrdersWithLeverageRatio > 0 {
|
||||
return nil, fmt.Errorf("proceeding with the order would put maximum orders using leverage ratio beyond its limit of %v to %v and %w", lookup.MaximumOrdersWithLeverageRatio, ratio, errCannotPlaceLeverageOrder)
|
||||
}
|
||||
if retOrder.GetLeverage() > lookup.MaxLeverageRate && lookup.MaxLeverageRate > 0 {
|
||||
return nil, fmt.Errorf("proceeding with the order would put leverage rate beyond its limit of %v to %v and %w", lookup.MaxLeverageRate, retOrder.GetLeverage(), errCannotPlaceLeverageOrder)
|
||||
}
|
||||
}
|
||||
if len(latestHoldings) > 1 {
|
||||
ratio := assessHoldingsRatio(o.Pair(), latestHoldings)
|
||||
if lookup.MaximumHoldingRatio > 0 && ratio != 1 && ratio > lookup.MaximumHoldingRatio {
|
||||
return nil, fmt.Errorf("order would exceed maximum holding ratio of %v to %v for %v %v %v. %w", lookup.MaximumHoldingRatio, ratio, ex, a, p, errCannotPlaceLeverageOrder)
|
||||
}
|
||||
}
|
||||
return retOrder, nil
|
||||
}
|
||||
|
||||
// existingLeverageRatio compares orders with leverage to the total number of orders
|
||||
// a proof of concept to demonstrate risk manager's ability to prevent an order from being placed
|
||||
// when an order exceeds a config setting
|
||||
func existingLeverageRatio(s compliance.Snapshot) float64 {
|
||||
if len(s.Orders) == 0 {
|
||||
return 0
|
||||
}
|
||||
var ordersWithLeverage float64
|
||||
for o := range s.Orders {
|
||||
if s.Orders[o].Leverage != 0 {
|
||||
ordersWithLeverage++
|
||||
}
|
||||
}
|
||||
return ordersWithLeverage / float64(len(s.Orders))
|
||||
}
|
||||
|
||||
func assessHoldingsRatio(c currency.Pair, h []holdings.Holding) float64 {
|
||||
resp := make(map[currency.Pair]float64)
|
||||
totalPosition := 0.0
|
||||
for i := range h {
|
||||
resp[h[i].Pair] += h[i].PositionsValue
|
||||
totalPosition += h[i].PositionsValue
|
||||
}
|
||||
|
||||
if totalPosition == 0 {
|
||||
return 0
|
||||
}
|
||||
ratio := resp[c] / totalPosition
|
||||
|
||||
return ratio
|
||||
}
|
||||
141
backtester/eventhandlers/portfolio/risk/risk_test.go
Normal file
141
backtester/eventhandlers/portfolio/risk/risk_test.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package risk
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/compliance"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
func TestAssessHoldingsRatio(t *testing.T) {
|
||||
t.Parallel()
|
||||
ratio := assessHoldingsRatio(currency.NewPair(currency.BTC, currency.USDT), []holdings.Holding{
|
||||
{
|
||||
Pair: currency.NewPair(currency.BTC, currency.USDT),
|
||||
PositionsValue: 2,
|
||||
},
|
||||
{
|
||||
Pair: currency.NewPair(currency.LTC, currency.USDT),
|
||||
PositionsValue: 2,
|
||||
},
|
||||
})
|
||||
if ratio != 0.5 {
|
||||
t.Errorf("expected %v received %v", 0.5, ratio)
|
||||
}
|
||||
|
||||
ratio = assessHoldingsRatio(currency.NewPair(currency.BTC, currency.USDT), []holdings.Holding{
|
||||
{
|
||||
Pair: currency.NewPair(currency.BTC, currency.USDT),
|
||||
PositionsValue: 1,
|
||||
},
|
||||
{
|
||||
Pair: currency.NewPair(currency.LTC, currency.USDT),
|
||||
PositionsValue: 2,
|
||||
},
|
||||
{
|
||||
Pair: currency.NewPair(currency.DOGE, currency.USDT),
|
||||
PositionsValue: 1,
|
||||
},
|
||||
})
|
||||
if ratio != 0.25 {
|
||||
t.Errorf("expected %v received %v", 0.25, ratio)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvaluateOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
r := Risk{}
|
||||
_, err := r.EvaluateOrder(nil, nil, compliance.Snapshot{})
|
||||
if !errors.Is(err, common.ErrNilArguments) {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
o := &order.Order{}
|
||||
h := []holdings.Holding{}
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
e := "binance"
|
||||
a := asset.Spot
|
||||
o.Exchange = e
|
||||
o.AssetType = a
|
||||
o.CurrencyPair = p
|
||||
r.CurrencySettings = make(map[string]map[asset.Item]map[currency.Pair]*CurrencySettings)
|
||||
r.CurrencySettings[e] = make(map[asset.Item]map[currency.Pair]*CurrencySettings)
|
||||
r.CurrencySettings[e][a] = make(map[currency.Pair]*CurrencySettings)
|
||||
_, err = r.EvaluateOrder(o, h, compliance.Snapshot{})
|
||||
if !errors.Is(err, errNoCurrencySettings) {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
r.CurrencySettings[e][a][p] = &CurrencySettings{
|
||||
MaximumOrdersWithLeverageRatio: 0.3,
|
||||
MaxLeverageRate: 0.3,
|
||||
MaximumHoldingRatio: 0.3,
|
||||
}
|
||||
|
||||
h = append(h, holdings.Holding{
|
||||
Pair: p,
|
||||
PositionsSize: 1,
|
||||
})
|
||||
_, err = r.EvaluateOrder(o, h, compliance.Snapshot{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
h = append(h, holdings.Holding{
|
||||
Pair: currency.NewPair(currency.DOGE, currency.USDT),
|
||||
PositionsSize: 0,
|
||||
})
|
||||
o.Leverage = 1.1
|
||||
r.CurrencySettings[e][a][p].MaximumHoldingRatio = 0
|
||||
_, err = r.EvaluateOrder(o, h, compliance.Snapshot{})
|
||||
if !errors.Is(err, errLeverageNotAllowed) {
|
||||
t.Error(err)
|
||||
}
|
||||
r.CanUseLeverage = true
|
||||
_, err = r.EvaluateOrder(o, h, compliance.Snapshot{})
|
||||
if !errors.Is(err, errCannotPlaceLeverageOrder) {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
r.MaximumLeverage = 33
|
||||
r.CurrencySettings[e][a][p].MaxLeverageRate = 33
|
||||
_, err = r.EvaluateOrder(o, h, compliance.Snapshot{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
r.MaximumLeverage = 33
|
||||
r.CurrencySettings[e][a][p].MaxLeverageRate = 33
|
||||
|
||||
_, err = r.EvaluateOrder(o, h, compliance.Snapshot{
|
||||
Orders: []compliance.SnapshotOrder{
|
||||
{
|
||||
Detail: &gctorder.Detail{
|
||||
Leverage: 3,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
if !errors.Is(err, errCannotPlaceLeverageOrder) {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
h = append(h, holdings.Holding{Pair: p, PositionsValue: 1337}, holdings.Holding{Pair: p, PositionsValue: 1337.42})
|
||||
r.CurrencySettings[e][a][p].MaximumHoldingRatio = 0.1
|
||||
_, err = r.EvaluateOrder(o, h, compliance.Snapshot{})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
h = append(h, holdings.Holding{Pair: currency.NewPair(currency.DOGE, currency.LTC), PositionsValue: 1337})
|
||||
_, err = r.EvaluateOrder(o, h, compliance.Snapshot{})
|
||||
if !errors.Is(err, errCannotPlaceLeverageOrder) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
36
backtester/eventhandlers/portfolio/risk/risk_types.go
Normal file
36
backtester/eventhandlers/portfolio/risk/risk_types.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package risk
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/compliance"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
)
|
||||
|
||||
var (
|
||||
errNoCurrencySettings = errors.New("lacking currency settings, cannot evaluate order")
|
||||
errLeverageNotAllowed = errors.New("order is using leverage when leverage is not enabled in config")
|
||||
errCannotPlaceLeverageOrder = errors.New("cannot place leveraged order")
|
||||
)
|
||||
|
||||
// Handler defines what is expected to be able to assess risk of an order
|
||||
type Handler interface {
|
||||
EvaluateOrder(order.Event, []holdings.Holding, compliance.Snapshot) (*order.Order, error)
|
||||
}
|
||||
|
||||
// Risk contains all currency settings in order to evaluate potential orders
|
||||
type Risk struct {
|
||||
CurrencySettings map[string]map[asset.Item]map[currency.Pair]*CurrencySettings
|
||||
CanUseLeverage bool
|
||||
MaximumLeverage float64
|
||||
}
|
||||
|
||||
// CurrencySettings contains relevant limits to assess risk
|
||||
type CurrencySettings struct {
|
||||
MaximumOrdersWithLeverageRatio float64
|
||||
MaxLeverageRate float64
|
||||
MaximumHoldingRatio float64
|
||||
}
|
||||
37
backtester/eventhandlers/portfolio/settings/settings.go
Normal file
37
backtester/eventhandlers/portfolio/settings/settings.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings"
|
||||
)
|
||||
|
||||
// GetLatestHoldings returns the latest holdings after being sorted by time
|
||||
func (e *Settings) GetLatestHoldings() holdings.Holding {
|
||||
if e.HoldingsSnapshots == nil {
|
||||
// no holdings yet
|
||||
return holdings.Holding{Offset: 1}
|
||||
}
|
||||
|
||||
return e.HoldingsSnapshots[len(e.HoldingsSnapshots)-1]
|
||||
}
|
||||
|
||||
// GetHoldingsForTime returns the holdings for a time period, or an empty holding if not found
|
||||
func (e *Settings) GetHoldingsForTime(t time.Time) holdings.Holding {
|
||||
if e.HoldingsSnapshots == nil {
|
||||
// no holdings yet
|
||||
return holdings.Holding{}
|
||||
}
|
||||
for i := len(e.HoldingsSnapshots) - 1; i >= 0; i-- {
|
||||
if e.HoldingsSnapshots[i].Timestamp.Equal(t) {
|
||||
return e.HoldingsSnapshots[i]
|
||||
}
|
||||
}
|
||||
return holdings.Holding{}
|
||||
}
|
||||
|
||||
// Value returns the total value of the latest holdings
|
||||
func (e *Settings) Value() float64 {
|
||||
latest := e.GetLatestHoldings()
|
||||
return latest.TotalValue
|
||||
}
|
||||
39
backtester/eventhandlers/portfolio/settings/settings_test.go
Normal file
39
backtester/eventhandlers/portfolio/settings/settings_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings"
|
||||
)
|
||||
|
||||
func TestGetLatestHoldings(t *testing.T) {
|
||||
t.Parallel()
|
||||
cs := Settings{}
|
||||
h := cs.GetLatestHoldings()
|
||||
if !h.Timestamp.IsZero() {
|
||||
t.Error("expected zero time")
|
||||
}
|
||||
tt := time.Now()
|
||||
cs.HoldingsSnapshots = append(cs.HoldingsSnapshots, holdings.Holding{Timestamp: tt})
|
||||
|
||||
h = cs.GetLatestHoldings()
|
||||
if !h.Timestamp.Equal(tt) {
|
||||
t.Errorf("expected %v, received %v", tt, h.Timestamp)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValue(t *testing.T) {
|
||||
t.Parallel()
|
||||
cs := Settings{}
|
||||
v := cs.Value()
|
||||
if v != 0 {
|
||||
t.Error("expected 0")
|
||||
}
|
||||
cs.HoldingsSnapshots = append(cs.HoldingsSnapshots, holdings.Holding{TotalValue: 1337})
|
||||
|
||||
v = cs.Value()
|
||||
if v != 1337 {
|
||||
t.Errorf("expected %v, received %v", 1337, v)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/config"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/compliance"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings"
|
||||
)
|
||||
|
||||
// Settings holds all important information for the portfolio manager
|
||||
// to assess purchasing decisions
|
||||
type Settings struct {
|
||||
InitialFunds float64
|
||||
Fee float64
|
||||
BuySideSizing config.MinMax
|
||||
SellSideSizing config.MinMax
|
||||
Leverage config.Leverage
|
||||
HoldingsSnapshots []holdings.Holding
|
||||
ComplianceManager compliance.Manager
|
||||
}
|
||||
48
backtester/eventhandlers/portfolio/size/README.md
Normal file
48
backtester/eventhandlers/portfolio/size/README.md
Normal file
@@ -0,0 +1,48 @@
|
||||
# GoCryptoTrader Backtester: Size package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/size)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This size package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Size package overview
|
||||
|
||||
The sizing package ensures that all potential orders raised are within both the CurrencySettings limits as well as the portfolio manager's limits.
|
||||
- In the event that the order is to large, the sizing package will reduce the order until it fits that limit, inclusive of fees.
|
||||
- When an order is sized under the limits, an order event cannot be raised an no order will be submitted by the exchange
|
||||
- The portfolio manager's sizing rules override any CurrencySettings' rules if the sizing is outside the portfolio manager's
|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
116
backtester/eventhandlers/portfolio/size/size.go
Normal file
116
backtester/eventhandlers/portfolio/size/size.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package size
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/config"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
// SizeOrder is responsible for ensuring that the order size is within config limits
|
||||
func (s *Size) SizeOrder(o order.Event, amountAvailable float64, cs *exchange.Settings) (*order.Order, error) {
|
||||
if o == nil || cs == nil {
|
||||
return nil, common.ErrNilArguments
|
||||
}
|
||||
if amountAvailable <= 0 {
|
||||
return nil, errNoFunds
|
||||
}
|
||||
retOrder := o.(*order.Order)
|
||||
var amount float64
|
||||
var err error
|
||||
switch retOrder.GetDirection() {
|
||||
case gctorder.Buy:
|
||||
// check size against currency specific settings
|
||||
amount, err = s.calculateBuySize(retOrder.Price, amountAvailable, cs.ExchangeFee, cs.BuySide)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// check size against portfolio specific settings
|
||||
var portfolioSize float64
|
||||
portfolioSize, err = s.calculateBuySize(retOrder.Price, amountAvailable, cs.ExchangeFee, s.BuySide)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// global settings overrule individual currency settings
|
||||
if amount > portfolioSize {
|
||||
amount = portfolioSize
|
||||
}
|
||||
|
||||
case gctorder.Sell:
|
||||
// check size against currency specific settings
|
||||
amount, err = s.calculateSellSize(retOrder.Price, amountAvailable, cs.ExchangeFee, cs.SellSide)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// check size against portfolio specific settings
|
||||
portfolioSize, err := s.calculateSellSize(retOrder.Price, amountAvailable, cs.ExchangeFee, s.SellSide)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// global settings overrule individual currency settings
|
||||
if amount > portfolioSize {
|
||||
amount = portfolioSize
|
||||
}
|
||||
}
|
||||
if amount <= 0 {
|
||||
return retOrder, fmt.Errorf("%w at %v for %v %v %v", errCannotAllocate, o.GetTime(), o.GetExchange(), o.GetAssetType(), o.Pair())
|
||||
}
|
||||
retOrder.SetAmount(amount)
|
||||
|
||||
return retOrder, nil
|
||||
}
|
||||
|
||||
// calculateBuySize respects config rules and calculates the amount of money
|
||||
// that is allowed to be spent/sold for an event.
|
||||
// As fee calculation occurs during the actual ordering process
|
||||
// this can only attempt to factor the potential fee to remain under the max rules
|
||||
func (s *Size) calculateBuySize(price, availableFunds, feeRate float64, minMaxSettings config.MinMax) (float64, error) {
|
||||
if availableFunds <= 0 {
|
||||
return 0, errNoFunds
|
||||
}
|
||||
if price == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
amount := availableFunds * (1 - feeRate) / price
|
||||
if minMaxSettings.MaximumSize > 0 && amount > minMaxSettings.MaximumSize {
|
||||
amount = minMaxSettings.MaximumSize * (1 - feeRate)
|
||||
}
|
||||
if minMaxSettings.MaximumTotal > 0 && (amount+feeRate)*price > minMaxSettings.MaximumTotal {
|
||||
amount = minMaxSettings.MaximumTotal * (1 - feeRate) / price
|
||||
}
|
||||
if amount < minMaxSettings.MinimumSize && minMaxSettings.MinimumSize > 0 {
|
||||
return 0, fmt.Errorf("%w. Sized: '%.8f' Minimum: '%v'", errLessThanMinimum, amount, minMaxSettings.MinimumSize)
|
||||
}
|
||||
|
||||
return amount, nil
|
||||
}
|
||||
|
||||
// calculateSellSize respects config rules and calculates the amount of money
|
||||
// that is allowed to be spent/sold for an event.
|
||||
// baseAmount is the base currency quantity that the portfolio currently has that can be sold
|
||||
// eg BTC-USD baseAmount will be BTC to be sold
|
||||
// As fee calculation occurs during the actual ordering process
|
||||
// this can only attempt to factor the potential fee to remain under the max rules
|
||||
func (s *Size) calculateSellSize(price, baseAmount, feeRate float64, minMaxSettings config.MinMax) (float64, error) {
|
||||
if baseAmount <= 0 {
|
||||
return 0, errNoFunds
|
||||
}
|
||||
if price == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
amount := baseAmount * (1 - feeRate)
|
||||
if minMaxSettings.MaximumSize > 0 && amount > minMaxSettings.MaximumSize {
|
||||
amount = minMaxSettings.MaximumSize * (1 - feeRate)
|
||||
}
|
||||
if minMaxSettings.MaximumTotal > 0 && amount*price > minMaxSettings.MaximumTotal {
|
||||
amount = minMaxSettings.MaximumTotal * (1 - feeRate) / price
|
||||
}
|
||||
if amount < minMaxSettings.MinimumSize && minMaxSettings.MinimumSize > 0 {
|
||||
return 0, fmt.Errorf("%w. Sized: '%.8f' Minimum: '%v'", errLessThanMinimum, amount, minMaxSettings.MinimumSize)
|
||||
}
|
||||
|
||||
return amount, nil
|
||||
}
|
||||
177
backtester/eventhandlers/portfolio/size/size_test.go
Normal file
177
backtester/eventhandlers/portfolio/size/size_test.go
Normal file
@@ -0,0 +1,177 @@
|
||||
package size
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/config"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/exchange"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
func TestSizingAccuracy(t *testing.T) {
|
||||
t.Parallel()
|
||||
globalMinMax := config.MinMax{
|
||||
MinimumSize: 0,
|
||||
MaximumSize: 1,
|
||||
MaximumTotal: 1337,
|
||||
}
|
||||
sizer := Size{
|
||||
BuySide: globalMinMax,
|
||||
SellSide: globalMinMax,
|
||||
}
|
||||
price := 1338.0
|
||||
availableFunds := 1338.0
|
||||
feeRate := 0.02
|
||||
|
||||
amountWithoutFee, err := sizer.calculateBuySize(price, availableFunds, feeRate, globalMinMax)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
totalWithFee := (price * amountWithoutFee) + (globalMinMax.MaximumTotal * feeRate)
|
||||
if totalWithFee != globalMinMax.MaximumTotal {
|
||||
t.Error("incorrect amount calculation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSizingOverMaxSize(t *testing.T) {
|
||||
t.Parallel()
|
||||
globalMinMax := config.MinMax{
|
||||
MinimumSize: 0,
|
||||
MaximumSize: 0.5,
|
||||
MaximumTotal: 1337,
|
||||
}
|
||||
sizer := Size{
|
||||
BuySide: globalMinMax,
|
||||
SellSide: globalMinMax,
|
||||
}
|
||||
price := 1338.0
|
||||
availableFunds := 1338.0
|
||||
feeRate := 0.02
|
||||
|
||||
amount, err := sizer.calculateBuySize(price, availableFunds, feeRate, globalMinMax)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if amount > globalMinMax.MaximumSize {
|
||||
t.Error("greater than max")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSizingUnderMinSize(t *testing.T) {
|
||||
t.Parallel()
|
||||
globalMinMax := config.MinMax{
|
||||
MinimumSize: 1,
|
||||
MaximumSize: 2,
|
||||
MaximumTotal: 1337,
|
||||
}
|
||||
sizer := Size{
|
||||
BuySide: globalMinMax,
|
||||
SellSide: globalMinMax,
|
||||
}
|
||||
price := 1338.0
|
||||
availableFunds := 1338.0
|
||||
feeRate := 0.02
|
||||
|
||||
_, err := sizer.calculateBuySize(price, availableFunds, feeRate, globalMinMax)
|
||||
if !errors.Is(err, errLessThanMinimum) {
|
||||
t.Errorf("expected: %v, received %v", errLessThanMinimum, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSizingErrors(t *testing.T) {
|
||||
t.Parallel()
|
||||
globalMinMax := config.MinMax{
|
||||
MinimumSize: 1,
|
||||
MaximumSize: 2,
|
||||
MaximumTotal: 1337,
|
||||
}
|
||||
sizer := Size{
|
||||
BuySide: globalMinMax,
|
||||
SellSide: globalMinMax,
|
||||
}
|
||||
price := 1338.0
|
||||
availableFunds := 0.0
|
||||
feeRate := 0.02
|
||||
|
||||
_, err := sizer.calculateBuySize(price, availableFunds, feeRate, globalMinMax)
|
||||
if !errors.Is(err, errNoFunds) {
|
||||
t.Errorf("expected: %v, received %v", errNoFunds, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalculateSellSize(t *testing.T) {
|
||||
t.Parallel()
|
||||
globalMinMax := config.MinMax{
|
||||
MinimumSize: 1,
|
||||
MaximumSize: 2,
|
||||
MaximumTotal: 1337,
|
||||
}
|
||||
sizer := Size{
|
||||
BuySide: globalMinMax,
|
||||
SellSide: globalMinMax,
|
||||
}
|
||||
price := 1338.0
|
||||
availableFunds := 0.0
|
||||
feeRate := 0.02
|
||||
|
||||
_, err := sizer.calculateSellSize(price, availableFunds, feeRate, globalMinMax)
|
||||
if !errors.Is(err, errNoFunds) {
|
||||
t.Errorf("expected: %v, received %v", errNoFunds, err)
|
||||
}
|
||||
availableFunds = 1337
|
||||
_, err = sizer.calculateSellSize(price, availableFunds, feeRate, globalMinMax)
|
||||
if !errors.Is(err, errLessThanMinimum) {
|
||||
t.Errorf("expected: %v, received %v", errLessThanMinimum, err)
|
||||
}
|
||||
price = 12
|
||||
availableFunds = 1339
|
||||
_, err = sizer.calculateSellSize(price, availableFunds, feeRate, globalMinMax)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSizeOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := Size{}
|
||||
_, err := s.SizeOrder(nil, 0, nil)
|
||||
if !errors.Is(err, common.ErrNilArguments) {
|
||||
t.Error(err)
|
||||
}
|
||||
o := &order.Order{}
|
||||
cs := &exchange.Settings{}
|
||||
_, err = s.SizeOrder(o, 0, cs)
|
||||
if !errors.Is(err, errNoFunds) {
|
||||
t.Errorf("expected: %v, received %v", errNoFunds, err)
|
||||
}
|
||||
|
||||
_, err = s.SizeOrder(o, 1337, cs)
|
||||
if !errors.Is(err, errCannotAllocate) {
|
||||
t.Errorf("expected: %v, received %v", errCannotAllocate, err)
|
||||
}
|
||||
|
||||
o.Direction = gctorder.Buy
|
||||
o.Price = 1
|
||||
s.BuySide.MaximumSize = 1
|
||||
s.BuySide.MinimumSize = 1
|
||||
_, err = s.SizeOrder(o, 1337, cs)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
o.Direction = gctorder.Sell
|
||||
_, err = s.SizeOrder(o, 1337, cs)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
s.SellSide.MaximumSize = 1
|
||||
s.SellSide.MinimumSize = 1
|
||||
_, err = s.SizeOrder(o, 1337, cs)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
19
backtester/eventhandlers/portfolio/size/size_types.go
Normal file
19
backtester/eventhandlers/portfolio/size/size_types.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package size
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/config"
|
||||
)
|
||||
|
||||
var (
|
||||
errNoFunds = errors.New("no funds available")
|
||||
errLessThanMinimum = errors.New("sized amount less than minimum")
|
||||
errCannotAllocate = errors.New("portfolio manager cannot allocate funds for an order")
|
||||
)
|
||||
|
||||
// Size contains buy and sell side rules
|
||||
type Size struct {
|
||||
BuySide config.MinMax
|
||||
SellSide config.MinMax
|
||||
}
|
||||
47
backtester/eventhandlers/statistics/README.md
Normal file
47
backtester/eventhandlers/statistics/README.md
Normal file
@@ -0,0 +1,47 @@
|
||||
# GoCryptoTrader Backtester: Statistics package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/statistics)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This statistics package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Statistics package overview
|
||||
|
||||
The statistics package is used for storing all relevant data over the course of a GoCryptoTrader Backtesting run. All types of events are tracked by exchange, asset and currency pair.
|
||||
When multiple currencies are included in your strategy, the statistics package will be able to calculate which exchange asset currency pair has performed the best, along with the biggest drop downs in the market.
|
||||
|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
@@ -0,0 +1,73 @@
|
||||
# GoCryptoTrader Backtester: Currencystatistics package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/statistics/currencystatistics)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This currencystatistics package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Currencystatistics package overview
|
||||
|
||||
Currency Statistics is an important package to verify the effectiveness of your strategies.
|
||||
It can calculate the following:
|
||||
- Calmar ratio
|
||||
- Information ratio
|
||||
- Sharpe ratio
|
||||
- Sortino ratio
|
||||
- CAGR
|
||||
- Drawdowns, both the biggest and longest
|
||||
- Whether the strategy outperformed the market
|
||||
- If the strategy made a profit
|
||||
|
||||
## Ratios
|
||||
|
||||
| Ratio | Description | A good range |
|
||||
| ----- | ----------- | ------------ |
|
||||
| Calmar ratio | It is a function of the fund's average compounded annual rate of return versus its maximum drawdown. The higher the Calmar ratio, the better it performed on a risk-adjusted basis during the given time frame, which is mostly commonly set at 36 months. | 3.0 to 5.0 |
|
||||
| Information ratio| It is a measurement of portfolio returns beyond the returns of a benchmark, usually an index, compared to the volatility of those returns. The ratio is often used as a measure of a portfolio manager's level of skill and ability to generate excess returns relative to a benchmark | 0.40-0.60. Any positive number means that it has beaten the benchmark |
|
||||
| Sharpe ratio | The Sharpe Ratio is a financial metric often used by investors when assessing the performance of investment management products and professionals. It consists of taking the excess return of the portfolio, relative to the risk-free rate, and dividing it by the standard deviation of the portfolio's excess returns | Any Sharpe ratio greater than 1.0 is good. Higher than 2.0 is very good. 3.0 or higher is excellent. Under 1.0 is sub-optimal |
|
||||
| Sortino ratio | The Sortino ratio measures the risk-adjusted return of an investment asset, portfolio, or strategy. It is a modification of the Sharpe ratio but penalizes only those returns falling below a user-specified target or required rate of return, while the Sharpe ratio penalizes both upside and downside volatility equally. | The higher the better, but > 2 is considered good. |
|
||||
| Compound annual growth rate | Compound annual growth rate is the rate of return that would be required for an investment to grow from its beginning balance to its ending balance, assuming the profits were reinvested at the end of each year of the investment’s lifespan | Any positive number |
|
||||
|
||||
## Arithmetic or versus geometric?
|
||||
Both! We calculate ratios where an average is required using both types. The reasoning for using either is debated by finance and mathematicians. [This](https://www.investopedia.com/ask/answers/06/geometricmean.asp) is a good breakdown of both, but here is an extra simple table
|
||||
|
||||
| Average type | A reason to use it |
|
||||
| ------------ | ------------------ |
|
||||
| Arithmetic | The arithmetic mean is the average of a sum of numbers, which reflects the central tendency of the position of the numbers |
|
||||
| Geometric | The geometric mean differs from the arithmetic average, or arithmetic mean, in how it is calculated because it takes into account the compounding that occurs from period to period. Because of this, investors usually consider the geometric mean a more accurate measure of returns than the arithmetic mean. |
|
||||
|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
@@ -0,0 +1,332 @@
|
||||
package currencystatistics
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
gctcommon "github.com/thrasher-corp/gocryptotrader/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/common/math"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
)
|
||||
|
||||
// CalculateResults calculates all statistics for the exchange, asset, currency pair
|
||||
func (c *CurrencyStatistic) CalculateResults() error {
|
||||
var errs gctcommon.Errors
|
||||
first := c.Events[0]
|
||||
firstPrice := first.DataEvent.ClosePrice()
|
||||
last := c.Events[len(c.Events)-1]
|
||||
lastPrice := last.DataEvent.ClosePrice()
|
||||
for i := range last.Transactions.Orders {
|
||||
if last.Transactions.Orders[i].Side == gctorder.Buy {
|
||||
c.BuyOrders++
|
||||
} else if last.Transactions.Orders[i].Side == gctorder.Sell {
|
||||
c.SellOrders++
|
||||
}
|
||||
}
|
||||
for i := range c.Events {
|
||||
price := c.Events[i].DataEvent.ClosePrice()
|
||||
if c.LowestClosePrice == 0 || price < c.LowestClosePrice {
|
||||
c.LowestClosePrice = price
|
||||
}
|
||||
if price > c.HighestClosePrice {
|
||||
c.HighestClosePrice = price
|
||||
}
|
||||
}
|
||||
c.MarketMovement = ((lastPrice - firstPrice) / firstPrice) * 100
|
||||
c.StrategyMovement = ((last.Holdings.TotalValue - last.Holdings.InitialFunds) / last.Holdings.InitialFunds) * 100
|
||||
c.calculateHighestCommittedFunds()
|
||||
c.RiskFreeRate = last.Holdings.RiskFreeRate * 100
|
||||
returnPerCandle := make([]float64, len(c.Events))
|
||||
benchmarkRates := make([]float64, len(c.Events))
|
||||
|
||||
var allDataEvents []common.DataEventHandler
|
||||
for i := range c.Events {
|
||||
returnPerCandle[i] = c.Events[i].Holdings.ChangeInTotalValuePercent
|
||||
allDataEvents = append(allDataEvents, c.Events[i].DataEvent)
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
if c.Events[i].SignalEvent != nil && c.Events[i].SignalEvent.GetDirection() == common.MissingData {
|
||||
c.ShowMissingDataWarning = true
|
||||
}
|
||||
benchmarkRates[i] = (c.Events[i].DataEvent.ClosePrice() - c.Events[i-1].DataEvent.ClosePrice()) / c.Events[i-1].DataEvent.ClosePrice()
|
||||
}
|
||||
|
||||
// remove the first entry as its zero and impacts
|
||||
// ratio calculations as no movement has been made
|
||||
benchmarkRates = benchmarkRates[1:]
|
||||
returnPerCandle = returnPerCandle[1:]
|
||||
|
||||
var arithmeticBenchmarkAverage, geometricBenchmarkAverage float64
|
||||
var err error
|
||||
arithmeticBenchmarkAverage, err = math.ArithmeticMean(benchmarkRates)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
geometricBenchmarkAverage, err = math.FinancialGeometricMean(benchmarkRates)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
|
||||
c.MaxDrawdown = calculateMaxDrawdown(allDataEvents)
|
||||
interval := first.DataEvent.GetInterval()
|
||||
intervalsPerYear := interval.IntervalsPerYear()
|
||||
|
||||
riskFreeRatePerCandle := first.Holdings.RiskFreeRate / intervalsPerYear
|
||||
riskFreeRateForPeriod := riskFreeRatePerCandle * float64(len(benchmarkRates))
|
||||
|
||||
var arithmeticReturnsPerCandle, geometricReturnsPerCandle, arithmeticSharpe, arithmeticSortino,
|
||||
arithmeticInformation, arithmeticCalmar, geomSharpe, geomSortino, geomInformation, geomCalmar float64
|
||||
|
||||
arithmeticReturnsPerCandle, err = math.ArithmeticMean(returnPerCandle)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
geometricReturnsPerCandle, err = math.FinancialGeometricMean(returnPerCandle)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
|
||||
arithmeticSharpe, err = math.SharpeRatio(returnPerCandle, riskFreeRatePerCandle, arithmeticReturnsPerCandle)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
arithmeticSortino, err = math.SortinoRatio(returnPerCandle, riskFreeRatePerCandle, arithmeticReturnsPerCandle)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
arithmeticInformation, err = math.InformationRatio(returnPerCandle, benchmarkRates, arithmeticReturnsPerCandle, arithmeticBenchmarkAverage)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
arithmeticCalmar, err = math.CalmarRatio(c.MaxDrawdown.Highest.Price, c.MaxDrawdown.Lowest.Price, arithmeticReturnsPerCandle, riskFreeRateForPeriod)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
c.ArithmeticRatios = Ratios{
|
||||
SharpeRatio: arithmeticSharpe,
|
||||
SortinoRatio: arithmeticSortino,
|
||||
InformationRatio: arithmeticInformation,
|
||||
CalmarRatio: arithmeticCalmar,
|
||||
}
|
||||
|
||||
geomSharpe, err = math.SharpeRatio(returnPerCandle, riskFreeRatePerCandle, geometricReturnsPerCandle)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
geomSortino, err = math.SortinoRatio(returnPerCandle, riskFreeRatePerCandle, geometricReturnsPerCandle)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
geomInformation, err = math.InformationRatio(returnPerCandle, benchmarkRates, geometricReturnsPerCandle, geometricBenchmarkAverage)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
geomCalmar, err = math.CalmarRatio(c.MaxDrawdown.Highest.Price, c.MaxDrawdown.Lowest.Price, geometricReturnsPerCandle, riskFreeRateForPeriod)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
c.GeometricRatios = Ratios{
|
||||
SharpeRatio: geomSharpe,
|
||||
SortinoRatio: geomSortino,
|
||||
InformationRatio: geomInformation,
|
||||
CalmarRatio: geomCalmar,
|
||||
}
|
||||
|
||||
c.CompoundAnnualGrowthRate, err = math.CompoundAnnualGrowthRate(
|
||||
last.Holdings.InitialFunds,
|
||||
last.Holdings.TotalValue,
|
||||
intervalsPerYear,
|
||||
float64(len(c.Events)))
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PrintResults outputs all calculated statistics to the command line
|
||||
func (c *CurrencyStatistic) PrintResults(e string, a asset.Item, p currency.Pair) {
|
||||
var errs gctcommon.Errors
|
||||
sort.Slice(c.Events, func(i, j int) bool {
|
||||
return c.Events[i].DataEvent.GetTime().Before(c.Events[j].DataEvent.GetTime())
|
||||
})
|
||||
last := c.Events[len(c.Events)-1]
|
||||
first := c.Events[0]
|
||||
c.StartingClosePrice = first.DataEvent.ClosePrice()
|
||||
c.EndingClosePrice = last.DataEvent.ClosePrice()
|
||||
c.TotalOrders = c.BuyOrders + c.SellOrders
|
||||
last.Holdings.TotalValueLost = last.Holdings.TotalValueLostToSlippage + last.Holdings.TotalValueLostToVolumeSizing
|
||||
currStr := fmt.Sprintf("------------------Stats for %v %v %v------------------------------------------", e, a, p)
|
||||
|
||||
log.Infof(log.BackTester, currStr[:61])
|
||||
log.Infof(log.BackTester, "Initial funds: $%.2f", last.Holdings.InitialFunds)
|
||||
log.Infof(log.BackTester, "Highest committed funds: $%.2f at %v\n\n", c.HighestCommittedFunds.Value, c.HighestCommittedFunds.Time)
|
||||
|
||||
log.Infof(log.BackTester, "Buy orders: %d", c.BuyOrders)
|
||||
log.Infof(log.BackTester, "Buy value: $%.2f", last.Holdings.BoughtValue)
|
||||
log.Infof(log.BackTester, "Buy amount: %.2f %v", last.Holdings.BoughtAmount, last.Holdings.Pair.Base)
|
||||
log.Infof(log.BackTester, "Sell orders: %d", c.SellOrders)
|
||||
log.Infof(log.BackTester, "Sell value: $%.2f", last.Holdings.SoldValue)
|
||||
log.Infof(log.BackTester, "Sell amount: %.2f %v", last.Holdings.SoldAmount, last.Holdings.Pair.Base)
|
||||
log.Infof(log.BackTester, "Total orders: %d\n\n", c.TotalOrders)
|
||||
|
||||
log.Info(log.BackTester, "------------------Max Drawdown-------------------------------")
|
||||
log.Infof(log.BackTester, "Highest Price of drawdown: $%.2f", c.MaxDrawdown.Highest.Price)
|
||||
log.Infof(log.BackTester, "Time of highest price of drawdown: %v", c.MaxDrawdown.Highest.Time)
|
||||
log.Infof(log.BackTester, "Lowest Price of drawdown: $%.2f", c.MaxDrawdown.Lowest.Price)
|
||||
log.Infof(log.BackTester, "Time of lowest price of drawdown: %v", c.MaxDrawdown.Lowest.Time)
|
||||
log.Infof(log.BackTester, "Calculated Drawdown: %.2f%%", c.MaxDrawdown.DrawdownPercent)
|
||||
log.Infof(log.BackTester, "Difference: $%.2f", c.MaxDrawdown.Highest.Price-c.MaxDrawdown.Lowest.Price)
|
||||
log.Infof(log.BackTester, "Drawdown length: %d\n\n", c.MaxDrawdown.IntervalDuration)
|
||||
|
||||
log.Info(log.BackTester, "------------------Rates-------------------------------------------------")
|
||||
log.Infof(log.BackTester, "Risk free rate: %.3f%%", c.RiskFreeRate)
|
||||
log.Infof(log.BackTester, "Compound Annual Growth Rate: %.2f\n\n", c.CompoundAnnualGrowthRate)
|
||||
|
||||
log.Info(log.BackTester, "------------------Arithmetic Ratios-------------------------------------")
|
||||
if c.ShowMissingDataWarning {
|
||||
log.Infoln(log.BackTester, "Missing data was detected during this backtesting run")
|
||||
log.Infoln(log.BackTester, "Ratio calculations will be skewed")
|
||||
}
|
||||
log.Infof(log.BackTester, "Sharpe ratio: %.2f", c.ArithmeticRatios.SharpeRatio)
|
||||
log.Infof(log.BackTester, "Sortino ratio: %.2f", c.ArithmeticRatios.SortinoRatio)
|
||||
log.Infof(log.BackTester, "Information ratio: %.2f", c.ArithmeticRatios.InformationRatio)
|
||||
log.Infof(log.BackTester, "Calmar ratio: %.2f\n\n", c.ArithmeticRatios.CalmarRatio)
|
||||
|
||||
log.Info(log.BackTester, "------------------Geometric Ratios-------------------------------------")
|
||||
if c.ShowMissingDataWarning {
|
||||
log.Infoln(log.BackTester, "Missing data was detected during this backtesting run")
|
||||
log.Infoln(log.BackTester, "Ratio calculations will be skewed")
|
||||
}
|
||||
log.Infof(log.BackTester, "Sharpe ratio: %.2f", c.GeometricRatios.SharpeRatio)
|
||||
log.Infof(log.BackTester, "Sortino ratio: %.2f", c.GeometricRatios.SortinoRatio)
|
||||
log.Infof(log.BackTester, "Information ratio: %.2f", c.GeometricRatios.InformationRatio)
|
||||
log.Infof(log.BackTester, "Calmar ratio: %.2f\n\n", c.GeometricRatios.CalmarRatio)
|
||||
|
||||
log.Info(log.BackTester, "------------------Results------------------------------------")
|
||||
log.Infof(log.BackTester, "Starting Close Price: $%.2f", c.StartingClosePrice)
|
||||
log.Infof(log.BackTester, "Finishing Close Price: $%.2f", c.EndingClosePrice)
|
||||
log.Infof(log.BackTester, "Lowest Close Price: $%.2f", c.LowestClosePrice)
|
||||
log.Infof(log.BackTester, "Highest Close Price: $%.2f", c.HighestClosePrice)
|
||||
|
||||
log.Infof(log.BackTester, "Market movement: %.4f%%", c.MarketMovement)
|
||||
log.Infof(log.BackTester, "Strategy movement: %.4f%%", c.StrategyMovement)
|
||||
log.Infof(log.BackTester, "Did it beat the market: %v", c.StrategyMovement > c.MarketMovement)
|
||||
|
||||
log.Infof(log.BackTester, "Value lost to volume sizing: $%.2f", last.Holdings.TotalValueLostToVolumeSizing)
|
||||
log.Infof(log.BackTester, "Value lost to slippage: $%.2f", last.Holdings.TotalValueLostToSlippage)
|
||||
log.Infof(log.BackTester, "Total Value lost: $%.2f", last.Holdings.TotalValueLost)
|
||||
log.Infof(log.BackTester, "Total Fees: $%.2f\n\n", last.Holdings.TotalFees)
|
||||
|
||||
log.Infof(log.BackTester, "Final funds: $%.2f", last.Holdings.RemainingFunds)
|
||||
log.Infof(log.BackTester, "Final holdings: %.2f", last.Holdings.PositionsSize)
|
||||
log.Infof(log.BackTester, "Final holdings value: $%.2f", last.Holdings.PositionsValue)
|
||||
log.Infof(log.BackTester, "Final total value: $%.2f\n\n", last.Holdings.TotalValue)
|
||||
|
||||
if len(errs) > 0 {
|
||||
log.Info(log.BackTester, "------------------Errors-------------------------------------")
|
||||
for i := range errs {
|
||||
log.Info(log.BackTester, errs[i].Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func calculateMaxDrawdown(closePrices []common.DataEventHandler) Swing {
|
||||
var lowestPrice, highestPrice float64
|
||||
var lowestTime, highestTime time.Time
|
||||
var swings []Swing
|
||||
if len(closePrices) > 0 {
|
||||
lowestPrice = closePrices[0].LowPrice()
|
||||
highestPrice = closePrices[0].HighPrice()
|
||||
lowestTime = closePrices[0].GetTime()
|
||||
highestTime = closePrices[0].GetTime()
|
||||
}
|
||||
for i := range closePrices {
|
||||
currHigh := closePrices[i].HighPrice()
|
||||
currLow := closePrices[i].LowPrice()
|
||||
currTime := closePrices[i].GetTime()
|
||||
if lowestPrice > currLow && currLow != 0 {
|
||||
lowestPrice = currLow
|
||||
lowestTime = currTime
|
||||
}
|
||||
if highestPrice < currHigh && highestPrice > 0 {
|
||||
intervals := gctkline.CalculateCandleDateRanges(highestTime, lowestTime, closePrices[i].GetInterval(), 0)
|
||||
if lowestTime.Equal(highestTime) {
|
||||
// create distinction if the greatest drawdown occurs within the same candle
|
||||
lowestTime = lowestTime.Add((time.Hour * 23) + (time.Minute * 59) + (time.Second * 59))
|
||||
}
|
||||
swings = append(swings, Swing{
|
||||
Highest: Iteration{
|
||||
Time: highestTime,
|
||||
Price: highestPrice,
|
||||
},
|
||||
Lowest: Iteration{
|
||||
Time: lowestTime,
|
||||
Price: lowestPrice,
|
||||
},
|
||||
DrawdownPercent: ((lowestPrice - highestPrice) / highestPrice) * 100,
|
||||
IntervalDuration: int64(len(intervals.Ranges[0].Intervals)),
|
||||
})
|
||||
// reset the drawdown
|
||||
highestPrice = currHigh
|
||||
highestTime = currTime
|
||||
lowestPrice = currLow
|
||||
lowestTime = currTime
|
||||
}
|
||||
}
|
||||
if (len(swings) > 0 && swings[len(swings)-1].Lowest.Price != closePrices[len(closePrices)-1].LowPrice()) || swings == nil {
|
||||
// need to close out the final drawdown
|
||||
intervals := gctkline.CalculateCandleDateRanges(highestTime, lowestTime, closePrices[0].GetInterval(), 0)
|
||||
drawdownPercent := 0.0
|
||||
if highestPrice > 0 {
|
||||
drawdownPercent = ((lowestPrice - highestPrice) / highestPrice) * 100
|
||||
}
|
||||
if lowestTime.Equal(highestTime) {
|
||||
// create distinction if the greatest drawdown occurs within the same candle
|
||||
lowestTime = lowestTime.Add((time.Hour * 23) + (time.Minute * 59) + (time.Second * 59))
|
||||
}
|
||||
swings = append(swings, Swing{
|
||||
Highest: Iteration{
|
||||
Time: highestTime,
|
||||
Price: highestPrice,
|
||||
},
|
||||
Lowest: Iteration{
|
||||
Time: lowestTime,
|
||||
Price: lowestPrice,
|
||||
},
|
||||
DrawdownPercent: drawdownPercent,
|
||||
IntervalDuration: int64(len(intervals.Ranges[0].Intervals)),
|
||||
})
|
||||
}
|
||||
|
||||
var maxDrawdown Swing
|
||||
if len(swings) > 0 {
|
||||
maxDrawdown = swings[0]
|
||||
}
|
||||
for i := range swings {
|
||||
if swings[i].DrawdownPercent < maxDrawdown.DrawdownPercent {
|
||||
// drawdowns are negative
|
||||
maxDrawdown = swings[i]
|
||||
}
|
||||
}
|
||||
|
||||
return maxDrawdown
|
||||
}
|
||||
|
||||
func (c *CurrencyStatistic) calculateHighestCommittedFunds() {
|
||||
for i := range c.Events {
|
||||
if c.Events[i].Holdings.CommittedFunds > c.HighestCommittedFunds.Value {
|
||||
c.HighestCommittedFunds.Value = c.Events[i].Holdings.CommittedFunds
|
||||
c.HighestCommittedFunds.Time = c.Events[i].Holdings.Timestamp
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
package currencystatistics
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/compliance"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/event"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/signal"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
const testExchange = "binance"
|
||||
|
||||
func TestCalculateResults(t *testing.T) {
|
||||
cs := CurrencyStatistic{}
|
||||
tt1 := time.Now()
|
||||
tt2 := time.Now().Add(gctkline.OneDay.Duration())
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
even := event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt1,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
}
|
||||
ev := EventStore{
|
||||
Holdings: holdings.Holding{
|
||||
ChangeInTotalValuePercent: 0.1333,
|
||||
Timestamp: tt1,
|
||||
InitialFunds: 1337,
|
||||
},
|
||||
Transactions: compliance.Snapshot{
|
||||
Orders: []compliance.SnapshotOrder{
|
||||
{
|
||||
ClosePrice: 1338,
|
||||
VolumeAdjustedPrice: 1338,
|
||||
SlippageRate: 1338,
|
||||
CostBasis: 1338,
|
||||
Detail: &order.Detail{Side: order.Buy},
|
||||
},
|
||||
{
|
||||
ClosePrice: 1337,
|
||||
VolumeAdjustedPrice: 1337,
|
||||
SlippageRate: 1337,
|
||||
CostBasis: 1337,
|
||||
Detail: &order.Detail{Side: order.Sell},
|
||||
},
|
||||
},
|
||||
},
|
||||
DataEvent: &kline.Kline{
|
||||
Base: even,
|
||||
Open: 2000,
|
||||
Close: 2000,
|
||||
Low: 2000,
|
||||
High: 2000,
|
||||
Volume: 2000,
|
||||
},
|
||||
SignalEvent: &signal.Signal{
|
||||
Base: even,
|
||||
ClosePrice: 2000,
|
||||
},
|
||||
}
|
||||
even2 := even
|
||||
even2.Time = tt2
|
||||
ev2 := EventStore{
|
||||
Holdings: holdings.Holding{
|
||||
ChangeInTotalValuePercent: 0.1337,
|
||||
Timestamp: tt2,
|
||||
InitialFunds: 1337,
|
||||
},
|
||||
Transactions: compliance.Snapshot{
|
||||
Orders: []compliance.SnapshotOrder{
|
||||
{
|
||||
ClosePrice: 1338,
|
||||
VolumeAdjustedPrice: 1338,
|
||||
SlippageRate: 1338,
|
||||
CostBasis: 1338,
|
||||
Detail: &order.Detail{Side: order.Buy},
|
||||
},
|
||||
{
|
||||
ClosePrice: 1337,
|
||||
VolumeAdjustedPrice: 1337,
|
||||
SlippageRate: 1337,
|
||||
CostBasis: 1337,
|
||||
Detail: &order.Detail{Side: order.Sell},
|
||||
},
|
||||
},
|
||||
},
|
||||
DataEvent: &kline.Kline{
|
||||
Base: even2,
|
||||
Open: 1337,
|
||||
Close: 1337,
|
||||
Low: 1337,
|
||||
High: 1337,
|
||||
Volume: 1337,
|
||||
},
|
||||
SignalEvent: &signal.Signal{
|
||||
Base: even2,
|
||||
ClosePrice: 1337,
|
||||
},
|
||||
}
|
||||
|
||||
cs.Events = append(cs.Events, ev, ev2)
|
||||
err := cs.CalculateResults()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if cs.MarketMovement != -33.15 {
|
||||
t.Error("expected -33.15")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintResults(t *testing.T) {
|
||||
cs := CurrencyStatistic{}
|
||||
tt1 := time.Now()
|
||||
tt2 := time.Now().Add(gctkline.OneDay.Duration())
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
even := event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt1,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
}
|
||||
ev := EventStore{
|
||||
Holdings: holdings.Holding{
|
||||
ChangeInTotalValuePercent: 0.1333,
|
||||
Timestamp: tt1,
|
||||
InitialFunds: 1337,
|
||||
},
|
||||
Transactions: compliance.Snapshot{
|
||||
Orders: []compliance.SnapshotOrder{
|
||||
{
|
||||
ClosePrice: 1338,
|
||||
VolumeAdjustedPrice: 1338,
|
||||
SlippageRate: 1338,
|
||||
CostBasis: 1338,
|
||||
Detail: &order.Detail{Side: order.Buy},
|
||||
},
|
||||
{
|
||||
ClosePrice: 1337,
|
||||
VolumeAdjustedPrice: 1337,
|
||||
SlippageRate: 1337,
|
||||
CostBasis: 1337,
|
||||
Detail: &order.Detail{Side: order.Sell},
|
||||
},
|
||||
},
|
||||
},
|
||||
DataEvent: &kline.Kline{
|
||||
Base: even,
|
||||
Open: 2000,
|
||||
Close: 2000,
|
||||
Low: 2000,
|
||||
High: 2000,
|
||||
Volume: 2000,
|
||||
},
|
||||
SignalEvent: &signal.Signal{
|
||||
Base: even,
|
||||
ClosePrice: 2000,
|
||||
},
|
||||
}
|
||||
even2 := even
|
||||
even2.Time = tt2
|
||||
ev2 := EventStore{
|
||||
Holdings: holdings.Holding{
|
||||
ChangeInTotalValuePercent: 0.1337,
|
||||
Timestamp: tt2,
|
||||
InitialFunds: 1337,
|
||||
},
|
||||
Transactions: compliance.Snapshot{
|
||||
Orders: []compliance.SnapshotOrder{
|
||||
{
|
||||
ClosePrice: 1338,
|
||||
VolumeAdjustedPrice: 1338,
|
||||
SlippageRate: 1338,
|
||||
CostBasis: 1338,
|
||||
Detail: &order.Detail{Side: order.Buy},
|
||||
},
|
||||
{
|
||||
ClosePrice: 1337,
|
||||
VolumeAdjustedPrice: 1337,
|
||||
SlippageRate: 1337,
|
||||
CostBasis: 1337,
|
||||
Detail: &order.Detail{Side: order.Sell},
|
||||
},
|
||||
},
|
||||
},
|
||||
DataEvent: &kline.Kline{
|
||||
Base: even2,
|
||||
Open: 1337,
|
||||
Close: 1337,
|
||||
Low: 1337,
|
||||
High: 1337,
|
||||
Volume: 1337,
|
||||
},
|
||||
SignalEvent: &signal.Signal{
|
||||
Base: even2,
|
||||
ClosePrice: 1337,
|
||||
},
|
||||
}
|
||||
|
||||
cs.Events = append(cs.Events, ev, ev2)
|
||||
err := cs.CalculateResults()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
cs.PrintResults(exch, a, p)
|
||||
}
|
||||
|
||||
func TestCalculateMaxDrawdown(t *testing.T) {
|
||||
tt1 := time.Now().Round(gctkline.OneDay.Duration())
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
var events []common.DataEventHandler
|
||||
for i := 0; i < 100; i++ {
|
||||
tt1 = tt1.Add(gctkline.OneDay.Duration())
|
||||
even := event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt1,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
}
|
||||
if i == 50 {
|
||||
// throw in a wrench, a spike in price
|
||||
events = append(events, &kline.Kline{
|
||||
Base: even,
|
||||
Close: 1336,
|
||||
High: 1336,
|
||||
Low: 1336,
|
||||
})
|
||||
} else {
|
||||
events = append(events, &kline.Kline{
|
||||
Base: even,
|
||||
Close: 1337 - float64(i),
|
||||
High: 1337 - float64(i),
|
||||
Low: 1337 - float64(i),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
tt1 = tt1.Add(gctkline.OneDay.Duration())
|
||||
even := event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt1,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
}
|
||||
events = append(events, &kline.Kline{
|
||||
Base: even,
|
||||
Close: 1338,
|
||||
High: 1338,
|
||||
Low: 1338,
|
||||
})
|
||||
|
||||
tt1 = tt1.Add(gctkline.OneDay.Duration())
|
||||
even = event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt1,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
}
|
||||
events = append(events, &kline.Kline{
|
||||
Base: even,
|
||||
Close: 1337,
|
||||
High: 1337,
|
||||
Low: 1337,
|
||||
})
|
||||
|
||||
tt1 = tt1.Add(gctkline.OneDay.Duration())
|
||||
even = event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt1,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
}
|
||||
events = append(events, &kline.Kline{
|
||||
Base: even,
|
||||
Close: 1339,
|
||||
High: 1339,
|
||||
Low: 1339,
|
||||
})
|
||||
|
||||
resp := calculateMaxDrawdown(events)
|
||||
if resp.Highest.Price != 1337 && resp.Lowest.Price != 1238 {
|
||||
t.Error("unexpected max drawdown")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalculateHighestCommittedFunds(t *testing.T) {
|
||||
c := CurrencyStatistic{}
|
||||
c.calculateHighestCommittedFunds()
|
||||
if !c.HighestCommittedFunds.Time.IsZero() {
|
||||
t.Error("expected no time with not committed funds")
|
||||
}
|
||||
tt1 := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
tt2 := time.Date(2021, 2, 1, 0, 0, 0, 0, time.UTC)
|
||||
tt3 := time.Date(2021, 3, 1, 0, 0, 0, 0, time.UTC)
|
||||
c.Events = append(c.Events,
|
||||
EventStore{Holdings: holdings.Holding{Timestamp: tt1, CommittedFunds: 10}},
|
||||
EventStore{Holdings: holdings.Holding{Timestamp: tt2, CommittedFunds: 1337}},
|
||||
EventStore{Holdings: holdings.Holding{Timestamp: tt3, CommittedFunds: 11}},
|
||||
)
|
||||
c.calculateHighestCommittedFunds()
|
||||
if c.HighestCommittedFunds.Time != tt2 {
|
||||
t.Errorf("expected %v, received %v", tt2, c.HighestCommittedFunds.Time)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package currencystatistics
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/compliance"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/fill"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/signal"
|
||||
)
|
||||
|
||||
// CurrencyStats defines what is expected in order to
|
||||
// calculate statistics based on an exchange, asset type and currency pair
|
||||
type CurrencyStats interface {
|
||||
TotalEquityReturn() (float64, error)
|
||||
MaxDrawdown() Swing
|
||||
LongestDrawdown() Swing
|
||||
SharpeRatio(float64) float64
|
||||
SortinoRatio(float64) float64
|
||||
}
|
||||
|
||||
// EventStore is used to hold all event information
|
||||
// at a time interval
|
||||
type EventStore struct {
|
||||
Holdings holdings.Holding
|
||||
Transactions compliance.Snapshot
|
||||
DataEvent common.DataEventHandler
|
||||
SignalEvent signal.Event
|
||||
OrderEvent order.Event
|
||||
FillEvent fill.Event
|
||||
}
|
||||
|
||||
// CurrencyStatistic Holds all events and statistics relevant to an exchange, asset type and currency pair
|
||||
type CurrencyStatistic struct {
|
||||
Events []EventStore `json:"-"`
|
||||
MaxDrawdown Swing `json:"max-drawdown,omitempty"`
|
||||
StartingClosePrice float64 `json:"starting-close-price"`
|
||||
EndingClosePrice float64 `json:"ending-close-price"`
|
||||
LowestClosePrice float64 `json:"lowest-close-price"`
|
||||
HighestClosePrice float64 `json:"highest-close-price"`
|
||||
MarketMovement float64 `json:"market-movement"`
|
||||
StrategyMovement float64 `json:"strategy-movement"`
|
||||
HighestCommittedFunds HighestCommittedFunds `json:"highest-committed-funds"`
|
||||
RiskFreeRate float64 `json:"risk-free-rate"`
|
||||
BuyOrders int64 `json:"buy-orders"`
|
||||
GeometricRatios Ratios `json:"geometric-ratios"`
|
||||
ArithmeticRatios Ratios `json:"arithmetic-ratios"`
|
||||
CompoundAnnualGrowthRate float64 `json:"compound-annual-growth-rate"`
|
||||
SellOrders int64 `json:"sell-orders"`
|
||||
TotalOrders int64 `json:"total-orders"`
|
||||
FinalHoldings holdings.Holding `json:"final-holdings"`
|
||||
FinalOrders compliance.Snapshot `json:"final-orders"`
|
||||
ShowMissingDataWarning bool `json:"-"`
|
||||
}
|
||||
|
||||
// Ratios stores all the ratios used for statistics
|
||||
type Ratios struct {
|
||||
SharpeRatio float64 `json:"sharpe-ratio"`
|
||||
SortinoRatio float64 `json:"sortino-ratio"`
|
||||
InformationRatio float64 `json:"information-ratio"`
|
||||
CalmarRatio float64 `json:"calmar-ratio"`
|
||||
}
|
||||
|
||||
// Swing holds a drawdown
|
||||
type Swing struct {
|
||||
Highest Iteration `json:"highest"`
|
||||
Lowest Iteration `json:"lowest"`
|
||||
DrawdownPercent float64 `json:"drawdown"`
|
||||
IntervalDuration int64
|
||||
}
|
||||
|
||||
// Iteration is an individual iteration of price at a time
|
||||
type Iteration struct {
|
||||
Time time.Time `json:"time"`
|
||||
Price float64 `json:"price"`
|
||||
}
|
||||
|
||||
// HighestCommittedFunds is an individual iteration of price at a time
|
||||
type HighestCommittedFunds struct {
|
||||
Time time.Time `json:"time"`
|
||||
Value float64 `json:"value"`
|
||||
}
|
||||
329
backtester/eventhandlers/statistics/statistics.go
Normal file
329
backtester/eventhandlers/statistics/statistics.go
Normal file
@@ -0,0 +1,329 @@
|
||||
package statistics
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/compliance"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/statistics/currencystatistics"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/fill"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/signal"
|
||||
gctcommon "github.com/thrasher-corp/gocryptotrader/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
)
|
||||
|
||||
// Reset returns the struct to defaults
|
||||
func (s *Statistic) Reset() {
|
||||
*s = Statistic{}
|
||||
}
|
||||
|
||||
// SetupEventForTime sets up the big map for to store important data at each time interval
|
||||
func (s *Statistic) SetupEventForTime(e common.DataEventHandler) error {
|
||||
if e == nil {
|
||||
return common.ErrNilEvent
|
||||
}
|
||||
ex := e.GetExchange()
|
||||
a := e.GetAssetType()
|
||||
p := e.Pair()
|
||||
s.setupMap(ex, a)
|
||||
lookup := s.ExchangeAssetPairStatistics[ex][a][p]
|
||||
if lookup == nil {
|
||||
lookup = ¤cystatistics.CurrencyStatistic{}
|
||||
}
|
||||
lookup.Events = append(lookup.Events,
|
||||
currencystatistics.EventStore{
|
||||
DataEvent: e,
|
||||
},
|
||||
)
|
||||
s.ExchangeAssetPairStatistics[ex][a][p] = lookup
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Statistic) setupMap(ex string, a asset.Item) {
|
||||
if s.ExchangeAssetPairStatistics == nil {
|
||||
s.ExchangeAssetPairStatistics = make(map[string]map[asset.Item]map[currency.Pair]*currencystatistics.CurrencyStatistic)
|
||||
}
|
||||
if s.ExchangeAssetPairStatistics[ex] == nil {
|
||||
s.ExchangeAssetPairStatistics[ex] = make(map[asset.Item]map[currency.Pair]*currencystatistics.CurrencyStatistic)
|
||||
}
|
||||
if s.ExchangeAssetPairStatistics[ex][a] == nil {
|
||||
s.ExchangeAssetPairStatistics[ex][a] = make(map[currency.Pair]*currencystatistics.CurrencyStatistic)
|
||||
}
|
||||
}
|
||||
|
||||
// SetEventForOffset sets the event for the time period in the event
|
||||
func (s *Statistic) SetEventForOffset(e common.EventHandler) error {
|
||||
if e == nil {
|
||||
return common.ErrNilEvent
|
||||
}
|
||||
if s.ExchangeAssetPairStatistics == nil {
|
||||
return errExchangeAssetPairStatsUnset
|
||||
}
|
||||
exch := e.GetExchange()
|
||||
a := e.GetAssetType()
|
||||
p := e.Pair()
|
||||
offset := e.GetOffset()
|
||||
lookup := s.ExchangeAssetPairStatistics[exch][a][p]
|
||||
if lookup == nil {
|
||||
return fmt.Errorf("%w for %v %v %v to set signal event", errCurrencyStatisticsUnset, exch, a, p)
|
||||
}
|
||||
for i := len(lookup.Events) - 1; i >= 0; i-- {
|
||||
if lookup.Events[i].DataEvent.GetOffset() == offset {
|
||||
return applyEventAtOffset(e, lookup, i)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func applyEventAtOffset(e common.EventHandler, lookup *currencystatistics.CurrencyStatistic, i int) error {
|
||||
switch t := e.(type) {
|
||||
case common.DataEventHandler:
|
||||
lookup.Events[i].DataEvent = t
|
||||
case signal.Event:
|
||||
lookup.Events[i].SignalEvent = t
|
||||
case order.Event:
|
||||
lookup.Events[i].OrderEvent = t
|
||||
case fill.Event:
|
||||
lookup.Events[i].FillEvent = t
|
||||
default:
|
||||
return fmt.Errorf("unknown event type received: %v", e)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddHoldingsForTime adds all holdings to the statistics at the time period
|
||||
func (s *Statistic) AddHoldingsForTime(h *holdings.Holding) error {
|
||||
if s.ExchangeAssetPairStatistics == nil {
|
||||
return errExchangeAssetPairStatsUnset
|
||||
}
|
||||
lookup := s.ExchangeAssetPairStatistics[h.Exchange][h.Asset][h.Pair]
|
||||
if lookup == nil {
|
||||
return fmt.Errorf("%w for %v %v %v to set holding event", errCurrencyStatisticsUnset, h.Exchange, h.Asset, h.Pair)
|
||||
}
|
||||
for i := len(lookup.Events) - 1; i >= 0; i-- {
|
||||
if lookup.Events[i].DataEvent.GetOffset() == h.Offset {
|
||||
lookup.Events[i].Holdings = *h
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddComplianceSnapshotForTime adds the compliance snapshot to the statistics at the time period
|
||||
func (s *Statistic) AddComplianceSnapshotForTime(c compliance.Snapshot, e fill.Event) error {
|
||||
if e == nil {
|
||||
return common.ErrNilEvent
|
||||
}
|
||||
if s.ExchangeAssetPairStatistics == nil {
|
||||
return errExchangeAssetPairStatsUnset
|
||||
}
|
||||
exch := e.GetExchange()
|
||||
a := e.GetAssetType()
|
||||
p := e.Pair()
|
||||
lookup := s.ExchangeAssetPairStatistics[exch][a][p]
|
||||
if lookup == nil {
|
||||
return fmt.Errorf("%w for %v %v %v to set compliance snapshot", errCurrencyStatisticsUnset, exch, a, p)
|
||||
}
|
||||
for i := len(lookup.Events) - 1; i >= 0; i-- {
|
||||
if lookup.Events[i].DataEvent.GetOffset() == e.GetOffset() {
|
||||
lookup.Events[i].Transactions = c
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CalculateAllResults calculates the statistics of all exchange asset pair holdings,
|
||||
// orders, ratios and drawdowns
|
||||
func (s *Statistic) CalculateAllResults() error {
|
||||
log.Info(log.BackTester, "calculating backtesting results")
|
||||
s.PrintAllEvents()
|
||||
currCount := 0
|
||||
var finalResults []FinalResultsHolder
|
||||
for exchangeName, exchangeMap := range s.ExchangeAssetPairStatistics {
|
||||
for assetItem, assetMap := range exchangeMap {
|
||||
for pair, stats := range assetMap {
|
||||
currCount++
|
||||
err := stats.CalculateResults()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
stats.PrintResults(exchangeName, assetItem, pair)
|
||||
last := stats.Events[len(stats.Events)-1]
|
||||
stats.FinalHoldings = last.Holdings
|
||||
stats.FinalOrders = last.Transactions
|
||||
s.AllStats = append(s.AllStats, *stats)
|
||||
|
||||
finalResults = append(finalResults, FinalResultsHolder{
|
||||
Exchange: exchangeName,
|
||||
Asset: assetItem,
|
||||
Pair: pair,
|
||||
MaxDrawdown: stats.MaxDrawdown,
|
||||
MarketMovement: stats.MarketMovement,
|
||||
StrategyMovement: stats.StrategyMovement,
|
||||
})
|
||||
s.TotalBuyOrders += stats.BuyOrders
|
||||
s.TotalSellOrders += stats.SellOrders
|
||||
if stats.ShowMissingDataWarning {
|
||||
s.WasAnyDataMissing = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
s.TotalOrders = s.TotalBuyOrders + s.TotalSellOrders
|
||||
if currCount > 1 {
|
||||
s.BiggestDrawdown = s.GetTheBiggestDrawdownAcrossCurrencies(finalResults)
|
||||
s.BestMarketMovement = s.GetBestMarketPerformer(finalResults)
|
||||
s.BestStrategyResults = s.GetBestStrategyPerformer(finalResults)
|
||||
s.PrintTotalResults()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PrintTotalResults outputs all results to the CMD
|
||||
func (s *Statistic) PrintTotalResults() {
|
||||
log.Info(log.BackTester, "------------------Strategy-----------------------------------")
|
||||
log.Infof(log.BackTester, "Strategy Name: %v", s.StrategyName)
|
||||
log.Infof(log.BackTester, "Strategy Nickname: %v", s.StrategyNickname)
|
||||
log.Infof(log.BackTester, "Strategy Goal: %v\n\n", s.StrategyGoal)
|
||||
log.Info(log.BackTester, "------------------Total Results------------------------------")
|
||||
log.Info(log.BackTester, "------------------Orders----------------------------------")
|
||||
log.Infof(log.BackTester, "Total buy orders: %v", s.TotalBuyOrders)
|
||||
log.Infof(log.BackTester, "Total sell orders: %v", s.TotalSellOrders)
|
||||
log.Infof(log.BackTester, "Total orders: %v\n\n", s.TotalOrders)
|
||||
|
||||
if s.BiggestDrawdown != nil {
|
||||
log.Info(log.BackTester, "------------------Biggest Drawdown------------------------")
|
||||
log.Infof(log.BackTester, "Exchange: %v Asset: %v Currency: %v", s.BiggestDrawdown.Exchange, s.BiggestDrawdown.Asset, s.BiggestDrawdown.Pair)
|
||||
log.Infof(log.BackTester, "Highest Price: $%.2f", s.BiggestDrawdown.MaxDrawdown.Highest.Price)
|
||||
log.Infof(log.BackTester, "Highest Price Time: %v", s.BiggestDrawdown.MaxDrawdown.Highest.Time)
|
||||
log.Infof(log.BackTester, "Lowest Price: $%v", s.BiggestDrawdown.MaxDrawdown.Lowest.Price)
|
||||
log.Infof(log.BackTester, "Lowest Price Time: %v", s.BiggestDrawdown.MaxDrawdown.Lowest.Time)
|
||||
log.Infof(log.BackTester, "Calculated Drawdown: %.2f%%", s.BiggestDrawdown.MaxDrawdown.DrawdownPercent)
|
||||
log.Infof(log.BackTester, "Difference: $%.2f", s.BiggestDrawdown.MaxDrawdown.Highest.Price-s.BiggestDrawdown.MaxDrawdown.Lowest.Price)
|
||||
log.Infof(log.BackTester, "Drawdown length: %v\n\n", s.BiggestDrawdown.MaxDrawdown.IntervalDuration)
|
||||
}
|
||||
if s.BestMarketMovement != nil && s.BestStrategyResults != nil {
|
||||
log.Info(log.BackTester, "------------------Orders----------------------------------")
|
||||
log.Infof(log.BackTester, "Best performing market movement: %v %v %v %v%%", s.BestMarketMovement.Exchange, s.BestMarketMovement.Asset, s.BestMarketMovement.Pair, s.BestMarketMovement.MarketMovement)
|
||||
log.Infof(log.BackTester, "Best performing strategy movement: %v %v %v %v%%\n\n", s.BestStrategyResults.Exchange, s.BestStrategyResults.Asset, s.BestStrategyResults.Pair, s.BestStrategyResults.StrategyMovement)
|
||||
}
|
||||
}
|
||||
|
||||
// GetBestMarketPerformer returns the best final market movement
|
||||
func (s *Statistic) GetBestMarketPerformer(results []FinalResultsHolder) *FinalResultsHolder {
|
||||
result := &FinalResultsHolder{}
|
||||
for i := range results {
|
||||
if results[i].MarketMovement > result.MarketMovement || result.MarketMovement == 0 {
|
||||
result = &results[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// GetBestStrategyPerformer returns the best performing strategy result
|
||||
func (s *Statistic) GetBestStrategyPerformer(results []FinalResultsHolder) *FinalResultsHolder {
|
||||
result := &FinalResultsHolder{}
|
||||
for i := range results {
|
||||
if results[i].StrategyMovement > result.StrategyMovement || result.StrategyMovement == 0 {
|
||||
result = &results[i]
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// GetTheBiggestDrawdownAcrossCurrencies returns the biggest drawdown across all currencies in a backtesting run
|
||||
func (s *Statistic) GetTheBiggestDrawdownAcrossCurrencies(results []FinalResultsHolder) *FinalResultsHolder {
|
||||
result := &FinalResultsHolder{}
|
||||
for i := range results {
|
||||
if results[i].MaxDrawdown.DrawdownPercent > result.MaxDrawdown.DrawdownPercent || result.MaxDrawdown.DrawdownPercent == 0 {
|
||||
result = &results[i]
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// PrintAllEvents outputs all event details in the CMD
|
||||
func (s *Statistic) PrintAllEvents() {
|
||||
log.Info(log.BackTester, "------------------Events-------------------------------------")
|
||||
var errs gctcommon.Errors
|
||||
for e, x := range s.ExchangeAssetPairStatistics {
|
||||
for a, y := range x {
|
||||
for p, c := range y {
|
||||
for i := range c.Events {
|
||||
switch {
|
||||
case c.Events[i].FillEvent != nil:
|
||||
direction := c.Events[i].FillEvent.GetDirection()
|
||||
if direction == common.CouldNotBuy ||
|
||||
direction == common.CouldNotSell ||
|
||||
direction == common.DoNothing ||
|
||||
direction == common.MissingData ||
|
||||
direction == "" {
|
||||
log.Infof(log.BackTester, "%v | Price: $%v - Direction: %v - Reason: %s",
|
||||
c.Events[i].FillEvent.GetTime().Format(gctcommon.SimpleTimeFormat),
|
||||
c.Events[i].FillEvent.GetClosePrice(),
|
||||
c.Events[i].FillEvent.GetDirection(),
|
||||
c.Events[i].FillEvent.GetReason())
|
||||
} else {
|
||||
log.Infof(log.BackTester, "%v | Price: $%v - Amount: %v - Fee: $%v - Total: $%v - Direction %v - Reason: %s",
|
||||
c.Events[i].FillEvent.GetTime().Format(gctcommon.SimpleTimeFormat),
|
||||
c.Events[i].FillEvent.GetPurchasePrice(),
|
||||
c.Events[i].FillEvent.GetAmount(),
|
||||
c.Events[i].FillEvent.GetExchangeFee(),
|
||||
c.Events[i].FillEvent.GetTotal(),
|
||||
c.Events[i].FillEvent.GetDirection(),
|
||||
c.Events[i].FillEvent.GetReason(),
|
||||
)
|
||||
}
|
||||
case c.Events[i].SignalEvent != nil:
|
||||
log.Infof(log.BackTester, "%v | Price: $%v - Reason: %v",
|
||||
c.Events[i].SignalEvent.GetTime().Format(gctcommon.SimpleTimeFormat),
|
||||
c.Events[i].SignalEvent.GetPrice(),
|
||||
c.Events[i].SignalEvent.GetReason())
|
||||
case c.Events[i].DataEvent != nil:
|
||||
log.Infof(log.BackTester, "%v | Price: $%v - Reason: %v",
|
||||
c.Events[i].DataEvent.GetTime().Format(gctcommon.SimpleTimeFormat),
|
||||
c.Events[i].DataEvent.ClosePrice(),
|
||||
c.Events[i].DataEvent.GetReason())
|
||||
default:
|
||||
errs = append(errs, fmt.Errorf("%v %v %v unexpected data received %+v", e, a, p, c.Events[i]))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
log.Info(log.BackTester, "------------------Errors-------------------------------------")
|
||||
for i := range errs {
|
||||
log.Info(log.BackTester, errs[i].Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SetStrategyName sets the name for statistical identification
|
||||
func (s *Statistic) SetStrategyName(name string) {
|
||||
s.StrategyName = name
|
||||
}
|
||||
|
||||
// Serialise outputs the Statistic struct in json
|
||||
func (s *Statistic) Serialise() (string, error) {
|
||||
resp, err := json.MarshalIndent(s, "", " ")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(resp), nil
|
||||
}
|
||||
721
backtester/eventhandlers/statistics/statistics_test.go
Normal file
721
backtester/eventhandlers/statistics/statistics_test.go
Normal file
@@ -0,0 +1,721 @@
|
||||
package statistics
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/compliance"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/statistics/currencystatistics"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/event"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/fill"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/order"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/signal"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
const testExchange = "binance"
|
||||
|
||||
func TestReset(t *testing.T) {
|
||||
s := Statistic{
|
||||
TotalOrders: 1,
|
||||
}
|
||||
s.Reset()
|
||||
if s.TotalOrders != 0 {
|
||||
t.Error("expected 0")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddDataEventForTime(t *testing.T) {
|
||||
tt := time.Now()
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
s := Statistic{}
|
||||
err := s.SetupEventForTime(nil)
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilEvent, err)
|
||||
}
|
||||
err = s.SetupEventForTime(&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
Close: 1337,
|
||||
Low: 1337,
|
||||
High: 1337,
|
||||
Volume: 1337,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if s.ExchangeAssetPairStatistics == nil {
|
||||
t.Error("expected not nil")
|
||||
}
|
||||
if len(s.ExchangeAssetPairStatistics[exch][a][p].Events) != 1 {
|
||||
t.Error("expected 1 event")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddSignalEventForTime(t *testing.T) {
|
||||
tt := time.Now()
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
s := Statistic{}
|
||||
err := s.SetEventForOffset(nil)
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilEvent, err)
|
||||
}
|
||||
err = s.SetEventForOffset(&signal.Signal{})
|
||||
if !errors.Is(err, errExchangeAssetPairStatsUnset) {
|
||||
t.Errorf("expected: %v, received %v", errExchangeAssetPairStatsUnset, err)
|
||||
}
|
||||
s.setupMap(exch, a)
|
||||
s.ExchangeAssetPairStatistics = make(map[string]map[asset.Item]map[currency.Pair]*currencystatistics.CurrencyStatistic)
|
||||
err = s.SetEventForOffset(&signal.Signal{})
|
||||
if !errors.Is(err, errCurrencyStatisticsUnset) {
|
||||
t.Errorf("expected: %v, received %v", errCurrencyStatisticsUnset, err)
|
||||
}
|
||||
|
||||
err = s.SetupEventForTime(&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
Close: 1337,
|
||||
Low: 1337,
|
||||
High: 1337,
|
||||
Volume: 1337,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = s.SetEventForOffset(&signal.Signal{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
ClosePrice: 1337,
|
||||
Direction: gctorder.Buy,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddExchangeEventForTime(t *testing.T) {
|
||||
tt := time.Now()
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
s := Statistic{}
|
||||
err := s.SetEventForOffset(nil)
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilEvent, err)
|
||||
}
|
||||
err = s.SetEventForOffset(&order.Order{})
|
||||
if !errors.Is(err, errExchangeAssetPairStatsUnset) {
|
||||
t.Errorf("expected: %v, received %v", errExchangeAssetPairStatsUnset, err)
|
||||
}
|
||||
s.setupMap(exch, a)
|
||||
s.ExchangeAssetPairStatistics = make(map[string]map[asset.Item]map[currency.Pair]*currencystatistics.CurrencyStatistic)
|
||||
err = s.SetEventForOffset(&order.Order{})
|
||||
if !errors.Is(err, errCurrencyStatisticsUnset) {
|
||||
t.Errorf("expected: %v, received %v", errCurrencyStatisticsUnset, err)
|
||||
}
|
||||
|
||||
err = s.SetupEventForTime(&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
Close: 1337,
|
||||
Low: 1337,
|
||||
High: 1337,
|
||||
Volume: 1337,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = s.SetEventForOffset(&order.Order{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
ID: "1337",
|
||||
Direction: gctorder.Buy,
|
||||
Status: gctorder.New,
|
||||
Price: 1337,
|
||||
Amount: 1337,
|
||||
OrderType: gctorder.Stop,
|
||||
Leverage: 1337,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddFillEventForTime(t *testing.T) {
|
||||
tt := time.Now()
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
s := Statistic{}
|
||||
err := s.SetEventForOffset(nil)
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilEvent, err)
|
||||
}
|
||||
err = s.SetEventForOffset(&fill.Fill{})
|
||||
if err != nil && err.Error() != "exchangeAssetPairStatistics not setup" {
|
||||
t.Error(err)
|
||||
}
|
||||
s.setupMap(exch, a)
|
||||
s.ExchangeAssetPairStatistics = make(map[string]map[asset.Item]map[currency.Pair]*currencystatistics.CurrencyStatistic)
|
||||
err = s.SetEventForOffset(&fill.Fill{})
|
||||
if !errors.Is(err, errCurrencyStatisticsUnset) {
|
||||
t.Errorf("expected: %v, received %v", errCurrencyStatisticsUnset, err)
|
||||
}
|
||||
|
||||
err = s.SetupEventForTime(&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
Close: 1337,
|
||||
Low: 1337,
|
||||
High: 1337,
|
||||
Volume: 1337,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = s.SetEventForOffset(&fill.Fill{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Direction: gctorder.Buy,
|
||||
Amount: 1337,
|
||||
ClosePrice: 1337,
|
||||
VolumeAdjustedPrice: 1337,
|
||||
PurchasePrice: 1337,
|
||||
ExchangeFee: 1337,
|
||||
Slippage: 1337,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddHoldingsForTime(t *testing.T) {
|
||||
tt := time.Now()
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
s := Statistic{}
|
||||
err := s.AddHoldingsForTime(&holdings.Holding{})
|
||||
if !errors.Is(err, errExchangeAssetPairStatsUnset) {
|
||||
t.Errorf("expected: %v, received %v", errExchangeAssetPairStatsUnset, err)
|
||||
}
|
||||
s.ExchangeAssetPairStatistics = make(map[string]map[asset.Item]map[currency.Pair]*currencystatistics.CurrencyStatistic)
|
||||
err = s.AddHoldingsForTime(&holdings.Holding{})
|
||||
if !errors.Is(err, errCurrencyStatisticsUnset) {
|
||||
t.Errorf("expected: %v, received %v", errCurrencyStatisticsUnset, err)
|
||||
}
|
||||
|
||||
err = s.SetupEventForTime(&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
Close: 1337,
|
||||
Low: 1337,
|
||||
High: 1337,
|
||||
Volume: 1337,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = s.AddHoldingsForTime(&holdings.Holding{
|
||||
Pair: p,
|
||||
Asset: a,
|
||||
Exchange: exch,
|
||||
Timestamp: tt,
|
||||
InitialFunds: 1337,
|
||||
PositionsSize: 1337,
|
||||
PositionsValue: 1337,
|
||||
SoldAmount: 1337,
|
||||
SoldValue: 1337,
|
||||
BoughtAmount: 1337,
|
||||
BoughtValue: 1337,
|
||||
RemainingFunds: 1337,
|
||||
TotalValueDifference: 1337,
|
||||
ChangeInTotalValuePercent: 1337,
|
||||
BoughtValueDifference: 1337,
|
||||
SoldValueDifference: 1337,
|
||||
PositionsValueDifference: 1337,
|
||||
TotalValue: 1337,
|
||||
TotalFees: 1337,
|
||||
TotalValueLostToVolumeSizing: 1337,
|
||||
TotalValueLostToSlippage: 1337,
|
||||
TotalValueLost: 1337,
|
||||
RiskFreeRate: 1337,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAddComplianceSnapshotForTime(t *testing.T) {
|
||||
tt := time.Now()
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
s := Statistic{}
|
||||
|
||||
err := s.AddComplianceSnapshotForTime(compliance.Snapshot{}, nil)
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilEvent, err)
|
||||
}
|
||||
err = s.AddComplianceSnapshotForTime(compliance.Snapshot{}, &fill.Fill{})
|
||||
if !errors.Is(err, errExchangeAssetPairStatsUnset) {
|
||||
t.Errorf("expected: %v, received %v", errExchangeAssetPairStatsUnset, err)
|
||||
}
|
||||
s.setupMap(exch, a)
|
||||
s.ExchangeAssetPairStatistics = make(map[string]map[asset.Item]map[currency.Pair]*currencystatistics.CurrencyStatistic)
|
||||
err = s.AddComplianceSnapshotForTime(compliance.Snapshot{}, &fill.Fill{})
|
||||
if !errors.Is(err, errCurrencyStatisticsUnset) {
|
||||
t.Errorf("expected: %v, received %v", errCurrencyStatisticsUnset, err)
|
||||
}
|
||||
|
||||
err = s.SetupEventForTime(&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
Close: 1337,
|
||||
Low: 1337,
|
||||
High: 1337,
|
||||
Volume: 1337,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = s.AddComplianceSnapshotForTime(compliance.Snapshot{
|
||||
Timestamp: tt,
|
||||
}, &fill.Fill{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSerialise(t *testing.T) {
|
||||
s := Statistic{}
|
||||
_, err := s.Serialise()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetStrategyName(t *testing.T) {
|
||||
s := Statistic{}
|
||||
s.SetStrategyName("test")
|
||||
if s.StrategyName != "test" {
|
||||
t.Error("expected test")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintTotalResults(t *testing.T) {
|
||||
s := Statistic{}
|
||||
s.BiggestDrawdown = s.GetTheBiggestDrawdownAcrossCurrencies([]FinalResultsHolder{
|
||||
{
|
||||
Exchange: "test",
|
||||
MaxDrawdown: currencystatistics.Swing{
|
||||
DrawdownPercent: 1337,
|
||||
},
|
||||
},
|
||||
})
|
||||
s.BestStrategyResults = s.GetBestStrategyPerformer([]FinalResultsHolder{
|
||||
{
|
||||
Exchange: "test",
|
||||
Asset: asset.Spot,
|
||||
Pair: currency.NewPair(currency.BTC, currency.DOGE),
|
||||
MaxDrawdown: currencystatistics.Swing{},
|
||||
MarketMovement: 1337,
|
||||
StrategyMovement: 1337,
|
||||
},
|
||||
})
|
||||
s.BestMarketMovement = s.GetBestMarketPerformer([]FinalResultsHolder{
|
||||
{
|
||||
Exchange: "test",
|
||||
MarketMovement: 1337,
|
||||
},
|
||||
})
|
||||
s.PrintTotalResults()
|
||||
}
|
||||
|
||||
func TestGetBestStrategyPerformer(t *testing.T) {
|
||||
s := Statistic{}
|
||||
resp := s.GetBestStrategyPerformer(nil)
|
||||
if resp.Exchange != "" {
|
||||
t.Error("expected unset details")
|
||||
}
|
||||
|
||||
resp = s.GetBestStrategyPerformer([]FinalResultsHolder{
|
||||
{
|
||||
Exchange: "test",
|
||||
Asset: asset.Spot,
|
||||
Pair: currency.NewPair(currency.BTC, currency.DOGE),
|
||||
MaxDrawdown: currencystatistics.Swing{},
|
||||
MarketMovement: 1337,
|
||||
StrategyMovement: 1337,
|
||||
},
|
||||
{
|
||||
Exchange: "test2",
|
||||
Asset: asset.Spot,
|
||||
Pair: currency.NewPair(currency.BTC, currency.DOGE),
|
||||
MaxDrawdown: currencystatistics.Swing{},
|
||||
MarketMovement: 1338,
|
||||
StrategyMovement: 1338,
|
||||
},
|
||||
})
|
||||
|
||||
if resp.Exchange != "test2" {
|
||||
t.Error("expected test2")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTheBiggestDrawdownAcrossCurrencies(t *testing.T) {
|
||||
s := Statistic{}
|
||||
result := s.GetTheBiggestDrawdownAcrossCurrencies(nil)
|
||||
if result.Exchange != "" {
|
||||
t.Error("expected empty")
|
||||
}
|
||||
|
||||
result = s.GetTheBiggestDrawdownAcrossCurrencies([]FinalResultsHolder{
|
||||
{
|
||||
Exchange: "test",
|
||||
MaxDrawdown: currencystatistics.Swing{
|
||||
DrawdownPercent: 1337,
|
||||
},
|
||||
},
|
||||
{
|
||||
Exchange: "test2",
|
||||
MaxDrawdown: currencystatistics.Swing{
|
||||
DrawdownPercent: 1338,
|
||||
},
|
||||
},
|
||||
})
|
||||
if result.Exchange != "test2" {
|
||||
t.Error("expected test2")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetBestMarketPerformer(t *testing.T) {
|
||||
s := Statistic{}
|
||||
result := s.GetBestMarketPerformer(nil)
|
||||
if result.Exchange != "" {
|
||||
t.Error("expected empty")
|
||||
}
|
||||
|
||||
result = s.GetBestMarketPerformer([]FinalResultsHolder{
|
||||
{
|
||||
Exchange: "test",
|
||||
MarketMovement: 1337,
|
||||
},
|
||||
{
|
||||
Exchange: "test2",
|
||||
MarketMovement: 1336,
|
||||
},
|
||||
})
|
||||
if result.Exchange != "test" {
|
||||
t.Error("expected test")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrintAllEvents(t *testing.T) {
|
||||
s := Statistic{}
|
||||
s.PrintAllEvents()
|
||||
tt := time.Now()
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
err := s.SetupEventForTime(nil)
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilEvent, err)
|
||||
}
|
||||
err = s.SetupEventForTime(&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
Close: 1337,
|
||||
Low: 1337,
|
||||
High: 1337,
|
||||
Volume: 1337,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = s.SetEventForOffset(&fill.Fill{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Direction: gctorder.Buy,
|
||||
Amount: 1337,
|
||||
ClosePrice: 1337,
|
||||
VolumeAdjustedPrice: 1337,
|
||||
PurchasePrice: 1337,
|
||||
ExchangeFee: 1337,
|
||||
Slippage: 1337,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = s.SetEventForOffset(&signal.Signal{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
ClosePrice: 1337,
|
||||
Direction: gctorder.Buy,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
s.PrintAllEvents()
|
||||
}
|
||||
|
||||
func TestCalculateTheResults(t *testing.T) {
|
||||
s := Statistic{}
|
||||
err := s.CalculateAllResults()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
tt := time.Now()
|
||||
tt2 := time.Now().Add(time.Hour)
|
||||
exch := testExchange
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
p2 := currency.NewPair(currency.DOGE, currency.DOGE)
|
||||
err = s.SetupEventForTime(nil)
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilEvent, err)
|
||||
}
|
||||
err = s.SetupEventForTime(&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
Close: 1337,
|
||||
Low: 1337,
|
||||
High: 1337,
|
||||
Volume: 1337,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = s.SetEventForOffset(&signal.Signal{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
OpenPrice: 1337,
|
||||
HighPrice: 1337,
|
||||
LowPrice: 1337,
|
||||
ClosePrice: 1337,
|
||||
Volume: 1337,
|
||||
Direction: gctorder.Buy,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = s.SetupEventForTime(&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p2,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1338,
|
||||
Close: 1338,
|
||||
Low: 1338,
|
||||
High: 1338,
|
||||
Volume: 1338,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = s.SetEventForOffset(&signal.Signal{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p2,
|
||||
AssetType: a,
|
||||
},
|
||||
OpenPrice: 1337,
|
||||
HighPrice: 1337,
|
||||
LowPrice: 1337,
|
||||
ClosePrice: 1337,
|
||||
Volume: 1337,
|
||||
Direction: gctorder.Buy,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = s.SetupEventForTime(&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt2,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1338,
|
||||
Close: 1338,
|
||||
Low: 1338,
|
||||
High: 1338,
|
||||
Volume: 1338,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = s.SetEventForOffset(&signal.Signal{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt2,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
OpenPrice: 1338,
|
||||
HighPrice: 1338,
|
||||
LowPrice: 1338,
|
||||
ClosePrice: 1338,
|
||||
Volume: 1338,
|
||||
Direction: gctorder.Buy,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
err = s.SetupEventForTime(&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt2,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p2,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1338,
|
||||
Close: 1338,
|
||||
Low: 1338,
|
||||
High: 1338,
|
||||
Volume: 1338,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
err = s.SetEventForOffset(&signal.Signal{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt2,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p2,
|
||||
AssetType: a,
|
||||
},
|
||||
OpenPrice: 1338,
|
||||
HighPrice: 1338,
|
||||
LowPrice: 1338,
|
||||
ClosePrice: 1338,
|
||||
Volume: 1338,
|
||||
Direction: gctorder.Buy,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
s.ExchangeAssetPairStatistics[exch][a][p].Events[1].Holdings.InitialFunds = 1337
|
||||
s.ExchangeAssetPairStatistics[exch][a][p].Events[1].Holdings.TotalValue = 13337
|
||||
s.ExchangeAssetPairStatistics[exch][a][p2].Events[1].Holdings.InitialFunds = 1337
|
||||
s.ExchangeAssetPairStatistics[exch][a][p2].Events[1].Holdings.TotalValue = 13337
|
||||
|
||||
err = s.CalculateAllResults()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
85
backtester/eventhandlers/statistics/statistics_types.go
Normal file
85
backtester/eventhandlers/statistics/statistics_types.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package statistics
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/compliance"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio/holdings"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/statistics/currencystatistics"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/fill"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
var (
|
||||
errExchangeAssetPairStatsUnset = errors.New("exchangeAssetPairStatistics not setup")
|
||||
errCurrencyStatisticsUnset = errors.New("no data")
|
||||
)
|
||||
|
||||
// Statistic holds all statistical information for a backtester run, from drawdowns to ratios.
|
||||
// Any currency specific information is handled in currencystatistics
|
||||
type Statistic struct {
|
||||
StrategyName string `json:"strategy-name"`
|
||||
StrategyDescription string `json:"strategy-description"`
|
||||
StrategyNickname string `json:"strategy-nickname"`
|
||||
StrategyGoal string `json:"strategy-goal"`
|
||||
ExchangeAssetPairStatistics map[string]map[asset.Item]map[currency.Pair]*currencystatistics.CurrencyStatistic `json:"-"`
|
||||
RiskFreeRate float64 `json:"risk-free-rate"`
|
||||
TotalBuyOrders int64 `json:"total-buy-orders"`
|
||||
TotalSellOrders int64 `json:"total-sell-orders"`
|
||||
TotalOrders int64 `json:"total-orders"`
|
||||
BiggestDrawdown *FinalResultsHolder `json:"biggest-drawdown,omitempty"`
|
||||
BestStrategyResults *FinalResultsHolder `json:"best-start-results,omitempty"`
|
||||
BestMarketMovement *FinalResultsHolder `json:"best-market-movement,omitempty"`
|
||||
AllStats []currencystatistics.CurrencyStatistic `json:"results"` // as ExchangeAssetPairStatistics cannot be rendered via json.Marshall, we append all result to this slice instead
|
||||
WasAnyDataMissing bool `json:"was-any-data-missing"`
|
||||
}
|
||||
|
||||
// FinalResultsHolder holds important stats about a currency's performance
|
||||
type FinalResultsHolder struct {
|
||||
Exchange string `json:"exchange"`
|
||||
Asset asset.Item `json:"asset"`
|
||||
Pair currency.Pair `json:"currency"`
|
||||
MaxDrawdown currencystatistics.Swing `json:"max-drawdown"`
|
||||
MarketMovement float64 `json:"market-movement"`
|
||||
StrategyMovement float64 `json:"strategy-movement"`
|
||||
}
|
||||
|
||||
// Handler interface details what a statistic is expected to do
|
||||
type Handler interface {
|
||||
SetStrategyName(string)
|
||||
SetupEventForTime(common.DataEventHandler) error
|
||||
SetEventForOffset(e common.EventHandler) error
|
||||
AddHoldingsForTime(*holdings.Holding) error
|
||||
AddComplianceSnapshotForTime(compliance.Snapshot, fill.Event) error
|
||||
CalculateAllResults() error
|
||||
Reset()
|
||||
Serialise() (string, error)
|
||||
}
|
||||
|
||||
// Results holds some statistics on results
|
||||
type Results struct {
|
||||
Pair string `json:"pair"`
|
||||
TotalEvents int `json:"totalEvents"`
|
||||
TotalTransactions int `json:"totalTransactions"`
|
||||
Events []ResultEvent `json:"events"`
|
||||
Transactions []ResultTransactions `json:"transactions"`
|
||||
StrategyName string `json:"strategyName"`
|
||||
}
|
||||
|
||||
// ResultTransactions stores details on a transaction
|
||||
type ResultTransactions struct {
|
||||
Time time.Time `json:"time"`
|
||||
Direction gctorder.Side `json:"direction"`
|
||||
Price float64 `json:"price"`
|
||||
Amount float64 `json:"amount"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
}
|
||||
|
||||
// ResultEvent stores the time
|
||||
type ResultEvent struct {
|
||||
Time time.Time `json:"time"`
|
||||
}
|
||||
61
backtester/eventhandlers/strategies/README.md
Normal file
61
backtester/eventhandlers/strategies/README.md
Normal file
@@ -0,0 +1,61 @@
|
||||
# GoCryptoTrader Backtester: Strategies package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/strategies)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This strategies package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Strategies package overview
|
||||
|
||||
Strategies are programmed instruction sets which act upon pricing data. After data has been loaded into the GoCryptoTrader, each tick is passed through your loaded strategy and is analysed in either the `OnSignal` function or the `OnSignals` function.
|
||||
|
||||
### Creating strategies
|
||||
The level customisation allowed in a strategy is extensive. They are required to be written in Golang.
|
||||
The strategy must adhere to the interface `strategies.Handler` by implementing the function signature `OnSignal(d data.Handler, _ portfolio.Handler) (signal.Event, error)`. The `data.Handler` allows you to access the current pricing information as well as all previous intervals. You can use this to feed any Technical Analysis package to create strategies based on market movements such as RSI (see `./strategies/rsi/rsi.go`). Strategies can also access the portfolio manager on signal(s) which allows analysis of existing holdings value, current orders and positions of other currencies in order to make complex decisions.
|
||||
When outputting the `signal.Event`, you are not dictating the price of an order, but rather signalling to the portfolio manager what ideally should occur. These options are to buy, sell or do nothing. Additional signals are to flag missing data, handled via checking `d.HasDataAtTime(d.Latest().GetTime()` to prevent any issues from occurring down the line.
|
||||
Additionally, you can utilise the `AppendWhy()` function to help understand what went into make a signalling decision when reviewing the results.
|
||||
|
||||
### What does Simultaneous Signal Processing mean?
|
||||
GoCryptoTrader Backtester config files may contain multiple `ExchangeSettings` which defined exchange, asset and currency pairs to iterate through a period of time.
|
||||
|
||||
If there are multiple entries to `ExchangeSettings` and SimultaneousProcessing is disabled, then each individual exchange, asset and currency pair candle event is evaluated individually and does not know about other exchange, asset and currency pair data events. It is a way to test a singular strategy against multiple assets simultaneously. But it isn't defined as Simultaneous Processing
|
||||
Simultaneous Signal Processing is a setting which allows multiple `ExchangeSettings` data events for a candle event to be considered simultaneously. This means that you can check if the price of BTC-USDT is 5% greater on Binance than it is on Kraken and choose to make signal a BUY event for Kraken and not Binance.
|
||||
|
||||
It allows for complex strategical decisions to be made when you consider the scope of the entire market at a given time, rather than in a vacuum when SimultaneousSignalProcessing is disabled.
|
||||
|
||||
### Loading strategies
|
||||
Each strategy has a unique name and is to be added to the function `getStrategies()` in order to be recognised.
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
45
backtester/eventhandlers/strategies/base/README.md
Normal file
45
backtester/eventhandlers/strategies/base/README.md
Normal file
@@ -0,0 +1,45 @@
|
||||
# GoCryptoTrader Backtester: Base package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/strategies/base)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This base package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Base package overview
|
||||
|
||||
The strategy base file has basic implementations of the `strategies.Handler` interface. Add any functions that can be used across all strategies here.
|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
48
backtester/eventhandlers/strategies/base/base.go
Normal file
48
backtester/eventhandlers/strategies/base/base.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/event"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/signal"
|
||||
)
|
||||
|
||||
// Strategy is base implementation of the Handler interface
|
||||
type Strategy struct {
|
||||
useSimultaneousProcessing bool
|
||||
}
|
||||
|
||||
// GetBaseData returns the non-interface version of the Handler
|
||||
func (s *Strategy) GetBaseData(d data.Handler) (signal.Signal, error) {
|
||||
if d == nil {
|
||||
return signal.Signal{}, common.ErrNilArguments
|
||||
}
|
||||
latest := d.Latest()
|
||||
if latest == nil {
|
||||
return signal.Signal{}, common.ErrNilEvent
|
||||
}
|
||||
return signal.Signal{
|
||||
Base: event.Base{
|
||||
Offset: latest.GetOffset(),
|
||||
Exchange: latest.GetExchange(),
|
||||
Time: latest.GetTime(),
|
||||
CurrencyPair: latest.Pair(),
|
||||
AssetType: latest.GetAssetType(),
|
||||
Interval: latest.GetInterval(),
|
||||
},
|
||||
ClosePrice: latest.ClosePrice(),
|
||||
HighPrice: latest.HighPrice(),
|
||||
OpenPrice: latest.OpenPrice(),
|
||||
LowPrice: latest.LowPrice(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UseSimultaneousProcessing returns whether multiple currencies can be assessed in one go
|
||||
func (s *Strategy) UseSimultaneousProcessing() bool {
|
||||
return s.useSimultaneousProcessing
|
||||
}
|
||||
|
||||
// SetSimultaneousProcessing sets whether multiple currencies can be assessed in one go
|
||||
func (s *Strategy) SetSimultaneousProcessing(b bool) {
|
||||
s.useSimultaneousProcessing = b
|
||||
}
|
||||
71
backtester/eventhandlers/strategies/base/base_test.go
Normal file
71
backtester/eventhandlers/strategies/base/base_test.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data"
|
||||
datakline "github.com/thrasher-corp/gocryptotrader/backtester/data/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/event"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
)
|
||||
|
||||
func TestGetBase(t *testing.T) {
|
||||
s := Strategy{}
|
||||
_, err := s.GetBaseData(nil)
|
||||
if !errors.Is(err, common.ErrNilArguments) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilArguments, err)
|
||||
}
|
||||
|
||||
_, err = s.GetBaseData(&datakline.DataFromKline{})
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilEvent, err)
|
||||
}
|
||||
tt := time.Now()
|
||||
exch := "binance"
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
d := data.Base{}
|
||||
d.SetStream([]common.DataEventHandler{&kline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: tt,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
Close: 1337,
|
||||
Low: 1337,
|
||||
High: 1337,
|
||||
Volume: 1337,
|
||||
}})
|
||||
|
||||
d.Next()
|
||||
_, err = s.GetBaseData(&datakline.DataFromKline{
|
||||
Item: gctkline.Item{},
|
||||
Base: d,
|
||||
Range: gctkline.IntervalRangeHolder{},
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetSimultaneousProcessing(t *testing.T) {
|
||||
s := Strategy{}
|
||||
is := s.UseSimultaneousProcessing()
|
||||
if is {
|
||||
t.Error("expected false")
|
||||
}
|
||||
s.SetSimultaneousProcessing(true)
|
||||
is = s.UseSimultaneousProcessing()
|
||||
if !is {
|
||||
t.Error("expected true")
|
||||
}
|
||||
}
|
||||
11
backtester/eventhandlers/strategies/base/base_types.go
Normal file
11
backtester/eventhandlers/strategies/base/base_types.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package base
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrCustomSettingsUnsupported = errors.New("custom settings not supported")
|
||||
ErrSimultaneousProcessingNotSupported = errors.New("does not support simultaneous processing and could not be loaded")
|
||||
ErrStrategyNotFound = errors.New("not found. Please ensure the strategy-settings field 'name' is spelled properly in your .start config")
|
||||
ErrInvalidCustomSettings = errors.New("invalid custom settings in config")
|
||||
ErrTooMuchBadData = errors.New("backtesting cannot continue as there is too much invalid data. Please review your dataset")
|
||||
)
|
||||
@@ -0,0 +1,47 @@
|
||||
# GoCryptoTrader Backtester: Dollarcostaverage package
|
||||
|
||||
<img src="/backtester/common/backtester.png?raw=true" width="350px" height="350px" hspace="70">
|
||||
|
||||
|
||||
[](https://travis-ci.org/thrasher-corp/gocryptotrader)
|
||||
[](https://github.com/thrasher-corp/gocryptotrader/blob/master/LICENSE)
|
||||
[](https://godoc.org/github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/strategies/dollarcostaverage)
|
||||
[](http://codecov.io/github/thrasher-corp/gocryptotrader?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/thrasher-corp/gocryptotrader)
|
||||
|
||||
|
||||
This dollarcostaverage package is part of the GoCryptoTrader codebase.
|
||||
|
||||
## This is still in active development
|
||||
|
||||
You can track ideas, planned features and what's in progress on this Trello board: [https://trello.com/b/ZAhMhpOy/gocryptotrader](https://trello.com/b/ZAhMhpOy/gocryptotrader).
|
||||
|
||||
Join our slack to discuss all things related to GoCryptoTrader! [GoCryptoTrader Slack](https://join.slack.com/t/gocryptotrader/shared_invite/enQtNTQ5NDAxMjA2Mjc5LTc5ZDE1ZTNiOGM3ZGMyMmY1NTAxYWZhODE0MWM5N2JlZDk1NDU0YTViYzk4NTk3OTRiMDQzNGQ1YTc4YmRlMTk)
|
||||
|
||||
## Dollarcostaverage package overview
|
||||
|
||||
The dollar cost average is a strategy which is designed to purchase on _every_ data candle. Unless data is missing, all output signals will be to buy.
|
||||
This strategy supports simultaneous signal processing, aka `config.StrategySettings.SimultaneousSignalProcessing` set to true will use the function `OnSignals(d []data.Handler, p portfolio.Handler) ([]signal.Event, error)`. This function, like the basic `OnSignal` function, will signal to buy on every iteration.
|
||||
This strategy does not support customisation
|
||||
|
||||
|
||||
### Please click GoDocs chevron above to view current GoDoc information for this package
|
||||
|
||||
## Contribution
|
||||
|
||||
Please feel free to submit any pull requests or suggest any desired features to be added.
|
||||
|
||||
When submitting a PR, please abide by our coding guidelines:
|
||||
|
||||
+ Code must adhere to the official Go [formatting](https://golang.org/doc/effective_go.html#formatting) guidelines (i.e. uses [gofmt](https://golang.org/cmd/gofmt/)).
|
||||
+ Code must be documented adhering to the official Go [commentary](https://golang.org/doc/effective_go.html#commentary) guidelines.
|
||||
+ Code must adhere to our [coding style](https://github.com/thrasher-corp/gocryptotrader/blob/master/doc/coding_style.md).
|
||||
+ Pull requests need to be based on and opened against the `master` branch.
|
||||
|
||||
## Donations
|
||||
|
||||
<img src="https://github.com/thrasher-corp/gocryptotrader/blob/master/web/src/assets/donate.png?raw=true" hspace="70">
|
||||
|
||||
If this framework helped you in any way, or you would like to support the developers working on it, please donate Bitcoin to:
|
||||
|
||||
***bc1qk0jareu4jytc0cfrhr5wgshsq8282awpavfahc***
|
||||
@@ -0,0 +1,92 @@
|
||||
package dollarcostaverage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/portfolio"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/strategies/base"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/signal"
|
||||
gctcommon "github.com/thrasher-corp/gocryptotrader/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
const (
|
||||
// Name is the strategy name
|
||||
Name = "dollarcostaverage"
|
||||
description = `Dollar-cost averaging (DCA) is an investment strategy in which an investor divides up the total amount to be invested across periodic purchases of a target asset in an effort to reduce the impact of volatility on the overall purchase. The purchases occur regardless of the asset's price and at regular intervals. In effect, this strategy removes much of the detailed work of attempting to time the market in order to make purchases of equities at the best prices.`
|
||||
)
|
||||
|
||||
// Strategy is an implementation of the Handler interface
|
||||
type Strategy struct {
|
||||
base.Strategy
|
||||
}
|
||||
|
||||
// Name returns the name
|
||||
func (s *Strategy) Name() string {
|
||||
return Name
|
||||
}
|
||||
|
||||
// Description provides a nice overview of the strategy
|
||||
// be it definition of terms or to highlight its purpose
|
||||
func (s *Strategy) Description() string {
|
||||
return description
|
||||
}
|
||||
|
||||
// OnSignal handles a data event and returns what action the strategy believes should occur
|
||||
// For dollarcostaverage, this means returning a buy signal on every event
|
||||
func (s *Strategy) OnSignal(d data.Handler, _ portfolio.Handler) (signal.Event, error) {
|
||||
if d == nil {
|
||||
return nil, common.ErrNilEvent
|
||||
}
|
||||
es, err := s.GetBaseData(d)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !d.HasDataAtTime(d.Latest().GetTime()) {
|
||||
es.SetDirection(common.MissingData)
|
||||
es.AppendReason(fmt.Sprintf("missing data at %v, cannot perform any actions", d.Latest().GetTime()))
|
||||
return &es, nil
|
||||
}
|
||||
|
||||
es.SetPrice(d.Latest().ClosePrice())
|
||||
es.SetDirection(order.Buy)
|
||||
es.AppendReason("DCA purchases on every iteration")
|
||||
return &es, nil
|
||||
}
|
||||
|
||||
// SupportsSimultaneousProcessing highlights whether the strategy can handle multiple currency calculation
|
||||
func (s *Strategy) SupportsSimultaneousProcessing() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// OnSimultaneousSignals analyses multiple data points simultaneously, allowing flexibility
|
||||
// in allowing a strategy to only place an order for X currency if Y currency's price is Z
|
||||
// For dollarcostaverage, the strategy is always "buy", so it uses the OnSignal function
|
||||
func (s *Strategy) OnSimultaneousSignals(d []data.Handler, p portfolio.Handler) ([]signal.Event, error) {
|
||||
var resp []signal.Event
|
||||
var errs gctcommon.Errors
|
||||
for i := range d {
|
||||
sigEvent, err := s.OnSignal(d[i], nil)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
} else {
|
||||
resp = append(resp, sigEvent)
|
||||
}
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
return nil, errs
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// SetCustomSettings not required for DCA
|
||||
func (s *Strategy) SetCustomSettings(_ map[string]interface{}) error {
|
||||
return base.ErrCustomSettingsUnsupported
|
||||
}
|
||||
|
||||
// SetDefaults not required for DCA
|
||||
func (s *Strategy) SetDefaults() {}
|
||||
@@ -0,0 +1,208 @@
|
||||
package dollarcostaverage
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/common"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/data/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventhandlers/strategies/base"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/event"
|
||||
eventkline "github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/kline"
|
||||
"github.com/thrasher-corp/gocryptotrader/backtester/eventtypes/signal"
|
||||
"github.com/thrasher-corp/gocryptotrader/currency"
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
||||
gctkline "github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
||||
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
|
||||
)
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
d := Strategy{}
|
||||
n := d.Name()
|
||||
if n != Name {
|
||||
t.Errorf("expected %v", Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSupportsSimultaneousProcessing(t *testing.T) {
|
||||
s := Strategy{}
|
||||
if !s.SupportsSimultaneousProcessing() {
|
||||
t.Error("expected true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetCustomSettings(t *testing.T) {
|
||||
s := Strategy{}
|
||||
err := s.SetCustomSettings(nil)
|
||||
if !errors.Is(err, base.ErrCustomSettingsUnsupported) {
|
||||
t.Errorf("expected: %v, received %v", base.ErrCustomSettingsUnsupported, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnSignal(t *testing.T) {
|
||||
s := Strategy{}
|
||||
_, err := s.OnSignal(nil, nil)
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilEvent, err)
|
||||
}
|
||||
|
||||
dStart := time.Date(2020, 1, 0, 0, 0, 0, 0, time.UTC)
|
||||
dInsert := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
dEnd := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
|
||||
exch := "binance"
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
d := data.Base{}
|
||||
d.SetStream([]common.DataEventHandler{&eventkline.Kline{
|
||||
Base: event.Base{
|
||||
Exchange: exch,
|
||||
Time: dInsert,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
Close: 1337,
|
||||
Low: 1337,
|
||||
High: 1337,
|
||||
Volume: 1337,
|
||||
}})
|
||||
d.Next()
|
||||
da := &kline.DataFromKline{
|
||||
Item: gctkline.Item{},
|
||||
Base: d,
|
||||
Range: gctkline.IntervalRangeHolder{},
|
||||
}
|
||||
var resp signal.Event
|
||||
resp, err = s.OnSignal(da, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp.GetDirection() != common.MissingData {
|
||||
t.Error("expected missing data")
|
||||
}
|
||||
|
||||
da.Item = gctkline.Item{
|
||||
Exchange: exch,
|
||||
Pair: p,
|
||||
Asset: a,
|
||||
Interval: gctkline.OneDay,
|
||||
Candles: []gctkline.Candle{
|
||||
{
|
||||
Time: dInsert,
|
||||
Open: 1337,
|
||||
High: 1337,
|
||||
Low: 1337,
|
||||
Close: 1337,
|
||||
Volume: 1337,
|
||||
},
|
||||
},
|
||||
}
|
||||
err = da.Load()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
ranger := gctkline.CalculateCandleDateRanges(dStart, dEnd, gctkline.OneDay, 100000)
|
||||
da.Range = ranger
|
||||
_ = da.Range.VerifyResultsHaveData(da.Item.Candles)
|
||||
resp, err = s.OnSignal(da, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if resp.GetDirection() != gctorder.Buy {
|
||||
t.Errorf("expected buy, received %v", resp.GetDirection())
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnSignals(t *testing.T) {
|
||||
s := Strategy{}
|
||||
_, err := s.OnSignal(nil, nil)
|
||||
if !errors.Is(err, common.ErrNilEvent) {
|
||||
t.Errorf("expected: %v, received %v", common.ErrNilEvent, err)
|
||||
}
|
||||
dStart := time.Date(2020, 1, 0, 0, 0, 0, 0, time.UTC)
|
||||
dInsert := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
dEnd := time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC)
|
||||
exch := "binance"
|
||||
a := asset.Spot
|
||||
p := currency.NewPair(currency.BTC, currency.USDT)
|
||||
d := data.Base{}
|
||||
d.SetStream([]common.DataEventHandler{&eventkline.Kline{
|
||||
Base: event.Base{
|
||||
Offset: 1,
|
||||
Exchange: exch,
|
||||
Time: dInsert,
|
||||
Interval: gctkline.OneDay,
|
||||
CurrencyPair: p,
|
||||
AssetType: a,
|
||||
},
|
||||
Open: 1337,
|
||||
Close: 1337,
|
||||
Low: 1337,
|
||||
High: 1337,
|
||||
Volume: 1337,
|
||||
}})
|
||||
d.Next()
|
||||
da := &kline.DataFromKline{
|
||||
Item: gctkline.Item{},
|
||||
Base: d,
|
||||
Range: gctkline.IntervalRangeHolder{},
|
||||
}
|
||||
var resp []signal.Event
|
||||
resp, err = s.OnSimultaneousSignals([]data.Handler{da}, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(resp) != 1 {
|
||||
t.Fatal("expected 1 response")
|
||||
}
|
||||
if resp[0].GetDirection() != common.MissingData {
|
||||
t.Error("expected missing data")
|
||||
}
|
||||
|
||||
da.Item = gctkline.Item{
|
||||
Exchange: exch,
|
||||
Pair: p,
|
||||
Asset: a,
|
||||
Interval: gctkline.OneDay,
|
||||
Candles: []gctkline.Candle{
|
||||
{
|
||||
Time: dInsert,
|
||||
Open: 1337,
|
||||
High: 1337,
|
||||
Low: 1337,
|
||||
Close: 1337,
|
||||
Volume: 1337,
|
||||
},
|
||||
},
|
||||
}
|
||||
err = da.Load()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
ranger := gctkline.CalculateCandleDateRanges(dStart, dEnd, gctkline.OneDay, 100000)
|
||||
da.Range = ranger
|
||||
_ = da.Range.VerifyResultsHaveData(da.Item.Candles)
|
||||
resp, err = s.OnSimultaneousSignals([]data.Handler{da}, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(resp) != 1 {
|
||||
t.Fatal("expected 1 response")
|
||||
}
|
||||
if resp[0].GetDirection() != gctorder.Buy {
|
||||
t.Error("expected buy")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDefaults(t *testing.T) {
|
||||
s := Strategy{}
|
||||
s.SetDefaults()
|
||||
if s != (Strategy{}) {
|
||||
t.Error("expected no changes")
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user