Two more conditions are
01 takes only one order per trend, ie one long order when the trend changes from bearish to bullish each time.
So to make that condition work i came up with a mechanism:
A variable that checks whether the order is placed or not "
A condition that checks if the trend is bearish: and if the trend is bearish then it resets the variable to false so that the strategy can place the order again.
if ((Low[0] < Low[1])
&& (Low[1] < Low[2]))
{
OrderPlaced = false;
}
Look i place limit orders and each time price moves up the limit order will also move up, please check set 2, 3, 4, and 5 for that. If yo see the orderplaced flag is set to true right after the EnterLongLimit, ao when a new high is detected and the strategy replaces the previous limit order by a new higher limit order, it finds the orderplaced flag true and it cancels the order.
if ((Close[0] > Close[1])
&& (Close[1] > Close[2])
&& (LimitPrice > 0)
&& (OrderPlaced == false))
{
EnterLongLimit(2, LimitPrice, "");
OrderPlaced = true;
}
if (Position.MarketPosition == MarketPosition.Long)
{
OrderPlaced = true;
}
So what i want to achieve is to set the order place flag to true once the order is filled not after the limit order is placed. Please help me!
Below is the complete script of the strategy builder.
{
public class CASUNoRep : Strategy
{
private double LimitPrice;
private bool OrderPlaced;
private bool IsBearish;
private Series<double> CustomHighSeries;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Enter Long After Price Drops";
Name = "Chart116UpTrendNoRep";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 20;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
LimitPrice = 0;
OrderPlaced = false;
IsBearish = false;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
CustomHighSeries = new Series<double>(this);
SetProfitTarget(CalculationMode.Ticks, 20);
SetStopLoss(CalculationMode.Ticks, 60);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 2)
return;
// Set 1
if ((Low[0] < Low[1])
&& (Low[1] < Low[2]))
{
OrderPlaced = false;
}
// Set 2
if (CurrentBars[0] > 0)
{
CustomHighSeries[0] = CustomHighSeries[1];
}
// Set 3
if (CurrentBars[0] == 1)
{
CustomHighSeries[0] = High[0];
}
// Set 4
if ((Close[0] > Close[1])
&& (Close[1] > Close[2]))
{
CustomHighSeries[0] = High[0];
LimitPrice = (CustomHighSeries[0] + (-40 * TickSize)) ;
}
// Set 5
if ((Close[0] > Close[1])
&& (Close[1] > Close[2])
&& (LimitPrice > 0)
&& (OrderPlaced == false))
{
EnterLongLimit(2, LimitPrice, "");
OrderPlaced = true;
}
}
}
}

Comment