- Joined
- 5 June 2013
- Posts
- 1
- Reactions
- 0
I would be grateful for help in finding the flaw with the following useless (but educational) piece of code. The code uses REF, so there might be a self-referencing problem.
Procedure:
- Remember the price on the first bar.
- Buy when C > price on first bar.
- Remember NewBuyPrice (the purpose of this code is to remember previous info).
- Buy when C > NewBuyPrice.
- Loop round the last 3 steps.
In the debug window, the first five lines are always correct.
The sixth line always says "Buy = 1"
The remaining lines would be correct if Buy really was "1"
//-----------------------------------------------------
Buy = 1 ; // force a buy on first bar
BuyPrice = C ;
EntryPrice = 0 ;
FirstBar = IIf (BarIndex()==0,1,0 ) ;
NotFirstBar = IIf( (C > Ref(EntryPrice, -1)), 1, 0 ) ;
Buy = IIf (FirstBar, 1, NotFirstBar) ;
EntryPrice = ValueWhen( Buy, BuyPrice, 1 );
Plot( C,"C",colorBlue,styleLine );
PlotShapes( Buy * shapeUpArrow, colorGreen );
"C = " + C ;
"FirstBar = " + FirstBar ;
"Ref(EntryPrice, -1) = " + Ref(EntryPrice, -1) ;
"C > Ref(EntryPrice, -1) = " + (C > Ref(EntryPrice, -1)) ;
"NotFirstBar = " + NotFirstBar ;
"Buy = " + Buy ;
"BuyPrice = " + BuyPrice ;
"ValueWhen(Buy, Buyprice, 1) = " + ValueWhen(Buy, BuyPrice, 1) ;
"EntryPrice = " + EntryPrice ;
//----------------------------------------------------
Procedure:
- Remember the price on the first bar.
- Buy when C > price on first bar.
- Remember NewBuyPrice (the purpose of this code is to remember previous info).
- Buy when C > NewBuyPrice.
- Loop round the last 3 steps.
In the debug window, the first five lines are always correct.
The sixth line always says "Buy = 1"
The remaining lines would be correct if Buy really was "1"
//-----------------------------------------------------
Buy = 1 ; // force a buy on first bar
BuyPrice = C ;
EntryPrice = 0 ;
FirstBar = IIf (BarIndex()==0,1,0 ) ;
NotFirstBar = IIf( (C > Ref(EntryPrice, -1)), 1, 0 ) ;
Buy = IIf (FirstBar, 1, NotFirstBar) ;
EntryPrice = ValueWhen( Buy, BuyPrice, 1 );
Plot( C,"C",colorBlue,styleLine );
PlotShapes( Buy * shapeUpArrow, colorGreen );
"C = " + C ;
"FirstBar = " + FirstBar ;
"Ref(EntryPrice, -1) = " + Ref(EntryPrice, -1) ;
"C > Ref(EntryPrice, -1) = " + (C > Ref(EntryPrice, -1)) ;
"NotFirstBar = " + NotFirstBar ;
"Buy = " + Buy ;
"BuyPrice = " + BuyPrice ;
"ValueWhen(Buy, Buyprice, 1) = " + ValueWhen(Buy, BuyPrice, 1) ;
"EntryPrice = " + EntryPrice ;
//----------------------------------------------------