RTT#2 Semiautomatic Trendline Trading Tool

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
maxmax68
Posts: 163
Joined: 20 Nov 2012
Has thanked: 55 times
Been thanked: 48 times

RTT#2 Semiautomatic Trendline Trading Tool

Postby maxmax68 » 25 May 2014

Code: Select all

//
//
// RTT#2 Semiautomatic Trendlines Trading Tool
// by Massimo Rizzi
// 26.05.2014
//
// Still in test phase - Use at your own risk
//
// I made this to trade on italian future
// This trading tool will draw 4 buttons on the screen.
// You must use SHIFT + LEFTMOUSECLICK on the buttons
// You can change the position on the screen, so you can use more than one at the same time.
// The first button switches tool from Paused to Active Mode, and shows Market Position
// The second button switches the conditions for trading.
// - Condition: Indicator crosses TrendLine, allows you to choose a custom indicator, i.e. average(close,10)
// The third button lets you choose the action you want to trade: Entry Long or Short, Exit Long or Short, Reverse.
// The combination of buttons 2 and 3 allows you to use the drawn trendline to Buy or Sell Stop or Limit, or Reverse opened position.
// So you can draw a trendline and choose to enter long if close or average(close,10) cross over or cross under the drawn trendline.
// My idea is to use more tools at the same time on the same chart to simulate chart trading,
// so for example one tool can enter a long position and others tools can control the opened position as a take profit or a stop loss.
// The fourth button is used to connect a drawn trendline to the study
//
// I tried to comment the code exhaustively,
// so that you can modify the code to suit your needs.
// Any your improvement or suggestion will be welcome.
//
//

[ProcessMouseEvents = True];
[IntrabarOrderGeneration = False];
//[RecoverDrawings = False];


Inputs:
vQuantita(1), // Input Quantity of Shares to Trade
CustomIndicator(close), // Input Indicator for TL1
vTL(0), // Input Trigger Line Price
vTick(5), // Ticks of TriggerLine movement
TLColor(white), // TrendLine color
TLSize(2), // TL size
ScreenPosition(0), // Position on the screen
VerticalSpacing(4), // Vertical Spacing of buttons
vEndSessionTime_s(173000); // EOD Time for closing open position

Vars:
recalcpersist vLastBarNumber(0), // registra il numero dell'ultima barra
recalcpersist vLastBarDate(0), // registra data ultima barra
recalcpersist vLastBarTime_s(0), // registra Time_s ultima barra
recalcpersist vLastBarPrice(0), // registra Price ultima barra
recalcpersist vValue1(0), // button 1 id
recalcpersist vValue2(0), // button 2 id
recalcpersist vValue3(0), // button 3 id
recalcpersist vValue4(0), // button 4 id
recalcpersist vMostLeftDateTime(0),
recalcpersist vMostRightDateTime(0),
recalcpersist vHighestDispValue(0),
recalcpersist vLowestDispValue(0),
recalcpersist vRangeDateTime(0),
recalcpersist vMinMove(1), // Movimento minimo scala prezzi
recalcpersist vX(0), // coordinate x buttons
recalcpersist vY(0), // coordinate y punto inferiore (click mouse)
recalcpersist vXOffset(0.01), // Offset % x from low-left sx angle
recalcpersist vYOffset(0.01), // Offset % y from low-left sx angle
recalcpersist vY1(0), // coordinate y button 1
recalcpersist vY2(0), // coordinate y button 2
recalcpersist vY3(0), // coordinate y button 3
recalcpersist vY4(0), // coordinate y button 4
recalcpersist vCounter1(0), // counter associato button 1
recalcpersist vCounter2(0), // counter associato button 2
recalcpersist vCounter3(0), // counter associato button 3
recalcpersist vCounter4(0), // counter associato button 4
recalcpersist vQTy(0), // Quantity of Shares to Trade
recalcpersist vTriggerLine(0), // Trigger Line Price
recalcpersist vMP(0), // MarketPosition at Broker 0=Flat 1=Long -1=Short
recalcpersist vStrategy_mp (0), // Inner Strategy Position
recalcpersist vBroker_mp (0), // Actual MarketPosition_at_Broker;
recalcpersist vTL1id(0), // id of TL linked (0=no TL linked, >0=id of TL linked)
recalcpersist vTLValue(0), // TL linked Value on last bar
recalcpersist vTLValue1(0); // TL linked Value on last bar-1

