Trading Tool: RTT #1 Line Breakout

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

Trading Tool: RTT #1 Line Breakout

Postby maxmax68 » 28 Jan 2014

Hi all,
if you don't mind I'll leave on this forum some exercises in programming trading tools
I made for my own trading.
Use at your own risk. Not tested yet !!!

Massimo


Code: Select all

{ by Massimo Rizzi 27.01.2014
  RTT # 1 : Line Breakout with Tracking
  I made this tool for my own trading on Ftse Mib 40, still under test !!!
  Use at your own risk.
  This simple tool draws a Line and buys or sells shares if price is up or down the line.
  The tool enters, or stops or reverses the actual position at broker.
  It draws buttons on the chart that allow you to interact with the tool.
  You must press shift + mouse click.
  If you click on chart simply move buttons on the clicked position.
  Else if you click on line buttons Up and Dn, increase or decrease line position.
  If you shift+mouse click on  buttons you can change buttons:
  - Active (if SA on) or Paused Mode
  - Entry Mode or Stop Mode or Reverse Mode
  - Buy Mode or Sell Mode
  To start SA on.
NON IOG con Timeframe 30s-max 1min
Settare per trading:
Niente trasforma market to limit
Asincrono indipendente dal broker quindi
invia ordini market il cui eseguito viene dato per scontato.
}
[ProcessMouseEvents = True];
[IntrabarOrderGeneration = False];
[RecoverDrawings = False];

Inputs:
vQuantita(1),   // Input Quantity of Shares to Trade
vTL(0),       // Input Trigger Line Price
vLeft (10),      // Numero di barre a sinistra del pivot
vRight (10),    // Numero di barre a destra del pivot
vTick(5),       // Ticks of TriggerLine movement
vEndSessionTime_s(172300); // EOD Time for closing open position

Vars:
recalcpersist vValue1(0), // id button n1
recalcpersist vValue2(0), // id button n2
recalcpersist vValue3(0), // id button n3
recalcpersist vValue4A(0), // id button n4A
recalcpersist vValue4B(0), // id button n4B
recalcpersist vValue4C(0), // id button n4C
recalcpersist vValue5(0), // id button n5
recalcpersist vValueTL(0), // id TriggerLine
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.1), // Offset % x from low-left sx angle
recalcpersist vYOffset(0.2), // 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 vY4B(0), // coordinate y button 4B
recalcpersist vY4C(0), // coordinate y button 4C
recalcpersist vY5(0), // coordinate y button 5
recalcpersist vCounter1(0), // counter associato button 1
recalcpersist vCounter2(0), // counter associato button 2
recalcpersist vCounter3(0), // counter associato button 3
recalcpersist vCounter4A(0), // counter associato button 4A
recalcpersist vCounter4B(0), // counter associato button 4B
recalcpersist vCounter4C(0), // counter associato button 4C
recalcpersist vCounter5(0), // counter associato button 5
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 vLastPivotHigh(0), //Records value of last pivot high
recalcpersist vLastPivotLow(0); //Records value of last pivot low

//
// 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;

//
// Sync Strategy Position with Broker Position
//
// if realtime and strategy auto
if getappinfo(airealtimecalc) = 1 and getappinfo(aistrategyauto) = 1 then begin
   vStrategy_mp = currentcontracts*marketposition;
   vBroker_mp = MarketPosition_at_Broker;
   // if not sync
   if vStrategy_mp <> vBroker_mp then
      ChangeMarketPosition(vBroker_mp - vStrategy_mp, Last, "Sync");
end;

//
// Calculates Initial Trigger Line Price
//
once vTriggerLine=0;
if vTL <> 0 and vTriggerLine=0 then vTriggerLine = vTL
else if vTL=0 then begin
   if Date <> Date[1] then vTriggerLine = Open;
end;


