Hi

All seemed to be going Ok with the code below until I found that a 
Sell on the same bar as the Buy is not being actioned  and therefore 
the position remains open for ever.  In the cases of same bar extry & 
exit Sell is assigned the value of 1, but it doesn't get actioned..

Other exits seem to be fine.

Obviously, I have not got it right.  Any suggestions, please.

The following code has been simplified a little to remove clutter.

Thanks

Graham


//==================  Inital Equity & Margin  ==================
vCapital = 30000;                                               
                                                                
                                        
//==================  Initial Trade Parameters  ==================
SetOption("AccountMargin", 10);                                 
        // Margin%                                      
SetOption("ActivateStopsImmediately", True);                    // 
IntraDay Stops                          
SetOption("AllowPositionShrinking", False);                     // 
Take partial trades if equity available
SetOption("AllowSameBarExit",True);                             
        // Permit same bar exit for profit stops                
        
SetOption("CommissionMode", 0);                                 
        // 1=Percent trade size commission, 2= $ per trade      
SetOption("FuturesMode",False);                                 
                                                
SetOption("InitialEquity", vCapital );                          
                                                        
SetOption("InterestRate",0);                                    
                // Set interest rate earned for free cash, zero to 
evaluate system 
SetOption("MaxOpenPositions", 6);                               
                // Max no positions                             
                        
SetOption("MinPosValue",1);                                     
                        // Min position value to make trade worthwhile
SetOption("MinShares", 1);                                      
                        // Min No shares zero
SetOption("PriceBoundChecking", True );                         
        // Price to stay in bar range                           
SetOption("UseCustomBacktestProc", True);                       
        // Use Custom backtest for reporting
SetOption("UsePrevBarEquityForPosSizing", True );       // Use last 
known bar of position sizing                                    
                

SetTradeDelays(0,0,0,0);                                        
                        // Intraday Entry & Exit signals, no forward 
references

vaValidEntry = Cross( Close, EMA( Close, 45 ) );

//==================  Limit Entry  ==================
vaLimitEntry = Close + (0.8 * (High - Low));

//==================  Stop Exit  ==================
vISATRPer = 30; //15;
vISMult = 6;
vaIStopVal = ATR(vISATRPer) * vISMult;

//==================  Target Exit  ==================
vaTargetVal = Ref(H, -3);

//==================  Position Size  ==================
vMaxPosn = 10000;
vMaxRisk = 2000;
vMaxTOPc = 3;
vaPosnSize = vMaxRisk / Ref(vaIStopVal, -1) * Ref(vaLimitEntry, -1);
vaPosnSize = Min(vaPosnSize, vMaxPosn);
SetPositionSize(vaPosnSize, spsValue);

//==================  Trade  ==================
Sell = 0;
Buy = Ref(vaValidEntry,-1) AND Low <= Ref(vaLimitEntry, -1);
BuyPrice = Min(Open, Ref(vaLimitEntry, -1));
Sell = 0;

vaGapDown = GapDown();
vaGapUp = GapUp();
vaInitStop = Null;
vaSellStop =  0;
vaSellTarget = 0;
VEntryBar = 0;
vTempStop = 0;
vTempTarget = 0;
vTradeLength = 8;
vTrigger = 0;

for(vCnt = 1; vCnt < BarCount; vCnt++)
{
        if(vTempStop == 0
                AND Buy[vCnt] == 1)     // Entry Bar & 0 positions 
open
        {
                // Position Opened              
                vTempStop = BuyPrice[vCnt] - vaIStopVal[vCnt - 1];
                vTempTarget = High[vCnt - 3];
                vEntryBar = vCnt;
        }
        else
        {
                Buy[vCnt] = 0;  // Remove excess Buy signals
        }       // Not Entry Bar

        if(vTempStop == 0)
        {
                vaTargetVal[vCnt - 1] = Null;
        }
        else
        {
                if (vCnt - vEntryBar == vTradeLength + 1)
                {
                        vTempStop = Max(vTempStop, BuyPrice
[vEntryBar]);
                }       // test # Bars Open

                if (vCnt > vEntryBar + 1)       // eliminate Entry Bar
                {
                        if (vaGapDown[vCnt - 1] == 1)
                        {
                                vTempStop = Max(vTempStop, Low[vCnt - 
1]);
                        }       // GapDown

                        if (vaGapUp[vCnt - 1] == 1)
                        {
                                vTempStop = Max(vTempStop, High[vCnt -
 1]);
                        }       // GapUp
                }       // Position Open & not Entry Bar

                vaInitStop[vCnt - 1] = vTempStop;               
                if(Low[vCnt] <= vaInitStop[vCnt - 1])
                {
                        Sell[vCnt] = 1;
                        SellPrice[vCnt] = Min(Open[vCnt], vaInitStop
[vCnt - 1]);
                        vTempStop = 0;
                }       
                else
                {
                        if(High[vCnt] >= vaTargetVal[vCnt - 1])
                        {
                                Sell[vCnt] = 1;
                                SellPrice[vCnt] = Max(Open[vCnt], 
vaTargetVal[vCnt - 1]);
                                vTempStop = 0;
_TRACE("Bar " + BarIndex() + " Buy " + Buy + " Sell " + Sell + " 
Target " + vaTargetVal + " Stop " + vTempStop);
                        }
                }       // exits
        }       // Position Open
}       // for

// Remove excess signals                                        
                        
Buy = ExRem(Buy, Sell);                                         
                //  Remove additional signals, 
Sell = ExRem(Sell, Buy);

Reply via email to