if (LastBarOnChart) then begin
//
// Get DateTime MostLeft and MostRight
// and Highest and Lowest price displayed on the chart.
//
vMostLeftDateTime = GetAppInfo(aiLeftDispDateTime);
vMostRightDateTime = GetAppInfo(aiRightDispDateTime);
vHighestDispValue = GetAppInfo(aiHighestDispValue);
vLowestDispValue = GetAppInfo(aiLowestDispValue);
vMinMove = (vHighestDispValue-vLowestDispValue)/100;
//
// Calculates new X e Y buttons coordinates
//
vX = vMostLeftDateTime + (vMostRightDateTime - vMostLeftDateTime) * vXOffset;
vY = vLowestDispValue + (ScreenPosition) + (vHighestDispValue-vLowestDispValue) * vYOffset;
vY1= vY+(3*VerticalSpacing * vMinMove);
vY2= vY+(2*VerticalSpacing * vMinMove);
vY3= vY+(1*VerticalSpacing * vMinMove);
vY4= vY+(0*VerticalSpacing * vMinMove);
//
// Creates buttons on the chart once first time
//
once begin
vValue1=Text_New_Dt(vX, vY1,"");
vValue2=Text_New_Dt(vX, vY2,"");
vValue3=Text_New_Dt(vX, vY3,"");
vValue4=Text_New_Dt(vX, vY4,"");
end; // once
//
// Records last bar data
//
vLastBarNumber=currentbar;
vLastBarDate=Date;
vLastBarTime_s=Time_s;
vLastBarPrice=Close;
//
// if TL linked (vTL1id>0) then calculates TL value of current bar
//
if vTL1id>0 then begin
vTLValue=tl_getvalue_bn(vTL1id,vLastBarNumber);
vTLValue1=tl_getvalue_bn(vTL1id,vLastBarNumber-1);
end
// else reset vTLValue to 0
else begin
vTLValue=0;
vTLValue1=0;
end;
{/
// REALTIME MODE Calculates Quantity of Shares to Trade
//
if MarketPosition_at_Broker = 0 then begin
vQTy = vQuantita;
vMP = 0;
end else if MarketPosition_at_Broker > 0 then begin
vQTy = MarketPosition_at_Broker;
vMP = 1;
end else if MarketPosition_at_Broker < 0 then begin
vQTy = -1*MarketPosition_at_Broker;
vMP = -1;
end;}


//
// TEST MODE Calculates Quantity of Shares to Trade
//
if MarketPosition = 0 then begin
vQTy = vQuantita;
vMP = 0;
end else if MarketPosition > 0 then begin
vQTy = vQuantita;
vMP = 1;
end else if MarketPosition < 0 then begin
vQTy = vQuantita;
vMP = -1;
end;
//
// If MouseClickShiftPressed ...
//
if MouseClickShiftPressed then begin
// Active-Paused Button (vCounter1=0 Paused, vCounter1=1 Active)
if MC_Text_GetActive=vValue1 then begin
vCounter1=vCounter1+1;
if vCounter1>1 then vCounter1=0;
//
// if vCounter1=1 (Active Mode) checks for Actions vs Indicator/Conditions vs TL position before activate button ...
//
if vCounter1=1 then begin
// if vCounter4=0 (No TL Linked) resets vCounter1=0 (Paused Mode)
if vCounter4=0 then vCounter1=0;
// if vCounter4=2 (TL Linked) then ...
end;
end
// Indicator-Conditions Button (vCounter2=0 Indicator, vCounter2=1 Condition1, etc ...)
else if MC_Text_GetActive=vValue2 then begin
vCounter2=vCounter2+1;
if vCounter2>4 then vCounter2=0;
// On every change sets Paused Mode (vCounter1=0)
vCounter1=0;
end
// Action Button (vCounter3=0 Entry Long, vCounter3=1 Entry Short,
// vCounter3=2 Exit Long, vCounter3=3 Exit Short, vCounter3=4 Reverse )
else if MC_Text_GetActive=vValue3 then begin
vCounter3=vCounter3+1;
if vCounter3>4 then vCounter3=0;
// On every change sets Paused Mode (vCounter1=0)
vCounter1=0;
end
// TL Link Button (vCounter4=0 No TL Linked, vCounter4=1 TL Linking, vCounter4=2 TL Linked)
else if MC_Text_GetActive=vValue4 then begin
vCounter4=vCounter4+1;
if vCounter4>2 then vCounter4=0;
// if vCounter4=0 (no TL linked) resets vTL1id to 0
if vCounter4=0 then begin
if vTL1id>0 then begin
tl_setcolor(vTL1id, lightgray);
tl_setsize(vTL1id,0);
end;
vTL1id=0;
end;
// On every change sets Paused Mode (vCounter1=0)
vCounter1=0;
end;
end; // if MouseClickShiftPressed