// Draws TriggerLine
if TL_Exist(vValueTL)=true then tl_delete(vValueTL);
if TL_Exist(vValueTL)=false then vValueTL = tl_new(Date, Time[1], vTriggerLine, Date, Time, vTriggerLine);
TL_SetExtLeft(vValueTL,True);
TL_SetExtRight(vValueTL,True);
TL_Lock(vValueTL,True);

//
// 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;

//
// Initializes starting values of vX e vY
// and calculates ordinate values (vYn) of buttons
//
if vX=0 then vX = vMostLeftDateTime + vXOffset;
if vY=0 then vY = vLowestDispValue + (vHighestDispValue-vLowestDispValue) * vYOffset;
vY1= vY+20 * vMinMove;
vY2= vY+15 * vMinMove;
vY3= vY+10 * vMinMove;
vY4= vY+5 * vMinMove;
vY5= vY+0 * vMinMove;
vY4B= vLowestDispValue+((vHighestDispValue-vLowestDispValue)*0.5) + vMinMove;
vY4C= vLowestDispValue+((vHighestDispValue-vLowestDispValue)*0.5) - vMinMove;

//
// Creates buttons on the chart
//
once begin
   vValue1=Text_New_Dt(vX, vY1,"");
   vValue2=Text_New_Dt(vX, vY2,"");
   vValue3=Text_New_Dt(vX, vY3,"");
   vValue4A=Text_New_Dt(vX, vY4,"");
   vValue4B=Text_New_Dt(vX, vY4B,"");
   vValue4C=Text_New_Dt(vX, vY4C,"");
   vValue5=Text_New_Dt(vX, vY5,"RTT: Line BreakOut ");
end; // once

//
// If MouseClickShiftPressed ...
//
if MouseClickShiftPressed then begin
   //
   // if click on the chart and no button actived
   //
   if mc_text_getactive=-1 then begin
      // calculates new offsets for x & y buttons coordinates
      vXOffset = (MouseClickDateTime - vMostLeftDateTime) / (vMostRightDateTime - vMostLeftDateTime);
      vYOffset = (MouseClickPrice - vLowestDispValue) / (vHighestDispValue-vLowestDispValue);
   end
   // Checks for MouseClick on TextBoxes and updates vCounters
   else if MC_Text_GetActive=vValue1 then begin
      vCounter1=vCounter1+1;
      if vCounter1>1 then vCounter1=0;
   end
   else if MC_Text_GetActive=vValue2 then begin
         vCounter2=vCounter2+1;
         if vCounter2>2 then vCounter2=0;
   end
   else if MC_Text_GetActive=vValue3 then begin
         vCounter3=vCounter3+1;
         if vCounter3>1 then vCounter3=0;
   end
   else if MC_Text_GetActive=vValue4B then begin
      // increase TriggerLine
         vTriggerLine = vTriggerLine + (vTick/PriceScale);
         tl_setbegin(vValueTL,Date,Time[1], vTriggerLine);
      tl_setend(vValueTL,Date,Time, vTriggerLine);
      text_setstring(vValue4A," TL: "+NumToStr(vTriggerLine,0)+" ");
   end
   else if MC_Text_GetActive=vValue4C then begin
      // decrease TriggerLine
         vTriggerLine = vTriggerLine - (vTick/PriceScale);
         tl_setbegin(vValueTL,Date,Time[1], vTriggerLine);
      tl_setend(vValueTL,Date,Time, vTriggerLine);
      text_setstring(vValue4A," TL: "+NumToStr(vTriggerLine,0)+" ");
   end

   else if MC_Text_GetActive=vValue5 then begin
         vCounter5=vCounter5+1;
         if vCounter5>1 then vCounter5=0;
   end;
end; // if MouseClickShiftPressed

//
// Calculates new X e Y buttons coordinates
//
vX = vMostLeftDateTime + (vMostRightDateTime - vMostLeftDateTime) * vXOffset;
vY = vLowestDispValue + (vHighestDispValue-vLowestDispValue) * vYOffset;
vY1= vY+20 * vMinMove;
vY2= vY+15 * vMinMove;
vY3= vY+10 * vMinMove;
vY4= vY+5 * vMinMove;
vY4B= vLowestDispValue+((vHighestDispValue-vLowestDispValue)*0.5) + vMinMove;
vY4C= vLowestDispValue+((vHighestDispValue-vLowestDispValue)*0.5) - vMinMove;
vY5= vY+0 * vMinMove;

