Hello,

I would like to test a simple breakout strategy for multiple Forex pairs. The trading setup is like this:

If a certain Forex pair makes a upward move of {say} 36 basis points from the open of the week, is it going to be profitable to open a long position in that pair and close that position at the end of the week? For the same move of 36 b.p. positions should be opened for the other pairs if that move is made by the end of first trading day of the week {Monday}.

So here is a chunk of the code I have written so far:

using System;

namespace QuantConnect
{
public partial class WeekTrading : QCAlgorithm
{
string [] pairs = new string []
{
"AUDJPY",
"AUDUSD",
"EURAUD",
"EURJPY",
"EURNZD",
"GBPJPY",
"NZDCHF",
"NZDJPY",
"NZDUSD"
};

// GMT/UTC start hour on Monday
int start_hour = 6;

// GMT/UTC end hour on Monday
int end_hour = 20;
decimal bank_perc = 0.48m;
decimal stop_size = 0.12m;

// opening prices dictionary for every pair
Dictionary<string, decimal> dict_open_pr = new Dictionary<string, decimal>();

// percentage of the initial move from the start of the day
decimal move_perc = 0.36m;

public override void Initialize()
{
SetStartDate(2007, 1, 1);
SetEndDate(2019, 12, 31);
SetCash(10000);

SetBrokerageModel(BrokerageName.OandaBrokerage, AccountType.Margin);

foreach (var pair in pairs)
{
AddForex(pair, Resolution.Minute, Market.Oanda, true, 500);

Debug("Added as source " + pair);
}
}

public override void OnData(Slice data)
{
// check if data for all pairs are present in #data
foreach(var pair in pairs)
{
if(!data.ContainsKey(pair))
return;
}

Debug("I checked that those pairs are present in #data");

// opening price dictionary for every pair
foreach(var pair in pairs)
{
if(
data.Time.DayOfWeek == DayOfWeek.Monday &&
data.Time.Hour == start_hour &&
data.Time.Minute == 0
)
{
// price for long position
dict_open_pr.Add(pair, data[pair].Ask.Close);
}
}

// some code for opening positions

// some code for closing all positions at the end of the week
}
}
}

That code doesn't seem to work and I an getting the following error:

"Runtime Error: The given key 'AUDJPY' was not present in the dictionary. (Open Stacktrace)"

It adds all the pairs and prints correctly "Debug("Added as source " + pair)" but can't get to the second Debug("I checked that those pairs are present in #data");

Obviously I am doing something completely wrong. Could you kindly give me a hand on this one?

EDIT: Typos.

Author