//
// If vCounter4=1 (TL linking) and TL active (tl_getactive>-1) then vTL1id gets TL active id
//
if vCounter4=1 AND mc_tl_getactive>-1 then begin
vTL1id=mc_tl_getactive;
tl_setextright(vTL1id ,true);
tl_setcolor(vTL1id, TLColor);
tl_setsize(vTL1id,TLSize);
vCounter4=2; // (=TL Linked)
// On every change sets Paused Mode (vCounter1=0)
vCounter1=0;
end;

//
// Executes some checks and
// Formats e Redraws buttons on every recalc
//
// if Reverse Mode(vCounter3=4) and Long(vMP=1) and Condition Close>TL(vCounter2=1) then sets Condition Close<TL(vCounter2=2)
if vCounter3=4 AND vMP=1 AND vCounter2=1 then vCounter2=2
// if Reverse Mode(vCounter3=4) and Short(vMP=-1) and Condition Close<TL(vCounter2=2) then sets Condition Close>TL(vCounter2=1)
else if vCounter3=4 AND vMP=-1 AND vCounter2=2 then vCounter2=1;


// REALTIME MODE if not Auto Trade then Paused mode (vCounter1=0)
//if getappinfo(aiStrategyAuto)=0 then vCounter1=0;
// if time_s > vEndSessionTime_s and Flat position (vMP=0) set Paused Mode (vCounter1=0)
//if time_s > vEndSessionTime_s AND vMP=0 then vCounter1=0;

// Formats and Redraws Button1
text_setborder(vValue1,TRUE);
text_setColor(vValue1,blue);
if vCounter1=0 then text_setBGColor(vValue1,RGB(255,200,0))
else if vCounter1=1 then text_setBGColor(vValue1,cyan);
if vCounter1=0 then begin
if vMP=0 then text_setstring(vValue1," PAUSED: MP FLAT "+"0")
else if vMP=1 then text_setstring(vValue1," PAUSED: MP LONG "+NumToStr(vQuantita,0))
else if vMP=-1 then text_setstring(vValue1," PAUSED: MP SHORT "+NumToStr(vQuantita,0));
end else if vCounter1=1 then begin
if vMP=0 then text_setstring(vValue1," ACTIVE: MP FLAT "+"0")
else if vMP=1 then text_setstring(vValue1," ACTIVE: MP LONG "+NumToStr(vQuantita,0))
else if vMP=-1 then text_setstring(vValue1," ACTIVE: MP SHORT "+NumToStr(vQuantita,0));
end;
{//REALTIME MODE
if vCounter1=0 then begin
if vMP=0 then text_setstring(vValue1,"PAUSED: MP FLAT "+NumToStr(MarketPosition_at_Broker,0))
else if vMP=1 then text_setstring(vValue1,"PAUSED: MP LONG "+NumToStr(MarketPosition_at_Broker,0))
else if vMP=-1 then text_setstring(vValue1,"PAUSED: MP SHORT "+NumToStr(MarketPosition_at_Broker,0));
end else if vCounter1=1 then begin
if vMP=0 then text_setstring(vValue1,"ACTIVE: MP FLAT "+NumToStr(MarketPosition_at_Broker,0))
else if vMP=1 then text_setstring(vValue1,"ACTIVE: MP LONG "+NumToStr(MarketPosition_at_Broker,0))
else if vMP=-1 then text_setstring(vValue1,"ACTIVE: MP SHORT "+NumToStr(MarketPosition_at_Broker,0));
end;}
text_SetStyle(vValue1,0,1);
text_lock(vValue1,TRUE);
Text_SetLocation_DT(vValue1,vX,vY1);
//
// Formats and Redraws Button2
//
text_setborder(vValue2,TRUE);
text_setColor(vValue2,blue);
if vCounter2=0 then text_setBGColor(vValue2,magenta)
else if vCounter2>0 then text_setBGColor(vValue2,yellow);
if vCounter2=0 then text_setstring(vValue2,text(" Indicator crosses TL "))
else if vCounter2=1 then text_setstring(vValue2,text(" Cond: Close > TL "))
else if vCounter2=2 then text_setstring(vValue2,text(" Cond: Close < TL "))
else if vCounter2=3 then text_setstring(vValue2,text(" Cond: C[1]>C[2] & Close > TL "))
else if vCounter2=4 then text_setstring(vValue2,text(" Cond: C[1]<C[2] & Close < TL "));
text_SetStyle(vValue2,0,1);
text_lock(vValue2,TRUE);
Text_SetLocation_DT(vValue2,vX,vY2);
//
// Formats and Redraws Button3
//
text_setborder(vValue3,TRUE);
text_setColor(vValue3,blue);
if vCounter3=0 then text_setBGColor(vValue3,green)
else if vCounter3=1 then text_setBGColor(vValue3,red)
else if vCounter3=2 then text_setBGColor(vValue3,green)
else if vCounter3=3 then text_setBGColor(vValue3,red)
else if vCounter3=4 then text_setBGColor(vValue3,magenta);
if vCounter3=0 then text_setstring(vValue3," Entry LONG : "+NumToStr(vQty,0)+" ")
else if vCounter3=1 then text_setstring(vValue3," Entry SHORT : "+NumToStr(vQty,0)+" ")
else if vCounter3=2 then text_setstring(vValue3," Exit LONG : "+NumToStr(vQty,0)+" ")
else if vCounter3=3 then text_setstring(vValue3," Exit SHORT : "+NumToStr(vQty,0)+" ")
else if vCounter3=4 then text_setstring(vValue3," REVERSE : "+NumToStr(vQty,0)+" ");
text_SetStyle(vValue3,0,1);
text_lock(vValue3,TRUE);
Text_SetLocation_DT(vValue3,vX,vY3);
//
// Formats and Redraws Button4
//
text_setborder(vValue4,TRUE);
text_setColor(vValue4,blue);
text_setBGColor(vValue4,yellow);
if vCounter4=0 then text_setBGColor(vValue4,lightgray)
else if vCounter4=1 then text_setBGColor(vValue4,yellow)
else if vCounter4=2 then text_setBGColor(vValue4,RGB(255,200,0));
if vCounter4=0 then text_setstring(vValue4," TL not linked ")
else if vCounter4=1 then text_setstring(vValue4," TL linking ... ")
else if vCounter4=2 then text_setstring(vValue4," TL linked id: "+NumToStr(vTL1id,0));
text_SetStyle(vValue4,0,1);
text_lock(vValue4,TRUE);
Text_SetLocation_DT(vValue4,vX,vY4);
end;