//
// Calculates new Pivot high and pivot low value
// and new vTriggerline value
//
if vLastPivotHigh=0 then vLastPivotHigh=vTriggerLine;
// calculates pivothigh
if (High[vRight]>=Highest(High, vLeft)[vRight+1]) and (High[vRight]>Highest(High, vRight)) then vLastPivotHigh=High[vRight];
// if Track mode (vCounter5=1) and Buy Mode (vCounter3=0) updates vTriggerLine
if vCounter5=1 and vCounter3=0 then vTriggerLine=Minlist(vTriggerLine, vLastPivotHigh);
if vLastPivotLow=0 then vLastPivotLow=vTriggerLine;
// calculates pivotlow
if (Low[vRight]<=Lowest(Low, vLeft)[vRight+1]) and (Low[vRight]<Lowest(Low, vRight)) then vLastPivotLow=Low[vRight];
// if Track mode (vCounter5=1) and Sell Mode (vCounter3=1) updates vTriggerLine
if vCounter5=1 and vCounter3=1 then vTriggerLine=Maxlist(vTriggerLine, vLastPivotLow);


//
// Formats e Redraws buttons on every recalc
//
// 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 (vCounter=0)
if time_s > vEndSessionTime_s AND vMP=0 then vCounter1=0;
// if flat (vMP=0) and Stop Mode (vCounter2=2) set Paused Mode (vCounter1=0)
if vMP=0 AND vCounter2=2 then vCounter1=0;
// if flat (vMP=0) set Entry Mode (vCounter2=0)
if vMP=0 then vCounter2=0 ;
// if long (vMP=1) and Entry Mode (vCounter2=0) set Reverse Mode (vCounter2=1)
if (vMP=1 AND vCounter2=0) then vCounter2=1;
// if long (vMP=1) set buy/sell to Sell (vCounter3=1)
if vMP=1 then vCounter3=1;
// if short (vMP=-1) and Entry Mode (vCounter2=0) set Reverse Mode (vCounter2=1)
if (vMP=-1 AND vCounter2=0) then vCounter2=1;
// if short (vMP=-1) set buy/sell to Buy (vCounter3=0)
if vMP=-1 then vCounter3=0;
// Formats and Redraws Button1
text_setborder(vValue1,TRUE);
text_setColor(vValue1,blue);
if vCounter1=0 then text_setBGColor(vValue1,lightgray)
else if vCounter1=1 then text_setBGColor(vValue1,cyan);
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_setstring(vValue2," Entry Mode ")
else if vCounter2=1 then text_setstring(vValue2," Reverse Mode ")
else if vCounter2=2 then text_setstring(vValue2," Stop Mode ");
if vCounter2=0 then text_setBGColor(vValue2,green)
else if vCounter2=1 then text_setBGColor(vValue2, magenta)
else if vCounter2=2 then text_setBGColor(vValue2,red);
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);
if vCounter3=0 then text_setstring(vValue3,"  Buy  : "+NumToStr(vQty,0)+" ")
else if vCounter3=1 then text_setstring(vValue3,"  Sell  : "+NumToStr(vQty,0)+" ");
text_SetStyle(vValue3,0,1);
text_lock(vValue3,TRUE);
Text_SetLocation_DT(vValue3,vX,vY3);
// Formats and Redraws Button4A
text_setborder(vValue4A,TRUE);
text_setColor(vValue4A,blue);
text_setBGColor(vValue4A,yellow);
if vTriggerLine>100 then text_setstring(vValue4A," TL: "+NumToStr(vTriggerLine,0)+" ")
else text_setstring(vValue4A," TL: "+NumToStr(vTriggerLine,3)+" ");
text_SetStyle(vValue4A,0,1);
text_lock(vValue4A,TRUE);
Text_SetLocation_DT(vValue4A,vX,vY4);
// Formats and Redraws Button4B
text_setborder(vValue4B,TRUE);
text_setColor(vValue4B,blue);
text_setBGColor(vValue4B,green);
text_setstring(vValue4B," Up ");
text_SetStyle(vValue4B,0,1);
text_lock(vValue4B,TRUE);
Text_SetLocation_DT(vValue4B,vX,vY4B);
// Formats and Redraws Button4C
text_setborder(vValue4C,TRUE);
text_setColor(vValue4C,blue);
text_setBGColor(vValue4C,red);
text_setstring(vValue4C," Dn ");
text_SetStyle(vValue4C,0,0);
text_lock(vValue4C,TRUE);
Text_SetLocation_DT(vValue4C,vX,vY4C);
// Formats and Redraws Button5
if vCounter5=0 then begin
   text_setstring(vValue5, "RTT#1: Line BreakOut ");
   text_setBGColor(vValue5,yellow);
