Modified the Position handling to use long/short instead of buy/sell for side/position_type. Also modified the unit tests for both Portfolio and Position to reflect these changes. Added a basic historical backtesting capability via backtest.py and using CSV tick data for currency pairs.

This commit is contained in:
Michael Halls-Moore
2015-04-17 12:34:31 +01:00
parent d9a7444fc2
commit e74777802b
12 changed files with 519 additions and 192 deletions
+13 -9
View File
@@ -4,14 +4,14 @@ import threading
import time
from decimal import Decimal, getcontext
from qsforex.execution.execution import Execution
from qsforex.execution.execution import OANDAExecutionHandler
from qsforex.portfolio.portfolio import Portfolio
from qsforex.settings import STREAM_DOMAIN, API_DOMAIN, ACCESS_TOKEN, ACCOUNT_ID
from qsforex import settings
from qsforex.strategy.strategy import TestStrategy
from qsforex.streaming.streaming import StreamingForexPrices
def trade(events, strategy, portfolio, execution):
def trade(events, strategy, portfolio, execution, heartbeat):
"""
Carries out an infinite while loop that polls the
events queue and directs each event to either the
@@ -39,9 +39,9 @@ if __name__ == "__main__":
# Set the number of decimal places to 2
getcontext().prec = 2
heartbeat = 0.5 # Half a second between polling
heartbeat = 0.0 # Half a second between polling
events = Queue.Queue()
equity = Decimal("99949.82")
equity = settings.EQUITY
# Trade "Cable"
instrument = "GBP_USD"
@@ -49,8 +49,8 @@ if __name__ == "__main__":
# Create the OANDA market price streaming class
# making sure to provide authentication commands
prices = StreamingForexPrices(
STREAM_DOMAIN, ACCESS_TOKEN, ACCOUNT_ID,
instrument, events
settings.STREAM_DOMAIN, settings.ACCESS_TOKEN,
settings.ACCOUNT_ID, instrument, events
)
# Create the strategy/signal generator, passing the
@@ -64,13 +64,17 @@ if __name__ == "__main__":
# Create the execution handler making sure to
# provide authentication commands
execution = Execution(API_DOMAIN, ACCESS_TOKEN, ACCOUNT_ID)
execution = OANDAExecutionHandler(
settings.API_DOMAIN,
settings.ACCESS_TOKEN,
settings.ACCOUNT_ID
)
# Create two separate threads: One for the trading loop
# and another for the market price streaming class
trade_thread = threading.Thread(
target=trade, args=(
events, strategy, portfolio, execution
events, strategy, portfolio, execution, heartbeat
)
)
price_thread = threading.Thread(target=prices.stream_to_queue, args=[])