//
// Starts trading routine --------------------------------------------------------------------
//
//if LastBarOnChart AND getappinfo(aiStrategyAuto)=1 then begin
//
// If Active Mode (vCounter1=1)
//
if vCounter1=1 then begin
//
// if time_s >= vEndSessionTime_s
if time_s >= vEndSessionTime_s then begin
// if Flat
if vMP=0 then vCounter1=0
// if Long
else if vMP=1 then begin
SELL vQty shares next bar market;
vCounter1=0;
vMP=0;
end
// if Short
else if vMP=-1 then begin
BuytoCover vQty shares next bar market;
vCounter1=0;
vMP=0;
end;
end
//
// if Entry Long(vCounter3=0) and Flat(vMP=0)
else if vCounter3=0 and vMP=0 then begin
// if Indicator crosses TL(vCounter2=0) and Indicator value crosses TL value
if vCounter2=0 AND ((CustomIndicator[0]>vTLValue AND CustomIndicator[1]<vTLValue1) or (CustomIndicator[0]<vTLValue AND CustomIndicator[1]>vTLValue1)) then begin
BUY vQty shares next bar market;
vCounter1=0;
vMP=1;
end
// if BUY STOP - Conditon Close>TL(vCounter2=1) and Close value>TL value
else if vCounter2=1 AND Close[0]>vTLValue then begin
BUY vQty shares next bar market;
vCounter1=0;
vMP=1;
end
// if BUY LIMIT - Conditon Close<TL(vCounter2=2) and Close value<TL value
else if vCounter2=2 AND Close[0]<vTLValue then begin
BUY vQty shares next bar market;
vCounter1=0;
vMP=1;
end;
// ... and so on for all conditions
end
//
// else if Entry Short(vCounter3=1) and Flat(vMP=0)
else if vCounter3=1 and vMP=0 then begin
// if Indicator crosses TL(vCounter2=0) and Indicator value crosses TL value
if vCounter2=0 AND ((CustomIndicator[0]>vTLValue AND CustomIndicator[1]<vTLValue1) or (CustomIndicator[0]<vTLValue AND CustomIndicator[1]>vTLValue1)) then begin
sellshort vQty shares next bar market;
vCounter1=0;
vMP=-1;
end
// if SELL LIMIT - Conditon Close>TL(vCounter2=1) and Close value>TL value
else if vCounter2=1 AND Close[0]>vTLValue then begin
sellshort vQty shares next bar market;
vCounter1=0;
vMP=-1;
end
// if SELL STOP - Conditon Close<TL(vCounter2=2) and Close value<TL value
else if vCounter2=2 AND Close[0]<vTLValue then begin
sellshort vQty shares next bar market;
vCounter1=0;
vMP=-1;
end;
// ... and so on for all conditions
end
//
// else if Exit Long(vCounter3=2) and Long Position(vMP=1)
else if vCounter3=2 and vMP=1 then begin
// if Indicator crosses TL(vCounter2=0) and Indicator value crosses TL value
if vCounter2=0 AND ((CustomIndicator[0]>vTLValue AND CustomIndicator[1]<vTLValue1) or (CustomIndicator[0]<vTLValue AND CustomIndicator[1]>vTLValue1)) then begin
SELL vQty shares next bar market;
vCounter1=0;
vMP=0;
end
// if Exit Long Profit Target - Conditon Close>TL(vCounter2=1) and Close value>TL value
else if vCounter2=1 AND Close[0]>vTLValue then begin
SELL vQty shares next bar market;
vCounter1=0;
vMP=0;
end
// if Exit Long Stop Loss - Conditon Close<TL(vCounter2=2) and Close value<TL value
else if vCounter2=2 AND Close[0]<vTLValue then begin
SELL vQty shares next bar market;
vCounter1=0;
vMP=0;
end;
// ... and so on for all conditions
end
//
// else if Exit Short(vCounter3=3) and Short Position(vMP=-1)
else if vCounter3=3 and vMP=-1 then begin
// if Indicator crosses TL(vCounter2=0) and Indicator value crosses TL value
if vCounter2=0 AND ((CustomIndicator[0]>vTLValue AND CustomIndicator[1]<vTLValue1) or (CustomIndicator[0]<vTLValue AND CustomIndicator[1]>vTLValue1)) then begin
BuytoCover vQty shares next bar market;
vCounter1=0;
vMP=0;
end
// if Exit Short Stop Loss - Conditon Close>TL(vCounter2=1) and Close value>TL value
else if vCounter2=1 AND Close[0]>vTLValue then begin
BuytoCover vQty shares next bar market;
vCounter1=0;
vMP=0;
end
// if Exit Short Profit Target - Conditon Close<TL(vCounter2=2) and Close value<TL value
else if vCounter2=2 AND Close[0]<vTLValue then begin
BuytoCover vQty shares next bar market;
vCounter1=0;
vMP=0;
end;
// ... and so on for all conditions
end
//
// else if Reverse Position(vCounter3=4) and not Flat(vMP<>0)
else if vCounter3=4 and vMP<>0 then begin
// if Long (vMP=1)
if vMP=1 then begin
// if Indicator VS TL(vCounter2=0) and Indicator value <TL value
if vCounter2=0 AND CustomIndicator[0]<vTLValue then begin
sellshort vQty shares next bar market;
vCounter1=1;
vMP=-1;
end
// if Reverse Short - Conditon Close<TL(vCounter2=2) and Close value<TL value
else if vCounter2=2 AND Close[0]<vTLValue then begin
sellshort vQty shares next bar market;
vCounter1=1;
vMP=-1;
vCounter2=1;
end;
// ... and so on for all conditions
end
// if Short (vMP=-1)
else if vMP=-1 then begin
// if Indicator VS TL(vCounter2=0) and Indicator value >TL value
if vCounter2=0 AND CustomIndicator[0]>vTLValue then begin
BUY vQty shares next bar market;
vCounter1=1;
vMP=1;
end
// if Reverse Long - Conditon Close>TL(vCounter2=1) and Close value>TL value
else if vCounter2=1 AND Close[0]>vTLValue then begin
BUY vQty shares next bar market;
vCounter1=1;
vMP=1;
vCounter2=2;
end;
// ... and so on for all conditions
end;
end;
end; // if vCounter1=1
//end; // if LastBarOnChart
Attachments
RTT#2_Semiautomatic_Trendline_Trading_Tool.png
(103.46 KiB) Downloaded 939 times

Return to “User Contributed Studies and Indicator Library”