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.
Marin Draganov
I found what I had done wrong. The handling of #dict_open_pr is wrong: The first time price is available for a pair it should add it and after that it should change the opening price for that pair.
// opening price dictionary for every pair foreach(var pair in pairs) { if( qbars.Time.DayOfWeek == DayOfWeek.Monday && qbars.Time.Hour == start_hour && qbars.Time.Minute == 0 ) { if (dict_open_pr.ContainsKey(pair)) { dict_open_pr[pair] = qbars[pair].Ask.Close; } else { // price for long position dict_open_pr.Add(pair, qbars[pair].Ask.Close); } } }
Â
Marin Draganov
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!