I am having issues with my code not limiting the number of trades in the short direction even though it appears to be working in the long direction.
My original approach was using the BuysToday and ShortsToday counter which was working but then I noticed it was taking more than one trade in the short direction. I then started trying to debug it and tried the TradeCounter approach but that isn't working either. I am wondering if there is a problem I am just missing to see in the code.
Here is an image showing how it is taking more than one short position after the previous one stopped out. Is it registering that it is in fact flat because it stopped out therefore it is flat?
If ( MP <> 0 ) and (MP[1] <> MP ) then TradeCounter = TradeCounter+ 1; //Checks to see change in state
if Marketposition=1 then BuysToday=1; //Increments to 1 if there is an open trade
if Marketposition=-1 then ShortsToday=1; //Increments to 1 if there is an open trade
if time >=630 and time <=640 then
Begin
RangeHigh=Highest(High,2); //Defines high of range
RangeLow=Lowest(Low,2); //Defines low of the range
BuysToday=0; ShortsToday=0; TradeCounter=0; //Resets trade counter to zero on the open
End;
If time>=timestart and time <timeend then
Begin
If TradeCounter<MaxTrades and close<RangeLow then Sell Short 1 contracts next bar at market;
If BuysToday=0 and close>RangeHigh then Buy 1 contracts next bar at market;
It looks like all your short trades (except last one) entered and exited on the same bar. So the MarketPosition keyword would never get updated - things only get updated at end of the bar.
I tried adjusting your code, I did not verify or test this, but I did the buy and sell trade counter differently. Maybe this will work better for you.
If it doesn't work, give me some exact chart details of where it is a problem, and I can debug easier...
The following user says Thank You to kevinkdog for this post:
Do you know if the TotalTrades function counts the total number of trades for all the data you have loaded or just the total number of trades on the current day?
The EL reference PDF doesn't specify.
TotalTrades
Otherwise it does seem like the markeposition not calculating until the end of the bar was issue.