I receive the error "The name 'lastTrade' does not exist in the current context" although I'm using it in the 'lastProfit' code. Any ideas?
I expect it to be the last filled quantity, not the intended lot size that I'm trying to fill. Ultimately I'm trying to monitor consecutive losses and skip trades, not stop the strategy as 'SampleTradeObject2' does. I'm not capable of modifying the SampleTradeObject2 to make it into a strategy that continues rather than stops so any help there would be appreciated also.
protected override void Initialize()
{
SetProfitTarget("LongEntry", CalculationMode.Ticks, Target);
SetProfitTarget("ShortEntry", CalculationMode.Ticks, Target);
SetStopLoss("LongEntry", CalculationMode.Ticks, Stop, false);
SetStopLoss("ShortEntry", CalculationMode.Ticks, Stop, false);
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
double lastProfit = 0;
double lastProfit1 = 0;
double lastProfit2 = 0;
double lastProfit3 = 0;
double lastProfit4 = 0;
double priorTradesCount = 0;
double priorTradesCumProfit = 0;
bool SkipTrade = false;
if (CurrentBar < 250)
{
return;
}
// At the start of a new session
if (Bars.FirstBarOfSession)
{
// Store the strategy's prior cumulated realized profit and number of trades
priorTradesCount = Performance.RealtimeTrades.Count;
priorTradesCumProfit = Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit;
/* NOTE: Using .AllTrades will include both historical virtual trades as well as real-time trades.
If you want to only count profits from real-time trades please use .RealtimeTrades. */
}
/* Prevents further trading if the current session's realized profit exceeds $1000 or if realized losses exceed $400. */
if (Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit - priorTradesCumProfit >= 10000
|| Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit - priorTradesCumProfit <= -7000)
{
/* TIP FOR EXPERIENCED CODERS: This only prevents trade logic in the context of the OnBarUpdate() method. If you are utilizing
other methods like OnOrderUpdate() or OnMarketData() you will need to insert this code segment there as well. */
// Returns out of the OnBarUpdate() method. This prevents any further evaluation of trade logic in the OnBarUpdate() method.
return;
}
if (Performance.RealtimeTrades.Count >= 1)
{
Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity;
}
if (Performance.RealtimeTrades.Count >= 2)
{
Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity;
Trade lastTrade1 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 2];
lastProfit1 = lastTrade1.ProfitCurrency*lastTrade1.Quantity;
}
if (Performance.RealtimeTrades.Count >= 3)
{
Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity;
Trade lastTrade1 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 2];
lastProfit1 = lastTrade1.ProfitCurrency*lastTrade1.Quantity;
Trade lastTrade2 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 3];
lastProfit2 = lastTrade2.ProfitCurrency*lastTrade2.Quantity;
}
if (Performance.RealtimeTrades.Count >= 4)
{
Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity;
Trade lastTrade1 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 2];
lastProfit1 = lastTrade1.ProfitCurrency*lastTrade1.Quantity;
Trade lastTrade2 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 3];
lastProfit2 = lastTrade2.ProfitCurrency*lastTrade2.Quantity;
Trade lastTrade3 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 4];
lastProfit3 = lastTrade3.ProfitCurrency*lastTrade3.Quantity;
}
// if (Performance.RealtimeTrades.Count >= 5)
//
// {
// Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
// lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity;
// Trade lastTrade1 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 2];
// lastProfit1 = lastTrade1.ProfitCurrency*lastTrade1.Quantity;
// Trade lastTrade2 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 3];
// lastProfit2 = lastTrade2.ProfitCurrency*lastTrade2.Quantity;
// Trade lastTrade3 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 4];
// lastProfit3 = lastTrade3.ProfitCurrency*lastTrade3.Quantity;
// Trade lastTrade4 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 5];
// lastProfit4 = lastTrade4.ProfitCurrency*lastTrade4.Quantity;
// }
//Hault Trading after three loosers in a row. lastProfit1 & 2 are the 2 contract order. lastProfit 3 is one contract of the Qty 6//
if (lastProfit < 0
&& lastProfit1 < 0
&& lastProfit2 < 0
&& lastProfit3 <0
// && lastProfit4 < 0
)
{
SkipTrade = true;
}
Print("The last trade profit is " + lastProfit);
//Print ("LastTrade Quantity is" + lastTrade.Quantity);
Print ("LastProfit1 is" + lastProfit1);
Print ("LastProfit2 is" + lastProfit2);
Print ("LastProfit3 is" = lastProfit3);

Comment