I can't test right now, but it may be enough to just use a toggle for the 3rd argument to bo.EnterTrade. Give it a try and see what happens.
i.e. bo.EnterTrade( bar + 1, symb, !pos.IsLong, price, 10000 ); // reverse position Mike --- In [email protected], "progster01" <progs...@...> wrote: > > > This code (at least in my testing) goes short after the first 2k of > profit, then continues to close the short positions and reopen them > (still short) with each additional 2k of profit. > > Could someone kindly provide a tweak to make the positions flip-flop > between short and long with each new 2k of profit? (My efforts must > be missing the obvious!) > > > --- In [email protected], "Tomasz Janeczko" <groups@> wrote: > > > > Hello, > > > > One of AmiBroker users brought this to my attention: > > > > http://forum.tssupport.com/viewtopic.php?t=6058 > > > > Rarely I see so much nonsense posted about AmiBroker. > > > > Below is the AFL that does what MC people were thinking is > "impossible" in AB: > > > > > > SetPositionSize( 10000, spsValue ); // dollar value of each position > > > > Buy = True; // just open long trade at very first bar of test > > Sell = Short = Cover = False; // the rest done in custom backtester > code. > > > > SetOption("MaxOpenPositions", 2 ); > > > > SetOption("UseCustomBacktestProc", True ); > > > > if( Status("action") == actionPortfolio ) > > { > > bo = GetBacktesterObject(); > > > > bo.PreProcess(); // Initialize backtester > > > > for(bar=0; bar < BarCount; bar++) > > { > > bo.ProcessTradeSignals( bar ); > > > > totalprofit = 0; > > for( pos = bo.GetFirstOpenPos(); pos; pos = bo.GetNextOpenPos () ) > > { > > totalprofit += pos.GetProfit(); > > } > > > > if( totalprofit > 2000 ) > > { > > for( pos = bo.GetFirstOpenPos(); pos; ) > > { > > price = pos.GetPrice( bar + 1, "O" ); > > symb = pos.Symbol; > > > > pos = bo.GetNextOpenPos(); > > > > bo.ExitTrade( bar + 1, symb, price ); // exit long > > bo.EnterTrade( bar + 1, symb, False, price, 10000 ); // > enter short > > } > > } > > } > > bo.PostProcess(); // Finalize backtester > > } > > > > Best regards, > > Tomasz Janeczko > > amibroker.com > > >
