Close Positions when Price closes above SMA
Close Positions when Price closes above SMA
03 Jun 2013, 10:11
Hi,
Can you please give me the code to close all open positions when the price (marketseries.close) closes above a simple moving average.
My code compiles but during the backtests it simply doesn't do what it should,
(I have a 100 pip stop loss and it should not get hit because the SMA is supposed to close the open positions as a condition)
This is the code I have tried:
private void OpenPosition()
{
foreach (var position in Account.Positions)
{
if (MA1.Result.LastValue < MarketSeries.Close.LastValue)
{
ClosePosition();
}
}
}
I also tried this:
private void OpenPosition()
{
foreach (var position in Account.Positions)
{
if (MarketSeries.Close.HasCrossedAbove(HG.Result.LastValue))
{
ClosePosition();
}
}
}
I also tried putting the code in the OnTick() and onBar() method, to no avail.
Any help/code would be greatly appreciated thanks.
Replies
Delta_Gamma
03 Jun 2013, 19:55
( Updated at: 21 Dec 2023, 09:20 )
I would appreciate some feedback from the admin.
The attached pic explains what I need the program to do for clarity:
basically, (referring to the pic) I wanted that trade to close as soon as the price closed under the green line (which is a customised simple moving average indicator).
How would I code this?
Thanks
@Delta_Gamma
cAlgo_Fanatic
04 Jun 2013, 09:30
You may want to use the previous to the last value since the last value changes on each tick. Also, the code should probably go in the OnBar or OnTick event.
@cAlgo_Fanatic
Delta_Gamma
04 Jun 2013, 12:38
( Updated at: 21 Dec 2023, 09:20 )
Hi Admin,
Thanks for the response. By previous to last value do you mean I make it LastValue-1 like this?
I tried this in a backtest and it didn't seem to close the trade. (attached pic)
Do you have any recommendations on what I can do to achieve this? I just need the trade to close when it either touches or closes above the SMA.
The attached pic has the area circled where I need the trade to close.
Thanks again.
protected override void OnTick()
{
Buy();
foreach (var position in Account.Positions)
if (MarketSeries.Close.HasCrossedAbove(MA1.Result.LastValue-1,0))
{
ClosePosition();
}
@Delta_Gamma
cAlgo_Fanatic
04 Jun 2013, 14:41
The functions HasCrossedAbove/HasCrossedBelow use a period parameter to determine whether one series has crossed above/below the other over a given period up to the last value. You may use these functions with a period greater than zero (period = 0 will only compare the last value, period = 1 will compare the last and previous to last values, etc. ) or just compare the previous to the last values of your series, depending on your desired solution.
To use the previous to the last determine the last index and apply it to the series:
var lastIndex = MarketSeries.Close.Count - 1; // last index var valueMa = MA1.Result[lastIndex - 1]; // previous to last MA1 value var valueClose = MarketSeries.Close[lastIndex - 1]; // previous to last Close value // compare the two values if(valueClose >= valueMa) { //... do something }
@cAlgo_Fanatic
Delta_Gamma
04 Jun 2013, 17:22
Hi,
Thanks very much for the help. It's working great now.
I just have to do some refinements and it should ready.
Thanks again.
@Delta_Gamma
Delta_Gamma
03 Jun 2013, 10:13
Hi just noticed an error, I copied and pasted the wrong code, it should be MA1 not HG:
private void OpenPosition()
{
foreach (var position in Account.Positions)
{
if (MarketSeries.Close.HasCrossedAbove(MA1.Result.LastValue))
{
ClosePosition();
}
}
}
@Delta_Gamma