Bar color:
I color the bars based on the signals given by indicators. Basically I'm using MACD and RSI. The colors are defined like this:
Green: if both indicators give a long signal
Red: if both indicators give a short signal
Black: if the indicators give different signals (white bars on the chart)
In the attatched file I have used a paintbar indicator to color the bars. The orange lines are drawn using the drawing tools
Here is the part where I struggle:
The entry level for going long is the hight of the first green bar (first orange line in the attached image). If the price crosses the entry level, then it should produce a buying signal.
If the bar color changes to red or black, then the entry level is no longer valid and should be disabled.
I hope the above makes sense. Here is the code I use
Code: Select all
////////////////////////////MACD////////////////////////////
inputs:
MACDFastLength( 12 ),
MACDSlowLength( 26 ),
MACDLength( 9 ) ;
variables:
MACDsig ( 0 ),
MACDAvg( 0 ),
MACDDiff( 0 ),
MACDLongEntry (true),
MACDShortEntry (true),
MACDLongExit (true),
MACDShortExit (true);
MACDsig = MACD( Close, MACDFastLength, MACDSlowLength ) ;
MACDAvg = XAverage( MACDsig , MACDLength ) ;
MACDDiff= MACDsig - MACDAvg;
//MACD Entry
MACDLongEntry = MACDDiff crosses over 0;
MACDShortEntry = MACDDiff crosses under 0;
//MACD Exit
MACDLongExit = MACDDiff crosses under 0;
MACDShortExit = MACDDiff crosses over 0;
////////////////////////////RSI////////////////////////////
inputs:
RSILength (14),
RSIOverSold (50),
RSIOverBought(50);
variables:
RSILine( 0 ),
RSILongEntry (true),
RSIShortEntry (true),
RSILongExit (true),
RSIShortExit (true);
RSILine= RSI( close, RSILength) ;
//RSI Entry
RSILongEntry = RSILine crosses over RSIOverSold ;
RSIShortEntry = RSILine crosses under RSIOverBought;
//RSI Exit
RSILongExit = RSILine crosses under RSIOverSold ;
RSIShortExit = RSILine crosses over RSIOverBought;
////////////////////////////DEFINE BAR COLOR////////////////////////////
vars:
setGreenBar (false),
setRedBar (false),
setBlackBar (false),
entryTriggerLong (false),
entryLevelLong(0);
//Define green bar
setGreenBar = MACDLongEntry = true and RSILongEntry = true;
//Define red bar
setRedBar = MACDLongEntry = false and RSILongEntry = false;
//Define black bar
setBlackBar = (MACDLongEntry = true or RSILongEntry = true) and (MACDLongEntry = false or RSILongEntry = false);
//First Green bar
if setGreenBar = true and setGreenBar[1] = false then begin
entryLevelLong = High;
entryTriggerLong = true;
end;
if setBlackBar = true or setRedBar = true then begin
entryTriggerLong = false;
end;
////////////////////////////ENTRY SIGNAL////////////////////////////
//Entry Long
if marketposition = 0 and setGreenBar = true and close > entryLevelLong then begin;
Buy ( "TestEntry" ) 1 contracts next bar at market ;
end;
////////////////////////////EXIT////////////////////////////
if marketposition = 1 and MACDLongExit = true or RSILongExit = true then
Sell ( "TestExit" ) 1 contracts next bar at market ;