Ejemplo n.º 1
0
def ExecuteTest(results, pair, data_file, strategy_path="X:\Projects\StrategyTester\ui\StrategyUI\src\Strategy\strategy.py", equity=10000.0):
    pairs = [pair] # ["GBPUSD"]  #

    #strategy = imp.load_source('strategy', strategy_path)
    equity = Decimal(equity)  # Decimal(10000.0)  #
    file_path = os.path.join(CSV_DATA_DIR,data_file)  # "GBPUSD_20140102.csv")  #
    event_queue = queue.Queue()
    market_data = HistoricCSVPriceHandler(pairs,event_queue,file_path) # LoadData, generates MARKET event on new data arrived
    strategy = None
    if strategy_path.find("TestStrategy")>=0:
        strategy = TestStrategy(pairs, event_queue) # Trading strategy - later will use Strategy patter to change during execution
    else:
        strategy = MovingAverageCrossStrategy(pairs, event_queue)
    portfolio = Portfolio(market_data, event_queue, equity=equity, backtest=True) # Stores openned positions, and will analyze trading events according to Portfolio and
                                   # current market state
    execution = SimulatedExecution()
    iters = 0
    max_iters = 100
    while iters < max_iters and market_data.continue_backtest:
        try:
            event = event_queue.get(False)
        except queue.Empty:
            market_data.stream_next_tick()
        else:
            if event is not None:
                if event.type == 'TICK':
                    strategy.calculate_signals(event)
                    portfolio.update_portfolio(event,results)
                elif event.type == 'SIGNAL':
                    portfolio.execute_signal(event)
                elif event.type == 'ORDER':
                    execution.execute_order(event)
        time.sleep(0.1)
        iters += 1
Ejemplo n.º 2
0
import os

pairs = [sys.argv[1]] # ["GBPUSD"]  #
strategy_params = {
    "short_window": 500,
    "long_window": 2000
}
equity = Decimal(sys.argv[2])  # Decimal(10000.0)  #
file_path = os.path.join(settings.CSV_DATA_DIR,sys.argv[3])  # "GBPUSD_20140102.csv")  #
event_queue = queue.Queue()
market_data = HistoricCSVPriceHandler(pairs,event_queue,file_path) # LoadData, generates MARKET event on new data arrived
strategy = MovingAverageCrossStrategy(pairs, event_queue, **strategy_params) # Trading strategy - later will use Strategy patter to change during execution
portfolio = Portfolio(market_data, event_queue, equity=equity,backtest=True) # Stores openned positions, and will analyze trading events according to Portfolio and
                               # current market state

execution = SimulatedExecution()

print("Running Backtest...")
iters = 0
max_iters = 100
while iters < max_iters and market_data.continue_backtest:
    try:
        event = event_queue.get(False)
    except queue.Empty:
        market_data.stream_next_tick()
    else:
        if event is not None:
            if event.type == 'TICK':
                strategy.calculate_signals(event)
                portfolio.update_portfolio(event)
            elif event.type == 'SIGNAL':