end
else if vCounter5=1 then begin
   text_setstring(vValue5, "RTT#1: Line BreakOut w/Track ");
   text_setBGColor(vValue5,RGB(255,200,0));
end;
text_setborder(vValue5,TRUE);
text_setColor(vValue5,blue);
text_SetStyle(vValue5,0,1);
text_lock(vValue5,TRUE);
Text_SetLocation_DT(vValue5,vX,vY5);


//
// Starts trading routine  --------------------------------------------------------------------
//
//if LastBarOnChart AND getappinfo(aiStrategyAuto)=1 then begin
   //
   // If Active Mode (vCounter1=1) And TriggerLine drawed(exists) and TriggerLine value > 0
   //
   if (vCounter1=1 AND TL_Exist(vValueTL) AND  vTriggerLine>0) then begin
      //
      // if Flat Position (vMP=0) and Entry Mode (vCounter2=0)
      if vMP=0 AND vCounter2=0 then begin
         // if Buy Mode (vCounter3=0) and Buy Condition true (close[0]>triggerline)
         if vCounter3=0 AND Close[0]>vTriggerLine then begin
            BUY vQty shares next bar market;
            vMP=1;
         end
         // if Sell Mode (vCounter3=1) and Sell Condition true (close[0]<triggerline)
         else if vCounter3=1 AND Close[0]<vTriggerLine then begin
            sellshort vQty shares next bar market;
            vMP=-1;
         end;
      end
      //
      // il Long Position (vMP=1)
      else if vMP=1 then begin
         // if Stop Mode (vCounter2=2) and Stop Condition true (close[0]<triggerline ) or
         // if time_s > vEndSessionTime_s close position
         if (vCounter2=2 AND Close[0]<vTriggerLine) or (time_s >= vEndSessionTime_s) then begin
            SELL vQty shares next bar market;
            vMP=0;
         end
         // if Reverse Mode (vCounter2=1) and Reverse Condition true (close[0]<triggerline )
         else if vCounter2=1 AND Close[0]<vTriggerLine then begin
            sellShort vQty shares next bar market;
            vMP=-1;
         end;
      end
      //
      // il Short Position (vMP=-1)
      else if vMP=-1 then begin
         // if Stop Mode (vCounter2=2) and Stop Condition true (close[0]>triggerline )or
         // if time_s > vEndSessionTime_s close position
         if (vCounter2=2 AND Close[0]>vTriggerLine) or (time_s >= vEndSessionTime_s) then begin
            BuytoCover vQty shares next bar market;
            vMP=0;
         end
         // if Reverse Mode (vCounter2=1) and Reverse Condition true (close[0]>triggerline )
         else if vCounter2=1 AND Close[0]>vTriggerLine then begin
            BUY vQty shares next bar market;
            vMP=1;
         end;
      end;
   //   
   end; // if vCounter1=1
//end; // if LastBarOnChart

Return to “User Contributed Studies and Indicator Library”