Here is my code (granted, this is a basic template of the code, which is why it wont make sense logically but don't worry about that). I'm looking to find a way to make the algorithm recognize to stop placing orders a billion times.
namespace QuantConnect
{
/*
* OANDA/QuantConnect Basic Template:
* Fundamentals to using a QuantConnect algorithm.
*
* You can view the QCAlgorithm base class on Github:
* https://github.com/QuantConnect/Lean/tree/master/Algorithm
*/
public class BasicTemplateAlgorithm : QCAlgorithm
{
//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{
// Date range for your backtest
// In live trading these are ignored.
SetStartDate(2016, 1, 1);
SetEndDate(2016, 1, 7);
// Set cash allocation for backtest
// In live trading this is ignored and your real account is used.
SetCash(50000);
// Specify the OANDA Brokerage: This gives lets us know the fee models & data.
SetBrokerageModel(BrokerageName.OandaBrokerage);
//Add as many securities as you like. All the data will be passed into the event handler:
AddSecurity(SecurityType.Forex, "EURUSD", Resolution.Second, true);
}
// Event handler for the price events
public override void OnData(Slice data)
{
int holdings = 0;
if (!data.ContainsKey("EURUSD")) return;
var spread2 = (data["EURUSD"].Ask.Close-data["EURUSD"].Bid.Close)*10000;
var unitval2 = Portfolio.Cash*2;
unitval2 = Decimal.ToInt32(unitval2);
if (Portfolio.HoldStock == false && spread2<2 && holdings == 0)
{
holdings = 1;
Random rand = new Random();
int random = rand.Next(0,101);
if(random<50)
{
Sell("EURUSD", unitval2);
}
else if(random>=50)
{
Buy("EURUSD", unitval2);
}
}
while (holdings == 1)
{
if(Portfolio.TotalProfit>=Portfolio.Cash*(2/1000))
{
Liquidate("EURUSD");
holdings = 0;
}
else if(Portfolio.TotalProfit<=(44/10)*Portfolio.Cash*(2/1000))
{
Liquidate("EURUSD");
holdings = 0;
}
else return;
}
}
}
}