The Code entry with a crossing of two SMA , with a stoploss of 4% becomes 2% if the volatility is low.
With the 1° target , sell (1 / 2 ) position and the remaining managed TrailingStop (based on a channel MAX / MIN 0.25 * ATR).
The Code with F5 is OK, but when i test in 10 years, you see only n.1 trade for stock.
Risk management, such as writing the code is not implemented.
The Code:
#region Variables
private bool be1 = false;
private bool be2 = false;
#endregion
#region Positions
private void GoLong()
{
SetStopLoss("StopLoss 4% LongBuy",CalculationMode.Percent,0.04,false);
EnterLong(500, "LongBuy");
}
private void GoShort()
{
SetStopLoss("StopLoss 4% ShortSell",CalculationMode.Percent,0.04,false);
EnterShort(500, "ShortSell");
}
#endregion
#region OrderRouting
private void ManagerOrder ()
{
if (Position.MarketPosition == MarketPosition.Long)
{
if (Be1 && Close[0] > Position.AvgPrice && ATR(14)[0] > EMA(ATR(14), 14)[0])// High Volatility
SetStopLoss("StopLoss 4% LongBuy",CalculationMode.Percent,0.04,false);
int pippo = 1;
if( Be2 && Close[0] > Position.AvgPrice && ATR(14)[0] < EMA(ATR(14), 14)[0])// Low Volatility
SetStopLoss("StopLoss 2% LongBuy",CalculationMode.Percent,0.02,false);
int pluto = 1;
if( pippo == 1 && High[0] >= Position.AvgPrice + ATR (14)[0] && ATR(14)[0] > EMA(ATR(14), 14)[0] )// High Volatility
ExitLong(250, "Exit Long 1/2 Position", "LongBuy High Volatility");
SetStopLoss("StopLoss High Volatility",CalculationMode.Price,Position.AvgPric e ,false);
SetTrailStop("StopTrailing High Volatility",CalculationMode.Price,MIN(Low, 10)[1] - (ATR(14)[0] * 0.35),false);
if( pluto == 1 && High[0] >= Position.AvgPrice + ATR (14)[0] && ATR(14)[0] < EMA(ATR(14), 14)[0]) // Low Volatility
ExitLong(250, "Exit Long 1/2 Position", "LongBuy Low Volatility");
SetStopLoss("StopLoss Low Volatility",CalculationMode.Price,Position.AvgPric e ,false);
SetTrailStop("StopTrailing Low Volatility",CalculationMode.Price,MIN(Low, 10)[1] - (ATR(14)[0] * 0.25),false);
}
if (Position.MarketPosition == MarketPosition.Short).......................
protected override void OnBarUpdate()
{
ManagerOrder ();
if (Position.MarketPosition != MarketPosition.Flat) return;
if (CrossAbove(SMA(14),SMA(20),1)
{
GoLong();
}
if(CrossBelow(SMA(14),SMA(20),1)
{
GoShort();
}
why not enter the stop? What is wrong?
Ciao.
Italy
Comment