View Full Version : SetStopLoss() and SetProfitTarget()
SamIam
09-01-2007, 12:28 AM
My question is related to Forex trading. Which fomat is best to use with currency trading to set stop loss and profit target? Would it be CalculationMode.Price or CalculationMode.Ticks
I have used following code but can't seem to make it work. My strategy does not execute either stop loss or profit target orders.
SetStopLoss("Short", CalculationMode.Ticks, 50, true);
SetProfitTarget("Short",CalculationMode.Ticks, 100);
I have tried CalculationMode.Price also but that did not help either.
I am placing these stoploss and profittarget conditions in the Initialize() section.
Thanks,
Sami
NinjaTrader_Ray
09-01-2007, 09:41 AM
Either one is fine.
Please remove the reference to "Short".
SetProfitTarget(CalculationMode.Ticks, 50);
I suspect that you do not have an entry signal with the name "Short" and this is why not stops/targets are submitted.
SamIam
09-01-2007, 11:37 AM
Actually I do have a entershort order by the name of "short" but the setstoploss and setprofittarget still doesn't work.
NinjaTrader_Ray
09-01-2007, 01:01 PM
It may be case sensitive, can you try "Short" for both? Right now you have "Short" and "short".
Thanks.
SamIam
09-01-2007, 01:37 PM
It is "Short" and "Short" in both place. It was a typo in my last post. Any other suggestions?
NinjaTrader_Ray
09-01-2007, 04:03 PM
As a last resort, take the attached script which demonstrates the functionality discussed in this thread and modify it to make it your own.
SamIam
09-01-2007, 10:28 PM
Ray,
Thank you for the code. I think I have isolated the problem. The strategy seems to work when I apply it to the chart but it does not reflect the same results in the backtesting mode. On the chart I have multiple position opened and stoped (loss or profit) but when I backtest the same currency pair I am only getting one order which open at the begining of the testing period and closes at the end. Why the code works on the chart but not in the backtesting mode?
Sami
SamIam
09-02-2007, 01:06 AM
How can I set a dynamic stoploss? One that re-calculate with each bar.
I have used the following code but not getting any order, and not even the strategy seems to be executing at all.
protected override void Initialize()
{
CalculateOnBarClose = true;
ExitOnClose = false;
ExcludeWeekend = false;
double stopPrice=ATR(45)[1];
SetStopLoss("Long",CalculationMode.Price,Close[0]-stopPrice,false);
}
Need help.
Sami
NinjaTrader_Ray
09-02-2007, 08:59 AM
Hi Sami,
You will have to debug your code. The reference sample I provided works as expected in both real-time and backtest. What I do when I run into walls is strip things down to a core working state and then layer my logic on step by step until I reach a point where it does not work as expected. Then I know what code is causing the problem. I use Print() statements to print out key data at at different points in the script.
In your last post, I suspect accessing value in ATR() causes an exception. You will see this in the Log tab of the Control Center window. I believe you can not access indicator values in Initialize().
To dynamically change a stop loss, you can call the SetStopLoss() method in OnBarUpdate() and pass in a new price. You must remember to reset the SetStopLoss() values in OnBarUpdate() when you are flat so that on opening a position, they are set to some default "ticks away" value.
SamIam
09-05-2007, 08:49 PM
Ray,
I followed you suggestion of layering the logic step by step and got stuck when I added the SetStopLoss statement. The work around I found was to use the ExitLongStop and ExitShortStop instead of SetStopLoss and it seems to have worked.
Thank you for your help,
Sami